c11e3251943e92faee77a589b53db043e1ac98cc
[csit.git] / resources / libraries / python / honeycomb / HcAPIKwACL.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 """This module implements keywords to manipulate ACL data structures using
15 Honeycomb REST API."""
16
17 from resources.libraries.python.HTTPRequest import HTTPCodes
18 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
19 from resources.libraries.python.honeycomb.HoneycombUtil \
20     import HoneycombUtil as HcUtil
21 from resources.libraries.python.honeycomb.HoneycombUtil \
22     import DataRepresentation
23
24
25 class ACLKeywords(object):
26     """Implementation of keywords which make it possible to:
27     - add classify table(s),
28     - remove classify table(s),
29     - get operational data about classify table(s),
30     - add classify session(s),
31     - remove classify session(s),
32     - get operational data about classify sessions(s).
33     """
34
35     def __init__(self):
36         pass
37
38     @staticmethod
39     def _set_classify_table_properties(node, path, data=None):
40         """Set classify table properties and check the return code.
41
42         :param node: Honeycomb node.
43         :param path: Path which is added to the base path to identify the data.
44         :param data: The new data to be set. If None, the item will be removed.
45         :type node: dict
46         :type path: str
47         :type data: dict
48         :return: Content of response.
49         :rtype: bytearray
50         :raises HoneycombError: If the status code in response to PUT is not
51         200 = OK.
52         """
53
54         if data:
55             status_code, resp = HcUtil.\
56                 put_honeycomb_data(node, "config_classify_table", data, path,
57                                    data_representation=DataRepresentation.JSON)
58         else:
59             status_code, resp = HcUtil.\
60                 delete_honeycomb_data(node, "config_classify_table", path)
61
62         if status_code != HTTPCodes.OK:
63             raise HoneycombError(
64                 "The configuration of classify table was not successful. "
65                 "Status code: {0}.".format(status_code))
66         return resp
67
68     @staticmethod
69     def add_classify_table(node, table):
70         """Add a classify table to the list of classify tables. The keyword does
71         not validate given data.
72
73         :param node: Honeycomb node.
74         :param table: Classify table to be added.
75         :type node: dict
76         :type table: dict
77         :return: Content of response.
78         :rtype: bytearray
79         """
80
81         path = "/classify-table/" + table["name"]
82         data = {"classify-table": [table, ]}
83         return ACLKeywords._set_classify_table_properties(node, path, data)
84
85     @staticmethod
86     def remove_all_classify_tables(node):
87         """Remove all classify tables defined on the node.
88
89         :param node: Honeycomb node.
90         :type node: dict
91         :return: Content of response.
92         :rtype: bytearray
93         """
94
95         return ACLKeywords._set_classify_table_properties(node, path="")
96
97     @staticmethod
98     def remove_classify_table(node, table_name):
99         """Remove the given classify table.
100
101         :param node: Honeycomb node.
102         :param table_name: Name of the classify table to be removed.
103         :type node: dict
104         :type table_name: str
105         :return: Content of response.
106         :rtype: bytearray
107         """
108
109         path = "/classify-table/" + table_name
110         return ACLKeywords._set_classify_table_properties(node, path)
111
112     @staticmethod
113     def get_all_classify_tables_oper_data(node):
114         """Get operational data about all classify tables present on the node.
115
116         :param node: Honeycomb node.
117         :type node: dict
118         :return: List of classify tables.
119         :rtype: list
120         """
121
122         status_code, resp = HcUtil.\
123             get_honeycomb_data(node, "oper_classify_table")
124
125         if status_code != HTTPCodes.OK:
126             raise HoneycombError(
127                 "Not possible to get operational information about the "
128                 "classify tables. Status code: {0}.".format(status_code))
129         try:
130             return resp["vpp-classifier"]["classify-table"]
131         except (KeyError, TypeError):
132             return []
133
134     @staticmethod
135     def get_classify_table_oper_data(node, table_name):
136         """Get operational data about the given classify table.
137
138         :param node: Honeycomb node.
139         :param table_name: Name of the classify table.
140         :type node: dict
141         :type table_name: str
142         :return: Operational data about the given classify table.
143         :rtype: dict
144         """
145
146         path = "/classify-table/" + table_name
147         status_code, resp = HcUtil.\
148             get_honeycomb_data(node, "oper_classify_table", path)
149
150         if status_code != HTTPCodes.OK:
151             raise HoneycombError(
152                 "Not possible to get operational information about the "
153                 "classify tables. Status code: {0}.".format(status_code))
154         try:
155             return resp["classify-table"][0]
156         except (KeyError, TypeError):
157             return []
158
159     @staticmethod
160     def get_all_classify_tables_cfg_data(node):
161         """Get configuration data about all classify tables present on the node.
162
163         :param node: Honeycomb node.
164         :type node: dict
165         :return: List of classify tables.
166         :rtype: list
167         """
168
169         status_code, resp = HcUtil.\
170             get_honeycomb_data(node, "config_classify_table")
171
172         if status_code != HTTPCodes.OK:
173             raise HoneycombError(
174                 "Not possible to get operational information about the "
175                 "classify tables. Status code: {0}.".format(status_code))
176         try:
177             return resp["vpp-classifier"]["classify-table"]
178         except (KeyError, TypeError):
179             return []
180
181     @staticmethod
182     def add_classify_session(node, table_name, session):
183         """Add a classify session to the classify table.
184
185         :param node: Honeycomb node.
186         :param table_name: Name of the classify table.
187         :param session: Classify session to be added to the classify table.
188         :type node: dict
189         :type table_name: str
190         :type session: dict
191         :return: Content of response.
192         :rtype: bytearray
193         """
194
195         path = "/classify-table/" + table_name + \
196                "/classify-session/" + session["match"]
197         data = {"classify-session": [session, ]}
198         return ACLKeywords._set_classify_table_properties(node, path, data)
199
200     @staticmethod
201     def remove_classify_session(node, table_name, session_match):
202         """Remove the given classify session from the classify table.
203
204         :param node: Honeycomb node.
205         :param table_name: Name of the classify table.
206         :param session_match: Classify session match.
207         :type node: dict
208         :type table_name: str
209         :type session_match: str
210         :return: Content of response.
211         :rtype: bytearray
212         """
213
214         path = "/classify-table/" + table_name + \
215                "/classify-session/" + session_match
216         return ACLKeywords._set_classify_table_properties(node, path)
217
218     @staticmethod
219     def get_all_classify_sessions_oper_data(node, table_name):
220         """Get operational data about all classify sessions in the classify
221         table.
222
223         :param node: Honeycomb node.
224         :param table_name: Name of the classify table.
225         :type node: dict
226         :type table_name: str
227         :return: List of classify sessions present in the classify table.
228         :rtype: list
229         """
230
231         table_data = ACLKeywords.get_classify_table_oper_data(node, table_name)
232         try:
233             return table_data["classify-table"][0]["classify-session"]
234         except (KeyError, TypeError):
235             return []
236
237     @staticmethod
238     def get_classify_session_oper_data(node, table_name, session_match):
239         """Get operational data about the given classify session in the classify
240         table.
241
242         :param node: Honeycomb node.
243         :param table_name: Name of the classify table.
244         :param session_match: Classify session match.
245         :type node: dict
246         :type table_name: str
247         :type session_match: str
248         :return: Classify session operational data.
249         :rtype: dict
250         """
251
252         path = "/classify-table/" + table_name + \
253                "/classify-session/" + session_match
254         status_code, resp = HcUtil.\
255             get_honeycomb_data(node, "oper_classify_table", path)
256
257         if status_code != HTTPCodes.OK:
258             raise HoneycombError(
259                 "Not possible to get operational information about the "
260                 "classify tables. Status code: {0}.".format(status_code))
261         try:
262             return resp["classify-session"][0]
263         except (KeyError, TypeError):
264             return {}