def test_vlan_interface_configuration(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxr') dev2 = Device(testbed=testbed, name='PE2', os='iosxr') intf1 = Interface(name='GigabitEthernet0/0/1', device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2', device=dev2) intf3 = Interface(name='GigabitEthernet0/0/3', device=dev1) link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() link.add_feature(vlan) vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].interface_attr[intf1] vlan.device_attr[dev1].interface_attr[intf1].eth_encap_type1 = 'dot1q' vlan.device_attr[dev1].interface_attr[intf1].eth_encap_val1 = 2 vlan.device_attr[dev1].interface_attr[ intf1].eth_encap_type2 = 'second-dot1q' vlan.device_attr[dev1].interface_attr[intf1].eth_encap_val2 = 5 cfg1 = vlan.build_config(apply=False) self.assertCountEqual(cfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(cfg1['PE1']), '\n'.join([ 'interface GigabitEthernet0/0/1', ' encapsulation dot1q 2 second-dot1q 5', ' exit', ]))
def test_cfg_without_igmp(self): Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='nxos') with self.assertNoWarnings(): Genie.testbed = None with self.assertRaises(TypeError): vlan = Vlan() Genie.testbed = testbed vlan = Vlan(vlan='102') vlan.device_attr[dev1].vlan_attr['102'].name = 'vlan2' vlan.device_attr[dev1].vlan_attr['102'].state = 'active' vlan.device_attr[dev1].vlan_attr['102'].mode = 'ce' vlan.device_attr[dev1].config_vlan_attr['102'].config_vlan_id = '102' self.assertIs(vlan.testbed, testbed) dev1.add_feature(vlan) cfgs = vlan.build_config(apply=False) self.assertCountEqual(cfgs.keys(), [dev1.name]) self.assertMultiLineEqual(str(cfgs[dev1.name]), '\n'.join([ 'vlan 102', ' name vlan2', ' state active', ' mode ce', ' exit', 'vlan configuration 102', ' exit', ]))
def test_basic_cfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe', context='yang') dev2 = Device(testbed=testbed, name='PE2', os='iosxe', context='yang') intf1 = Interface(name='GigabitEthernet0/0/1', device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2', device=dev2) intf3 = Interface(name='Vlan100', device=dev1) link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() for dev in testbed.devices: dev.connections = Mock() dev.connections = {'netconf': NetconfConnectionInfo()} link.add_feature(vlan) vlan.vlan_id = 100 cfg1 = vlan.build_config(apply=False) self.assertCountEqual(cfg1.keys(), ['PE1', 'PE2']) compare = "" for i in cfg1['PE1']: compare += str(i) self.assertMultiLineEqual( compare, '\n'.join([ '<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n' ' <target>\n' ' <running></running>\n' ' </target>\n' ' <config>\n' ' <vlan xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">\n' ' <vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan">\n' ' <id>100</id>\n' ' </vlan-list>\n' ' </vlan>\n' ' </config>\n' '</edit-config>\n' ])) compare = "" for i in cfg1['PE2']: compare += str(i) self.assertMultiLineEqual( compare, '\n'.join([ '<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n' ' <target>\n' ' <running></running>\n' ' </target>\n' ' <config>\n' ' <vlan xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">\n' ' <vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan">\n' ' <id>100</id>\n' ' </vlan-list>\n' ' </vlan>\n' ' </config>\n' '</edit-config>\n' ]))
def test_new_vlan_cfg(self): Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') with self.assertNoWarnings(): Genie.testbed = None with self.assertRaises(TypeError): vlan = Vlan() Genie.testbed = testbed vlan = Vlan(vlan='100') vlan.device_attr[dev1].vlan_attr['100'].name = 'new_vlan' vlan.device_attr[dev1].vlan_attr['100'].state = 'active' self.assertIs(vlan.testbed, testbed) dev1.add_feature(vlan) cfgs = vlan.build_config(apply=False) self.assertCountEqual(cfgs.keys(), [dev1.name]) self.assertMultiLineEqual(str(cfgs[dev1.name]), '\n'.join([ 'vlan 100', ' name new_vlan', ' state active', ' exit', ]))
def vlans_remove(netbox_vlans): results = [] for vlan in netbox_vlans: print(f"Removing {vlan.display_name}") new_vlan = Vlan(vlan_id=vlan.vid, name=vlan.name) device.add_feature(new_vlan) output = new_vlan.build_unconfig() results.append({vlan.name: output}) return results
def vlans_configure(netbox_vlans): results = [] for vlan in netbox_vlans: print(f"Creating {vlan.display_name}") new_vlan = Vlan(vlan_id=str(vlan.vid), name=vlan.name) device.add_feature(new_vlan) output = new_vlan.build_config() results.append({vlan.name: output}) # output = testbed.build_config() # return output return results
def test_new_vlan_uncfg(self): Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') with self.assertNoWarnings(): vlan = Vlan(vlan='100') vlan.device_attr[dev1].vlan_attr['100'] self.assertIs(vlan.testbed, testbed) dev1.add_feature(vlan) uncfgs = vlan.build_unconfig(apply=False) self.assertCountEqual(uncfgs.keys(), [dev1.name]) self.assertMultiLineEqual(str(uncfgs[dev1.name]), '\n'.join([ 'no vlan 100', ]))
def test_vlan_interface_configuration(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxr', context='yang') dev2 = Device(testbed=testbed, name='PE2', os='iosxr', context='yang') intf1 = Interface(name='GigabitEthernet0/0/1', device=dev1, context='yang') intf2 = Interface(name='GigabitEthernet0/0/2', device=dev2, context='yang') intf3 = Interface(name='GigabitEthernet0/0/3', device=dev1, context='yang') link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() for dev in testbed.devices: dev.connections = Mock() dev.connections = {'netconf': NetconfConnectionInfo()} link.add_feature(vlan) vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].interface_attr[intf1] vlan.device_attr[dev1].interface_attr[intf1].eth_encap_type1 = 'dot1q' vlan.device_attr[dev1].interface_attr[intf1].eth_encap_val1 = 2 vlan.device_attr[dev1].interface_attr[ intf1].eth_encap_type2 = 'second-dot1q' vlan.device_attr[dev1].interface_attr[intf1].eth_encap_val2 = 5
def test_init(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') dev2 = Device(testbed=testbed, name='PE2', os='iosxe') intf1 = Interface(name='GigabitEthernet0/0/1',device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2',device=dev2) intf3 = Interface(name='Vlan100',device=dev1) link = Link(name='1_2_1',testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) self.assertSetEqual( set(link.find_interfaces()), set([intf1, intf2])) self.assertSetEqual( set(dev1.find_interfaces()), set([intf1, intf3])) self.assertSetEqual( set(dev2.find_interfaces()), set([intf2])) vlan = Vlan() self.assertSetEqual(set(vlan.devices), set([])) self.assertSetEqual(set(vlan.links), set([])) link.add_feature(vlan) self.assertCountEqual(vlan.devices, [dev1, dev2]) self.assertSetEqual(set(vlan.links), set([link])) self.assertSetEqual(set(vlan.interfaces), set([intf1, intf2])) with self.assertRaises(AttributeError): vlan.access_map_id with self.assertRaises(AttributeError): vlan.vlan_configuration_id
def test_new_vlan_unshut(self): Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') with self.assertNoWarnings(): vlan = Vlan(vlan='100') vlan.device_attr[dev1].vlan_attr['100'] vlan.device_attr[dev1].vlan_attr['100'].shutdown = False self.assertIs(vlan.testbed, testbed) dev1.add_feature(vlan) partial_uncfg = vlan.build_config(apply=False) self.assertCountEqual(partial_uncfg.keys(), [dev1.name]) self.assertMultiLineEqual(str(partial_uncfg[dev1.name]), '\n'.join([ 'vlan 100', ' no shutdown', ' exit', ]))
def test_uncfg_new_vlan_name(self): Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') with self.assertNoWarnings(): vlan = Vlan(vlan='100') vlan.device_attr[dev1].vlan_attr['100'].name = "new_vlan" self.assertIs(vlan.testbed, testbed) dev1.add_feature(vlan) uncfgs = vlan.build_unconfig(apply=False, attributes={'device_attr': \ {'*': {'vlan_attr': \ {'*': "name"}}}}) self.assertCountEqual(uncfgs.keys(), [dev1.name]) self.assertMultiLineEqual(str(uncfgs[dev1.name]), '\n'.join([ 'vlan 100', ' no name new_vlan', ' exit', ]))
def test_basic_uncfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxr') dev2 = Device(testbed=testbed, name='PE2', os='iosxr') intf1 = Interface(name='GigabitEthernet0/0/1', device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2', device=dev2) intf3 = Interface(name='GigabitEthernet0/0/3', device=dev1) link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() link.add_feature(vlan) # Defining attributes section vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].interface_attr[intf1] vlan.device_attr[dev1].interface_attr[intf1].eth_encap_type1 = 'dot1q' vlan.device_attr[dev1].interface_attr[intf1].eth_encap_val1 = 2 vlan.device_attr[dev1].interface_attr[ intf1].eth_encap_type2 = 'second-dot1q' vlan.device_attr[dev1].interface_attr[intf1].eth_encap_val2 = 5 # Unconfig testing # Set a mock dev1.cli = Mock() dev1.configure = Mock() dev2.cli = Mock() dev2.configure = Mock() dev1.add_feature(vlan) dev2.add_feature(vlan) # Mock config output = vlan.build_config(apply=True) uncfg = vlan.build_unconfig(apply=False) self.assertMultiLineEqual( str(uncfg['PE1']), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no encapsulation dot1q 2 second-dot1q 5', ' exit', ])) all_vlan_interface_uncfg = vlan.build_unconfig(apply=False, attributes={'device_attr':\ {'*':{'interface_attr':'*'}}}) self.assertCountEqual(all_vlan_interface_uncfg.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(all_vlan_interface_uncfg['PE1']), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no encapsulation dot1q 2 second-dot1q 5', ' exit', ])) partial_vlan_interface_uncfg = vlan.build_unconfig(apply=False, attributes={'device_attr':\ {'*':{'interface_attr':\ {'*':"eth_encap_type1"}}}}) self.assertCountEqual(partial_vlan_interface_uncfg.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_vlan_interface_uncfg['PE1']), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no encapsulation dot1q 2 second-dot1q 5', ' exit', ]))
def test_enable_disable(self): # For failures self.maxDiff = None # Set testbed Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='nxos') # Create Vlan object vlan1 = Vlan() dev1.add_feature(vlan1) vlan1.device_attr[dev1].enabled = True # Check config strings built correctly cfgs = vlan1.build_config(apply=False) self.assertMultiLineEqual( str(cfgs[dev1.name]), '\n'.join([ 'feature interface-vlan', 'feature vn-segment-vlan-based', ])) # Unconfigure uncfgs = vlan1.build_unconfig(apply=False) self.assertMultiLineEqual( str(uncfgs[dev1.name]), '\n'.join([ 'no feature interface-vlan', 'no feature vn-segment-vlan-based', ])) dev1.remove_feature(vlan1) # Create Vlan object vlan2 = Vlan() dev1.add_feature(vlan2) vlan2.device_attr[dev1].enabled_interface_vlan = True # Check config strings built correctly cfgs = vlan2.build_config(apply=False) self.assertMultiLineEqual( str(cfgs[dev1.name]), '\n'.join([ 'feature interface-vlan', ])) # Unconfigure uncfgs = vlan2.build_unconfig(apply=False) self.assertMultiLineEqual( str(uncfgs[dev1.name]), '\n'.join([ 'no feature interface-vlan', ])) # Remove feature dev1.remove_feature(vlan2) # Create Vlan object vlan3 = Vlan() dev1.add_feature(vlan3) vlan3.device_attr[dev1].enabled_vn_segment_vlan_based = True # Build config cfgs = vlan3.build_config(apply=False) self.assertMultiLineEqual( str(cfgs[dev1.name]), '\n'.join([ 'feature vn-segment-vlan-based', ])) # Unconfigure uncfgs = vlan3.build_unconfig(apply=False) self.assertMultiLineEqual( str(uncfgs[dev1.name]), '\n'.join([ 'no feature vn-segment-vlan-based', ]))
def test_basic_uncfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') dev2 = Device(testbed=testbed, name='PE2', os='iosxe') intf1 = Interface(name='GigabitEthernet0/0/1',device=dev1) intf1.switchport = True intf2 = Interface(name='GigabitEthernet0/0/2',device=dev2) intf3 = Interface(name='Vlan100',device=dev1) vlan = Vlan() vlan.device_attr[dev1].vlan_id = 100 vlan.device_attr[dev2].vlan_id = 300 access_map_id = 'ed' vlan_configuration_id = '3' vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].interface_attr[intf1].switchport_mode = \ 'trunk' vlan.device_attr[dev1].interface_attr[intf1].sw_trunk_allowed_vlan = \ '200-201' vlan.device_attr[dev1].access_map_attr[access_map_id] vlan.device_attr[dev2].access_map_attr[access_map_id] vlan.device_attr[dev1].vlan_configuration_attr[vlan_configuration_id] vlan.device_attr[dev2].vlan_configuration_attr[vlan_configuration_id] # Defining attributes section with self.assertRaises(ValueError): vlan.media = 'invalid' vlan.media = 'enet' self.assertIs(type(vlan.media), Vlan.Media) vlan.accounting_type = 'input' vlan.device_attr[dev1].access_map_action = 'drop' # Unconfig testing # Set a mock dev1.configure = Mock() dev2.configure = Mock() dev1.add_feature(vlan) dev2.add_feature(vlan) # Mock config uncfg1 = vlan.build_unconfig(apply=False) self.assertCountEqual(uncfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(uncfg1['PE1']), '\n'.join([ 'no vlan 100', 'no vlan accounting input', 'no vlan access-map ed', 'no vlan configuration 3', 'interface GigabitEthernet0/0/1', ' no switchport mode trunk', ' no switchport trunk allowed vlan 200-201', ' exit', ])) self.assertMultiLineEqual( str(uncfg1['PE2']), '\n'.join([ 'no vlan 300', 'no vlan accounting input', 'no vlan access-map ed', 'no vlan configuration 3', ])) uncfg_intf = intf1.build_unconfig(apply=False, attributes="switchport") self.assertMultiLineEqual( str(uncfg_intf), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no switchport', ' exit', ])) partial_uncfg1 = vlan.build_unconfig(apply=False, attributes={'device_attr':\ {'*':"media"}}) self.assertCountEqual(partial_uncfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_uncfg1['PE1']), '\n'.join([ 'vlan 100', ' no media enet', ' exit', ])) partial_unconfigure = vlan.build_unconfig(apply=False, attributes={'device_attr':\ {'*':{'access_map_attr':\ {'*':"access_map_action"}}}}) self.assertCountEqual(partial_unconfigure.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_unconfigure['PE1']), '\n'.join([ 'vlan access-map ed', ' no action drop', ' exit', ])) all_vlan_interface_uncfg = vlan.build_unconfig(apply=False, attributes={'device_attr':\ {'*':{'interface_attr':'*'}}}) self.assertCountEqual(all_vlan_interface_uncfg.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(all_vlan_interface_uncfg['PE1']), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no switchport mode trunk', ' no switchport trunk allowed vlan 200-201', ' exit', ])) partial_vlan_interface_uncfg = vlan.build_unconfig(apply=False, attributes={'device_attr':\ {'*':{'interface_attr':\ {'*':"sw_trunk_allowed_vlan"}}}}) self.assertCountEqual(partial_vlan_interface_uncfg.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_vlan_interface_uncfg['PE1']), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no switchport trunk allowed vlan 200-201', ' exit', ]))
def test_basic_uncfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='nxos') dev2 = Device(testbed=testbed, name='PE2', os='nxos') intf1 = Interface(name='Ethernet0/0/1', device=dev1, layer=Layer.L3) intf2 = Interface(name='Ethernet0/0/2', device=dev2, layer=Layer.L2) link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() link.add_feature(vlan) vlan.vlan_id = 100 access_map_id = 'ed' vlan_configuration_id = '3' vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].access_map_attr[access_map_id] vlan.device_attr[dev2].access_map_attr[access_map_id] vlan.device_attr[dev1].interface_attr[intf1] vlan.device_attr[dev2].interface_attr[intf2] vlan.device_attr[dev2].interface_attr[intf2].switchport_mode = \ L2_type.TRUNK vlan.device_attr[dev2].interface_attr[intf2].sw_trunk_allowed_vlan = \ '200-201' vlan.device_attr[dev1].vlan_configuration_attr[vlan_configuration_id] vlan.device_attr[dev2].vlan_configuration_attr[vlan_configuration_id] # Defining attributes section vlan.shutdown = False with self.assertRaises(ValueError): vlan.media = 'invalid' vlan.media = 'enet' self.assertIs(type(vlan.media), Vlan.Media) vlan.egress_port_channel_load_balance_random = True vlan.device_attr[dev1].access_map_action = 'drop' vlan.datalink_flow_monitor = True # Unconfig testing # Set a mock dev1.cli = Mock() dev1.configure = Mock() dev2.cli = Mock() dev2.configure = Mock() dev1.add_feature(vlan) dev2.add_feature(vlan) # Mock config output = vlan.build_config(apply=True) uncfg1 = vlan.build_unconfig(apply=False) self.assertCountEqual(uncfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(uncfg1['PE1']), '\n'.join([ 'no vlan 100', 'no vlan access-map ed', 'no vlan configuration 3', ])) self.assertMultiLineEqual( str(uncfg1['PE2']), '\n'.join([ 'no vlan 100', 'no vlan access-map ed', 'no vlan configuration 3', 'interface Ethernet0/0/2', ' no switchport mode trunk', ' no switchport trunk allowed vlan 200-201', ' exit', ])) partial_uncfg1 = vlan.build_unconfig(apply=False, attributes={'device_attr': \ {'*': "media"}}) self.assertCountEqual(partial_uncfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_uncfg1['PE1']), '\n'.join([ 'vlan 100', ' no media enet', ' exit', ])) partial_unconfigure = vlan.build_unconfig(apply=False, attributes={'device_attr': \ {'*': {'access_map_attr': \ {'*': "access_map_action"}}}}) self.assertCountEqual(partial_unconfigure.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_unconfigure['PE1']), '\n'.join([ 'vlan access-map ed', ' no action drop', ' exit', ])) all_vlan_interface_uncfg = vlan.build_unconfig(apply=False, attributes={ \ 'device_attr': {'*': { \ 'interface_attr': '*'}}}) self.assertCountEqual(all_vlan_interface_uncfg.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(all_vlan_interface_uncfg['PE2']), '\n'.join([ 'interface Ethernet0/0/2', ' no switchport mode trunk', ' no switchport trunk allowed vlan 200-201', ' exit', ])) partial_vlan_interface_uncfg = vlan.build_unconfig(apply=False, attributes={ \ 'device_attr': {'*': \ {'interface_attr': \ { '*': "sw_trunk_allowed_vlan"}}}}) self.assertCountEqual(partial_vlan_interface_uncfg.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(partial_vlan_interface_uncfg['PE2']), '\n'.join([ 'interface Ethernet0/0/2', ' no switchport trunk allowed vlan 200-201', ' exit', ]))
def test_cfg_with_igmp(self): Genie.testbed = testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='nxos') with self.assertNoWarnings(): Genie.testbed = None with self.assertRaises(TypeError): vlan = Vlan() Genie.testbed = testbed vlan = Vlan(vlan='102') vlan.device_attr[dev1].vlan_attr['102'].name = 'vlan2' vlan.device_attr[dev1].vlan_attr['102'].state = 'active' vlan.device_attr[dev1].vlan_attr['102'].mode = 'ce' vlan.device_attr[dev1].config_vlan_attr['102'].config_vlan_id = '102' vlan.device_attr[dev1].config_vlan_attr['102'].ip_igmp_snooping = True self.assertIs(vlan.testbed, testbed) dev1.add_feature(vlan) cfgs = vlan.build_config(apply=False) self.assertCountEqual(cfgs.keys(), [dev1.name]) self.assertMultiLineEqual(str(cfgs[dev1.name]), '\n'.join([ 'vlan 102', ' name vlan2', ' state active', ' mode ce', ' exit', 'vlan configuration 102', ' ip igmp snooping', ' exit' ])) un_cfgs = vlan.build_unconfig(apply=False) self.assertMultiLineEqual(str(un_cfgs[dev1.name]), '\n'.join([ 'no vlan 102', 'no vlan configuration 102', ])) un_cfgs = vlan.build_unconfig(apply=False, attributes={'device_attr': { dev1: { 'vlan_attr': { '102': {"mode": None} }, 'config_vlan_attr': { '102': {"ip_igmp_snooping": None} } } } }) self.assertCountEqual(un_cfgs.keys(), [dev1.name]) self.assertMultiLineEqual(str(un_cfgs[dev1.name]), '\n'.join([ 'vlan 102', ' no mode ce', ' exit', 'vlan configuration 102', ' no ip igmp snooping', ' exit', ]))
def test_basic_cfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='nxos') dev2 = Device(testbed=testbed, name='PE2', os='nxos') intf1 = Interface(name='Ethernet0/0/1', device=dev1, layer=Layer.L3) intf2 = Interface(name='Ethernet0/0/2', device=dev2, layer=Layer.L2) link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() link.add_feature(vlan) vlan.vlan_id = 100 access_map_id = 'ed' vlan_configuration_id = '3' vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].interface_attr[intf1] vlan.device_attr[dev2].interface_attr[intf2] vlan.device_attr[dev2].interface_attr[intf2].switchport_mode = \ L2_type.TRUNK vlan.device_attr[dev2].interface_attr[intf2].sw_trunk_allowed_vlan = \ '200-201' vlan.device_attr[dev1].access_map_attr[access_map_id] vlan.device_attr[dev2].access_map_attr[access_map_id] vlan.device_attr[dev1].vlan_configuration_attr[vlan_configuration_id] vlan.device_attr[dev2].vlan_configuration_attr[vlan_configuration_id] # Defining attributes section vlan.shutdown = False with self.assertRaises(ValueError): vlan.media = 'invalid' vlan.media = 'enet' self.assertIs(type(vlan.media), Vlan.Media) vlan.egress_load_balance = True vlan.device_attr[dev1].access_map_action = 'drop' vlan.datalink_flow_monitor = True intf1.ipv4 = '201.0.12.1' intf1.ipv4.netmask = '255.255.255.0' intf1.speed = 1000 intf1.mtu = 500 intf1.ipv6 = '2001::12:1' cfg1 = vlan.build_config(apply=False) cfg2 = intf1.build_config(apply=False) self.assertCountEqual(cfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(cfg1['PE1']), '\n'.join([ 'vlan 100', ' media enet', ' no shutdown', ' exit', 'vlan access-map ed', ' action drop', ' exit', 'vlan configuration 3', ' egress port-channel load-balance random', ' exit', 'interface Ethernet0/0/1', ' mtu 500', ' ip address 201.0.12.1 255.255.255.0', ' ipv6 address 2001::12:1/128', ' speed 1000', ' exit', ])) self.assertMultiLineEqual( str(cfg1['PE2']), '\n'.join([ 'vlan 100', ' media enet', ' no shutdown', ' exit', 'vlan access-map ed', ' exit', 'vlan configuration 3', ' egress port-channel load-balance random', ' exit', 'interface Ethernet0/0/2', ' switchport mode trunk', ' switchport trunk allowed vlan 200-201', ' exit', ])) self.assertMultiLineEqual( str(cfg2), '\n'.join([ 'interface Ethernet0/0/1', ' mtu 500', ' ip address 201.0.12.1 255.255.255.0', ' ipv6 address 2001::12:1/128', ' speed 1000', ' exit', ]))
def test_basic_uncfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe', context='yang') dev2 = Device(testbed=testbed, name='PE2', os='iosxe', context='yang') intf1 = Interface(name='GigabitEthernet0/0/1', device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2', device=dev2) intf3 = Interface(name='Vlan100', device=dev1) vlan = Vlan() for dev in testbed.devices: dev.connections = Mock() dev.connections = {'netconf': NetconfConnectionInfo()} vlan.device_attr[dev1].vlan_id = 100 vlan.device_attr[dev2].vlan_id = 300 # Unconfig testing # Set a mock dev1.configure = Mock() dev2.configure = Mock() dev1.add_feature(vlan) dev2.add_feature(vlan) # Mock config uncfg1 = vlan.build_unconfig(apply=False) self.assertCountEqual(uncfg1.keys(), ['PE1', 'PE2']) compare = "" for i in uncfg1['PE1']: compare += str(i) # A case has been already raised by JB so the unconfig for yang works as expected. # Currently, retruned xml for yang unconfig is exactly the same for the unconfig one. self.assertMultiLineEqual( compare, '\n'.join([ '<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n' ' <target>\n' ' <running></running>\n' ' </target>\n' ' <config>\n' ' <vlan xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">\n' ' <vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan">\n' ' <id>100</id>\n' ' </vlan-list>\n' ' </vlan>\n' ' </config>\n' '</edit-config>\n' ])) compare = "" for i in uncfg1['PE2']: compare += str(i) self.assertMultiLineEqual( compare, '\n'.join([ '<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n' ' <target>\n' ' <running></running>\n' ' </target>\n' ' <config>\n' ' <vlan xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">\n' ' <vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan">\n' ' <id>300</id>\n' ' </vlan-list>\n' ' </vlan>\n' ' </config>\n' '</edit-config>\n' ]))
def test_vlan_interface_confoguration(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxr') dev2 = Device(testbed=testbed, name='PE2', os='iosxr') intf1 = Interface(name='GigabitEthernet0/0/1', device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2', device=dev2) intf3 = Interface(name='GigabitEthernet0/0/3', device=dev1) link = Link(name='1_2_1', testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() link.add_feature(vlan) vlan.device_attr[dev1] vlan.device_attr[dev2] # Defining attributes section intf1.transceiver_permit_pid_all = True intf1.duplex = 'full' intf1.mac_address = 'aaaa.bbbb.cccc' intf1.eth_encap_type1 = 'dot1q' intf1.eth_encap_val1 = 2 intf1.eth_encap_val2 = 5 intf1.eth_dot1q_type = 'tunneling ethertype' intf1.eth_dot1q_value = '0x9100' intf1.ipv4 = '201.0.12.1' intf2.eth_dot1q_type = 'native vlan' intf2.eth_dot1q_value = '15' cfg1 = intf1.build_config(apply=False) self.assertMultiLineEqual( str(cfg1), '\n'.join([ 'interface GigabitEthernet0/0/1', ' dot1q tunneling ethertype 0x9100', ' encapsulation dot1q 2 second-dot1q 5', ' ipv4 address 201.0.12.1/32', ' transceiver permit pid all', ' duplex full', ' mac-address aaaa.bbbb.cccc', ' exit', ])) cfg2 = intf2.build_config(apply=False) self.assertMultiLineEqual( str(cfg2), '\n'.join([ 'interface GigabitEthernet0/0/2', ' dot1q native vlan 15', ' exit', ])) uncfg = intf1.build_unconfig(apply=False, attributes={ 'mac_address': True, 'duplex': True, 'ipv4': True, 'eth_dot1q_value': True, 'eth_encap_type1': True, }) self.assertMultiLineEqual( str(uncfg), '\n'.join([ 'interface GigabitEthernet0/0/1', ' no encapsulation dot1q 2 second-dot1q 5', ' no ipv4 address 201.0.12.1/32', ' no duplex full', ' no mac-address', ' exit', ]))
def test_basic_uncfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='nxos', context='yang') dev2 = Device(testbed=testbed, name='PE2', os='nxos', context='yang') intf1 = Interface(name='Ethernet0/0/1', device=dev1) intf2 = Interface(name='Ethernet0/0/2', device=dev2) vlan = Vlan() for dev in testbed.devices: dev.connections = Mock() dev.connections = {'netconf': NetconfConnectionInfo()} vlan.device_attr[dev1].vlan_id = 100 vlan.device_attr[dev2].vlan_id = 300 # Unconfig testing # Set a mock dev1.configure = Mock() dev2.configure = Mock() dev1.add_feature(vlan) dev2.add_feature(vlan) # Mock config uncfg1 = vlan.build_unconfig(apply=False) self.assertCountEqual(uncfg1.keys(), ['PE1', 'PE2']) compare = "" for i in uncfg1['PE1']: compare += str(i) self.assertMultiLineEqual( compare, '\n'.join([ '<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n' ' <target>\n' ' <running></running>\n' ' </target>\n' ' <config>\n' ' <vlan xmlns="http://cisco.com/ns/yang/ned/ios">\n' ' <vlan-list>\n' ' <id>100</id>\n' ' </vlan-list>\n' ' </vlan>\n' ' </config>\n' '</edit-config>\n' ])) compare = "" for i in uncfg1['PE2']: compare += str(i) self.assertMultiLineEqual( compare, '\n'.join([ '<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\n' ' <target>\n' ' <running></running>\n' ' </target>\n' ' <config>\n' ' <vlan xmlns="http://cisco.com/ns/yang/ned/ios">\n' ' <vlan-list>\n' ' <id>300</id>\n' ' </vlan-list>\n' ' </vlan>\n' ' </config>\n' '</edit-config>\n' ]))
def test_basic_cfg(self): testbed = Genie.testbed = Testbed() dev1 = Device(testbed=testbed, name='PE1', os='iosxe') dev2 = Device(testbed=testbed, name='PE2', os='iosxe') intf1 = Interface(name='GigabitEthernet0/0/1',device=dev1) intf2 = Interface(name='GigabitEthernet0/0/2',device=dev2) intf3 = Interface(name='Vlan100',device=dev1) link = Link(name='1_2_1',testbed=testbed) link.connect_interface(interface=intf1) link.connect_interface(interface=intf2) vlan = Vlan() link.add_feature(vlan) vlan.vlan_id = 100 access_map_id = 'ed' vlan_configuration_id = '3' vlan.device_attr[dev1] vlan.device_attr[dev2] vlan.device_attr[dev1].interface_attr[intf1].switchport_mode = \ 'trunk' vlan.device_attr[dev1].interface_attr[intf1].sw_trunk_allowed_vlan = \ '200-201' vlan.device_attr[dev1].access_map_attr[access_map_id] vlan.device_attr[dev2].access_map_attr[access_map_id] vlan.device_attr[dev1].vlan_configuration_attr[vlan_configuration_id] vlan.device_attr[dev2].vlan_configuration_attr[vlan_configuration_id] # Defining attributes section vlan.shutdown = False with self.assertRaises(ValueError): vlan.media = 'invalid' vlan.media = 'enet' self.assertIs(type(vlan.media), Vlan.Media) vlan.accounting_type = 'input' vlan.device_attr[dev1].access_map_action = 'drop' vlan.datalink_flow_monitor = True vlan.private_vlan_type = 'association' vlan.private_vlan_association_action = 'add' vlan.private_vlan_association_ids = '201,205' cfg1 = vlan.build_config(apply=False) self.assertCountEqual(cfg1.keys(), ['PE1', 'PE2']) self.assertMultiLineEqual( str(cfg1['PE1']), '\n'.join([ 'vlan 100', ' media enet', ' no shutdown', ' private-vlan association add 201,205', ' exit', 'vlan accounting input', 'vlan access-map ed', ' action drop', ' exit', 'vlan configuration 3', ' datalink flow monitor', ' exit', 'interface GigabitEthernet0/0/1', ' switchport mode trunk', ' switchport trunk allowed vlan 200-201', ' exit', ])) self.assertMultiLineEqual( str(cfg1['PE2']), '\n'.join([ 'vlan 100', ' media enet', ' no shutdown', ' private-vlan association add 201,205', ' exit', 'vlan accounting input', 'vlan access-map ed', ' exit', 'vlan configuration 3', ' datalink flow monitor', ' exit', ]))
interfaces = device.learn("interface").info interfaces["Ethernet1/3"] interfaces["Ethernet1/3"]["enabled"] interfaces["Ethernet1/3"]["oper_status"] interfaces["Ethernet1/3"]["counters"]["out_errors"] interfaces["Ethernet1/3"]["switchport_mode"] interfaces["Ethernet1/3"]["access_vlan"] # Demo: Using genie.conf to configure vlans and interfaces from genie.libs.conf.vlan import Vlan from genie.libs.conf.interface import Interface # Create new Vlan object new_vlan = Vlan(vlan_id="3001", name="GenieConfigured") # Add new vlan to device as a feature device.add_feature(new_vlan) new_vlan.devices # Build the configuration for the vlan, but don't send to device output = new_vlan.build_config(apply=False) output print(output["sbx-n9kv-ao"]) # Build and send the configuration to devices output = new_vlan.build_config(apply=True) # Build and print out the configuraiton to REMOVE the Vlan from the devices output = new_vlan.build_unconfig(apply=False)