1 # Copyright (c) 2018 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 NSH-SFC 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 NSHKeywords(object):
26 """Implementation of keywords which make it possible to:
27 - add and remove NSH entries,
28 - get operational data about NSH entries,
29 - add and remove NSH maps,
30 - get operational data about NSH maps.
37 def _set_nsh_properties(node, path, data=None):
38 """Set NSH properties and check the return code.
40 :param node: Honeycomb node.
41 :param path: Path which is added to the base path to identify the data.
42 :param data: The new data to be set. If None, the item will be removed.
46 :returns: Content of response.
48 :raises HoneycombError: If the status code in response to PUT is not
53 status_code, resp = HcUtil. \
54 put_honeycomb_data(node, "config_nsh", data, path,
55 data_representation=DataRepresentation.JSON)
57 status_code, resp = HcUtil. \
58 delete_honeycomb_data(node, "config_nsh", path)
60 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
62 "The configuration of NSH-SFC was not successful. "
63 "Status code: {0}.".format(status_code))
67 def add_nsh_entry(node, name, data):
68 """Add an NSH entry to the list of entries. The keyword does
69 not validate given data.
71 :param node: Honeycomb node.
72 :param name: Name for the NSH entry.
73 :param data: Settings for the new entry.
77 :returns: Content of response.
81 path = "/nsh-entries/nsh-entry/{0}".format(name)
83 return NSHKeywords._set_nsh_properties(node, path, data)
86 def add_nsh_map(node, name, data):
87 """Add an NSH map to the list of maps. The keyword does
88 not validate given data.
90 :param node: Honeycomb node.
91 :param name: Name for the NSH map.
92 :param data: Settings for the new map.
96 :returns: Content of response.
99 path = "/nsh-maps/nsh-map/{0}".format(name)
101 return NSHKeywords._set_nsh_properties(node, path, data)
104 def remove_nsh_entry(node, name):
105 """Remove an NSH entry from the list of entries.
106 :param node: Honeycomb node.
107 :param name: Name of the NSH entry.
110 :returns: Content of response.
114 path = "/nsh-entries/nsh-entry/{0}".format(name)
115 return NSHKeywords._set_nsh_properties(node, path)
118 def remove_nsh_map(node, name):
119 """Remove an NSH map from the list of maps.
120 :param node: Honeycomb node.
121 :param name: Name of the NSH map.
124 :returns: Content of response.
128 path = "/nsh-maps/nsh-map/{0}".format(name)
129 return NSHKeywords._set_nsh_properties(node, path)
132 def get_nsh_oper_data(node, entry_name=None, map_name=None):
133 """Get all NSH operational data present on the node. Optionally
134 filter out data for a specific entry or map.
136 :param node: Honeycomb node.
137 :param entry_name: Name of a specific NSH entry. Optional.
138 :param map_name: Name of a specific NSH map. Optional. Do not use
139 together with entry_name.
141 :type entry_name: str
143 :returns: List of classify tables.
147 path = "/nsh-entries/nsh-entry/{0}".format(entry_name)
149 path = "/nsh-maps/nsh-map/{0}".format(map_name)
153 status_code, resp = HcUtil. \
154 get_honeycomb_data(node, "oper_nsh", path)
156 if status_code != HTTPCodes.OK:
157 raise HoneycombError(
158 "Not possible to get operational information about the "
159 "classify tables. Status code: {0}.".format(status_code))
164 def clear_nsh_settings(node):
165 """Remove the entire NSH container with all of its entries and maps.
167 :param node: Honeycomb node.
169 :returns: Content of response.
173 return NSHKeywords._set_nsh_properties(node, '')