Cisco VPP CSIT调用API过程

原文地址:https://blog.csdn.net/u010827484/article/details/79390016

csit_cmd: vpp_ipsec_add_sad_entry
file: vpp-csit\csit\resources\libraries\python\IPsecUtil.py

1、组织数据
ckey = crypto_key.encode('hex')
        ikey = integ_key.encode('hex')
        tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
            if tunnel_src is not None and tunnel_dst is not None else ''

2、调用SSH
out = VatExecutor.cmd_from_template(node,
                                            "ipsec/ipsec_sad_add_entry.vat",
                                            sad_id=sad_id, spi=spi,
                                            calg=crypto_alg.alg_name, ckey=ckey,
                                            ialg=integ_alg.alg_name, ikey=ikey,
                                            tunnel=tunnel)
    2.1: ipsec/ipsec_sad_add_entry.vat 内容
        file: vpp-csit\csit\resources\templates\vat\ipsec\ipsec_sad_add_entry.vat
        date: ipsec_sad_add_del_entry esp sad_id {sad_id} spi {spi} crypto_alg {calg} crypto_key {ckey} integ_alg {ialg} integ_key {ikey} {tunnel}

    2.2: VatExecutor.cmd_from_template 调用过程
        2.2.1:file: vpp-csit\csit\resources\libraries\python\VatExecutor.py
        func:
        def cmd_from_template(node, vat_template_file, **vat_args):
        """Execute VAT script on specified node. This method supports
        script templates with parameters.

        :param node: Node in topology on witch the script is executed.
        :param vat_template_file: Template file of VAT script.
        :param vat_args: Arguments to the template file.
        :return: List of JSON objects returned by VAT.
        """
        with VatTerminal(node) as vat:
            return vat.vat_terminal_exec_cmd_from_template(vat_template_file,
                                                           **vat_args)
        describute:
        构造VatTerminal对象时,VatTerminal首先打开SSH连接(interactive_terminal_open),然后
        调用interactive_terminal_exec_command启动(VAT_BIN_NAME = 'vpp_api_test'),终端进入
        VAT模式,此时的SSH调用都在VAT环境中进行
        1、连接SSH
        2、启动vpp_api_test进程(vpp_api_test进程使用共享内存方式与VPP交互)

        2.2.2:file: vpp-csit\csit\resources\libraries\python\VatExecutor.py
        func:
        def vat_terminal_exec_cmd_from_template(self, vat_template_file, **args):
        """Execute VAT script from a file.

        :param vat_template_file: Template file name of a VAT script.
        :param args: Dictionary of parameters for VAT script.
        :return: List of JSON objects returned by VAT.
        """
        file_path = '{}/{}'.format(Constants.RESOURCES_TPL_VAT,
                                   vat_template_file)
        with open(file_path, 'r') as template_file:
            cmd_template = template_file.readlines()
        ret = []
        for line_tmpl in cmd_template:
            vat_cmd = line_tmpl.format(**args)
            ret.append(self.vat_terminal_exec_cmd(vat_cmd.replace('\n', '')))
        return ret

        2.2.3:file: vpp-csit\csit\resources\libraries\python\VatExecutor.py
        func:
        def vat_terminal_exec_cmd(self, cmd):
        """Execute command on the opened VAT terminal.
        :param cmd: Command to be executed.
        :return: Command output in python representation of JSON format or
        None if not in JSON mode.
        """
        VatHistory.add_to_vat_history(self._node, cmd)
        logger.debug("Executing command in VAT terminal: {0}".format(cmd))
        try:
            out = self._ssh.interactive_terminal_exec_command(self._tty, cmd,
                                                              self.__VAT_PROMPT)
        ........
        ........
        ........

        2.2.4: file: vpp-csit\csit\resources\libraries\python\ssh.py
        func:
        chan.sendall('{c}\n'.format(c=cmd))
        buf = ''
        while not buf.endswith(prompt):
            try:
                chunk = chan.recv(self.__MAX_RECV_BUF)
                if not chunk:
                    break
                buf += chunk
        ........
        ........
        ........

csit_cmd: show_vpp_settings
file: vpp-csit\csit\resources\libraries\python\VPPUtil.py
func:
    def_setting_tb_displayed = {
        'IPv6 FIB': 'ip6 fib',
        'IPv4 FIB': 'ip fib',
        'Interface IP': 'int addr',
        'Interfaces': 'int',
        'ARP': 'ip arp',
        'Errors': 'err'
    }

    if additional_cmds:
        for cmd in additional_cmds:
            def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
    ssh = SSH()
    ssh.connect(node)
    for _, value in def_setting_tb_displayed.items():
        ssh.exec_command_sudo('vppctl sh {}'.format(value))
1、组织数据
2、打开SSH终端
3、使用vppctl方式调用CLI命令

猜你喜欢

转载自blog.csdn.net/u010827484/article/details/79390016