Beispiel #1
0
    def test_get_vlans(self):
        all_vlans = [Vlan(1, 'first'), Vlan(2, 'second')]

        self.real_switch_mock.should_receive("get_vlans").once().and_return(
            all_vlans)
        assert_that(self.switch.get_vlans(), is_(all_vlans))
        assert_that(self.switch.get_vlans(), is_(all_vlans))
Beispiel #2
0
    def test_remove_ip_from_vlan(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(2, ips=[IPNetwork("2.2.2.2/24"),
                                      IPNetwork("1.1.1.1/24"),
                                      IPNetwork("1.1.1.2/24"),
                                      IPNetwork("1.1.1.3/24"),
                                      IPNetwork("1.1.1.4/24")])])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("remove_ip_from_vlan").once() \
            .with_args(2, ExactIpNetwork("1.1.1.2/24"))

        self.switch.remove_ip_from_vlan(2, IPNetwork("1.1.1.2/24"))

        assert_that(
            self.switch.get_vlans(),
            is_([
                Vlan(2,
                     ips=[
                         ExactIpNetwork("2.2.2.2/24"),
                         ExactIpNetwork("1.1.1.1/24"),
                         ExactIpNetwork("1.1.1.3/24"),
                         ExactIpNetwork("1.1.1.4/24")
                     ])
            ]))
