Esempio n. 1
0
    def get_interface_mac_from_json(interface_dump_json, sw_if_index):
        """Get interface MAC address from given JSON output by sw_if_index.

        :param interface_dump_json: JSON output from dump_interface_list VAT
            command.
        :param sw_if_index: SW interface index.
        :type interface_dump_json: str
        :type sw_if_index: int
        :returns: Interface MAC address.
        :rtype: str
        :raises ValueError: If interface not found in interface_dump_json.
        """
        logger.trace(interface_dump_json)
        interface_list = JsonParser().parse_data(interface_dump_json)
        for interface in interface_list:
            try:
                if interface[u"sw_if_index"] == sw_if_index:
                    mac_from_json = interface[u"l2_address"][:6] \
                        if u"l2_address" in list(interface.keys()) else u""
                    mac_address = u":".join(
                        f"{item:02x}" for item in mac_from_json
                    )
                    logger.debug(
                        f"Interface with sw_if_index {sw_if_index} "
                        f"has MAC address {mac_address}."
                    )
                    return mac_address
            except KeyError:
                pass
        raise ValueError(f"Interface with sw_if_index {sw_if_index} not found.")
Esempio n. 2
0
    def update_vpp_interface_data_from_json(node, interface_dump_json):
        """Update vpp node data in node__DICT from JSON interface dump.

        This method updates vpp interface names and sw if indexes according to
        interface MAC addresses found in interface_dump_json.

        :param node: Node dictionary.
        :param interface_dump_json: JSON output from dump_interface_list VAT
            command.
        :type node: dict
        :type interface_dump_json: str
        """
        interface_list = JsonParser().parse_data(interface_dump_json)
        for ifc in node[u"interfaces"].values():
            if_mac = ifc[u"mac_address"]
            interface_dict = VatJsonUtil.get_vpp_interface_by_mac(
                interface_list, if_mac
            )
            if not interface_dict:
                logger.trace(f"Interface {ifc} not found by MAC {if_mac}")
                ifc[u"vpp_sw_index"] = None
                continue
            ifc[u"name"] = interface_dict[u"interface_name"]
            ifc[u"vpp_sw_index"] = interface_dict[u"sw_if_index"]
            ifc[u"mtu"] = interface_dict[u"mtu"]
Esempio n. 3
0
    def get_interface_mac_from_json(interface_dump_json, sw_if_index):
        """Get interface MAC address from given JSON output by sw_if_index.

        :param interface_dump_json: JSON output from dump_interface_list VAT
            command.
        :param sw_if_index: SW interface index.
        :type interface_dump_json: str
        :type sw_if_index: int
        :returns: Interface MAC address.
        :rtype: str
        :raises ValueError: If interface not found in interface_dump_json.
        """
        logger.trace(interface_dump_json)
        interface_list = JsonParser().parse_data(interface_dump_json)
        for interface in interface_list:
            try:
                if interface['sw_if_index'] == sw_if_index:
                    mac_from_json = interface['l2_address'][:6] \
                        if 'l2_address' in interface.keys() else ''
                    mac_address = ':'.join('{:02x}'.format(item)
                                           for item in mac_from_json)
                    logger.debug('Interface with sw_if_index {idx} has MAC'
                                 ' address {addr}.'.format(idx=sw_if_index,
                                                           addr=mac_address))
                    return mac_address
            except KeyError:
                pass
        raise ValueError('Interface with sw_if_index {idx} not found.'.format(
            idx=sw_if_index))
Esempio n. 4
0
    def get_interface_name_from_json(interface_dump_json, sw_if_index):
        """Get interface name from given JSON output by sw_if_index.

        :param interface_dump_json: JSON output from dump_interface_list VAT
            command.
        :param sw_if_index: SW interface index.
        :type interface_dump_json: str
        :type sw_if_index: int
        :returns: Interface name.
        :rtype: str
        :raises ValueError: If interface not found in interface_dump_json.
        """
        logger.trace(interface_dump_json)
        interface_list = JsonParser().parse_data(interface_dump_json)
        for interface in interface_list:
            try:
                if interface[u"sw_if_index"] == sw_if_index:
                    interface_name = interface[u"interface_name"]
                    logger.debug(
                        f"Interface with sw_if_index {sw_if_index} "
                        f"has name {interface_name}."
                    )
                    return interface_name
            except KeyError:
                pass
        raise ValueError(f"Interface with sw_if_index {sw_if_index} not found.")
Esempio n. 5
0
    def get_interface_sw_index_from_json(interface_dump_json, interface_name):
        """Get sw_if_index from given JSON output by interface name.

        :param interface_dump_json: JSON output from dump_interface_list VAT
            command.
        :param interface_name: Interface name.
        :type interface_dump_json: str
        :type interface_name: str
        :returns: SW interface index.
        :rtype: int
        :raises ValueError: If interface not found in interface_dump_json.
        """
        logger.trace(interface_dump_json)
        interface_list = JsonParser().parse_data(interface_dump_json)
        for interface in interface_list:
            try:
                if interface['interface_name'] == interface_name:
                    index = interface['sw_if_index']
                    logger.debug(
                        'Interface with name {} has sw_if_index {}.'.format(
                            interface_name, index))
                    return index
            except KeyError:
                pass
        raise ValueError(
            'Interface with name {} not found.'.format(interface_name))
