def run(self): try: self.startConnectToController() finally: self.stopConnectToController() #send del port to controller for snmpPosition, port in self.dictActivePort.items(): packed_data = portStatus(reason=portReason.OFPPR_DELETE, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) # del port from dict all active port in TDA if len(self.dictActivePort.items()) > 0: try: self.lock.acquire_write() for snmpPosition, port in self.dictActivePort.items(): del self.dictAllActivePortInTDA[(port.hw_addr, port.name)] self.lock.release() except: self.lock.release() del self.dictActivePort self.dictActivePort = {}
def checkStatusOfActivePort(self): tempDictActivePort = {} # dict for activeport at cal ma listDictPortPresent = {} dictRemoteSwitchData = self.getRemoteSwitchDataFromSnmpVersion2C() dictPort = self.getPortFromSnmpVersion2C(0) for snmpPosition in dictPort: if snmpPosition in dictRemoteSwitchData: listDictPortPresent[snmpPosition] = dictPort[snmpPosition] for snmpPosition, port in listDictPortPresent.items(): #print("55555555 : " + port.hw_addr) if snmpPosition in self.dictActivePort: tempDictActivePort[snmpPosition] = port del self.dictActivePort[snmpPosition] else: #send new port to controller tempDictActivePort[snmpPosition] = port packed_data = portStatus(reason=portReason.OFPPR_ADD, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) self.lock.acquire_write() # add port to dict all active port in tda self.dictAllActivePortInTDA[(port.hw_addr, port.name)] = [ self.datapathId, port.port_no ] self.lock.release() print("release at add port") print("list dict active port") print(self.dictActivePort) for snmpPosition, port in self.dictActivePort.items(): print(port) #send del port to controller for snmpPosition, port in self.dictActivePort.items(): packed_data = portStatus(reason=portReason.OFPPR_DELETE, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) # del port from dict all active port in TDA if len(self.dictActivePort.items()) > 0: self.lock.acquire_write() for snmpPosition, port in self.dictActivePort.items(): print("--------------delete-------------") print("hw_addr : " + port.hw_addr) print("hw_desc : " + port.name) print(self.dictAllActivePortInTDA) del self.dictAllActivePortInTDA[(port.hw_addr, port.name)] print(self.dictAllActivePortInTDA) print("--------------delete-------------") self.lock.release() del self.dictActivePort self.dictActivePort = tempDictActivePort del self.dictRemoteSwitchDataFromPort self.dictRemoteSwitchDataFromPort = dictRemoteSwitchData """ for item in listDictPortPresent: #print("item 1 : " + str(item[1].port_no)) if item in self.dictActivePort : tempDictActivePort[item] = self.dictActivePort[item] del self.listActivePort[item[0]] else: #send new port to controller tempDictActivePort[item[] = item[1] packed_data = portStatus( reason=portReason.OFPPR_ADD , desc=item[1] ) packed_data = packed_data.pack() self.s.send( packed_data ) """ """
def checkStatusOfActivePort(self): tempDictActivePort = {} # dict for activeport at cal ma listDictPortPresent = {} dictRemoteSwitchData = self.getRemoteSwitchDataFromSnmpVersion2C() dictPort = self.getPortFromSnmpVersion2C( 0 ) for snmpPosition in dictPort : if snmpPosition in dictRemoteSwitchData : listDictPortPresent[snmpPosition] = dictPort[snmpPosition] for snmpPosition , port in listDictPortPresent.items(): if snmpPosition in self.dictActivePort: tempDictActivePort[snmpPosition] = port del self.dictActivePort[snmpPosition] else: #send new port to controller tempDictActivePort[snmpPosition] = port packed_data = portStatus( reason=portReason.OFPPR_ADD , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) print("list dict active port") for snmpPosition , port in self.dictActivePort.items(): print(port) #send del port to controller for snmpPosition , port in self.dictActivePort.items(): print("555555555555555") packed_data = portStatus( reason=portReason.OFPPR_DELETE , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) print("555555555555555") del self.dictActivePort self.dictActivePort = tempDictActivePort del self.dictRemoteSwitchDataFromPort self.dictRemoteSwitchDataFromPort = dictRemoteSwitchData """ for item in listDictPortPresent: #print("item 1 : " + str(item[1].port_no)) if item in self.dictActivePort : tempDictActivePort[item] = self.dictActivePort[item] del self.listActivePort[item[0]] else: #send new port to controller tempDictActivePort[item[] = item[1] packed_data = portStatus( reason=portReason.OFPPR_ADD , desc=item[1] ) packed_data = packed_data.pack() self.s.send( packed_data ) """ """ num = input() port = self.listActivePort["06:fd:e2:ae:ab:60"][2] print( "delete port : " + str(port.port_no)) packed_data = portStatus( reason=portReason.OFPPR_DELETE , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) num = input() port.port_no = 10 print( "add port : " + str(port.port_no)) packed_data = portStatus( reason=portReason.OFPPR_ADD , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) return """ #self.listActivePort #print(listSnmpPosition) #print(tempListActivePort) """ print("self.dictActivePort") for key,value in self.dictActivePort.items(): print("key : " + str(key) + " port_no : " + str(value.port_no) ) print("tempDictActivePort") for key,value in tempDictActivePort.items(): print("key : " + str(key) + " port_no : " + str(value.port_no) ) """ """
def checkStatusOfActivePort(self): tempDictActivePort = {} # dict for activeport at cal ma listDictPortPresent = {} dictPort, tempHwAddr = self.getPortFromSnmpVersion2C(0, False) if tempHwAddr != None: dictRemoteSwitchData = self.getRemoteSwitchDataFromSnmpVersion2C( False) else: dictRemoteSwitchData = {} """ if self.switchIp == "192.168.0.104": print("--------------192.168.0.104-----------------") print("active port before check : self.dictActivePort") print(self.dictActivePort) print("active port before check : self.dictAllActivePortInTDA") print(self.dictAllActivePortInTDA) print("--------------192.168.0.104-----------------") """ for snmpPosition in dictPort: if snmpPosition in dictRemoteSwitchData: listDictPortPresent[snmpPosition] = dictPort[snmpPosition] for snmpPosition, port in listDictPortPresent.items(): #print("55555555 : " + port.hw_addr) if snmpPosition in self.dictActivePort: tempDictActivePort[snmpPosition] = port del self.dictActivePort[snmpPosition] else: #send new port to controller tempDictActivePort[snmpPosition] = port packed_data = portStatus(reason=portReason.OFPPR_ADD, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) try: tempPortID = str(port.port_no) tempPortID = ("0" * (8 - len(tempPortID))) + tempPortID packer = struct.Struct('bbbb') tempPortID = packer.pack(int(tempPortID[0:2], 16), int(tempPortID[2:4], 16), int(tempPortID[4:6], 16), int(tempPortID[6:8], 16)) self.lock.acquire_write() # add port to dict all active port in tda self.dictAllActivePortInTDA[(port.hw_addr, port.name)] = [ self.datapathId, tempPortID ] self.lock.release() except: self.lock.release() #print("release at add port") #send del port to controller for snmpPosition, port in self.dictActivePort.items(): packed_data = portStatus(reason=portReason.OFPPR_DELETE, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) # del port from dict all active port in TDA if len(self.dictActivePort.items()) > 0: try: self.lock.acquire_write() for snmpPosition, port in self.dictActivePort.items(): """ print("--------------delete-------------") print("hw_addr : " + port.hw_addr) print("hw_desc : " + port.name) print(self.dictAllActivePortInTDA) """ del self.dictAllActivePortInTDA[(port.hw_addr, port.name)] """ print(self.dictAllActivePortInTDA) print("--------------delete-------------") """ self.lock.release() except: self.lock.release() """ if self.switchIp == "192.168.0.104": print("--------------192.168.0.104-----------------") print("active port after check : tempDictActivePort") print(tempDictActivePort) print("active port after check : self.dictAllActivePortInTDA") print(self.dictAllActivePortInTDA) print("--------------192.168.0.104-----------------") """ del self.dictActivePort self.dictActivePort = tempDictActivePort #print("Active Port : " + self.switchIp) #print(tempDictActivePort) del self.dictRemoteSwitchDataFromPort self.dictRemoteSwitchDataFromPort = dictRemoteSwitchData
def getPortFromSnmpVersion2C(self, mininetOption, initConnection): # init value count = 0 cmdGen = None tempHwAddr = "" # list port dictPort = {} try: # create object for create snmp command cmdGen = cmdgen.CommandGenerator() except Exception as err: print(" 94 Switch ip " + self.switchIp + " terminate because handling run-time error : " + str(err)) sys.exit() while count < self.numberOfRetransmission: try: # connect to snmp at switch errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( cmdgen.CommunityData('public'), cmdgen.UdpTransportTarget((self.switchIp, 161)), '1.0.8802.1.1.2.1.3.7.1.3', '1.0.8802.1.1.2.1.3.7.1.4') if errorIndication: print(errorIndication) count += 1 if count == 1: #send del port to controller for snmpPosition, port in self.dictActivePort.items(): packed_data = portStatus( reason=portReason.OFPPR_DELETE, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) # del port from dict all active port in TDA if len(self.dictActivePort.items()) > 0: try: self.lock.acquire_write() for snmpPosition, port in self.dictActivePort.items( ): del self.dictAllActivePortInTDA[( port.hw_addr, port.name)] self.lock.release() except: self.lock.release() del self.dictActivePort self.dictActivePort = {} else: if errorStatus: print( '%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex) - 1] or '?')) count += 1 if count == 1: #send del port to controller for snmpPosition, port in self.dictActivePort.items( ): packed_data = portStatus( reason=portReason.OFPPR_DELETE, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) # del port from dict all active port in TDA if len(self.dictActivePort.items()) > 0: try: self.lock.acquire_write() for snmpPosition, port in self.dictActivePort.items( ): del self.dictAllActivePortInTDA[( port.hw_addr, port.name)] self.lock.release() except: self.lock.release() del self.dictActivePort self.dictActivePort = {} else: # init value #index = 1 # number of port if self.mininetOption == 1 and len(varBindTable) > 0: del varBindTable[len(varBindTable) - 1] for i in varBindTable: # mac address tempHwAddr = i[0][1].prettyPrint() tempHwAddr = tempHwAddr[2:len(tempHwAddr)] if (len(tempHwAddr) != 12): print( "Error invalid mac address from local port in snmp" ) else: tempHwAddr = tempHwAddr[0:2] + ":" + tempHwAddr[ 2:4] + ":" + tempHwAddr[ 4:6] + ":" + tempHwAddr[ 6:8] + ":" + tempHwAddr[ 8:10] + ":" + tempHwAddr[10:12] tempPPort = PPort(i[0][0][-1], tempHwAddr, i[1][1].prettyPrint(), 0, 0, 192, 0, 0, 0) dictPort[str( i[0][0][-1] )] = tempPPort # type interger chage to str """ print("key : " + str(i[0][0][-1]) ) print("item : " + tempPPort ) """ """ key : poistion of active port in snmp (2) item : PPort(object) """ #index += 1 return dictPort, tempHwAddr except Exception as err: count += 1 if count == 1: #send del port to controller for snmpPosition, port in self.dictActivePort.items(): packed_data = portStatus( reason=portReason.OFPPR_DELETE, desc=port) packed_data = packed_data.pack() self.s.send(packed_data) # del port from dict all active port in TDA if len(self.dictActivePort.items()) > 0: try: self.lock.acquire_write() for snmpPosition, port in self.dictActivePort.items( ): del self.dictAllActivePortInTDA[(port.hw_addr, port.name)] self.lock.release() except: self.lock.release() del self.dictActivePort self.dictActivePort = {} print(" 165 Switch ip " + self.switchIp + " handling run-time error : " + str(err)) print(" Switch ip " + self.switchIp + " terminate because : it has problem about snmp ") if initConnection: sys.exit() else: return dictPort, None
def checkStatusOfActivePort(self): listSnmpPosition = self.getActivePortForCheckStatusFromSnmpVersion2C( 0 ) #['2', '3', '5', '8', '9'] tempListActivePort = {} # item = ( snmpPosition , PPort ) for item in listSnmpPosition: #print("item 1 : " + str(item[1].port_no)) if item[0] in self.listActivePort : tempListActivePort[item[0]] = self.listActivePort[item[0]] del self.listActivePort[item[0]] else: #send new port to controller tempListActivePort[item[0]] = item[1] packed_data = portStatus( reason=portReason.OFPPR_ADD , desc=item[1] ) packed_data = packed_data.pack() self.s.send( packed_data ) """ num = input() port = self.listActivePort["06:fd:e2:ae:ab:60"][2] print( "delete port : " + str(port.port_no)) packed_data = portStatus( reason=portReason.OFPPR_DELETE , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) num = input() port.port_no = 10 print( "add port : " + str(port.port_no)) packed_data = portStatus( reason=portReason.OFPPR_ADD , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) return """ # send del port to controller for snmpPosition , port in self.listActivePort.items(): packed_data = portStatus( reason=portReason.OFPPR_DELETE , desc=port ) packed_data = packed_data.pack() self.s.send( packed_data ) #self.listActivePort #print(listSnmpPosition) #print(tempListActivePort) """ print("self.listActivePort") for key,value in self.listActivePort.items(): print("key : " + str(key) + " port_no : " + str(value.port_no) ) print("tempListActivePort") for key,value in tempListActivePort.items(): print("key : " + str(key) + " port_no : " + str(value.port_no) ) """ del self.listActivePort self.listActivePort = tempListActivePort """