Пример #1
0
    def test_get_interfaces(self):
        all_interfaces = [Interface('xe-1/0/1'), Interface('xe-1/0/2')]

        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return(all_interfaces)
        assert_that(self.switch.get_interfaces(), is_(all_interfaces))
        assert_that(self.switch.get_interfaces(), is_(all_interfaces))
Пример #2
0
 def node_to_interface(self, interface_node, config):
     interface = Interface()
     if interface_node is not None:
         interface.name = value_of(interface_node.xpath("name"))
         interface.bond_master = get_bond_master(interface_node)
     self.fill_interface_from_node(interface, interface_node, config)
     return interface
Пример #3
0
 def node_to_interface(self, interface_node, config):
     interface = Interface()
     if interface_node is not None:
         interface.name = value_of(interface_node.xpath("name"))
         interface.bond_master = get_bond_master(interface_node)
     self.fill_interface_from_node(interface, interface_node, config)
     return interface
Пример #4
0
def parse_interface(if_data):
    if regex.match("^\w*Ethernet([^\s]*) is (\w*).*", if_data[0]):
        i = Interface(name="ethernet {}".format(regex[0]),
                      port_mode=ACCESS,
                      shutdown=regex[1] == "disabled")
        for line in if_data:
            if regex.match("Port name is (.*)", line): i.description = regex[0]
        return i
Пример #5
0
    def test_reset_interface_invalidate_cache(self):
        self.real_switch_mock.should_receive("get_interface").with_args("eth0").once().\
            and_return([Interface('eth0')])
        self.switch.get_interface('eth0')

        self.real_switch_mock.should_receive("reset_interface").with_args('eth0')
        self.switch.reset_interface('eth0')

        self.real_switch_mock.should_receive("get_interface").with_args("eth0").once(). \
            and_return([Interface('eth0')])
        self.switch.get_interface('eth0')
Пример #6
0
    def test_unset_interface_access_vlan(self):
        self.real_switch_mock.should_receive(
            "get_interfaces").once().and_return(
                [Interface('eth0', access_vlan=123)])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("unset_interface_access_vlan").once() \
            .with_args('eth0')

        self.switch.unset_interface_access_vlan('eth0')

        assert_that(self.switch.get_interfaces(), is_([Interface('eth0')]))
Пример #7
0
    def test_set_trunk_mode(self):
        self.real_switch_mock.should_receive(
            "get_interfaces").once().and_return([Interface('eth0')])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("set_trunk_mode").once() \
            .with_args('eth0')

        self.switch.set_trunk_mode('eth0')

        assert_that(self.switch.get_interfaces(),
                    is_([Interface('eth0', port_mode=TRUNK)]))
Пример #8
0
    def test_unset_interface_state(self):
        self.real_switch_mock.should_receive("get_interfaces").once().and_return([Interface('xe-1/0/2')])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("unset_interface_state").once().with_args('xe-1/0/2')

        self.real_switch_mock.should_receive("get_interface").once() \
            .with_args('xe-1/0/2').and_return(Interface('xe-1/0/2', shutdown=False))

        self.switch.unset_interface_state('xe-1/0/2')

        assert_that(self.switch.get_interface('xe-1/0/2').shutdown, is_(False))
Пример #9
0
    def test_set_interface_state_on(self):
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2')])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("set_interface_state").once() \
            .with_args('xe-1/0/2', ON)

        self.switch.set_interface_state('xe-1/0/2', ON)

        assert_that(self.switch.get_interfaces(),
                    is_([Interface('xe-1/0/2', shutdown=False)]))
Пример #10
0
    def test_set_interface_auto_negotiation_state_on(self):
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2')])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("set_interface_auto_negotiation_state").once() \
            .with_args('xe-1/0/2', ON)

        self.switch.set_interface_auto_negotiation_state('xe-1/0/2', ON)

        assert_that(self.switch.get_interfaces(),
                    is_([Interface('xe-1/0/2', auto_negotiation=True)]))
Пример #11
0
    def test_unset_interface_native_vlan(self):
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2', trunk_native_vlan=20)])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("unset_interface_native_vlan").once() \
            .with_args('xe-1/0/2')

        self.switch.unset_interface_native_vlan('xe-1/0/2')

        assert_that(self.switch.get_interfaces(),
                    is_([Interface('xe-1/0/2', trunk_native_vlan=None)]))