Esempio n. 6
0
    def get_interface_name_from_json(interface_dump_json, sw_if_index):
        """Get interface name from given JSON output by sw_if_index.

        :param interface_dump_json: JSON output from dump_interface_list VAT
            command.
        :param sw_if_index: SW interface index.
        :type interface_dump_json: str
        :type sw_if_index: int
        :returns: Interface name.
        :rtype: str
        :raises ValueError: If interface not found in interface_dump_json.
        """
        logger.trace(interface_dump_json)
        interface_list = JsonParser().parse_data(interface_dump_json)
        for interface in interface_list:
            try:
                if interface['sw_if_index'] == sw_if_index:
                    interface_name = interface['interface_name']
                    logger.debug('Interface with sw_if_index {idx} has name'
                                 ' {name}.'.format(idx=sw_if_index,
                                                   name=interface_name))
                    return interface_name
            except KeyError:
                pass
        raise ValueError(
            'Interface with sw_if_index {} not found.'.format(sw_if_index))
Esempio n. 7
0
    def vpp_show_lisp_map_register(node):
        """Get LISP Map Register from VPP node.

        :param node: VPP node.
        :type node: dict
        :returns: LISP Map Register as python list.
        :rtype: list
        """

        vat = VatExecutor()
        vat.execute_script_json_out('lisp/show_lisp_map_register.vat', node)
        return JsonParser().parse_data(vat.get_script_stdout())
Esempio n. 8
0
    def vpp_show_lisp_eid_table(node):
        """Get lisp eid table from VPP node.

        :param node: VPP node.
        :type node: dict
        :returns: Lisp eid table as python list.
        :rtype: list
        """

        vat = VatExecutor()
        vat.execute_script_json_out('lisp/show_lisp_eid_table.vat', node)
        return JsonParser().parse_data(vat.get_script_stdout())
Esempio n. 9
0
    def vpp_show_lisp_state(node):
        """Get lisp state from VPP node.

        :param node: VPP node.
        :type node: dict
        :returns: Lisp gpe state.
        :rtype: list
        """

        vat = VatExecutor()
        vat.execute_script_json_out('lisp/show_lisp_status.vat', node)
        return JsonParser().parse_data(vat.get_script_stdout())
Esempio n. 10
0
    def vpp_show_lisp_pitr(node):
        """Get Lisp PITR feature config from VPP node.

        :param node: VPP node.
        :type node: dict
        :returns: Lisp PITR config data.
        :rtype: dict
        """

        vat = VatExecutor()
        vat.execute_script_json_out('lisp/show_lisp_pitr.vat', node)
        return JsonParser().parse_data(vat.get_script_stdout())
Esempio n. 11
0
    def vpp_show_lisp_rloc_config(node):
        """Get LISP RLOC configuration from VPP node.

        :param node: VPP node.
        :type node: dict
        :returns: LISP RLOC configuration as python list.
        :rtype: list
        """

        vat = VatExecutor()
        vat.execute_script_json_out('lisp/show_lisp_rloc_config.vat', node)
        return JsonParser().parse_data(vat.get_script_stdout())
Esempio n. 12
0
    def update_tg_interface_data_on_node(node):
        """Update interface name for TG/linux node in DICT__nodes.

        .. note::
            # for dev in `ls /sys/class/net/`;
            > do echo "\"`cat /sys/class/net/$dev/address`\": \"$dev\""; done
            "52:54:00:9f:82:63": "eth0"
            "52:54:00:77:ae:a9": "eth1"
            "52:54:00:e1:8a:0f": "eth2"
            "00:00:00:00:00:00": "lo"

        .. note:: TODO: parse lshw -json instead

        :param node: Node selected from DICT__nodes.
        :type node: dict
        :raises RuntimeError: If getting of interface name and MAC fails.
        """
        # First setup interface driver specified in yaml file
        InterfaceUtil.tg_set_interfaces_default_driver(node)

        # Get interface names
        ssh = SSH()
        ssh.connect(node)

        cmd = ('for dev in `ls /sys/class/net/`; do echo "\\"`cat '
               '/sys/class/net/$dev/address`\\": \\"$dev\\""; done;')

        (ret_code, stdout, _) = ssh.exec_command(cmd)
        if int(ret_code) != 0:
            raise RuntimeError('Get interface name and MAC failed')
        tmp = "{" + stdout.rstrip().replace('\n', ',') + "}"
        interfaces = JsonParser().parse_data(tmp)
        for interface in node['interfaces'].values():
            name = interfaces.get(interface['mac_address'])
            if name is None:
                continue
            interface['name'] = name

        # Set udev rules for interfaces
        InterfaceUtil.tg_set_interfaces_udev_rules(node)