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 """Keywords to manipulate bridge domain configuration using Honeycomb REST API.
16 The keywords make possible to put and get configuration data and to get
21 from resources.libraries.python.HTTPRequest import HTTPCodes
22 from resources.libraries.python.HoneycombSetup import HoneycombError
23 from resources.libraries.python.HoneycombUtil import HoneycombUtil as HcUtil
24 from resources.libraries.python.HoneycombUtil import DataRepresentation
27 class BridgeDomainKeywords(object):
28 """Keywords to manipulate bridge domain configuration.
30 Implements keywords which get configuration and operational data about
31 bridge domains and put the bridge domains' parameters using Honeycomb REST
35 PARAMS = ("flood", "forward", "learn", "unknown-unicast-flood",
42 def _configure_bd(node, bd_name, data,
43 data_representation=DataRepresentation.JSON):
44 """Send bridge domain configuration data and check the response.
46 :param node: Honeycomb node.
47 :param bd_name: The name of bridge domain.
48 :param data: Configuration data to be sent in PUT request.
49 :param data_representation: How the data is represented.
53 :type data_representation: DataRepresentation
54 :return: Content of response.
56 :raises HoneycombError: If the status code in response on PUT is not
60 status_code, resp = HcUtil.\
61 put_honeycomb_data(node, "config_bridge_domain", data,
62 data_representation=data_representation)
63 if status_code != HTTPCodes.OK:
65 "The configuration of bridge domain '{0}' was not successful. "
66 "Status code: {1}.".format(bd_name, status_code))
70 def _set_bd_properties(node, bd_name, path, new_value=None):
71 """Set bridge domain properties.
73 This method reads bridge domain configuration data, creates, changes or
74 removes the requested data and puts it back to Honeycomb.
76 :param node: Honeycomb node.
77 :param bd_name: The name of bridge domain.
78 :param path: Path to data we want to change, create or remove.
79 :param new_value: The new value to be set. If None, the item will be
84 :type new_value: str, dict or list
85 :return: Content of response.
87 :raises HoneycombError: If it is not possible to get or set the data.
90 status_code, resp = HcUtil.\
91 get_honeycomb_data(node, "config_bridge_domain")
92 if status_code != HTTPCodes.OK:
94 "Not possible to get configuration information about the "
95 "bridge domains. Status code: {0}.".format(status_code))
98 new_data = HcUtil.set_item_value(resp, path, new_value)
100 new_data = HcUtil.remove_item(resp, path)
101 return BridgeDomainKeywords._configure_bd(node, bd_name, new_data)
104 def _create_bd_structure(bd_name, **kwargs):
105 """Create the bridge domain data structure as it is expected by
108 :param bd_name: Bridge domain name.
109 :param kwargs: Parameters and their values. The accepted parameters are
110 defined in BridgeDomainKeywords.PARAMS.
113 :return: Bridge domain data structure.
117 bd_structure = {"name": bd_name}
119 for param, value in kwargs.items():
120 if param not in BridgeDomainKeywords.PARAMS:
121 raise HoneycombError("The parameter {0} is invalid.".
123 bd_structure[param] = str(value)
128 def get_all_bds_cfg_data(node):
129 """Get configuration data about all bridge domains from Honeycomb.
131 :param node: Honeycomb node.
133 :return: Configuration data about all bridge domains from Honeycomb.
135 :raises HoneycombError: If it is not possible to get configuration data.
138 status_code, resp = HcUtil.\
139 get_honeycomb_data(node, "config_bridge_domain")
140 if status_code != HTTPCodes.OK:
141 raise HoneycombError(
142 "Not possible to get configuration information about the "
143 "bridge domains. Status code: {0}.".format(status_code))
145 return resp["bridge-domains"]["bridge-domain"]
147 except (KeyError, TypeError):
151 def get_bd_cfg_data(node, bd_name):
152 """Get configuration data about the given bridge domain from Honeycomb.
154 :param node: Honeycomb node.
155 :param bd_name: The name of bridge domain.
158 :return: Configuration data about the given bridge domain from
163 intfs = BridgeDomainKeywords.get_all_bds_cfg_data(node)
165 if intf["name"] == bd_name:
170 def get_all_bds_oper_data(node):
171 """Get operational data about all bridge domains from Honeycomb.
173 :param node: Honeycomb node.
175 :return: Operational data about all bridge domains from Honeycomb.
177 :raises HoneycombError: If it is not possible to get operational data.
180 status_code, resp = HcUtil.\
181 get_honeycomb_data(node, "oper_bridge_domains")
182 if status_code != HTTPCodes.OK:
183 raise HoneycombError(
184 "Not possible to get operational information about the "
185 "bridge domains. Status code: {0}.".format(status_code))
187 return resp["bridge-domains"]["bridge-domain"]
189 except (KeyError, TypeError):
193 def get_bd_oper_data(node, bd_name):
194 """Get operational data about the given bridge domain from Honeycomb.
196 :param node: Honeycomb node.
197 :param bd_name: The name of bridge domain.
200 :return: Operational data about the given bridge domain from Honeycomb.
204 intfs = BridgeDomainKeywords.get_all_bds_oper_data(node)
206 if intf["name"] == bd_name:
211 def add_first_bd(node, bd_name, **kwargs):
212 """Add the first bridge domain.
214 If there are any other bridge domains configured, they will be removed.
216 :param node: Honeycomb node.
217 :param bd_name: Bridge domain name.
218 :param kwargs: Parameters and their values. The accepted parameters are
219 defined in BridgeDomainKeywords.PARAMS
223 :return: Bridge domain data structure.
227 path = ("bridge-domains", )
228 new_bd = BridgeDomainKeywords._create_bd_structure(bd_name, **kwargs)
229 bridge_domain = {"bridge-domain": [new_bd, ]}
230 return BridgeDomainKeywords._set_bd_properties(node, bd_name, path,
234 def add_bd(node, bd_name, **kwargs):
235 """Add a bridge domain.
237 :param node: Honeycomb node.
238 :param bd_name: Bridge domain name.
239 :param kwargs: Parameters and their values. The accepted parameters are
240 defined in BridgeDomainKeywords.PARAMS
244 :return: Bridge domain data structure.
248 path = ("bridge-domains", "bridge-domain")
249 new_bd = BridgeDomainKeywords._create_bd_structure(bd_name, **kwargs)
250 bridge_domain = [new_bd, ]
251 return BridgeDomainKeywords._set_bd_properties(node, bd_name, path,
255 def remove_all_bds(node):
256 """Remove all bridge domains.
258 :param node: Honeycomb node.
260 :return: Content of response.
262 :raises HoneycombError: If it is not possible to remove all bridge
266 data = {"bridge-domains": {"bridge-domain": []}}
267 status_code, resp = HcUtil.\
268 put_honeycomb_data(node, "config_bridge_domain", data)
269 if status_code != HTTPCodes.OK:
270 raise HoneycombError("Not possible to remove all bridge domains. "
271 "Status code: {0}.".format(status_code))
275 def remove_bridge_domain(node, bd_name):
276 """Remove a bridge domain.
278 :param node: Honeycomb node.
279 :param bd_name: The name of bridge domain to be removed.
282 :return: Content of response.
284 :raises HoneycombError:If it is not possible to remove the bridge
288 path = ("bridge-domains", ("bridge-domain", "name", bd_name))
290 status_code, resp = HcUtil.\
291 get_honeycomb_data(node, "config_bridge_domain")
292 if status_code != HTTPCodes.OK:
293 raise HoneycombError(
294 "Not possible to get configuration information about the "
295 "bridge domains. Status code: {0}.".format(status_code))
297 new_data = HcUtil.remove_item(resp, path)
298 status_code, resp = HcUtil.\
299 put_honeycomb_data(node, "config_bridge_domain", new_data)
300 if status_code != HTTPCodes.OK:
301 raise HoneycombError("Not possible to remove bridge domain {0}. "
303 format(bd_name, status_code))
307 def configure_bridge_domain(node, bd_name, param, value):
308 """Configure a bridge domain.
310 :param node: Honeycomb node.
311 :param bd_name: Bridge domain name.
312 :param param: Parameter to set, change or remove. The accepted
313 parameters are defined in BridgeDomainKeywords.PARAMS
314 :param value: The new value to be set, change or remove. If None, the
315 item will be removed.
320 :return: Content of response.
324 if param not in BridgeDomainKeywords.PARAMS:
325 raise HoneycombError("The parameter {0} is invalid.".format(param))
327 path = ("bridge-domains", ("bridge-domain", "name", bd_name), param)
328 return BridgeDomainKeywords.\
329 _set_bd_properties(node, bd_name, path, value)