def __trunkDeactivate(self, iosConnection, provider, pic): interface = provider.getSwitchInterface() if iosConnection.isFexHost: # print "Can't modify trunk port on a FEX host" self.logger.logError("Can't modify trunk port on a FEX host") return False switchConfig = iosConnection.getConfig(interface, flatten=False) self.logger.logBefore(pic.name, interface, switchConfig) description = iosConnection.getDescription(interface, switchConfig) if description is None or description != pic.getDescription(): self.logger.logError("Can't modify trunk port on a FEX host") # print "DESCRIPTIONS DON'T MATCH ON MODIFY - PIC: {0}, provider: {1}".format(pic.name, provider) iosConnection.enterConfigMode() iosConnection.enterInterfaceConfig(interface) # Shutdown port iosConnection.shutdown() # Switchport trunk allowed vlan 1 iosConnection.setTaggedVlans(Vlan.Vlan1()) iosConnection.setNativeVlan(Vlan.Vlan1()) # no speed iosConnection.setSpeed(Speed.NoSpeed()) iosConnection.leaveInterfaceConfig() iosConnection.leaveConfigMode() self.hostChanged = True
def test_switch(self): # switchport trunk native vlan 1000 # switchport access vlan 167 # switchport voice vlan 2981 vlan1 = Vlan(switchString='switchport trunk native vlan 1000') self.assertIsNotNone(vlan1) self.assertEqual(vlan1.tag, 1000) self.assertTrue(vlan1.trunk) self.assertFalse(vlan1.voice) # self.assertEqual(vlan1.name, "") self.assertIsNone(vlan1.ipAddress) self.assertIsNone(vlan1.mask) vlan2 = Vlan(switchString='switchport access vlan 167') self.assertIsNotNone(vlan2) self.assertEqual(vlan2.tag, 167) self.assertFalse(vlan2.trunk) self.assertFalse(vlan2.voice) # self.assertEqual(vlan2.name, '') self.assertIsNone(vlan2.ipAddress) self.assertIsNone(vlan2.mask) vlan3 = Vlan(switchString='switchport voice vlan 2981') self.assertIsNotNone(vlan3) self.assertEqual(vlan3.tag, 2981) self.assertFalse(vlan3.trunk) self.assertTrue(vlan3.voice) # self.assertEqual(vlan3.name, '') self.assertIsNone(vlan3.ipAddress) self.assertIsNone(vlan3.mask)
def test_duplicateTrunks(self): vlans1 = [1] list1 = Vlan.AllowedVlanList('switchport trunk allowed vlan add 1,1,1') self.assertEqual(len(list1), len(vlans1)) vlans2 = [1, 2, 3] list21 = Vlan.AllowedVlanList('switchport trunk allowed vlan add 1,2') list22 = Vlan.AllowedVlanList('switchport trunk allowed vlan add 2,3') list2 = Vlan.JoinAllowedVlanList(list21, list22) self.assertEqual(len(list2), len(vlans2))
class PICConfig: voiceVlan = None vlan = None # Can be an array [Native vlan or access vlan] taggedVlans = None vlanList = False speed = None trunk = False services = None def __init__(self, vlan, speed): if type(speed) is not str or vlan is None: raise AttributeError("PICConfig given null attributes") if type(vlan) is str: self.vlan = Vlan(risqueString=vlan) self.vlanList = False elif isinstance(vlan, list): self.trunk = True self.vlan = [] for v in vlan: self.vlan.append(Vlan(risqueString=v)) self.vlanList = True self.speed = Speed(risqueString=speed) def addVoiceVlan(self, voiceVlan): if isinstance(voiceVlan, Vlan): self.voiceVlan = voiceVlan return if type(voiceVlan) is not str: raise AttributeError("addVoiceVlan given null attributes") self.voiceVlan = Vlan(risqueString=voiceVlan) def addTaggedVlans(self, taggedVlans): if not isinstance(taggedVlans, list): raise AttributeError("addTaggedVlans given null attributes") self.taggedVlans = list() for vlan in taggedVlans: self.taggedVlans.append(Vlan(risqueString=vlan)) self.trunk = True def addServices(self, services): if not isinstance(services, list): raise AttributeError("addServices given null attributes") self.services = Services(services) # returns a new PICConfig of the differences between two configs, null if the same @staticmethod def diffConfig(oldConfig, newConfig): if oldConfig is None or newConfig is None: raise AttributeError("diffConfig given null attributes") if not isinstance(oldConfig, PICConfig) or not isinstance( newConfig, PICConfig): raise AttributeError( "diffConfig can't diff objects that aren't of type PICConfig")
def test_trunkList(self): # list1 = Vlan.AllowedVlanList('switchport trunk allowed vlan 738,857,878,1000,1001,1307-1310,1313,1324,1325') # list2 = Vlan.AllowedVlanList('switchport trunk allowed vlan add 2700,3017') # Vlan.JoinAllowedVlanList(list1, list2) # for i in Vlan.JoinAllowedVlanList(list1, list2): # print i.tag vlans11 = [ 738, 857, 878, 1000, 1001, 1307, 1308, 1309, 1310, 1313, 1324, 1325 ] vlans12 = [2700, 3017] list11 = Vlan.AllowedVlanList( 'switchport trunk allowed vlan 738,857,878,1000,1001,1307-1310,1313,1324,1325' ) self.assertIsNotNone(list11) for vlan in vlans11: found = False for vlanObj in list11: if vlanObj.tag == vlan: self.assertEqual(vlanObj.tag, vlan) found = True break self.assertTrue(found) list12 = Vlan.AllowedVlanList( 'switchport trunk allowed vlan add 2700,3017') self.assertIsNotNone(list12) for vlan in vlans12: found = False for vlanObj in list12: if vlanObj.tag == vlan: self.assertEqual(vlanObj.tag, vlan) found = True break self.assertTrue(found) list13 = Vlan.JoinAllowedVlanList(list11, list12) for vlan in vlans11: found = False for vlanObj in list13: if vlanObj.tag == vlan: self.assertEqual(vlanObj.tag, vlan) found = True break self.assertTrue(found) for vlan in vlans12: found = False for vlanObj in list13: if vlanObj.tag == vlan: self.assertEqual(vlanObj.tag, vlan) found = True break self.assertTrue(found)
def __init__(self, vlan, speed): if type(speed) is not str or vlan is None: raise AttributeError("PICConfig given null attributes") if type(vlan) is str: self.vlan = Vlan(risqueString=vlan) self.vlanList = False elif isinstance(vlan, list): self.trunk = True self.vlan = [] for v in vlan: self.vlan.append(Vlan(risqueString=v)) self.vlanList = True self.speed = Speed(risqueString=speed)
def addTaggedVlans(self, taggedVlans): if not isinstance(taggedVlans, list): raise AttributeError("addTaggedVlans given null attributes") self.taggedVlans = list() for vlan in taggedVlans: self.taggedVlans.append(Vlan(risqueString=vlan)) self.trunk = True
def addVoiceVlan(self, voiceVlan): if isinstance(voiceVlan, Vlan): self.voiceVlan = voiceVlan return if type(voiceVlan) is not str: raise AttributeError("addVoiceVlan given null attributes") self.voiceVlan = Vlan(risqueString=voiceVlan)
def __basicDeactivate(self, iosConnection, provider, pic): interface = None if iosConnection.isFexHost: interface = iosConnection.findFexInterface(pic, provider) else: interface = provider.getSwitchInterface() switchConfig = iosConnection.getConfig(interface, flatten=False) self.logger.logBefore(pic.name, interface, switchConfig) description = iosConnection.getDescription(interface, switchConfig) if description is None or description != pic.getDescription(): # print "DESCRIPTIONS DON'T MATCH ON MODIFY - PIC: {0}, provider: {1}".format(pic.name, provider) self.logger.logWarning( "DESCRIPTIONS DON'T MATCH ON MODIFY - PIC: {0}, provider: {1}". format(pic.name, provider), True) iosConnection.enterConfigMode() iosConnection.enterInterfaceConfig(interface) # Shutdown port iosConnection.shutdown() # Switchport access vlan 1 iosConnection.setVlan(Vlan.Vlan1()) # no speed iosConnection.setSpeed(Speed.NoSpeed()) iosConnection.leaveInterfaceConfig() iosConnection.leaveConfigMode() self.hostChanged = True
def test_PIC(self): pic = PIC('GCMB-103-B', 'gcmb-100-c3560cg-01-Gi0/6', 'gcmb-100-c3560cg-01-Gi0/6', 'Activate', None) pic.applyNewConfig(None, '128.210.021.000/24 Public Subnet (21)', '10/100/1000T-SW-A') self.assertEqual(pic.newConfig.vlan.tag, 21) self.assertEqual(pic.newConfig.vlan.ipAddress, '128.210.021.000') self.assertEqual(pic.newConfig.speed.speedTuple[0], 10) self.assertEqual(pic.newConfig.speed.speedTuple[1], 100) self.assertEqual(pic.newConfig.speed.speedTuple[2], 1000) pic2 = PIC('MJIS-1063HW-UPS', 'mjis-1063-c9348uxm-01:01-Te1/0/48', 'mjis-1063-c9348uxm-01:01-Te1/0/48', 'Modify', None) pic2.applyCurrentConfig( None, 'ITIS Networks Wired Device Management Subnet (1000)', '100T-SW-F') pic2.applyNewConfig( None, 'ITIS Networks Wired Device Management Subnet (1000)', '100/1000/2.5G/5G/10G-SW-A') self.assertTrue( Vlan.equal(pic2.currentConfig.vlan, pic2.newConfig.vlan)) self.assertEqual(pic2.currentConfig.vlan.tag, 1000) self.assertEqual(pic2.currentConfig.speed.speedTuple[1], 100) self.assertEqual(pic2.newConfig.speed.speedTuple[1], 100) self.assertEqual(pic2.newConfig.speed.speedTuple[5], 10000)
vlan_address = m.group(1) if vlan_description is None: vlan_interface_description_line = device.config.get_filtered_lines( "interfaces", ["unit " + vlan_id + " ", "description"], []) if len(vlan_interface_description_line) > 0: m = re.search('description (.*)', vlan_interface_description_line[0]) vlan_description = m.group(1) else: vlan_description = "" if "153.9." in vlan_address: if "de" in vlan_description: vlan_description = vlan_description[:-2] vlans.append( Vlan(vlan_name, vlan_id, vlan_description, vlan_address)) device.set_vlans(vlans) for device in devices: print device.name for vlan in device.vlans: print "\t" + vlan.name + " " + vlan.id + " " + vlan.description + " " + vlan.address pl = packetlogic2.connect("10.254.29.11", user, password) r = pl.Ruleset() for device in devices: device_obj_name = device.name + "no" obj = r.object_get('/NetObjects/' + device_obj_name) if not obj: r.object_add('/NetObjects/' + device_obj_name)
def __verifyTrunkWork(self, iosConnection, provider, pic): interface = provider.getSwitchInterface() switchConfig = iosConnection.getConfig(interface, flatten=False) self.logger.logAfter(pic.name, interface, switchConfig) risqueConfig = pic.getConfig() if switchConfig is None: # print("{0} - Failed to get config".format(pic.name) self.logger.logError("{0} - Failed to get config".format(pic.name), True) return False passed = True voiceVlan = iosConnection.getVoiceVlan(interface, switchConfig) description = iosConnection.getDescription(interface, switchConfig) mode = iosConnection.getSwitchportMode(interface, switchConfig) speed = iosConnection.getSpeed(interface, switchConfig) # Check speed if speed is None or speed.speedTuple != risqueConfig.speed.speedTuple: # print("{0} - incorrect speed".format(pic.name) # print("\trisque: {0}, switch: {1}".format(risqueConfig.speed, speed) self.logger.logError("{0} - incorrect speed".format(pic.name), True) self.logger.logError( "\trisque: {0}, switch: {1}".format(risqueConfig.speed, speed), True) passed = False # Check shut status if iosConnection.isShutdown(interface, switchConfig): # print("{0} - port is shutdown".format(pic.name) self.logger.logError("{0} - port is shutdown".format(pic.name), True) passed = False # Check voice if risqueConfig.voiceVlan is not None: if voiceVlan is None or voiceVlan.tag != risqueConfig.voiceVlan.tag: # print("{0} - incorrect voice vlan".format(pic.name) # print("\trisque: {0}, switch: {1}".format(risqueConfig.voiceVlan, voiceVlan) self.logger.logError( "{0} - incorrect voice vlan".format(pic.name), True) self.logger.logError( "\trisque: {0}, switch: {1}".format( risqueConfig.voiceVlan, voiceVlan), True) passed = False nativeVlan = iosConnection.getNativeVlan(interface, switchConfig) taggedVlans = iosConnection.getTaggedVlans(interface, switchConfig) # Check mode if mode != "trunk": # print("{0} - switchport is not set to trunk mode".format(pic.name) self.logger.logError( "{0} - switchport is not set to trunk mode".format(pic.name), True) passed = False # Check the native vlan (IT MAY NOT EXIST) if risqueConfig.vlan is not None and risqueConfig.vlan.tag != 1: if nativeVlan is None or nativeVlan.tag != risqueConfig.vlan.tag: # print("{0} - incorrect native vlan".format(pic.name) # print("\trisque: {0}, switch: {1}".format(risqueConfig.vlan, nativeVlan) self.logger.logError( "{0} - incorrect native vlan".format(pic.name), True) self.logger.logError( "\trisque: {0}, switch: {1}".format( risqueConfig.vlan, nativeVlan), True) passed = False # Check tagged vlans for vlan in risqueConfig.taggedVlans: if vlan is None or not Vlan.tagInVlanList(taggedVlans, vlan.tag): # print("{0} missing tagged vlan {1}".format(pic.name, vlan) self.logger.logError( "{0} missing tagged vlan {1}".format(pic.name, vlan), True) passed = False return passed
def addVlanList(self, consoleOutput): processingList = consoleOutput.rstrip("\n").split("\n") processingList = processingList[3:] for i in processingList: vlan = i.split() self.vlans.append(Vlan(vlan[1], vlan[0]))
def test_risque(self): # Vlan(risqueString='128.046.170.000/24 Public Subnet (470)').printDebug() # Vlan(risqueString='128.210.071.000/24 Public Subnet').printDebug() # Vlan(risqueString='172.021.031.000/24-VetSchoolPrintersDev (906)').printDebug() # Vlan(risqueString='010.162.017.000/24-STEW-CSDS-Supported_Computers_1 (1211)').printDebug() # Vlan(risqueString='128.046.202.000/24 Public Subnet (502)').printDebug() vlan1 = Vlan(risqueString='128.046.170.000/24 Public Subnet (470)') self.assertIsNotNone(vlan1) self.assertEqual(vlan1.name, 'Public Subnet') self.assertEqual(vlan1.ipAddress, '128.046.170.000') self.assertEqual(vlan1.trunk, False) self.assertEqual(vlan1.mask, 24) self.assertEqual(vlan1.tag, 470) vlan2 = Vlan(risqueString='128.210.071.000/24 Public Subnet') self.assertIsNotNone(vlan2) self.assertEqual(vlan2.name, 'Public Subnet') self.assertEqual(vlan2.ipAddress, '128.210.071.000') self.assertEqual(vlan2.trunk, False) self.assertEqual(vlan2.mask, 24) self.assertIsNone(vlan2.tag) vlan3 = Vlan( risqueString='172.021.031.000/24-VetSchoolPrintersDev (906)') self.assertIsNotNone(vlan3) self.assertEqual(vlan3.name, 'VetSchoolPrintersDev') self.assertEqual(vlan3.ipAddress, '172.021.031.000') self.assertEqual(vlan3.trunk, False) self.assertEqual(vlan3.mask, 24) self.assertEqual(vlan3.tag, 906) vlan4 = Vlan( risqueString= '010.162.017.000/24-STEW-CSDS-Supported_Computers_1 (1211)') self.assertIsNotNone(vlan4) self.assertEqual(vlan4.name, 'STEW-CSDS-Supported_Computers_1') self.assertEqual(vlan4.ipAddress, '010.162.017.000') self.assertEqual(vlan4.trunk, False) self.assertEqual(vlan4.mask, 24) self.assertEqual(vlan4.tag, 1211) vlan5 = Vlan(risqueString='128.046.202.000/24 Public Subnet (502)') self.assertIsNotNone(vlan5) self.assertEqual(vlan5.name, 'Public Subnet') self.assertEqual(vlan5.ipAddress, '128.046.202.000') self.assertEqual(vlan5.trunk, False) self.assertEqual(vlan5.mask, 24) self.assertEqual(vlan5.tag, 502) vlan6 = Vlan( risqueString='ITIS Networks Wired Device Management Subnet (1000)') self.assertIsNotNone(vlan6) self.assertEqual(vlan6.name, 'ITIS Networks Wired Device Management Subnet') self.assertIsNone(vlan6.ipAddress) self.assertEqual(vlan6.trunk, False) self.assertIsNone(vlan6.mask) self.assertEqual(vlan6.tag, 1000) vlan7 = Vlan( risqueString="KRAN/RAWL Card Swipe System Private Subnet (903)") self.assertIsNotNone(vlan7) with self.assertRaises(AttributeError) as cm: Vlan(risqueString='') self.assertEqual(cm.exception.message, "risqueString is not a valid vlan")