Пример #1
0
    def gre(self, node):
        node.gre_tunnels = []
        if not self.anm.has_overlay('gre_tunnel'):
            return

        g_gre_tunnel = self.anm['gre_tunnel']
        if node not in g_gre_tunnel:
            return  # no gre tunnel for node

        gre_node = g_gre_tunnel.node(node)
        neighbors = gre_node.neighbors()
        for index, neigh in enumerate(neighbors, start=1):
            stanza = ConfigStanza(id=index, endpoint=neigh)

            # TODO: try/except here
            # TODO: Explain logic here
            src_int = g_gre_tunnel.edge(node, neigh).src_int
            tunnel_source = node.interface(src_int).id
            stanza.source = tunnel_source
            stanza.destination = "0.0.0.0"  # placeholder for user to replace

            if neigh.tunnel_enabled_ipv4:
                ip_address = neigh.tunnel_ipv4_address
                cidr = neigh.tunnel_ipv4_cidr
                stanza.ipv4_address = ip_address
                stanza.ipv4_subnet = cidr
                stanza.use_ipv4 = True

            if neigh.tunnel_enabled_ipv6:
                cidr = neigh.tunnel_ipv6_cidr
                stanza.ipv4_subnet = cidr
                stanza.use_ipv6 = True

            node.gre_tunnels.append(stanza)
Пример #2
0
    def gre(self, node):
        node.gre_tunnels = []
        if not self.anm.has_overlay('gre_tunnel'):
            return

        g_gre_tunnel = self.anm['gre_tunnel']
        if node not in g_gre_tunnel:
            return   # no gre tunnel for node

        gre_node = g_gre_tunnel.node(node)
        neighbors = gre_node.neighbors()
        for index, neigh in enumerate(neighbors, start=1):
            stanza = ConfigStanza(id=index, endpoint=neigh)

            # TODO: try/except here
            # TODO: Explain logic here
            src_int = g_gre_tunnel.edge(node, neigh).src_int
            tunnel_source = node.interface(src_int).id
            stanza.source = tunnel_source
            stanza.destination = "0.0.0.0"  # placeholder for user to replace

            if neigh.tunnel_enabled_ipv4:
                ip_address = neigh.tunnel_ipv4_address
                cidr = neigh.tunnel_ipv4_cidr
                stanza.ipv4_address = ip_address
                stanza.ipv4_subnet = cidr
                stanza.use_ipv4 = True

            if neigh.tunnel_enabled_ipv6:
                cidr = neigh.tunnel_ipv6_cidr
                stanza.ipv4_subnet = cidr
                stanza.use_ipv6 = True

            node.gre_tunnels.append(stanza)
Пример #3
0
    def ospf(self, node):
        """Returns OSPF links, also sets process_id
        """

        g_ospf = self.anm['ospf']
        g_ipv4 = self.anm['ipv4']
        ospf_node = g_ospf.node(node)
        ospf_stanza = node.add_stanza("ospf")

        ospf_stanza.custom_config = ospf_node.custom_config

        node.ospf.ipv4_mpls_te = False  # default, inherited enable if necessary

        node.ospf.loopback_area = g_ospf.node(node).area or 0

        node.ospf.process_id = ospf_node.process_id
        node.ospf.lo_interface = self.lo_interface

        node.ospf.ospf_links = []

        # aggregate by area
        from collections import defaultdict
        interfaces_by_area = defaultdict(list)

        for interface in node.physical_interfaces:
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface

            ospf_int = g_ospf.interface(interface)
            if ospf_int and ospf_int.is_bound:
                area = ospf_int.area
                #TODO: can we remove the next line?
                area = str(area)  # can't serialize IPAddress object to JSON
                #TODO: put in interface rather than interface.id for consistency
                stanza = ConfigStanza(id = interface.id,
                        cost = int(ospf_int.cost), passive = False)

                if node.ip.use_ipv4:
                    stanza.ipv4_address = ospf_int['ipv4'].ip_address
                    stanza.ipv4_subnet = ospf_int['ipv4'].subnet
                if node.ip.use_ipv6:
                    stanza.ipv6_address = ospf_int['ipv6'].ip_address
                    stanza.ipv6_subnet = ospf_int['ipv6'].subnet

                interfaces_by_area[area].append(stanza)

        loopback_zero = node.loopback_zero
        ospf_loopback_zero = g_ospf.interface(loopback_zero)
        router_area = ospf_loopback_zero.area  # area assigned to router
        router_area = str(router_area)  # can't serialize IPAddress object to JSON
        stanza = ConfigStanza(id = node.loopback_zero.id,
            cost = 0, passive = True)
        interfaces_by_area[router_area].append(stanza)

        node.ospf.interfaces_by_area = ConfigStanza(**interfaces_by_area)

        added_networks = set()
        for interface in node.physical_interfaces:
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface
            ipv4_int = g_ipv4.interface(interface)
            ospf_int = g_ospf.interface(interface)
            if not ospf_int.is_bound:
                continue  # not an OSPF interface
            try:
                ospf_cost = int(ospf_int.cost)
            except TypeError:
                try:
                    ospf_cost = netaddr.IPAddress(ospf_int.cost)
                except (TypeError, netaddr.AddrFormatError):
                    log.debug('Using default OSPF cost of 1 for %s on %s'
                               % (ospf_int, node))
                    ospf_cost = 1  # default
            interface.ospf_cost = ospf_cost
            network = ipv4_int.subnet

            if ospf_int and ospf_int.is_bound and network \
                not in added_networks:  # don't add more than once
                added_networks.add(network)
                link_stanza = ConfigStanza(network = network, interface = interface, area = ospf_int.area)
                node.ospf.ospf_links.append(link_stanza)