Beispiel #3
0
    def test_remove_vlan(self):
        self.real_switch_mock.should_receive("get_vlans").once().and_return(
            [Vlan(1), Vlan(2)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("remove_vlan").once().with_args(1)
        self.switch.remove_vlan(1)

        assert_that(self.switch.get_vlans(), is_([Vlan(2)]))
    def test_add_vlan_first(self):
        all_vlans = [Vlan(1), Vlan(2), Vlan(123, name='allo')]

        self.real_switch_mock.should_receive("add_vlan").once().with_args(123, name='allo')
        self.switch.add_vlan(123, 'allo')

        self.real_switch_mock.should_receive("get_vlans").once().and_return(
            all_vlans)
        assert_that(self.switch.get_vlans(), is_(all_vlans))
        assert_that(self.switch.get_vlans(), is_(all_vlans))
Beispiel #5
0
    def test_remove_vrrp_group(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(1, vrrp_groups=[VrrpGroup(id=2)])])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("remove_vrrp_group").once() \
            .with_args(1, 2)

        self.switch.remove_vrrp_group(1, 2)

        assert_that(self.switch.get_vlans(), is_([Vlan(1, vrrp_groups=[])]))
Beispiel #6
0
    def test_set_vlan_arp_routing_state(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(2)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive('set_vlan_arp_routing_state').once() \
            .with_args(2, OFF)

        self.switch.set_vlan_arp_routing_state(2, OFF)

        assert_that(self.switch.get_vlans(), is_([Vlan(2, arp_routing=False)]))
Beispiel #7
0
    def test_add_dhcp_relay_server(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(2)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("add_dhcp_relay_server").once() \
            .with_args(2, IPAddress("1.2.3.4"))

        self.switch.add_dhcp_relay_server(2, IPAddress("1.2.3.4"))

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(2, dhcp_relay_servers=[IPAddress("1.2.3.4")])]))
Beispiel #8
0
    def test_unset_vlan_vrf(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(123, vrf_forwarding='vrf-name')])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("unset_vlan_vrf").once() \
            .with_args(123)

        self.switch.unset_vlan_vrf(123)

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(123, vrf_forwarding=None)]))
Beispiel #9
0
    def test_set_vlan_icmp_redirects_state(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(2)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive('set_vlan_icmp_redirects_state').once() \
            .with_args(2, False)

        self.switch.set_vlan_icmp_redirects_state(2, False)

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(2, icmp_redirects=False)]))
Beispiel #10
0
    def test_add_ip_to_vlan(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(2)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("add_ip_to_vlan").once() \
            .with_args(2, ExactIpNetwork("2.2.2.2/24"))

        self.switch.add_ip_to_vlan(2, IPNetwork("2.2.2.2/24"))

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(2, ips=[ExactIpNetwork("2.2.2.2/24")])]))
    def test_set_vlan_load_interval(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(123)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("set_vlan_load_interval").once() \
            .with_args(123, 30)
        self.switch.set_vlan_load_interval(123, 30)

        assert_that(
            self.switch.get_vlans(),
            is_([Vlan(123, load_interval=30)]))
Beispiel #12
0
    def test_set_vlan_access_group(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(123)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("set_vlan_access_group").once() \
            .with_args(123, IN, 'vlan-access-group')

        self.switch.set_vlan_access_group(123, IN, 'vlan-access-group')

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(123, access_group_in='vlan-access-group')]))
Beispiel #13
0
    def test_unset_vlan_access_group(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(123, access_group_out='vlan-access-group')])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("unset_vlan_access_group").once() \
            .with_args(123, OUT)

        self.switch.unset_vlan_access_group(123, OUT)

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(123, access_group_out=None)]))
    def test_set_vlan_mpls_ip_state_true(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(123, mpls_ip=False)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("set_vlan_mpls_ip_state").once() \
            .with_args(123, True)
        self.switch.set_vlan_mpls_ip_state(123, True)

        assert_that(
            self.switch.get_vlans(),
            is_([Vlan(123, mpls_ip=True)]))
Beispiel #15
0
    def test_unset_vlan_unicast_rpf_mode(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(2, unicast_rpf_mode=STRICT)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive('unset_vlan_unicast_rpf_mode').once() \
            .with_args(2)

        self.switch.unset_vlan_unicast_rpf_mode(2)

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(2, unicast_rpf_mode=None)]))
Beispiel #16
0
    def test_add_vrrp_group(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(1)])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("add_vrrp_group").once() \
            .with_args(1, 2, ips=None, priority=23, hello_interval=None,
                       dead_interval=None, track_id=None, track_decrement=None)

        self.switch.add_vrrp_group(1, 2, priority=23)

        assert_that(self.switch.get_vlans(),
                    is_([Vlan(1, vrrp_groups=[VrrpGroup(id=2, priority=23)])]))
    def test_remove_vlan_varp_ip(self):
        self.real_switch_mock.should_receive("get_vlans").once() \
            .and_return([Vlan(1, varp_ips=[IPNetwork("1.1.1.1/29")])])
        self.switch.get_vlans()

        self.real_switch_mock.should_receive("remove_vlan_varp_ip").once() \
            .with_args(1, IPNetwork("1.1.1.1/29"))

        self.switch.remove_vlan_varp_ip(1, IPNetwork("1.1.1.1/29"))

        assert_that(
            self.switch.get_vlans(),
            is_([Vlan(1, varp_ips=[])]))
    def test_access_new_vlan_after_vlan_list(self):
        all_vlans = [Vlan(1, 'first'), Vlan(2, 'second')]
        vlan3 = Vlan(3, 'third', ips=[IPNetwork("2.2.2.2/24")])

        self.real_switch_mock.should_receive("get_vlans").once().and_return(all_vlans)

        self.real_switch_mock.should_receive("add_ip_to_vlan").once().with_args(3, ExactIpNetwork("2.2.2.2/24"))
        self.real_switch_mock.should_receive("get_vlan").with_args(3).once().and_return(vlan3)

        assert_that(self.switch.get_vlans(), is_(all_vlans))

        self.switch.add_ip_to_vlan(3, IPNetwork("2.2.2.2/24"))

        assert_that(self.switch.get_vlan(3), is_(vlan3))
Beispiel #19
0
    def get_vlan_from_node(self, vlan_node, config):
        vlan_id_node = first(vlan_node.xpath("vlan-id"))

        vlan = None
        if vlan_id_node is not None:
            vlan = Vlan(number=int(vlan_id_node.text), icmp_redirects=True)

            description_node = first(vlan_node.xpath("description"))
            if description_node is not None:
                vlan.name = description_node.text

            l3_if_type, l3_if_name = self.custom_strategies.get_l3_interface(
                vlan_node)
            if l3_if_name is not None:
                interface_vlan_node = first(
                    config.xpath(
                        "data/configuration/interfaces/interface/name[text()=\"{}\"]/.."
                        "/unit/name[text()=\"{}\"]/..".format(
                            l3_if_type, l3_if_name)))
                if interface_vlan_node is not None:
                    vlan.ips = parse_ips(interface_vlan_node)
                    vlan.access_groups[IN] = parse_inet_filter(
                        interface_vlan_node, "input")
                    vlan.access_groups[OUT] = parse_inet_filter(
                        interface_vlan_node, "output")
                    vlan.vrrp_groups = self.custom_strategies.parse_vrrp_groups(
                        interface_vlan_node)
                    vlan.icmp_redirects = self.custom_strategies.parse_icmp_redirects(
                        interface_vlan_node)
        return vlan
