def addExcludedIp(self,ipStr,comment): ''' Add an IP to the exclusion list ''' with MutexStore.getObjectLock(self.getLockIdentifier()): #Check is not already allocated if not self.__isIpAvailable(ipStr): raise Exception("Ip already allocated or marked as excluded") #then forbidd if not IP4Utils.isIpInRange(ipStr,self.startIp,self.endIp): raise Exception("Ip is not in range") newIp = Ip4Slot.excludedIpFactory(self,ipStr,comment) self.ips.add(newIp) #if was nextSlot shift if self.nextAvailableIp == ipStr: try: it= IP4Utils.getIpIterator(self.nextAvailableIp,self.endIp,self.netMask) while True: ip = it.getNextIp() if self.__isIpAvailable(ip): break self.nextAvailableIp = ip except Exception as e: self.nextAvailableIp = None self.autoSave()
def allocateIp(self): ''' Allocates an IP address of the range ''' with MutexStore.getObjectLock(self.getLockIdentifier()): #Implements first fit algorithm if self.nextAvailableIp == None: raise Exception("Could not allocate any IP") newIp = Ip4Slot.ipFactory(self,self.nextAvailableIp) self.ips.add(newIp) #Try to find new slot try: it= IP4Utils.getIpIterator(self.nextAvailableIp,self.endIp,self.netMask) while True: ip = it.getNextIp() if self.__isIpAvailable(ip): break self.nextAvailableIp = ip except Exception as e: self.nextAvailableIp = None self.autoSave() return newIp
def constructor(ipRange,ip,excluded,comment="",save=True): self = Ip4Slot() try: #Check IP IP4Utils.checkValidIp(ip) self.ip = ip self.isExcluded = excluded self.ipRange = ipRange self.comment = comment self.doSave = save if save: self.save() except Exception as e: #self.delete() raise e return self
def rebasePointer(self): '''Used when pointer has lost track mostly due to bug #''' with MutexStore.getObjectLock(self.getLockIdentifier()): print "Rebasing pointer of range: "+str(self.id) print "Current pointer point to: "+self.nextAvailableIp try: it= IP4Utils.getIpIterator(self.startIp,self.endIp,self.netMask) while True: ip = it.getNextIp() if self.__isIpAvailable(ip): break self.nextAvailableIp = ip except Exception as e: self.nextAvailableIp = None print "Pointer will be rebased to: "+self.nextAvailableIp self.save()
def releaseIp(self,ipObj): ''' Releases an IP address of the range (but it does not destroy the object!!) ''' with MutexStore.getObjectLock(self.getLockIdentifier()): ipStr = ipObj.getIp() if not self.ips.filter(ip=ipStr,isExcluded=False).count() > 0: raise Exception("Cannot release Ip %s. Reason may be is unallocated or is an excluded Ip",ipStr) self.ips.remove(ipObj) #Determine new available Ip if not self.nextAvailableIp == None: if IP4Utils.compareIps(ipStr,self.nextAvailableIp) > 0: #Do nothing pass else: self.nextAvailableIp = ipStr else: #No more gaps self.nextAvailableIp = ipStr self.autoSave()
def removeExcludedIp(self,ipObj): ''' Deletes an IP from the exclusion list (but it does not destroy the object!!) ''' with MutexStore.getObjectLock(self.getLockIdentifier()): ipStr = ipObj.getIp() if not self.ips.get(ip=ipStr).isExcludedIp(): raise Exception("Cannot release Ip. Reason may be is unallocated or is not excluded Ip") self.ips.remove(ipObj) #Determine new available Ip if not self.nextAvailableIp == None: if IP4Utils.compareIps(ipStr,self.nextAvailableIp) > 0: #Do nothing pass else: self.nextAvailableIp = ipStr else: #No more gaps self.nextAvailableIp = ipStr self.autoSave()
def constructor(name,startIp,endIp,netmask,gw,dns1,dns2,isGlobal=True,save=True): self = Ip4Range() try: #Default constructor IP4Utils.checkValidIp(startIp) IP4Utils.checkValidIp(endIp) IP4Utils.checkValidNetmask(netmask) IP4Utils.checkValidIp(gw) IP4Utils.checkValidIp(dns1) if not dns2 == "": IP4Utils.checkValidIp(dns2) self.name = name self.isGlobal= isGlobal self.startIp = startIp self.endIp = endIp self.netMask = netmask self.gw = gw self.dns1 = dns1 if not dns2 == "": self.dns2 = dns2 #Create an iterator it= IP4Utils.getIpIterator(self.startIp,self.endIp,self.netMask) self.nextAvailableIp = it.getNextIp() #Number of Slots try: self.numberOfSlots = IP4Utils.getNumberOfSlotsInRange(startIp,endIp,netmask) except Exception as e: print "Exception doing slot calculation"+str(e) self.numberOfSlots = -1 self.doSave = save if save: self.save() except Exception as e: #self.delete() raise e return self
def __setNetmask(self, value): IP4Utils.checkValidNetmask(value) self.netMask = value self.autoSave()
def __setEndIp(self, value): IP4Utils.checkValidIp(value) self.endIp = value self.autoSave()
def __setStartIp(self, value): IP4Utils.checkValidIp(value) self.startIp = value self.autoSave()