Пример #4
0
    def ospf(self, node):
        """Returns OSPF links, also sets process_id
        """

        g_ospf = self.anm['ospf']
        g_ipv4 = self.anm['ipv4']
        ospf_node = g_ospf.node(node)
        ospf_stanza = node.add_stanza("ospf")

        ospf_stanza.custom_config = ospf_node.custom_config

        # default, inherited enable if necessary
        node.ospf.ipv4_mpls_te = False

        node.ospf.loopback_area = g_ospf.node(node).area or 0

        node.ospf.process_id = ospf_node.process_id
        node.ospf.lo_interface = self.lo_interface

        node.ospf.ospf_links = []

        # aggregate by area
        from collections import defaultdict
        interfaces_by_area = defaultdict(list)

        for interface in node.physical_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface

            ospf_int = g_ospf.interface(interface)
            if ospf_int and ospf_int.is_bound:
                area = ospf_int.area
                # TODO: can we remove the next line?
                area = str(area)  # can't serialize IPAddress object to JSON
                # TODO: put in interface rather than interface.id for
                # consistency
                try:
                    cost = int(ospf_int.cost)
                except TypeError:
                    #TODO: log warning here
                    cost = 1
                stanza = ConfigStanza(id=interface.id,
                                      cost=cost, passive=False)

                if node.ip.use_ipv4:
                    stanza.ipv4_address = ospf_int['ipv4'].ip_address
                    stanza.ipv4_subnet = ospf_int['ipv4'].subnet
                if node.ip.use_ipv6:
                    stanza.ipv6_address = ospf_int['ipv6'].ip_address
                    stanza.ipv6_subnet = ospf_int['ipv6'].subnet

                interfaces_by_area[area].append(stanza)

        loopback_zero = node.loopback_zero
        ospf_loopback_zero = g_ospf.interface(loopback_zero)
        router_area = ospf_loopback_zero.area  # area assigned to router
        # can't serialize IPAddress object to JSON
        router_area = str(router_area)
        stanza = ConfigStanza(id=node.loopback_zero.id,
                              cost=0, passive=True)
        interfaces_by_area[router_area].append(stanza)

        node.ospf.interfaces_by_area = ConfigStanza(**interfaces_by_area)

        # TODO: split this into a generic IGP function
        added_networks = set()
        for interface in node.physical_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface
            if not interface.use_ipv4:
                continue
            ipv4_int = g_ipv4.interface(interface)
            ospf_int = g_ospf.interface(interface)
            if not ospf_int.is_bound:
                continue  # not an OSPF interface
            try:
                ospf_cost = int(ospf_int.cost)
            except TypeError:
                try:
                    ospf_cost = netaddr.IPAddress(ospf_int.cost)
                except (TypeError, netaddr.AddrFormatError):
                    log.debug('Using default OSPF cost of 1 for %s on %s',
                              ospf_int, node)
                    ospf_cost = 1  # default
            interface.ospf_cost = ospf_cost
            network = ipv4_int.subnet

            #TODO: refactor to allow injecting loopback IPs, etc into IGP
            if ospf_int and ospf_int.is_bound and network \
                    not in added_networks:  # don't add more than once
                added_networks.add(network)
                link_stanza = ConfigStanza(
                    network=network, interface=interface, area=ospf_int.area)
                node.ospf.ospf_links.append(link_stanza)

        for interface in node.loopback_interfaces():
            phy_int = self.anm['phy'].interface(interface)
            ipv4_int = g_ipv4.interface(interface)
            if not phy_int.inject_to_igp:
                #TODO: need to confirm which area to use
                continue

            network = ipv4_int.subnet
            if network in added_networks:
                #TODO: may want to warn here
                continue # already advertised ("how? - warn if so!)

            # Use the same area as Loopback Zero
            area = node.ospf.loopback_area

            if not network:
                log.info("Not injecting unset network on loopback %s "
                    "to IGP", interface)
                continue

            link_stanza = ConfigStanza(
             network=network, interface=interface, area=area)
            node.ospf.ospf_links.append(link_stanza)
    def ospf(self, node):
        """Returns OSPF links, also sets process_id
        """

        g_ospf = self.anm['ospf']
        g_ipv4 = self.anm['ipv4']
        ospf_node = g_ospf.node(node)
        ospf_stanza = node.add_stanza("ospf")

        ospf_stanza.custom_config = ospf_node.custom_config

        # default, inherited enable if necessary
        node.ospf.ipv4_mpls_te = False

        node.ospf.loopback_area = g_ospf.node(node).area or 0
        node.ospf.process_id = ospf_node.process_id
        node.ospf.lo_interface = self.lo_interface

        node.ospf.ospf_links = []

        # aggregate by area
        from collections import defaultdict
        interfaces_by_area = defaultdict(list)
        for interface in node.physical_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface

            ospf_int = g_ospf.interface(interface)
            if ospf_int and ospf_int.is_bound:
                area = ospf_int.area
                # TODO: can we remove the next line?
                area = str(area)  # can't serialize IPAddress object to JSON
                # TODO: put in interface rather than interface.id for
                # consistency
                try:
                    cost = int(ospf_int.cost)
                except TypeError:
                    #TODO: log warning here
                    cost = 1
                stanza = ConfigStanza(id=interface.id,
                                      cost=cost,
                                      passive=False)

                if node.ip.use_ipv4:
                    stanza.ipv4_address = ospf_int['ipv4'].ip_address
                    stanza.ipv4_subnet = ospf_int['ipv4'].subnet
                if node.ip.use_ipv6:
                    stanza.ipv6_address = ospf_int['ipv6'].ip_address
                    stanza.ipv6_subnet = ospf_int['ipv6'].subnet

                interfaces_by_area[area].append(stanza)

        for interface in node.portchannel_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface

            ospf_int = g_ospf.interface(interface)
            if ospf_int and ospf_int.is_bound:
                area = ospf_int.area
                # TODO: can we remove the next line?
                area = str(area)  # can't serialize IPAddress object to JSON
                # TODO: put in interface rather than interface.id for
                # consistency
                try:
                    cost = int(ospf_int.cost)
                except TypeError:
                    #TODO: log warning here
                    cost = 1
                stanza = ConfigStanza(id=interface.id,
                                      cost=cost,
                                      passive=False)

                if node.ip.use_ipv4:
                    stanza.ipv4_address = ospf_int['ipv4'].ip_address
                    stanza.ipv4_subnet = ospf_int['ipv4'].subnet
                if node.ip.use_ipv6:
                    stanza.ipv6_address = ospf_int['ipv6'].ip_address
                    stanza.ipv6_subnet = ospf_int['ipv6'].subnet

                interfaces_by_area[area].append(stanza)

        loopback_zero = node.loopback_zero
        ospf_loopback_zero = g_ospf.interface(loopback_zero)
        router_area = ospf_loopback_zero.area  # area assigned to router
        # can't serialize IPAddress object to JSON
        router_area = str(router_area)
        stanza = ConfigStanza(id=node.loopback_zero.id, cost=0, passive=True)
        interfaces_by_area[router_area].append(stanza)

        node.ospf.interfaces_by_area = ConfigStanza(**interfaces_by_area)

        # TODO: split this into a generic IGP function
        added_networks = set()
        for interface in node.physical_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface
            if not interface.use_ipv4:
                continue
            ipv4_int = g_ipv4.interface(interface)
            ospf_int = g_ospf.interface(interface)
            if not ospf_int.is_bound:
                continue  # not an OSPF interface
            try:
                ospf_cost = int(ospf_int.cost)
            except TypeError:
                try:
                    ospf_cost = netaddr.IPAddress(ospf_int.cost)
                except (TypeError, netaddr.AddrFormatError):
                    log.debug('Using default OSPF cost of 1 for %s on %s',
                              ospf_int, node)
                    ospf_cost = 1  # default
            interface.ospf_cost = ospf_cost
            network = ipv4_int.subnet

            #TODO: refactor to allow injecting loopback IPs, etc into IGP
            if ospf_int and ospf_int.is_bound and network \
                    not in added_networks:  # don't add more than once
                added_networks.add(network)
                link_stanza = ConfigStanza(network=network,
                                           interface=interface,
                                           area=ospf_int.area)
                node.ospf.ospf_links.append(link_stanza)

        for interface in node.portchannel_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface
            if not interface.use_ipv4:
                continue
            ipv4_int = g_ipv4.interface(interface)
            ospf_int = g_ospf.interface(interface)
            if not ospf_int.is_bound:
                continue  # not an OSPF interface
            try:
                ospf_cost = int(ospf_int.cost)
            except TypeError:
                try:
                    ospf_cost = netaddr.IPAddress(ospf_int.cost)
                except (TypeError, netaddr.AddrFormatError):
                    log.debug('Using default OSPF cost of 1 for %s on %s',
                              ospf_int, node)
                    ospf_cost = 1  # default
            interface.ospf_cost = ospf_cost
            network = ipv4_int.subnet

            #TODO: refactor to allow injecting loopback IPs, etc into IGP
            if ospf_int and ospf_int.is_bound and network \
                    not in added_networks:  # don't add more than once
                added_networks.add(network)
                link_stanza = ConfigStanza(network=network,
                                           interface=interface,
                                           area=ospf_int.area)
                node.ospf.ospf_links.append(link_stanza)

        for interface in node.loopback_interfaces():
            phy_int = self.anm['phy'].interface(interface)
            ipv4_int = g_ipv4.interface(interface)
            if not phy_int.inject_to_igp:
                #TODO: need to confirm which area to use
                continue

            network = ipv4_int.subnet
            if network in added_networks:
                #TODO: may want to warn here
                continue  # already advertised ("how? - warn if so!)

            # Use the same area as Loopback Zero
            area = node.ospf.loopback_area

            if not network:
                log.info(
                    "Not injecting unset network on loopback %s "
                    "to IGP", interface)
                continue

            link_stanza = ConfigStanza(network=network,
                                       interface=interface,
                                       area=area)
            node.ospf.ospf_links.append(link_stanza)
