Beispiel #1
0
def parse_protocols(conf, etree_conf):
    handled_elements = []
    for rstp_interface_node in etree_conf.xpath(
            "protocols/rstp/interface/name/.."):
        handled_elements.append(rstp_interface_node)

        port = val(rstp_interface_node, "name")

        port = conf.get_port_by_partial_name(port)

        if first(rstp_interface_node.xpath("edge")) is not None:
            if resolve_operation(first(
                    rstp_interface_node.xpath("edge"))) == "delete":
                port.vendor_specific.pop("rstp-edge")
            else:
                port.vendor_specific["rstp-edge"] = True
        elif "rstp-edge" in port.vendor_specific:
            port.vendor_specific.pop("rstp-edge")

        if first(rstp_interface_node.xpath("no-root-port")) is not None:
            if resolve_operation(
                    first(rstp_interface_node.xpath(
                        "no-root-port"))) == "delete":
                port.vendor_specific.pop("rstp-no-root-port")
            else:
                port.vendor_specific["rstp-no-root-port"] = True
        elif "rstp-no-root-port" in port.vendor_specific:
            port.vendor_specific.pop("rstp-no-root-port")

    return handled_elements
Beispiel #2
0
 def get_config(self, request):
     source = first(request.xpath("source"))
     content = self.datastore.to_etree(resolve_source_name(source[0].tag))
     filtering = first(request.xpath("filter"))
     if filtering is not None:
         filter_content(content, filtering)
     return Response(content)
def parse_protocols(conf, etree_conf):
    handled_elements = []
    for rstp_interface_node in etree_conf.xpath("protocols/rstp/interface/name/.."):
        handled_elements.append(rstp_interface_node)

        port = val(rstp_interface_node, "name")

        port = conf.get_port_by_partial_name(port)

        if first(rstp_interface_node.xpath("edge")) is not None:
            if resolve_operation(first(rstp_interface_node.xpath("edge"))) == "delete":
                port.vendor_specific.pop("rstp-edge")
            else:
                port.vendor_specific["rstp-edge"] = True
        elif "rstp-edge" in port.vendor_specific:
            port.vendor_specific.pop("rstp-edge")

        if first(rstp_interface_node.xpath("no-root-port")) is not None:
            if resolve_operation(first(rstp_interface_node.xpath("no-root-port"))) == "delete":
                port.vendor_specific.pop("rstp-no-root-port")
            else:
                port.vendor_specific["rstp-no-root-port"] = True
        elif "rstp-no-root-port" in port.vendor_specific:
            port.vendor_specific.pop("rstp-no-root-port")

    return handled_elements
Beispiel #4
0
 def get_config(self, request):
     source = first(request.xpath("source"))
     content = self.datastore.to_etree(resolve_source_name(source[0].tag))
     filtering = first(request.xpath("filter"))
     if filtering is not None:
         filter_content(content, filtering)
     return Response(content)
Beispiel #5
0
def parse_protocols(conf, etree_conf):
    handled_elements = []
    for rstp_interface_node in etree_conf.xpath(
            "protocols/rstp/interface/name/.."):
        handled_elements.append(rstp_interface_node)

        port = conf.get_port_by_partial_name(val(rstp_interface_node, "name"))

        if first(rstp_interface_node.xpath("edge")) is not None:
            if resolve_operation(first(
                    rstp_interface_node.xpath("edge"))) == "delete":
                port.vendor_specific.pop("rstp-edge")
            else:
                port.vendor_specific["rstp-edge"] = True
        elif "rstp-edge" in port.vendor_specific:
            port.vendor_specific.pop("rstp-edge")

        if first(rstp_interface_node.xpath("no-root-port")) is not None:
            if resolve_operation(
                    first(rstp_interface_node.xpath(
                        "no-root-port"))) == "delete":
                port.vendor_specific.pop("rstp-no-root-port")
            else:
                port.vendor_specific["rstp-no-root-port"] = True
        elif "rstp-no-root-port" in port.vendor_specific:
            port.vendor_specific.pop("rstp-no-root-port")

    for lldp_interface_node in etree_conf.xpath(
            "protocols/lldp/interface/name/.."):
        handled_elements.append(lldp_interface_node)

        port = conf.get_port_by_partial_name(val(lldp_interface_node, "name"))

        if resolve_operation(lldp_interface_node) == "delete":
            port.vendor_specific["lldp"] = False
            port.lldp_transmit = None
            port.lldp_receive = None
        else:
            port.vendor_specific["lldp"] = True

            disable_node = first(lldp_interface_node.xpath("disable"))
            if disable_node is not None:
                if resolve_operation(disable_node) == "delete":
                    port.lldp_transmit = None
                    port.lldp_receive = None
                else:
                    port.lldp_transmit = False
                    port.lldp_receive = False

    return handled_elements
