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:
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 """This module implements keywords to manipulate ACL data structures using
15 Honeycomb REST API."""
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
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).
39 def _set_classify_table_properties(node, path, data=None):
40 """Set classify table properties and check the return code.
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.
48 :return: Content of response.
50 :raises HoneycombError: If the status code in response to PUT is not
55 status_code, resp = HcUtil.\
56 put_honeycomb_data(node, "config_classify_table", data, path,
57 data_representation=DataRepresentation.JSON)
59 status_code, resp = HcUtil.\
60 delete_honeycomb_data(node, "config_classify_table", path)
62 if status_code != HTTPCodes.OK:
64 "The configuration of classify table was not successful. "
65 "Status code: {0}.".format(status_code))
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.
73 :param node: Honeycomb node.
74 :param table: Classify table to be added.
77 :return: Content of response.
81 path = "/classify-table/" + table["name"]
82 data = {"classify-table": [table, ]}
83 return ACLKeywords._set_classify_table_properties(node, path, data)
86 def remove_all_classify_tables(node):
87 """Remove all classify tables defined on the node.
89 :param node: Honeycomb node.
91 :return: Content of response.
95 return ACLKeywords._set_classify_table_properties(node, path="")
98 def remove_classify_table(node, table_name):
99 """Remove the given classify table.
101 :param node: Honeycomb node.
102 :param table_name: Name of the classify table to be removed.
104 :type table_name: str
105 :return: Content of response.
109 path = "/classify-table/" + table_name
110 return ACLKeywords._set_classify_table_properties(node, path)
113 def get_all_classify_tables_oper_data(node):
114 """Get operational data about all classify tables present on the node.
116 :param node: Honeycomb node.
118 :return: List of classify tables.
122 status_code, resp = HcUtil.\
123 get_honeycomb_data(node, "oper_classify_table")
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))
130 return resp["vpp-classifier"]["classify-table"]
131 except (KeyError, TypeError):
135 def get_classify_table_oper_data(node, table_name):
136 """Get operational data about the given classify table.
138 :param node: Honeycomb node.
139 :param table_name: Name of the classify table.
141 :type table_name: str
142 :return: Operational data about the given classify table.
146 path = "/classify-table/" + table_name
147 status_code, resp = HcUtil.\
148 get_honeycomb_data(node, "oper_classify_table", path)
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))
155 return resp["classify-table"][0]
156 except (KeyError, TypeError):
160 def get_all_classify_tables_cfg_data(node):
161 """Get configuration data about all classify tables present on the node.
163 :param node: Honeycomb node.
165 :return: List of classify tables.
169 status_code, resp = HcUtil.\
170 get_honeycomb_data(node, "config_classify_table")
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))
177 return resp["vpp-classifier"]["classify-table"]
178 except (KeyError, TypeError):
182 def add_classify_session(node, table_name, session):
183 """Add a classify session to the classify table.
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.
189 :type table_name: str
191 :return: Content of response.
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)
201 def remove_classify_session(node, table_name, session_match):
202 """Remove the given classify session from the classify table.
204 :param node: Honeycomb node.
205 :param table_name: Name of the classify table.
206 :param session_match: Classify session match.
208 :type table_name: str
209 :type session_match: str
210 :return: Content of response.
214 path = "/classify-table/" + table_name + \
215 "/classify-session/" + session_match
216 return ACLKeywords._set_classify_table_properties(node, path)
219 def get_all_classify_sessions_oper_data(node, table_name):
220 """Get operational data about all classify sessions in the classify
223 :param node: Honeycomb node.
224 :param table_name: Name of the classify table.
226 :type table_name: str
227 :return: List of classify sessions present in the classify table.
231 table_data = ACLKeywords.get_classify_table_oper_data(node, table_name)
233 return table_data["classify-table"][0]["classify-session"]
234 except (KeyError, TypeError):
238 def get_classify_session_oper_data(node, table_name, session_match):
239 """Get operational data about the given classify session in the classify
242 :param node: Honeycomb node.
243 :param table_name: Name of the classify table.
244 :param session_match: Classify session match.
246 :type table_name: str
247 :type session_match: str
248 :return: Classify session operational data.
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)
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))
262 return resp["classify-session"][0]
263 except (KeyError, TypeError):