Пример #6
0
    def ospf(self, node):
        """Returns OSPF links, also sets process_id
        """

        g_ospf = self.anm['ospf']
        g_ipv4 = self.anm['ipv4']
        ospf_node = g_ospf.node(node)
        ospf_stanza = node.add_stanza("ospf")

        ospf_stanza.custom_config = ospf_node.custom_config

        node.ospf.ipv4_mpls_te = False  # default, inherited enable if necessary

        node.ospf.loopback_area = g_ospf.node(node).area or 0

        node.ospf.process_id = ospf_node.process_id
        node.ospf.lo_interface = self.lo_interface

        node.ospf.ospf_links = []

        # aggregate by area
        from collections import defaultdict
        interfaces_by_area = defaultdict(list)

        for interface in node.physical_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface

            ospf_int = g_ospf.interface(interface)
            if ospf_int and ospf_int.is_bound:
                area = ospf_int.area
                #TODO: can we remove the next line?
                area = str(area)  # can't serialize IPAddress object to JSON
                #TODO: put in interface rather than interface.id for consistency
                stanza = ConfigStanza(id=interface.id,
                                      cost=int(ospf_int.cost),
                                      passive=False)

                if node.ip.use_ipv4:
                    stanza.ipv4_address = ospf_int['ipv4'].ip_address
                    stanza.ipv4_subnet = ospf_int['ipv4'].subnet
                if node.ip.use_ipv6:
                    stanza.ipv6_address = ospf_int['ipv6'].ip_address
                    stanza.ipv6_subnet = ospf_int['ipv6'].subnet

                interfaces_by_area[area].append(stanza)

        loopback_zero = node.loopback_zero
        ospf_loopback_zero = g_ospf.interface(loopback_zero)
        router_area = ospf_loopback_zero.area  # area assigned to router
        router_area = str(
            router_area)  # can't serialize IPAddress object to JSON
        stanza = ConfigStanza(id=node.loopback_zero.id, cost=0, passive=True)
        interfaces_by_area[router_area].append(stanza)

        node.ospf.interfaces_by_area = ConfigStanza(**interfaces_by_area)

        added_networks = set()
        for interface in node.physical_interfaces():
            if interface.exclude_igp:
                continue  # don't configure IGP for this interface
            ipv4_int = g_ipv4.interface(interface)
            ospf_int = g_ospf.interface(interface)
            if not ospf_int.is_bound:
                continue  # not an OSPF interface
            try:
                ospf_cost = int(ospf_int.cost)
            except TypeError:
                try:
                    ospf_cost = netaddr.IPAddress(ospf_int.cost)
                except (TypeError, netaddr.AddrFormatError):
                    log.debug('Using default OSPF cost of 1 for %s on %s' %
                              (ospf_int, node))
                    ospf_cost = 1  # default
            interface.ospf_cost = ospf_cost
            network = ipv4_int.subnet

            if ospf_int and ospf_int.is_bound and network \
                not in added_networks:  # don't add more than once
                added_networks.add(network)
                link_stanza = ConfigStanza(network=network,
                                           interface=interface,
                                           area=ospf_int.area)
                node.ospf.ospf_links.append(link_stanza)