def parse_protocols(conf, etree_conf):
    handled_elements = []
    for rstp_interface_node in etree_conf.xpath("protocols/rstp/interface/name/.."):
        handled_elements.append(rstp_interface_node)

        port = conf.get_port_by_partial_name(val(rstp_interface_node, "name"))

        if first(rstp_interface_node.xpath("edge")) is not None:
            if resolve_operation(first(rstp_interface_node.xpath("edge"))) == "delete":
                port.vendor_specific.pop("rstp-edge")
            else:
                port.vendor_specific["rstp-edge"] = True
        elif "rstp-edge" in port.vendor_specific:
            port.vendor_specific.pop("rstp-edge")

        if first(rstp_interface_node.xpath("no-root-port")) is not None:
            if resolve_operation(first(rstp_interface_node.xpath("no-root-port"))) == "delete":
                port.vendor_specific.pop("rstp-no-root-port")
            else:
                port.vendor_specific["rstp-no-root-port"] = True
        elif "rstp-no-root-port" in port.vendor_specific:
            port.vendor_specific.pop("rstp-no-root-port")

    for lldp_interface_node in etree_conf.xpath("protocols/lldp/interface/name/.."):
        handled_elements.append(lldp_interface_node)

        port = conf.get_port_by_partial_name(val(lldp_interface_node, "name"))

        if resolve_operation(lldp_interface_node) == "delete":
            port.vendor_specific["lldp"] = False
            port.lldp_transmit = None
            port.lldp_receive = None
        else:
            port.vendor_specific["lldp"] = True

            disable_node = first(lldp_interface_node.xpath("disable"))
            if disable_node is not None:
                if resolve_operation(disable_node) == "delete":
                    port.lldp_transmit = None
                    port.lldp_receive = None
                else:
                    port.lldp_transmit = False
                    port.lldp_receive = False

    return handled_elements
def resolve_new_value(node, value_name, actual_value, transformer=None):
    value_node = first(node.xpath(value_name))
    if value_node is not None:
        operation = resolve_operation(value_node)

        if operation == "delete":
            if actual_value is None:
                raise NotFound(value_name)
            else:
                return None
        else:
            return value_node.text if transformer is None else transformer(value_node.text)
    else:
        return actual_value
Beispiel #8
0
 def parse_trunk_native_vlan(self, interface_node, port):
     if len(
             interface_node.xpath(
                 "unit/family/ethernet-switching/native-vlan-id")
     ) == 1 and interface_node.xpath(
             "unit/family/ethernet-switching/native-vlan-id"
     )[0].text is not None:
         port_attributes = first(
             interface_node.xpath("unit/family/ethernet-switching"))
         return resolve_new_value(port_attributes,
                                  "native-vlan-id",
                                  port.trunk_native_vlan,
                                  transformer=int)
     return port.trunk_native_vlan
Beispiel #9
0
 def parse_trunk_native_vlan(self, interface_node, port):
     if len(
             interface_node.xpath("unit/family/{}/native-vlan-id".format(
                 self.ETHERNET_SWITCHING_TAG))
     ) == 1 and interface_node.xpath("unit/family/{}/native-vlan-id".format(
             self.ETHERNET_SWITCHING_TAG))[0].text is not None:
         port_attributes = first(
             interface_node.xpath("unit/family/{}".format(
                 self.ETHERNET_SWITCHING_TAG)))
         return resolve_new_value(port_attributes,
                                  "native-vlan-id",
                                  port.trunk_native_vlan,
                                  transformer=int)
     return port.trunk_native_vlan
