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