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 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 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