def resolve_new_value(node, value_name, actual_value, transformer=None):
    value_node = first(node.xpath(value_name))
    if value_node is not None:
        operation = resolve_operation(value_node)

        if operation == "delete":
            if actual_value is None:
                raise NotFound(value_name)
            else:
                return None
        else:
            return value_node.text if transformer is None else transformer(value_node.text)
    else:
        return actual_value
    def parse_vlan_members(self, port, port_attributes):
        vlan_node = first(port_attributes.xpath("vlan-id"))
        if vlan_node is not None:
            if resolve_operation(vlan_node) == "delete":
                port.access_vlan = None
            else:
                port.access_vlan = vlan_node.text

        for member in port_attributes.xpath("vlan-id-list"):
            if resolve_operation(member) == "delete":
                if member.text:
                    port.trunk_vlans.remove(int(member.text))
                    if len(port.trunk_vlans) == 0:
                        port.trunk_vlans = None
                else:
                    port.trunk_vlans = None
            else:
                if port.trunk_vlans is None:
                    port.trunk_vlans = []
                port.trunk_vlans += parse_range(member.text)
Beispiel #12
0
 def unlock(self, request):
     target = first(request.xpath("target"))
     self.datastore.unlock(resolve_source_name(target[0].tag))
     return Response(etree.Element("ok"))
 def parse_trunk_native_vlan(self, interface_node, port):
     if len(interface_node.xpath("unit/family/ethernet-switching/native-vlan-id")) == 1 and interface_node.xpath("unit/family/ethernet-switching/native-vlan-id")[0].text is not None:
         port_attributes = first(interface_node.xpath("unit/family/ethernet-switching"))
         return resolve_new_value(port_attributes, "native-vlan-id", port.trunk_native_vlan,
                           transformer=int)
     return port.trunk_native_vlan
    def apply_interface_data(self, interface_node, port):
        port.description = resolve_new_value(interface_node, "description", port.description)
        port.mtu = resolve_new_value(interface_node, "mtu", port.mtu, transformer=_validate_mtu)

        shutdown_node = first(interface_node.xpath("disable"))
        if shutdown_node is not None:
            if port.shutdown is False and resolve_operation(shutdown_node) == "delete":
                raise NotFound('')
            port.shutdown = resolve_operation(shutdown_node) != "delete"

        ether_options_attributes = first(interface_node.xpath("ether-options"))
        if ether_options_attributes is not None:
            if resolve_operation(ether_options_attributes) != "delete":
                speed_node = first(ether_options_attributes.xpath("speed/*"))
                if speed_node is not None:
                    port.speed = speed_node.tag.split("-")[-1]

                self.edit_errors.extend(assign_auto_negotiation_state(ether_options_attributes, port))

                if resolve_operation(first(ether_options_attributes.xpath("ieee-802.3ad"))) == "delete":
                    if port.aggregation_membership is None:
                        raise NotFound("802.3ad")
                    port.aggregation_membership = None
                else:
                    port.aggregation_membership = resolve_new_value(ether_options_attributes, "ieee-802.3ad/bundle", port.aggregation_membership)
            else:
                port.speed = None
                port.aggregation_membership = None

        if "delete" in [resolve_operation(first(interface_node.xpath("unit"))), resolve_operation(first(interface_node.xpath("unit/family")))]:
            port.mode = None
            port.trunk_native_vlan = None
            port.access_vlan = None
            port.trunk_vlans = None
            port.trunk_vlans = None
            port.vendor_specific["has-ethernet-switching"] = False
        else:
            port_attributes = first(interface_node.xpath("unit/family/ethernet-switching"))
            if port_attributes is not None:
                port.vendor_specific["has-ethernet-switching"] = True

                port.mode = resolve_new_value(port_attributes, self.PORT_MODE_TAG, port.mode)
                if port.mode == "access":
                    port.trunk_vlans = None
                    port.trunk_native_vlan = None
                else:
                    port.access_vlan = None

                if resolve_operation(first(port_attributes.xpath("vlan"))) == "delete":
                    port.access_vlan = None
                    port.trunk_vlans = None
                else:
                    for member in port_attributes.xpath("vlan/members"):
                        if resolve_operation(member) == "delete":
                            if port_is_in_access_mode(port):
                                port.access_vlan = None
                            else:
                                port.trunk_vlans.remove(int(member.text))
                                if len(port.trunk_vlans) == 0:
                                    port.trunk_vlans = None
                        else:
                            if port_is_in_access_mode(port):
                                port.access_vlan = parse_range(member.text)[0]
                            else:
                                if port.trunk_vlans is None:
                                    port.trunk_vlans = []
                                port.trunk_vlans += parse_range(member.text)

            if resolve_operation(first(self.get_trunk_native_vlan_node(interface_node))) == "delete":
                port.trunk_native_vlan = None
            else:
                port.trunk_native_vlan = self.parse_trunk_native_vlan(interface_node, port)

        if isinstance(port, AggregatedPort):
            port.speed = resolve_new_value(interface_node, "aggregated-ether-options/link-speed", port.speed)
            port.auto_negotiation = resolve_new_value(interface_node, "aggregated-ether-options/auto-negotiation", port.auto_negotiation, transformer=lambda _: True)
            port.lacp_active = first(interface_node.xpath("aggregated-ether-options/lacp/active")) is not None
            port.lacp_periodic = resolve_new_value(interface_node, "aggregated-ether-options/lacp/periodic", port.lacp_periodic)