Beispiel #20
0
    def get_vlans(self):
        vlan_list = self.ssh.do("show vlan brief")

        vlans = {}
        for line in vlan_list:
            if regex.match('^(\d+)\s+(\S+).*', line):
                number, name = regex

                if name == ("VLAN{}".format(number)):
                    name = None

                vlans[number] = Vlan(int(number),
                                     name,
                                     icmp_redirects=True,
                                     arp_routing=True,
                                     ntp=True)

        for ip_interface_data in split_on_dedent(
                self.ssh.do("show ip interface")):
            if regex.match("^Vlan(\d+)\s.*", ip_interface_data[0]):
                current_vlan = vlans.get(regex[0])
                if current_vlan:
                    apply_interface_running_config_data(
                        current_vlan,
                        self.ssh.do(
                            "show running-config interface vlan {}".format(
                                current_vlan.number)))
        return vlans.values()
Beispiel #21
0
    def test_get_vlan(self):
        a_vlan = Vlan(1, 'first')

        self.real_switch_mock.should_receive("get_vlan").with_args(
            1).once().and_return(a_vlan)
        assert_that(self.switch.get_vlan(1), is_(a_vlan))
        assert_that(self.switch.get_vlan(1), is_(a_vlan))
Beispiel #22
0
    def test_add_vlan(self):
        self.real_switch_mock.should_receive("add_vlan").once().with_args(123)
        self.switch.add_vlan(123)
        self.real_switch_mock.should_receive("get_vlan").once().with_args(
            123).and_return(Vlan(number=123, name=""))

        assert_that(self.switch.get_vlan(123).name, is_(""))
Beispiel #23
0
 def get_vlan(self, number):
     vlan = Vlan(int(number), icmp_redirects=True)
     apply_vlan_running_config_data(vlan, self._get_vlan_run_conf(number))
     apply_interface_running_config_data(
         vlan,
         self.ssh.do("show running-config interface vlan {} | begin interface".format(number))
     )
     return vlan
Beispiel #24
0
    def get_vlan_interface_data(self, vlan_number):
        run_int_vlan_data = self.ssh.do('show running-config interface vlan {}'.format(vlan_number))
        if not run_int_vlan_data[0].startswith("Building configuration..."):
            self._get_vlan_run_conf(vlan_number)

        vlan = Vlan(vlan_number)
        apply_interface_running_config_data(vlan, run_int_vlan_data)
        return vlan
Beispiel #25
0
def _extract_vlans(vlans_info):
    vlan_list = []
    for id, vlan in vlans_info['vlans'].items():
        if vlan['name'] == "VLAN{:04d}".format(int(id)):
            vlan['name'] = None

        vlan_list.append(Vlan(number=int(id), name=vlan['name'], icmp_redirects=True,
                              arp_routing=True, ntp=True, mpls_ip=True))
    return vlan_list
Beispiel #26
0
    def get_vlan_from_node(self, vlan_node, config):
        vlan_id_node = first(vlan_node.xpath("vlan-id"))

        vlan = None
        if vlan_id_node is not None:
            vlan = Vlan(number=int(vlan_id_node.text))

            description_node = first(vlan_node.xpath("description"))
            if description_node is not None:
                vlan.name = description_node.text

            l3_if_type, l3_if_name = get_l3_interface(vlan_node)
            if l3_if_name is not None:
                interface_vlan_node = first(config.xpath("data/configuration/interfaces/interface/name[text()=\"{}\"]/.."
                                                    "/unit/name[text()=\"{}\"]/..".format(l3_if_type, l3_if_name)))
                if interface_vlan_node is not None:
                    vlan.ips = parse_ips(interface_vlan_node)
                    vlan.access_groups[IN] = parse_inet_filter(interface_vlan_node, "input")
                    vlan.access_groups[OUT] = parse_inet_filter(interface_vlan_node, "output")
        return vlan
Beispiel #27
0
def to_core(serialized):
    access_groups = serialized.pop('access_groups')
    ips = serialized.pop('ips')
    vrrp_groups = serialized.pop('vrrp_groups')
    dhcp_relay_servers = serialized.pop('dhcp_relay_servers')
    return Vlan(
        access_group_in=access_groups['in'],
        access_group_out=access_groups['out'],
        ips=[IPNetwork('{address}/{mask}'.format(**ip)) for ip in ips],
        vrrp_groups=[vrrp_group.to_core(group) for group in vrrp_groups],
        dhcp_relay_servers=[IPAddress(i) for i in dhcp_relay_servers],
        **serialized)