Пример #12
0
    def test_unset_interface_mtu(self):
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2', mtu=5000)])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("unset_interface_mtu").once() \
            .with_args('xe-1/0/2')

        self.switch.unset_interface_mtu('xe-1/0/2')

        assert_that(self.switch.get_interfaces(),
                    is_([Interface('xe-1/0/2', mtu=None)]))
Пример #13
0
    def test_remove_trunk_vlan(self):
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2', trunk_vlans=[1])])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("remove_trunk_vlan").once() \
            .with_args('xe-1/0/2', 1)

        self.switch.remove_trunk_vlan('xe-1/0/2', 1)

        assert_that(self.switch.get_interfaces(),
                    is_([Interface('xe-1/0/2', trunk_vlans=[])]))
Пример #14
0
    def test_set_access_mode(self):
        self.real_switch_mock.should_receive("get_interfaces").once().and_return(
            [Interface('eth0', trunk_native_vlan=1200, trunk_vlans=[1, 2, 3])])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("set_access_mode").once() \
            .with_args('eth0')

        self.switch.set_access_mode('eth0')

        assert_that(
            self.switch.get_interfaces(),
            is_([Interface('eth0', port_mode=ACCESS,
                           trunk_native_vlan=None, trunk_vlans=[])])
        )
Пример #15
0
    def test_get_interface(self):
        interface = Interface('xe-1/0/1')

        self.real_switch_mock.should_receive("get_interface").with_args("xe-1/0/1").once() \
            .and_return(interface)
        assert_that(self.switch.get_interface('xe-1/0/1'), is_(interface))
        assert_that(self.switch.get_interface('xe-1/0/1'), is_(interface))
Пример #16
0
    def test_remove_interface_from_bond(self):
        self.real_switch_mock.should_receive("get_bonds").once() \
            .and_return([Bond(1, members=['xe-1/0/2'])])
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2', bond_master=1)])

        self.switch.get_bonds()
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("remove_interface_from_bond").once() \
            .with_args('xe-1/0/2')

        self.switch.remove_interface_from_bond('xe-1/0/2')

        assert_that(self.switch.get_bonds(), is_([Bond(1, members=[])]))
        assert_that(self.switch.get_interfaces(),
                    is_([Interface('xe-1/0/2', bond_master=None)]))
Пример #17
0
    def read_interface(self, interface_name):
        data = self.get_interface_data(interface_name)

        interface = Interface(name=interface_name, port_mode=ACCESS, shutdown=False)
        for line in data:
            if regex.match("switchport mode \S+", line):
                interface.port_mode = TRUNK
            if regex.match("shutdown", line):
                interface.shutdown = True
            if regex.match("switchport access vlan (\d+)", line):
                interface.access_vlan = int(regex[0])
            if regex.match("switchport general pvid (\d+)", line):
                interface.trunk_native_vlan = int(regex[0])
            if regex.match("switchport \S+ allowed vlan add (\S+)", line):
                interface.trunk_vlans += parse_vlan_ranges(regex[0])

        return interface
Пример #18
0
    def test_unset_interface_auto_negotiation_state(self):
        self.real_switch_mock.should_receive("get_interfaces").once()\
            .and_return([Interface('xe-1/0/2', auto_negotiation=False)])
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("unset_interface_auto_negotiation_state").once().with_args('xe-1/0/2')

        self.switch.unset_interface_auto_negotiation_state('xe-1/0/2')

        assert_that(self.switch.get_interface('xe-1/0/2').auto_negotiation, is_(None))
Пример #19
0
 def interface(self):
     warnings.warn('Deprecated: Use directly the members of Bond instead.',
                   DeprecationWarning)
     return Interface(
         shutdown=self.shutdown,
         port_mode=self.port_mode,
         access_vlan=self.access_vlan,
         trunk_native_vlan=self.trunk_native_vlan,
         trunk_vlans=self.trunk_vlans,
     )