def val(node, xpath):
    return first(node.xpath(xpath)).text
    def apply_interface_data(self, interface_node, port):
        port.description = resolve_new_value(interface_node, "description", port.description)

        shutdown_node = first(interface_node.xpath("disable"))
        if shutdown_node is not None:
            if port.shutdown is False and resolve_operation(shutdown_node) == "delete":
                raise NotFound('')
            port.shutdown = resolve_operation(shutdown_node) != "delete"

        ether_options_attributes = first(interface_node.xpath("ether-options"))
        if ether_options_attributes is not None:
            if resolve_operation(ether_options_attributes) != "delete":
                speed_node = first(ether_options_attributes.xpath("speed/*"))
                if speed_node is not None:
                    port.speed = speed_node.tag.split("-")[-1]

                port.auto_negotiation = resolve_new_value(ether_options_attributes, "auto-negotiation", port.auto_negotiation, transformer=lambda _: True)

                if resolve_operation(first(ether_options_attributes.xpath("ieee-802.3ad"))) == "delete":
                    port.aggregation_membership = None
                else:
                    port.aggregation_membership = resolve_new_value(ether_options_attributes, "ieee-802.3ad/bundle", port.aggregation_membership)
            else:
                port.speed = None
                port.aggregation_membership = None

        if "delete" in [resolve_operation(first(interface_node.xpath("unit"))), resolve_operation(first(interface_node.xpath("unit/family")))]:
            port.mode = None
            port.trunk_native_vlan = None
            port.access_vlan = None
            port.trunk_vlans = None
            port.trunk_vlans = None
            port.vendor_specific["has-ethernet-switching"] = False
        else:
            port_attributes = first(interface_node.xpath("unit/family/ethernet-switching"))
            if port_attributes is not None:
                port.vendor_specific["has-ethernet-switching"] = True

                port.mode = resolve_new_value(port_attributes, self.PORT_MODE_TAG, port.mode)
                if port.mode == "access":
                    port.trunk_vlans = None
                    port.trunk_native_vlan = None
                else:
                    port.access_vlan = None

                if resolve_operation(first(port_attributes.xpath("vlan"))) == "delete":
                    port.access_vlan = None
                    port.trunk_vlans = None
                else:
                    for member in port_attributes.xpath("vlan/members"):
                        if resolve_operation(member) == "delete":
                            if port_is_in_access_mode(port):
                                port.access_vlan = None
                            else:
                                port.trunk_vlans.remove(int(member.text))
                                if len(port.trunk_vlans) == 0:
                                    port.trunk_vlans = None
                        else:
                            if port_is_in_access_mode(port):
                                port.access_vlan = parse_range(member.text)[0]
                            else:
                                if port.trunk_vlans is None:
                                    port.trunk_vlans = []
                                port.trunk_vlans += parse_range(member.text)

            if resolve_operation(first(self.get_trunk_native_vlan_node(interface_node))) == "delete":
                port.trunk_native_vlan = None
            else:
                port.trunk_native_vlan = self.parse_trunk_native_vlan(interface_node, port)

        if isinstance(port, AggregatedPort):
            port.speed = resolve_new_value(interface_node, "aggregated-ether-options/link-speed", port.speed)
            port.auto_negotiation = resolve_new_value(interface_node, "aggregated-ether-options/auto-negotiation", port.auto_negotiation, transformer=lambda _: True)
            port.lacp_active = first(interface_node.xpath("aggregated-ether-options/lacp/active")) is not None
            port.lacp_periodic = resolve_new_value(interface_node, "aggregated-ether-options/lacp/periodic", port.lacp_periodic)