Beispiel #28
0
def parse_vlan_list(result):
    vlans = []
    for line in result:
        if regex.match('^(\d+)(.*)', line):
            number, leftovers = regex
            name = None
            if regex.match('^\s{1,6}(\S+).*', leftovers):
                name = regex[0]
            vlan = Vlan(number=int(number),
                        name=name if int(number) > 1 else "default")
            vlans.append(vlan)
    return vlans
    def test_after_get_vlans_was_cached_adding_new_vlans_will_trigger_single_get_vlans_to_fill_the_gaps(self):
        all_vlans = [Vlan(1)]

        self.real_switch_mock.should_receive("get_vlans").once().ordered().and_return(all_vlans)
        assert_that(self.switch.get_vlans(), is_(all_vlans))

        self.real_switch_mock.should_receive("add_vlan").once().with_args(10)
        self.switch.add_vlan(10)
        self.real_switch_mock.should_receive("add_vlan").once().with_args(11)
        self.switch.add_vlan(11)
        self.real_switch_mock.should_receive("add_vlan").once().with_args(12)
        self.switch.add_vlan(12)

        self.real_switch_mock.should_receive("get_vlan").with_args(10).once().and_return(Vlan(10))
        self.real_switch_mock.should_receive("get_vlan").with_args(11).once().and_return(Vlan(11))
        self.real_switch_mock.should_receive("get_vlan").with_args(12).once().and_return(Vlan(12))
        assert_that(self.switch.get_vlans(), is_([Vlan(1), Vlan(10), Vlan(11), Vlan(12)]))
        assert_that(self.switch.get_vlans(), is_([Vlan(1), Vlan(10), Vlan(11), Vlan(12)]))
Beispiel #30
0
def to_core(serialized):
    access_groups = serialized.pop('access_groups')
    ips = serialized.pop('ips')
    vrrp_groups = serialized.pop('vrrp_groups')
    dhcp_relay_servers = serialized.pop('dhcp_relay_servers')
    varp_ips = serialized.pop('varp_ips')
    return Vlan(
        access_group_in=access_groups['in'],
        access_group_out=access_groups['out'],
        ips=deserialize_ip_network(ips),
        vrrp_groups=[vrrp_group.to_core(group) for group in vrrp_groups],
        dhcp_relay_servers=[IPAddress(i) for i in dhcp_relay_servers],
        varp_ips=deserialize_ip_network(varp_ips),
        **serialized
    )
Beispiel #31
0
    def get_vlans(self):
        config = self.query(all_vlans, all_interfaces)

        vlan_list = []
        for vlan_node in config.xpath("data/configuration/vlans/vlan"):
            number_node = first(vlan_node.xpath("vlan-id"))
            if number_node is not None:
                vlan = Vlan(number=int(number_node.text))
                vlan_list.append(vlan)

                description_node = first(vlan_node.xpath("description"))
                if description_node is not None:
                    vlan.name = description_node.text

                l3_if_type, l3_if_name = get_l3_interface(vlan_node)
                if l3_if_name is not None:
                    interface_node = first(config.xpath("data/configuration/interfaces/interface/name[text()=\"{}\"]/.."
                                                        "/unit/name[text()=\"{}\"]/..".format(l3_if_type, l3_if_name)))
                    if interface_node is not None:
                        vlan.ips = parse_ips(interface_node)
                        vlan.access_groups[IN] = parse_inet_filter(interface_node, "input")
                        vlan.access_groups[OUT] = parse_inet_filter(interface_node, "output")

        return vlan_list
Beispiel #32
0
def parse_vlan_list(result):
    vlans = []
    for line in result:
        if regex.match('^(\d+)(.*)', line):
            number, leftovers = regex
            name = None
            if regex.match('^\s{1,6}(\S+)\s+([A-Za-z0-9-,/]+)', leftovers):
                name, ports = regex
            elif regex.match('^\s{1,6}(\S+).*', leftovers):
                name = regex[0]

            if name == "VLAN{:0>4}".format(number):
                name = None
            vlan = Vlan(number=int(number),
                        name=name if int(number) > 1 else "default")
            vlans.append(vlan)
    return vlans