Пример #20
0
def parse_interfaces(interfaces_data, switchports_data):
    interfaces = []
    for interface_data in interfaces_data.values():
        if regex.match("(\w*Ethernet[^\s]*)", interface_data["name"]) or \
                regex.match("(Port-channel[^\s]*)", interface_data["name"]):

            interface = Interface(name=interface_data["name"], shutdown=False)

            if interface_data["lineProtocolStatus"] == "down":
                interface.shutdown = True

            interface.mtu = int(interface_data["mtu"])
            interface.auto_negotiation = ON if interface_data["autoNegotiate"] == "on" else OFF

            if interface.name in switchports_data:
                patch_switchport(interface, switchports_data[interface.name]["switchportInfo"])

            interfaces.append(interface)

    return interfaces
Пример #21
0
 def node_to_interface(self, interface_node, config):
     interface = Interface()
     interface.bond_master = get_bond_master(interface_node)
     interface.port_mode = self.get_port_mode(interface_node) or ACCESS
     vlans = list_vlan_members(interface_node, config)
     if interface.port_mode is ACCESS:
         interface.access_vlan = first(vlans)
     else:
         interface.trunk_vlans = vlans
     interface.trunk_native_vlan = value_of(interface_node.xpath("unit/family/ethernet-switching/native-vlan-id"), transformer=int)
     interface.name = value_of(interface_node.xpath("name"))
     interface.shutdown = first(interface_node.xpath("disable")) is not None
     return interface
Пример #22
0
    def test_add_interface_to_bond(self):
        self.real_switch_mock.should_receive("get_bonds").once() \
            .and_return([Bond(1)])
        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2')])

        self.switch.get_bonds()
        self.switch.get_interfaces()

        self.real_switch_mock.should_receive("add_interface_to_bond").once() \
            .with_args('xe-1/0/2', 1)

        self.switch.add_interface_to_bond('xe-1/0/2', 1)

        self.real_switch_mock.should_receive("get_interfaces").once() \
            .and_return([Interface('xe-1/0/2', bond_master=1, port_mode=BOND_MEMBER)])

        assert_that(self.switch.get_bonds(),
                    is_([Bond(1, members=['xe-1/0/2'])]))
        assert_that(
            self.switch.get_interfaces(),
            is_([Interface('xe-1/0/2', bond_master=1, port_mode=BOND_MEMBER)]))
Пример #23
0
 def to_api(self, bond):
     return dict(
         number=bond.number,
         link_speed=bond.link_speed,
         members=bond.members,
         interface=interface.to_api(Interface(
             shutdown=bond.shutdown,
             port_mode=bond.port_mode,
             access_vlan=bond.access_vlan,
             trunk_native_vlan=bond.trunk_native_vlan,
             trunk_vlans=bond.trunk_vlans,
             mtu=bond.mtu
         ))
     )
Пример #24
0
    def get_interfaces(self):
        interfaces = []
        interface_vlans = {}
        for if_data in split_on_dedent(self.shell.do("show interfaces")):
            if regex.match("^\w*Ethernet([^\s]*) is (\w*).*", if_data[0]):
                i = Interface(name="ethernet {}".format(regex[0]), port_mode=ACCESS, shutdown=regex[1] == "disabled")
                for line in if_data:
                    if regex.match("Port name is (.*)", line): i.description = regex[0]
                interfaces.append(i)
                interface_vlans[i.name] = {
                    "object": i,
                    "untagged": None,
                    "tagged": []
                }

        for vlan_data in split_on_bang(self.shell.do("show running-config vlan")):
            if regex.match("^vlan (\d*)", vlan_data[0]):
                vlan_id = int(regex[0])
                for line in vlan_data:
                    if regex.match(" untagged (.*)", line):
                        for name in _to_real_names(parse_if_ranges(regex[0])):
                            interface_vlans[name]["untagged"] = vlan_id
                    if regex.match(" tagged (.*)", line):
                        for name in _to_real_names(parse_if_ranges(regex[0])):
                            interface_vlans[name]["tagged"].append(vlan_id)

        for data in interface_vlans.values():
            if data["untagged"] is not None and len(data["tagged"]) == 0:
                data["object"].access_vlan = data["untagged"]
            elif data["untagged"] is not None and len(data["tagged"]) > 0:
                data["object"].trunk_native_vlan = data["untagged"]

            if len(data["tagged"]) > 0:
                data["object"].port_mode = TRUNK
                data["object"].trunk_vlans = data["tagged"]

        return interfaces
