ffc1644f1b1043cdfa7e2f2c066cbcc5d6fb1b18
[csit.git] / resources / libraries / python / VatHistory.py
1 # Copyright (c) 2016 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 VAT 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_VAT_HISTORY", "VatHistory"]
21
22
23 def setup_vat_history(nodes):
24     """Setup VAT history for DUT nodes.
25
26     :param nodes: DUT nodes to setup VAT history for.
27     :type nodes: dict
28     """
29     duts_vat_history = {}
30     try:
31         for node in nodes.values():
32             if node['type'] == NodeType.DUT:
33                 duts_vat_history[node['host']] = []
34         return duts_vat_history
35     except AttributeError:
36         # Necessary for the generation of source code documentation.
37         pass
38
39 DICT__DUTS_VAT_HISTORY = setup_vat_history(DICT__nodes)
40
41
42 class VatHistory(object):
43     """Contains methods to set up DUT VAT command history."""
44
45     @staticmethod
46     def reset_vat_history(node):
47         """Reset VAT command history for DUT node.
48
49         :param node: DUT node to reset VAT command history for.
50         :type node: dict
51         """
52         if node['type'] == NodeType.DUT:
53             DICT__DUTS_VAT_HISTORY[node['host']] = []
54
55     @staticmethod
56     def reset_vat_history_on_all_duts(nodes):
57         """Reset VAT command history for all DUT nodes.
58
59         :param nodes: Nodes to reset VAT command history for.
60         :type nodes: dict
61         """
62         for node in nodes.values():
63             if node['type'] == NodeType.DUT:
64                 VatHistory.reset_vat_history(node)
65
66     @staticmethod
67     def show_vat_history(node):
68         """Show VAT command history for DUT node.
69
70         :param node: DUT node to show VAT command history for.
71         :type node: dict
72         """
73         if node['type'] == NodeType.DUT:
74             sequence = "\nno VAT command executed"
75             if DICT__DUTS_VAT_HISTORY[node['host']]:
76                 sequence = "".join(["\n{}".format(
77                     cmd) for cmd in DICT__DUTS_VAT_HISTORY[node['host']]])
78             logger.trace(
79                 "{0} VAT command history:{1}\n".format(node['host'], sequence))
80
81     @staticmethod
82     def show_vat_history_on_all_duts(nodes):
83         """Show VAT command history for all DUT nodes.
84
85         :param nodes: Nodes to show VAT command history for.
86         :type nodes: dict
87         """
88         for node in nodes.values():
89             if node['type'] == NodeType.DUT:
90                 VatHistory.show_vat_history(node)
91
92     @staticmethod
93     def add_to_vat_history(node, cmd):
94         """Add command to VAT command history on DUT node.
95
96         :param node: DUT node to add command to VAT command history for.
97         :param cmd: Command to be added to VAT command history.
98         :type node: dict
99         :type cmd: str
100         """
101         if node['type'] == NodeType.DUT:
102             DICT__DUTS_VAT_HISTORY[node['host']].append(cmd)