Beispiel #17
0
    def edit_config(self, request):
        target = first(request.xpath("target"))
        config = first(request.xpath("config"))
        self.datastore.edit(resolve_source_name(target[0].tag), config[0])

        return Response(etree.Element("ok"))
Beispiel #18
0
    def edit_config(self, request):
        target = first(request.xpath("target"))
        config = first(request.xpath("config"))
        self.datastore.edit(resolve_source_name(target[0].tag), config[0])

        return Response(etree.Element("ok"))
Beispiel #19
0
 def unlock(self, request):
     target = first(request.xpath("target"))
     self.datastore.unlock(resolve_source_name(target[0].tag))
     return Response(etree.Element("ok"))
    def parse_vlan_interfaces(self, conf, interface_node):
        for unit_node in interface_node.xpath("unit/name/.."):
            unit_id = val(unit_node, "name")

            port_name = "irb.{}".format(unit_id)

            port = conf.get_port(port_name)
            if port is None:
                linked_vlan = find_vlan_with_routing_interface(conf, port_name)
                port = self.original_configuration.new(
                    "VlanPort",
                    vlan_id=linked_vlan.number if linked_vlan else None,
                    name=port_name)
                port.vendor_specific["irb-unit"] = unit_id
                conf.add_port(port)

            inet = first(
                unit_node.xpath("family/inet".format(
                    self.ETHERNET_SWITCHING_TAG)))
            if inet is not None:
                if first(inet.xpath("no-redirects")) is not None:
                    if resolve_operation(first(
                            inet.xpath("no-redirects"))) == "delete":
                        port.ip_redirect = True
                    else:
                        port.ip_redirect = False

                for address in inet.xpath("address/name/.."):
                    ip = IPNetwork(val(address, "name"))
                    if resolve_operation(address) == "delete":
                        port.remove_ip(ip)
                    else:
                        port.add_ip(ip)

                        for vrrp_node in address.xpath("vrrp-group/name/.."):
                            group_id = val(vrrp_node, "name")
                            vrrp_group = port.get_vrrp_group(group_id)

                            if vrrp_group is not None and resolve_operation(
                                    vrrp_node) == "delete":
                                port.vrrps.remove(vrrp_group)
                            else:
                                if vrrp_group is None:
                                    vrrp_group = self.original_configuration.new(
                                        "VRRP", group_id=group_id)
                                    port.vrrps.append(vrrp_group)

                                vrrp_group.related_ip_network = ip
                                vrrp_group.ip_addresses = [
                                    vip.text for vip in vrrp_node.xpath(
                                        "virtual-address")
                                    if resolve_operation(vip) != "delete"
                                ]
                                vrrp_group.priority = resolve_new_value(
                                    vrrp_node, "priority", vrrp_group.priority)

                                vrrp_group.preempt_delay_minimum = resolve_new_value(
                                    vrrp_node, "preempt/hold-time",
                                    vrrp_group.preempt_delay_minimum)
                                if resolve_operation(
                                        first(vrrp_node.xpath(
                                            "preempt"))) == "delete":
                                    vrrp_group.preempt_delay_minimum = None

                                if first(vrrp_node.xpath(
                                        "accept-data")) is not None:
                                    if resolve_operation(
                                            first(
                                                vrrp_node.xpath("accept-data"))
                                    ) == "delete":
                                        vrrp_group.vendor_specific.pop(
                                            "accept-data")
                                    else:
                                        vrrp_group.vendor_specific[
                                            "accept-data"] = True
                                elif "accept-data" in vrrp_group.vendor_specific:
                                    vrrp_group.vendor_specific.pop(
                                        "accept-data")

                                vrrp_group.vendor_specific[
                                    "authentication-type"] = resolve_new_value(
                                        vrrp_node, "authentication-type",
                                        vrrp_group.vendor_specific.get(
                                            "authentication-type"))
                                if vrrp_group.vendor_specific[
                                        "authentication-type"] is None:
                                    vrrp_group.vendor_specific.pop(
                                        "authentication-type")

                                vrrp_group.authentication = resolve_new_value(
                                    vrrp_node, "authentication-key",
                                    vrrp_group.authentication)

                                track = first(vrrp_node.xpath("track"))
                                if track is not None:
                                    if resolve_operation(track) == "delete":
                                        vrrp_group.track = {}
                                    else:
                                        vrrp_group.track = {
                                            val(track, "route/route_address"):
                                            val(track, "route/priority-cost")
                                        }
