Exemplo n.º 1
0
    def get_data(self) -> InterfaceData:
        """
        Retrieve the data representation of this interface.

        :return: interface data
        """
        if self.node:
            iface_id = self.node.get_iface_id(self)
        else:
            iface_id = self.othernet.get_iface_id(self)
        data = InterfaceData(id=iface_id,
                             name=self.name,
                             mac=str(self.mac) if self.mac else None)
        ip4 = self.get_ip4()
        if ip4:
            data.ip4 = str(ip4.ip)
            data.ip4_mask = ip4.prefixlen
        ip6 = self.get_ip6()
        if ip6:
            data.ip6 = str(ip6.ip)
            data.ip6_mask = ip6.prefixlen
        return data
Exemplo n.º 2
0
    def links(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
        """
        Build link data objects for this network. Each link object describes a link
        between this network and a node.

        :param flags: message type
        :return: list of link data
        """
        all_links = []

        # build a link message from this network node to each node having a
        # connected interface
        for iface in self.get_ifaces():
            uni = False
            linked_node = iface.node
            if linked_node is None:
                # two layer-2 switches/hubs linked together via linknet()
                if not iface.othernet:
                    continue
                linked_node = iface.othernet
                if linked_node.id == self.id:
                    continue
                iface.swapparams("_params_up")
                upstream_params = iface.getparams()
                iface.swapparams("_params_up")
                if iface.getparams() != upstream_params:
                    uni = True

            unidirectional = 0
            if uni:
                unidirectional = 1

            mac = str(iface.mac) if iface.mac else None
            iface2_data = InterfaceData(id=linked_node.get_iface_id(iface),
                                        name=iface.name,
                                        mac=mac)
            ip4 = iface.get_ip4()
            if ip4:
                iface2_data.ip4 = str(ip4.ip)
                iface2_data.ip4_mask = ip4.prefixlen
            ip6 = iface.get_ip6()
            if ip6:
                iface2_data.ip6 = str(ip6.ip)
                iface2_data.ip6_mask = ip6.prefixlen

            options_data = iface.get_link_options(unidirectional)
            link_data = LinkData(
                message_type=flags,
                type=self.linktype,
                node1_id=self.id,
                node2_id=linked_node.id,
                iface2=iface2_data,
                options=options_data,
            )
            all_links.append(link_data)

            if not uni:
                continue
            iface.swapparams("_params_up")
            options_data = iface.get_link_options(unidirectional)
            link_data = LinkData(
                message_type=MessageFlags.NONE,
                type=self.linktype,
                node1_id=linked_node.id,
                node2_id=self.id,
                options=options_data,
            )
            iface.swapparams("_params_up")
            all_links.append(link_data)
        return all_links
Exemplo n.º 3
0
    def links(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
        """
        Build CORE API TLVs for a point-to-point link. One Link message
        describes this network.

        :param flags: message flags
        :return: list of link data
        """
        all_links = []
        if len(self.ifaces) != 2:
            return all_links

        ifaces = self.get_ifaces()
        iface1 = ifaces[0]
        iface2 = ifaces[1]
        unidirectional = 0
        if iface1.getparams() != iface2.getparams():
            unidirectional = 1

        mac = str(iface1.mac) if iface1.mac else None
        iface1_data = InterfaceData(id=iface1.node.get_iface_id(iface1),
                                    name=iface1.name,
                                    mac=mac)
        ip4 = iface1.get_ip4()
        if ip4:
            iface1_data.ip4 = str(ip4.ip)
            iface1_data.ip4_mask = ip4.prefixlen
        ip6 = iface1.get_ip6()
        if ip6:
            iface1_data.ip6 = str(ip6.ip)
            iface1_data.ip6_mask = ip6.prefixlen

        mac = str(iface2.mac) if iface2.mac else None
        iface2_data = InterfaceData(id=iface2.node.get_iface_id(iface2),
                                    name=iface2.name,
                                    mac=mac)
        ip4 = iface2.get_ip4()
        if ip4:
            iface2_data.ip4 = str(ip4.ip)
            iface2_data.ip4_mask = ip4.prefixlen
        ip6 = iface2.get_ip6()
        if ip6:
            iface2_data.ip6 = str(ip6.ip)
            iface2_data.ip6_mask = ip6.prefixlen

        options_data = iface1.get_link_options(unidirectional)
        link_data = LinkData(
            message_type=flags,
            type=self.linktype,
            node1_id=iface1.node.id,
            node2_id=iface2.node.id,
            iface1=iface1_data,
            iface2=iface2_data,
            options=options_data,
        )
        all_links.append(link_data)

        # build a 2nd link message for the upstream link parameters
        # (swap if1 and if2)
        if unidirectional:
            iface1_data = InterfaceData(id=iface2.node.get_iface_id(iface2))
            iface2_data = InterfaceData(id=iface1.node.get_iface_id(iface1))
            options_data = iface2.get_link_options(unidirectional)
            link_data = LinkData(
                message_type=MessageFlags.NONE,
                type=self.linktype,
                node1_id=iface2.node.id,
                node2_id=iface1.node.id,
                iface1=iface1_data,
                iface2=iface2_data,
                options=options_data,
            )
            all_links.append(link_data)
        return all_links