Пример #25
0
    def read_interface(self, interface_name):
        data = self.get_interface_data(interface_name)

        interface = Interface(name=interface_name, port_mode=ACCESS, shutdown=False)
        for line in data:
            if regex.match("switchport mode \S+", line):
                interface.port_mode = TRUNK
            if regex.match("shutdown", line):
                interface.shutdown = True
            if regex.match("switchport access vlan (\d+)", line):
                interface.access_vlan = int(regex[0])
            if regex.match("switchport general pvid (\d+)", line):
                interface.trunk_native_vlan = int(regex[0])
            if regex.match("switchport \S* allowed vlan (add )?(\S+)", line):
                interface.trunk_vlans = parse_vlan_ranges(regex[1])

        return interface
Пример #26
0
def parse_interface(if_data):
    if regex.match("^\w*Ethernet([^\s]*) is (\w*).*", if_data[0]):
        i = Interface(name="ethernet {}".format(regex[0]), port_mode=ACCESS, shutdown=regex[1] == "disabled")
        for line in if_data:
            if regex.match("Port name is (.*)", line): i.description = regex[0]
        return i
Пример #27
0
 def to_interface(self):
     return Interface(name=self.name,
                      shutdown=self.shutdown,
                      port_mode=ACCESS)
Пример #28
0
def parse_interface(data):
    if data and (regex.match("interface (\w*Ethernet[^\s]*)", data[0])
                 or regex.match("interface (Port-channel[^\s]*)", data[0])):
        i = Interface(name=regex[0], shutdown=False)
        port_mode = access_vlan = native_vlan = trunk_vlans = None
        for line in data:
            if regex.match(" switchport mode (.*)", line):
                port_mode = regex[0]
            if regex.match(" switchport access vlan (\d*)", line):
                access_vlan = int(regex[0])
            if regex.match(" switchport trunk native vlan (\d*)", line):
                native_vlan = int(regex[0])
            if regex.match(" switchport trunk allowed vlan (.*)", line):
                trunk_vlans = regex[0]
            if regex.match(" shutdown", line):
                i.shutdown = True

        if not port_mode:
            i.port_mode = DYNAMIC
            i.access_vlan = access_vlan
            i.trunk_native_vlan = native_vlan
            i.trunk_vlans = parse_vlan_ranges(
                trunk_vlans) if trunk_vlans else []
        elif port_mode == 'access':
            i.port_mode = ACCESS
            i.access_vlan = access_vlan
        elif port_mode == 'trunk':
            i.port_mode = TRUNK
            i.trunk_native_vlan = native_vlan
            i.trunk_vlans = parse_vlan_ranges(
                trunk_vlans) if trunk_vlans else []

        return i
    return None
Пример #29
0
def parse_interface(data):
    if data and (regex.match("interface (\w*Ethernet[^\s]*)", data[0])
                 or regex.match("interface (Port-channel[^\s]*)", data[0])):
        i = Interface(name=regex[0], shutdown=False)
        port_mode = access_vlan = native_vlan = trunk_vlans = None
        for line in data:
            if regex.match(" switchport mode (.*)", line): port_mode = regex[0]
            if regex.match(" switchport access vlan (\d*)", line): access_vlan = int(regex[0])
            if regex.match(" switchport trunk native vlan (\d*)", line): native_vlan = int(regex[0])
            if regex.match(" switchport trunk allowed vlan (.*)", line): trunk_vlans = regex[0]
            if regex.match(" shutdown", line): i.shutdown = True

        if not port_mode:
            i.port_mode = DYNAMIC
            i.access_vlan = access_vlan
            i.trunk_native_vlan = native_vlan
            i.trunk_vlans = parse_vlan_ranges(trunk_vlans) if trunk_vlans else []
        elif port_mode == 'access':
            i.port_mode = ACCESS
            i.access_vlan = access_vlan
        elif port_mode == 'trunk':
            i.port_mode = TRUNK
            i.trunk_native_vlan = native_vlan
            i.trunk_vlans = parse_vlan_ranges(trunk_vlans) if trunk_vlans else []

        return i
    return None
Пример #30
0
 def to_core(self, serialized):
     params = dict(vars(base_interface.to_core(serialized)))
     params.update(sub_dict(serialized, 'name', 'bond_master'))
     return Interface(**params)