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:
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."""
18 from robot.api import logger
20 from resources.libraries.python.topology import Topology
21 from resources.libraries.python.HTTPRequest import HTTPCodes
22 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
23 from resources.libraries.python.honeycomb.HoneycombUtil \
24 import HoneycombUtil as HcUtil
25 from resources.libraries.python.honeycomb.HoneycombUtil \
26 import DataRepresentation
29 class ACLKeywords(object):
30 """Implementation of keywords which make it possible to:
31 - add classify table(s),
32 - remove classify table(s),
33 - get operational data about classify table(s),
34 - add classify session(s),
35 - remove classify session(s),
36 - get operational data about classify sessions(s).
43 def _set_classify_table_properties(node, path, data=None):
44 """Set classify table properties and check the return code.
46 :param node: Honeycomb node.
47 :param path: Path which is added to the base path to identify the data.
48 :param data: The new data to be set. If None, the item will be removed.
52 :returns: Content of response.
54 :raises HoneycombError: If the status code in response to PUT is not
59 status_code, resp = HcUtil.\
60 put_honeycomb_data(node, "config_classify_table", data, path,
61 data_representation=DataRepresentation.JSON)
63 status_code, resp = HcUtil.\
64 delete_honeycomb_data(node, "config_classify_table", path)
66 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
67 if data is None and '"error-tag":"data-missing"' in resp:
68 logger.debug("data does not exist in path.")
71 "The configuration of classify table was not successful. "
72 "Status code: {0}.".format(status_code))
76 def add_classify_table(node, table):
77 """Add a classify table to the list of classify tables. The keyword does
78 not validate given data.
80 :param node: Honeycomb node.
81 :param table: Classify table to be added.
84 :returns: Content of response.
88 path = "/classify-table/" + table["name"]
89 data = {"classify-table": [table, ]}
90 return ACLKeywords._set_classify_table_properties(node, path, data)
93 def remove_all_classify_tables(node):
94 """Remove all classify tables defined on the node.
96 :param node: Honeycomb node.
98 :returns: Content of response.
102 return ACLKeywords._set_classify_table_properties(node, path="")
105 def remove_classify_table(node, table_name):
106 """Remove the given classify table.
108 :param node: Honeycomb node.
109 :param table_name: Name of the classify table to be removed.
111 :type table_name: str
112 :returns: Content of response.
116 path = "/classify-table/" + table_name
117 return ACLKeywords._set_classify_table_properties(node, path)
120 def get_all_classify_tables_oper_data(node):
121 """Get operational data about all classify tables present on the node.
123 :param node: Honeycomb node.
125 :returns: List of classify tables.
129 status_code, resp = HcUtil.\
130 get_honeycomb_data(node, "oper_classify_table")
132 if status_code != HTTPCodes.OK:
133 raise HoneycombError(
134 "Not possible to get operational information about the "
135 "classify tables. Status code: {0}.".format(status_code))
137 return resp["vpp-classifier-state"]["classify-table"]
140 def get_classify_table_oper_data(node, table_name):
141 """Get operational data about the given classify table.
143 :param node: Honeycomb node.
144 :param table_name: Name of the classify table.
146 :type table_name: str
147 :returns: Operational data about the given classify table.
151 tables = ACLKeywords.get_all_classify_tables_oper_data(node)
153 if table["name"] == table_name:
155 raise HoneycombError("Table {0} not found in ACL table list.".format(
159 def get_all_classify_tables_cfg_data(node):
160 """Get configuration data about all classify tables present on the node.
162 :param node: Honeycomb node.
164 :returns: List of classify tables.
168 status_code, resp = HcUtil.\
169 get_honeycomb_data(node, "config_classify_table")
171 if status_code != HTTPCodes.OK:
172 raise HoneycombError(
173 "Not possible to get operational information about the "
174 "classify tables. Status code: {0}.".format(status_code))
176 return resp["vpp-classifier"]["classify-table"]
177 except (KeyError, TypeError):
181 def add_classify_session(node, table_name, session):
182 """Add a classify session to the classify table.
184 :param node: Honeycomb node.
185 :param table_name: Name of the classify table.
186 :param session: Classify session to be added to the classify table.
188 :type table_name: str
190 :returns: Content of response.
194 path = "/classify-table/" + table_name + \
195 "/classify-session/" + session["match"]
196 data = {"classify-session": [session, ]}
197 return ACLKeywords._set_classify_table_properties(node, path, data)
200 def remove_classify_session(node, table_name, session_match):
201 """Remove the given classify session from the classify table.
203 :param node: Honeycomb node.
204 :param table_name: Name of the classify table.
205 :param session_match: Classify session match.
207 :type table_name: str
208 :type session_match: str
209 :returns: Content of response.
213 path = "/classify-table/" + table_name + \
214 "/classify-session/" + session_match
215 return ACLKeywords._set_classify_table_properties(node, path)
218 def get_all_classify_sessions_oper_data(node, table_name):
219 """Get operational data about all classify sessions in the classify
222 :param node: Honeycomb node.
223 :param table_name: Name of the classify table.
225 :type table_name: str
226 :returns: List of classify sessions present in the classify table.
230 table_data = ACLKeywords.get_classify_table_oper_data(node, table_name)
232 return table_data["classify-session"]
235 def get_classify_session_oper_data(node, table_name, session_match):
236 """Get operational data about the given classify session in the classify
239 :param node: Honeycomb node.
240 :param table_name: Name of the classify table.
241 :param session_match: Classify session match.
243 :type table_name: str
244 :type session_match: str
245 :returns: Classify session operational data.
247 :raises HoneycombError: If no session the specified match Id is found.
250 sessions = ACLKeywords.get_all_classify_sessions_oper_data(
252 for session in sessions:
253 if session["match"] == session_match:
255 raise HoneycombError(
256 "Session with match value \"{0}\" not found"
257 " under ACL table {1}.".format(session_match, table_name))
260 def create_acl_plugin_classify_chain(node, list_name, data):
261 """Create classify chain using the ietf-acl node.
263 :param node: Honeycomb node.
264 :param list_name: Name for the classify list.
265 :param data: Dictionary of settings to send to Honeycomb.
269 :returns: Content of response.
271 :raises HoneycombError: If the operation fails.
274 path = "/acl/{0}".format(list_name)
276 status_code, resp = HcUtil.put_honeycomb_data(
277 node, "config_plugin_acl", data, path)
279 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
280 raise HoneycombError(
281 "Could not create classify chain."
282 "Status code: {0}.".format(status_code))
287 def set_acl_plugin_interface(node, interface, acl_name, direction):
288 """Assign an interface to an ietf-acl classify chain.
290 :param node: Honeycomb node.
291 :param interface: Name of an interface on the node.
292 :param acl_name: Name of an ACL chain configured through ACL-plugin.
293 :param direction: Classify incoming or outgoing packets.
294 Valid options are: ingress, egress
296 :type interface: str or int
299 :returns: Content of response.
301 :raises ValueError: If the direction argument is incorrect.
302 :raises HoneycombError: If the operation fails.
305 interface = Topology.convert_interface_reference(
306 node, interface, "name")
308 interface = interface.replace("/", "%2F")
310 if direction not in ("ingress", "egress"):
311 raise ValueError("Unknown traffic direction {0}. "
312 "Valid options are: ingress, egress."
315 path = "/attachment-points/interface/{0}/{1}/acl-sets/".format(
316 interface, direction)
326 status_code, resp = HcUtil.put_honeycomb_data(
327 node, "config_plugin_acl", data, path)
329 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
330 raise HoneycombError(
331 "Could not configure ACL on interface. "
332 "Status code: {0}.".format(status_code))
337 def delete_interface_plugin_acls(node, interface):
338 """Remove all plugin-acl assignments from an interface.
340 :param node: Honeycomb node.
341 :param interface: Name of an interface on the node.
343 :type interface: str or int
346 interface = Topology.convert_interface_reference(
347 node, interface, "name")
349 interface = interface.replace("/", "%2F")
351 path = "/attachment-points/interface/{0}/".format(interface)
352 status_code, _ = HcUtil.delete_honeycomb_data(
353 node, "config_plugin_acl", path)
355 if status_code != HTTPCodes.OK:
356 raise HoneycombError(
357 "Could not remove ACL assignment from interface. "
358 "Status code: {0}.".format(status_code))
361 def delete_acl_plugin_classify_chains(node):
362 """Remove all plugin-ACL classify chains.
364 :param node: Honeycomb node.
368 status_code, _ = HcUtil.delete_honeycomb_data(
369 node, "config_plugin_acl")
371 if status_code != HTTPCodes.OK:
372 raise HoneycombError(
373 "Could not remove plugin-acl chain. "
374 "Status code: {0}.".format(status_code))