Exemplo n.º 1
0
    def remove_route_gateway(self, element, network=None):
        """
        Remove a route element by href or Element. Use this if you want to
        remove a netlink or a routing element such as BGP or OSPF. Removing
        is done from within the routing interface context.
        ::
        
            interface0 = engine.routing.get(0)
            interface0.remove_route_gateway(StaticNetlink('mynetlink'))
            
        Only from a specific network on a multi-address interface::
        
            interface0.remove_route_gateway(
                StaticNetlink('mynetlink'),
                network='172.18.1.0/24')
        
        :param str,Element element: element to remove from this routing node
        :param str network: if network specified, only add OSPF to this
            network on interface
        :raises ModificationAborted: Change must be made at the interface level
        :raises UpdateElementFailed: failure to update routing table
        :return: Status of whether the entry was removed (i.e. or not found)
        :rtype: bool
        """
        if self.level not in ('interface'):
            raise ModificationAborted(
                'You must make this change from the '
                'interface routing level. Current node: {}'.format(self))

        node_changed = False
        element = element_resolver(element)
        for network in self:
            # Tunnel Interface binds gateways to the interface
            if network.level == 'gateway' and network.data.get(
                    'href') == element:
                network.delete()
                node_changed = True
                break
            for gateway in network:
                if gateway.data.get('href') == element:
                    gateway.delete()
                    node_changed = True
        return node_changed
Exemplo n.º 2
0
    def _add_gateway_node(self, gw_type, routing_node_gateway, network=None):
        """
        Add a gateway node to existing routing tree. Gateways are only added if
        they do not already exist. If they do exist, check the destinations of
        the existing gateway and add destinations that are not already there.
        A current limitation is that if a gateway doesn't exist and the
        destinations specified do not have IP addresses that are valid, they
        are still added (i.e. IPv4 gateway with IPv6 destination is considered
        invalid).
        
        :param Routing self: the routing node, should be the interface routing node
        :param str gw_type: type of gateway, i.e. netlink, ospfv2_area, etc
        :param list(Element) destinations: list of destinations if any
        :param str network: network to bind to. If none, all networks
        :return: Whether a change was made or not
        :rtype: bool
        """
        if self.level != 'interface':
            raise ModificationAborted(
                'You must make this change from the '
                'interface routing level. Current node: {}'.format(self))

        if self.related_element_type == 'tunnel_interface':
            return self._add_gateway_node_on_tunnel(routing_node_gateway)

        # Find any existing gateways
        routing_node = list(
            gateway_by_type(self, type=gw_type, on_network=network))

        _networks = [netwk for netwk in self if netwk.ip == network] if network is \
            not None else list(self)

        # Routing Node Gateway to add as Element
        gateway_element_type = routing_node_gateway.routing_node_element

        modified = False
        for network in _networks:
            # Short circuit for dynamic interfaces
            if getattr(network, 'dynamic_nicid', None):
                network.data.setdefault('routing_node',
                                        []).append(routing_node_gateway)
                modified = True
                break

            # Used for comparison to
            this_network_node = network.routing_node_element

            if routing_node and any(
                    netwk for _intf, netwk, gw in routing_node
                    if netwk.routing_node_element == this_network_node
                    and gateway_element_type == gw.routing_node_element):

                # A gateway exists on this network
                for gw in network:
                    if gw.routing_node_element == gateway_element_type:
                        existing_dests = [
                            node.routing_node_element for node in gw
                        ]
                        for destination in routing_node_gateway.destinations:
                            is_valid_destination = False
                            if destination not in existing_dests:
                                dest_ipv4, dest_ipv6 = _which_ip_protocol(
                                    destination)
                                if len(network.ip.split(':')) > 1:  # IPv6
                                    if dest_ipv6:
                                        is_valid_destination = True
                                else:
                                    if dest_ipv4:
                                        is_valid_destination = True

                                if is_valid_destination:
                                    gw.data.setdefault('routing_node',
                                                       []).append({
                                                           'level':
                                                           'any',
                                                           'href':
                                                           destination.href,
                                                           'name':
                                                           destination.name
                                                       })
                                    modified = True

            else:  # Gateway doesn't exist
                gw_ipv4, gw_ipv6 = _which_ip_protocol(
                    gateway_element_type)  # ipv4, ipv6 or both
                if len(network.ip.split(':')) > 1:
                    if gw_ipv6:
                        network.data.setdefault(
                            'routing_node', []).append(routing_node_gateway)
                        modified = True
                else:  # IPv4
                    if gw_ipv4:
                        network.data.setdefault(
                            'routing_node', []).append(routing_node_gateway)
                        modified = True

        if modified:
            self.update()
        return modified