13c483b4bceeca1423fa2c274e9413e61b076b77
[csit.git] / resources / libraries / python / PapiHistory.py
1 # Copyright (c) 2019 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """DUT PAPI command history setup library."""
15
16 from robot.api import logger
17
18 from resources.libraries.python.topology import NodeType, DICT__nodes
19
20 __all__ = ["DICT__DUTS_PAPI_HISTORY", "PapiHistory"]
21
22
23 DICT__DUTS_PAPI_HISTORY = dict()
24
25
26 class PapiHistory(object):
27     """Contains methods to set up DUT PAPI command history.
28     """
29
30     @staticmethod
31     def reset_papi_history(node):
32         """Reset PAPI command history for DUT node.
33
34         :param node: DUT node to reset PAPI command history for.
35         :type node: dict
36         """
37         DICT__DUTS_PAPI_HISTORY[node['host']] = list()
38
39     @staticmethod
40     def reset_papi_history_on_all_duts(nodes):
41         """Reset PAPI command history for all DUT nodes.
42
43         :param nodes: Nodes to reset PAPI command history for.
44         :type nodes: dict
45         """
46         for node in nodes.values():
47             if node['type'] == NodeType.DUT:
48                 PapiHistory.reset_papi_history(node)
49
50     @staticmethod
51     def add_to_papi_history(node, cmd, papi=True, **kwargs):
52         """Add command to PAPI command history on DUT node.
53
54         :param node: DUT node to add command to PAPI command history for.
55         :param cmd: Command to be added to PAPI command history.
56         :param papi: Says if the command to store is PAPi or VAT. Remove when
57             VAT executor is completely removed.
58         :param kwargs: Optional key-value arguments.
59         :type node: dict
60         :type cmd: str
61         :type papi: bool
62         :type kwargs: dict
63         """
64         if papi:
65             args = list()
66             for key, val in kwargs.iteritems():
67                 args.append("{key}={val}".format(key=key, val=val))
68             item = "{cmd}({args})".format(cmd=cmd, args=",".join(args))
69         else:
70             # This else part is here to store VAT commands.
71             # VAT history is not used.
72             # TODO: Remove when VatExecutor is completely removed.
73             item = "{cmd}".format(cmd=cmd)
74         DICT__DUTS_PAPI_HISTORY[node['host']].append(item)
75
76     @staticmethod
77     def show_papi_history(node):
78         """Show PAPI command history for DUT node.
79
80         :param node: DUT node to show PAPI command history for.
81         :type node: dict
82         """
83         history = "\nNo PAPI command executed"
84         if DICT__DUTS_PAPI_HISTORY[node['host']]:
85             history = "".join(["\n{}".format(
86                 cmd) for cmd in DICT__DUTS_PAPI_HISTORY[node['host']]])
87         logger.trace(
88             "{0} PAPI command history:\n{1}\n".format(node['host'], history))
89
90     @staticmethod
91     def show_papi_history_on_all_duts(nodes):
92         """Show PAPI command history for all DUT nodes.
93
94         :param nodes: Nodes to show PAPI command history for.
95         :type nodes: dict
96         """
97         for node in nodes.values():
98             if node['type'] == NodeType.DUT:
99                 PapiHistory.show_papi_history(node)
100
101
102 PapiHistory.reset_papi_history_on_all_duts(DICT__nodes)