Source code for smc.elements.netlink

#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.
"""
NetLink elements are used to represent alternative routes that lead to the
same destination IP addresses.

NetLinks usually represent Internet connections, but can be used for other
communications links as well.

You can use a single Router if a single route is enough for routing traffic
to a network through an interface or an aggregated link. If you want to create
separate routes for traffic to a network through two or more interfaces, you
must use NetLinks.

To use traffic handlers, you must first create the netlink type required, then
add this to the engine routing node.

Creating a static netlink element::

    StaticNetlink.create(
        name='netlink',
        gateway=Router('routerfoo'),
        network=[Network('mynetwork')],
        domain_server_address=['8.8.8.8', '8.8.4.4'],
        probe_address=['1.1.1.254'],
        comment='foobar')

Add the netlink to the desired routing interface::

    engine = Engine('vm')
    rnode = engine.routing.get(0) #interface 0
    rnode.add_traffic_handler(
        netlink=StaticNetlink('mynetlink'),
        netlink_gw=[Router('myrtr')])

.. seealso:: :class:`smc.core.route.Routing.add_traffic_handler`

Creating Multilink's require that you first have StaticNetlink or DynamicNetlink
elements. Once you have this created, you can create a multilink in a two step
process.

First create the multilink members specifying the created netlinks. A multilink
member encapsulates the creation process and collects the required information for
each netlink such as ip_range to use for source NAT (static netlink only) and
the network role::

    member = MultilinkMember.create(
        StaticNetlink('netlink1'), ip_range='1.1.1.1-1.1.1.2', netlink_role='active')

    member1 = MultilinkMember.create(
        StaticNetlink('netlink2'), ip_range='2.1.1.1-2.1.1.2', netlink_role='standby')

Then create the multilink specifying the multilink members::

        Multilink.create(name='internet', multilink_members=[member, member1])

.. seealso:: :class:`~Multilink`
"""
from smc.base.model import Element, ElementCreator, ElementCache, ElementRef, ElementList
from smc.vpn.elements import ConnectionType
from smc.base.util import element_resolver
from smc.core.general import RankedDNSAddress
from smc.compat import is_api_version_less_than_or_equal














[docs] class MultilinkMember(object): """ A multilink member represents an netlink member used on a multilink configuration. Multilink uses netlinks to specify settings specific to a connection, network, whether it should be active or standby and optionally QoS. Use this class to create mutlilink members that are required for creating a Multilink element. :ivar Network network: network element reference specifying netlink subnet :ivar StaticNetlink,DynamicNetlink netlink: netlink element reference """ network = ElementRef("network_ref") netlink = ElementRef("netlink_ref") def __init__(self, kwargs): self.data = ElementCache(kwargs) def __eq__(self, other): return all( [ self.ip_range == other.ip_range, self.netlink_role == other.netlink_role, self.data.get("network_ref") == other.data.get("network_ref"), self.data.get("netlink_ref") == other.data.get("netlink_ref"), ] ) def __ne__(self, other): return not self == other def __hash__(self): return hash( ( self.ip_range, self.netlink_role, self.data.get("network_ref"), self.data.get("netlink_ref"), ) ) @property def ip_range(self): """ Specifies the IP address range for dynamic source address translation (NAT) for the internal source IP addresses on the NetLink. Can also be set. :rtype: str """ return self.data.get("ip_range") @ip_range.setter def ip_range(self, value): if "-" in value: self.data.update(ip_range=value) @property def netlink_role(self): """ Shows whether the Netlink is active or standby. Active - traffic is routed through the NetLink according to the method you specify in the Outbound Multi-Link element properties. Standby - traffic is only routed through the netlink if all primary (active) netlinks are unavailable. :rtype: str """ return self.data.get("netlink_role") @netlink_role.setter def netlink_role(self, value): if value in ("standby", "active"): self.data.update(netlink_role=value)
[docs] @classmethod def create(cls, netlink, ip_range=None, netlink_role="active"): """ Create a multilink member. Multilink members are added to an Outbound Multilink configuration and define the ip range, static netlink to use, and the role. This element can be passed to the Multilink constructor to simplify creation of the outbound multilink. :param StaticNetlink,DynamicNetlink netlink: static netlink element to use as member :param str ip_range: the IP range for source NAT for this member. The IP range should be part of the defined network range used by this netlink. Not required for dynamic netlink :param str netlink_role: role of this netlink, 'active' or 'standby' :raises ElementNotFound: Specified netlink could not be found :rtype: MultilinkMember """ member_def = dict( netlink_ref=netlink.href, netlink_role=netlink_role, ip_range=ip_range if netlink.typeof == "netlink" else "0.0.0.0", ) if netlink.typeof == "netlink": # static netlink vs dynamic netlink member_def.update(network_ref=netlink.network[0].href) return cls(member_def)
def __repr__(self): return "MultilinkMember(netlink={},netlink_role={},ip_range={})".format( self.netlink, self.netlink_role, self.ip_range )
[docs] class LinkType(Element): """ This represents the Link Type. """ typeof = "link_type"