1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """DUT PAPI command history setup library."""
16 from robot.api import logger
18 from resources.libraries.python.topology import NodeType, DICT__nodes
20 __all__ = [u"DICT__DUTS_PAPI_HISTORY", u"PapiHistory"]
23 DICT__DUTS_PAPI_HISTORY = dict()
27 """Contains methods to set up DUT PAPI command history.
31 def reset_papi_history(node):
32 """Reset PAPI command history for DUT node.
34 :param node: DUT node to reset PAPI command history for.
37 DICT__DUTS_PAPI_HISTORY[node[u"host"]] = list()
40 def reset_papi_history_on_all_duts(nodes):
41 """Reset PAPI command history for all DUT nodes.
43 :param nodes: Nodes to reset PAPI command history for.
46 for node in nodes.values():
47 if node[u"type"] == NodeType.DUT:
48 PapiHistory.reset_papi_history(node)
51 def add_to_papi_history(node, csit_papi_command, papi=True, **kwargs):
52 """Add command to PAPI command history on DUT node.
54 Repr strings are used for argument values.
56 The argument name 'csit_papi_command' must be unique enough as it cannot
57 be repeated in kwargs.
59 Examples of PAPI history items:
61 Request without parameters:
64 Request with parameters:
65 ipsec_select_backend(index=1,protocol=1)
68 sw_interface_rx_placement_dump(sw_if_index=4)
71 vpp-stats(path=['^/if', '/err/ip4-input', '/sys/node/ip4-input'])
74 sw_interface_set_flags sw_if_index 3 admin-up link-up
76 :param node: DUT node to add command to PAPI command history for.
77 :param csit_papi_command: Command to be added to PAPI command history.
78 :param papi: Says if the command to store is PAPi or VAT. Remove when
79 VAT executor is completely removed.
80 :param kwargs: Optional key-value arguments.
82 :type csit_papi_command: str
88 for key, val in kwargs.items():
89 args.append(f"{key}={val!r}")
90 item = f"{csit_papi_command}({u','.join(args)})"
92 # This else part is here to store VAT commands.
93 # VAT history is not used.
94 # TODO: Remove when VatExecutor is completely removed.
95 item = f"{csit_papi_command}"
96 DICT__DUTS_PAPI_HISTORY[node[u"host"]].append(item)
99 def show_papi_history(node):
100 """Show PAPI command history for DUT node.
102 :param node: DUT node to show PAPI command history for.
105 history_list = DICT__DUTS_PAPI_HISTORY[node[u"host"]]
107 history_list = (u"No PAPI command executed", )
108 history = u'\n'.join(history_list)
109 logger.info(f"{node[u'host']} PAPI command history:\n{history}\n")
112 def show_papi_history_on_all_duts(nodes):
113 """Show PAPI command history for all DUT nodes.
115 :param nodes: Nodes to show PAPI command history for.
118 for node in nodes.values():
119 if node[u"type"] == NodeType.DUT:
120 PapiHistory.show_papi_history(node)
123 # This module can be imported outside usual Robot test context,
124 # e.g. in pylint or by tools generating docs from docstrings.
125 # For the tools to work, we need to avoid processing
126 # when DICT__nodes value is not usable.
128 PapiHistory.reset_papi_history_on_all_duts(DICT__nodes)