def val(node, xpath):
    return first(node.xpath(xpath)).text
Beispiel #22
0
    def apply_interface_data(self, interface_node, port):
        port.description = resolve_new_value(interface_node, "description",
                                             port.description)
        port.mtu = resolve_new_value(interface_node,
                                     "mtu",
                                     port.mtu,
                                     transformer=self._validate_mtu)

        shutdown_node = first(interface_node.xpath("disable"))
        if shutdown_node is not None:
            if port.shutdown is False and resolve_operation(
                    shutdown_node) == "delete":
                raise NotFound('')
            port.shutdown = resolve_operation(shutdown_node) != "delete"

        ether_options_attributes = first(
            interface_node.xpath(self.ETHER_OPTIONS_TAG))
        if ether_options_attributes is not None:
            if resolve_operation(ether_options_attributes) != "delete":
                speed_node = first(ether_options_attributes.xpath("speed/*"))
                if speed_node is not None:
                    port.speed = speed_node.tag.split("-")[-1]

                self.edit_errors.extend(
                    assign_auto_negotiation_state(ether_options_attributes,
                                                  port))

                if resolve_operation(
                        first(ether_options_attributes.xpath(
                            "ieee-802.3ad"))) == "delete":
                    if port.aggregation_membership is None:
                        raise NotFound("802.3ad")
                    port.aggregation_membership = None
                else:
                    port.aggregation_membership = resolve_new_value(
                        ether_options_attributes, "ieee-802.3ad/bundle",
                        port.aggregation_membership)
            else:
                port.speed = None
                port.aggregation_membership = None

        if "delete" in [
                resolve_operation(first(interface_node.xpath("unit"))),
                resolve_operation(first(interface_node.xpath("unit/family")))
        ]:
            port.mode = None
            port.trunk_native_vlan = None
            port.access_vlan = None
            port.trunk_vlans = None
            port.trunk_vlans = None
            port.vendor_specific["has-ethernet-switching"] = False
        else:
            port_attributes = first(
                interface_node.xpath("unit/family/{}".format(
                    self.ETHERNET_SWITCHING_TAG)))
            if port_attributes is not None:
                port.vendor_specific["has-ethernet-switching"] = True

                port.mode = resolve_new_value(port_attributes,
                                              self.PORT_MODE_TAG, port.mode)
                if port.mode == "access":
                    port.trunk_vlans = None
                    port.trunk_native_vlan = None
                else:
                    port.access_vlan = None

                if resolve_operation(first(
                        port_attributes.xpath("vlan"))) == "delete":
                    port.access_vlan = None
                    port.trunk_vlans = None
                else:
                    self.parse_vlan_members(port, port_attributes)

            if resolve_operation(
                    first(self.get_trunk_native_vlan_node(
                        interface_node))) == "delete":
                port.trunk_native_vlan = None
            else:
                port.trunk_native_vlan = self.parse_trunk_native_vlan(
                    interface_node, port)

        if isinstance(port, AggregatedPort):
            port.speed = resolve_new_value(
                interface_node, "aggregated-ether-options/link-speed",
                port.speed)
            port.auto_negotiation = resolve_new_value(
                interface_node,
                "aggregated-ether-options/auto-negotiation",
                port.auto_negotiation,
                transformer=lambda _: True)
            port.lacp_active = first(
                interface_node.xpath(
                    "aggregated-ether-options/lacp/active")) is not None
            port.lacp_periodic = resolve_new_value(
                interface_node, "aggregated-ether-options/lacp/periodic",
                port.lacp_periodic)