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 used with Honeycomb.
16 There are implemented keywords which work with:
17 - Honeycomb operations
21 The keywords make possible to put and get configuration data and to get
25 from json import dumps
27 from robot.api import logger
29 from resources.libraries.python.HTTPRequest import HTTPCodes
30 from resources.libraries.python.HoneycombSetup import HoneycombError
31 from resources.libraries.python.HoneycombUtil import HoneycombUtil as HcUtil
32 from resources.libraries.python.HoneycombUtil import DataRepresentation
35 class OperationsKeywords(object):
36 """Keywords which perform "operations" in Honeycomb.
38 The keywords in this class are not a part of a specific area in Honeycomb,
39 e.g.: interfaces or bridge domains, but they perform "operations" in any
47 def poll_oper_data(node):
48 """Poll operational data.
50 You can use this keyword when you configure something in Honeycomb and
51 you want configuration data to make effect immediately, e.g.:
53 | | Create Bridge Domain | ....
54 | | Add Bridge Domain To Interface | ....
55 | | Poll Oper Data | ....
56 | | ${br}= | Get Oper Info About Bridge Domain | ....
58 ..note:: This is not very reliable way how to poll operational data.
59 This keyword is only temporary workaround and will be removed when this
60 problem is solved in Honeycomb.
61 :param node: Honeycomb node.
63 :raises HoneycombError: If it is not possible to poll operational data.
66 status_code, _ = HcUtil.\
67 post_honeycomb_data(node, "poll_oper_data", data='',
68 data_representation=DataRepresentation.NO_DATA,
70 if status_code != HTTPCodes.OK:
71 raise HoneycombError("It was not possible to poll operational data "
72 "on node {0}.".format(node['host']))
75 class InterfaceKeywords(object):
76 """Keywords for Interface manipulation.
78 Implements keywords which get configuration and operational data about
79 vpp interfaces and set the interface's parameters using Honeycomb REST API.
86 def _configure_interface(node, interface, data,
87 data_representation=DataRepresentation.JSON):
88 """Send interface configuration data and check the response.
90 :param node: Honeycomb node.
91 :param interface: The name of interface.
92 :param data: Configuration data to be sent in PUT request.
93 :param data_representation: How the data is represented.
97 :type data_representation: DataRepresentation
98 :return: Content of response.
100 :raises HoneycombError: If the status code in response on PUT is not
104 status_code, resp = HcUtil.\
105 put_honeycomb_data(node, "config_vpp_interfaces", data,
106 data_representation=data_representation)
107 if status_code != HTTPCodes.OK:
108 raise HoneycombError(
109 "The configuration of interface '{0}' was not successful. "
110 "Status code: {1}.".format(interface, status_code))
114 def get_all_interfaces_cfg_data(node):
115 """Get configuration data about all interfaces from Honeycomb.
117 :param node: Honeycomb node.
119 :return: Configuration data about all interfaces from Honeycomb.
121 :raises HoneycombError: If it is not possible to get configuration data.
124 status_code, resp = HcUtil.get_honeycomb_data(node,
125 "config_vpp_interfaces")
126 if status_code != HTTPCodes.OK:
127 raise HoneycombError(
128 "Not possible to get configuration information about the "
129 "interfaces. Status code: {0}.".format(status_code))
131 intf = HcUtil.parse_json_response(resp, ("interfaces", "interface"))
137 def get_interface_cfg_info(node, interface):
138 """Get configuration data about the given interface from Honeycomb.
140 :param node: Honeycomb node.
141 :param interface: The name of interface.
144 :return: Configuration data about the given interface from Honeycomb.
148 intfs = InterfaceKeywords.get_all_interfaces_cfg_data(node)
150 if intf["name"] == interface:
155 def get_all_interfaces_oper_info(node):
156 """Get operational data about all interfaces from Honeycomb.
158 :param node: Honeycomb node.
160 :return: Operational data about all interfaces from Honeycomb.
162 :raises HoneycombError: If it is not possible to get operational data.
165 status_code, resp = HcUtil.get_honeycomb_data(node,
166 "oper_vpp_interfaces")
167 if status_code != HTTPCodes.OK:
168 raise HoneycombError(
169 "Not possible to get operational information about the "
170 "interfaces. Status code: {0}.".format(status_code))
172 intf = HcUtil.parse_json_response(resp, ("interfaces-state",
179 def get_interface_oper_info(node, interface):
180 """Get operational data about the given interface from Honeycomb.
182 :param node: Honeycomb node.
183 :param interface: The name of interface.
186 :return: Operational data about the given interface from Honeycomb.
190 intfs = InterfaceKeywords.get_all_interfaces_oper_info(node)
192 if intf["name"] == interface:
197 def set_interface_state(node, interface, state="up"):
198 """Set VPP interface state.
200 The keyword changes the administration state of interface to up or down
201 depending on the parameter "state".
203 :param node: Honeycomb node.
204 :param interface: The name of interface.
205 :param state: The requested state, only "up" and "down" are valid
210 :return: Content of response.
212 :raises KeyError: If the argument "state" is nor "up" or "down".
213 :raises HoneycombError: If the interface is not present on the node.
216 intf_state = {"up": "true",
218 intfs = InterfaceKeywords.get_all_interfaces_cfg_data(node)
220 if intf["name"] == interface:
221 intf["enabled"] = intf_state[state.lower()]
222 new_intf = {"interfaces": {"interface": intfs}}
223 return InterfaceKeywords._configure_interface(node, interface,
225 raise HoneycombError("The interface '{0}' is not present on node "
226 "'{1}'.".format(interface, node['host']))
229 def set_interface_up(node, interface):
230 """Set the administration state of VPP interface to up.
232 :param node: Honeycomb node.
233 :param interface: The name of interface.
236 :return: Content of response
240 return InterfaceKeywords.set_interface_state(node, interface, "up")
243 def set_interface_down(node, interface):
244 """Set the administration state of VPP interface to down.
246 :param node: Honeycomb node.
247 :param interface: The name of interface.
250 :return: Content of response.
254 return InterfaceKeywords.set_interface_state(node, interface, "down")
257 def add_bridge_domain_to_interface(node, interface, bd_name,
258 split_horizon_group=None, bvi=None):
259 """Add a new bridge domain to an interface and set its parameters.
261 :param node: Honeycomb node.
262 :param interface: The name of interface.
263 :param bd_name: Bridge domain name.
264 :param split_horizon_group: Split-horizon group name.
265 :param bvi: The bridged virtual interface.
269 :type split_horizon_group: str
271 :return: Content of response.
273 :raises HoneycombError: If the interface is not present on the node.
276 intfs = InterfaceKeywords.get_all_interfaces_cfg_data(node)
277 v3po_l2 = {"bridge-domain": str(bd_name)}
278 if split_horizon_group:
279 v3po_l2["split-horizon-group"] = str(split_horizon_group)
281 v3po_l2["bridged-virtual-interface"] = str(bvi)
283 if intf["name"] == interface:
284 intf["v3po:l2"] = v3po_l2
285 new_intf = {"interfaces": {"interface": intfs}}
286 return InterfaceKeywords._configure_interface(node, interface,
288 raise HoneycombError("The interface '{0}' is not present on node "
289 "'{1}'.".format(interface, node['host']))
292 class BridgeDomainKeywords(object):
293 """Keywords for Bridge domain manipulation.
295 Implements keywords which get configuration and operational data about
296 bridge domains and put the bridge domains' parameters using Honeycomb REST
304 def _create_json_bridge_domain_info(name, **kwargs):
305 """Generate bridge domain information in the structure as it is expected
308 The generated data structure is as follows:
317 "unknown-unicast-flood": "false",
318 "arp-termination": "false"
324 :param name: The name of new bridge-domain.
325 :param kwargs: named arguments:
326 flood (bool): If True, flooding is enabled.
327 forward (bool): If True, packet forwarding is enabled.
328 learn (bool): If True, learning is enabled.
329 uu_flood (bool): If True, unknown unicast flooding is enabled.
330 arp_termination (bool): If True, ARP termination is enabled.
333 :return: Bridge domain information in format suitable for Honeycomb.
335 :raises KeyError: If at least one of kwargs items is missing.
342 "flood": str(kwargs["flood"]).lower(),
343 "forward": str(kwargs["forward"]).lower(),
344 "learn": str(kwargs["learn"]).lower(),
345 "unknown-unicast-flood": str(kwargs["uu_flood"]).lower(),
346 "arp-termination": str(kwargs["arp_termination"]).lower()},
354 def create_bridge_domain(node, name, flood=True, forward=True, learn=True,
355 uu_flood=True, arp_termination=False):
356 """Create a bridge domain using Honeycomb.
358 This keyword adds a new bridge domain to the list of bridge domains and
359 sets its parameters. The existing bridge domains are untouched.
360 :param node: Node with Honeycomb where the bridge domain should be
362 :param name: The name of new bridge-domain.
363 :param flood: If True, flooding is enabled.
364 :param forward: If True, packet forwarding is enabled.
365 :param learn: If True, learning is enabled.
366 :param uu_flood: If True, unknown unicast flooding is enabled.
367 :param arp_termination: If True, ARP termination is enabled.
374 :type arp_termination: bool
375 :raises HoneycombError: If the bridge domain already exists or it has
379 existing_brds = BridgeDomainKeywords.\
380 get_all_bds_cfg_data(node, ignore_404=True)
382 for brd in existing_brds:
383 if brd["name"] == name:
384 raise HoneycombError("Bridge domain {0} already exists.".
387 brd_info = BridgeDomainKeywords._create_json_bridge_domain_info(
388 name, flood=flood, forward=forward, learn=learn, uu_flood=uu_flood,
389 arp_termination=arp_termination)
390 for brd in existing_brds:
391 brd_info["bridge-domains"]["bridge-domain"].append(brd)
393 status_code, _ = HcUtil.put_honeycomb_data(node, "config_bridge_domain",
395 if status_code != HTTPCodes.OK:
396 raise HoneycombError(
397 "Bridge domain {0} was not created. "
398 "Status code: {01}.".format(name, status_code))
401 def get_all_bds_oper_data(node):
402 """Get operational data about all bridge domains from Honeycomb.
404 :param node: Honeycomb node.
406 :return: Operational data about all bridge domains from Honeycomb.
408 :raises HoneycombError: If it is not possible to get information about
412 status_code, resp = HcUtil.get_honeycomb_data(node,
413 "oper_bridge_domains")
414 if status_code != HTTPCodes.OK:
415 raise HoneycombError(
416 "Not possible to get information about the bridge domains. "
417 "Status code: {0}.".format(status_code))
419 br_domains = HcUtil.parse_json_response(resp, ("bridge-domains",
426 def get_bd_oper_data(node, name):
427 """Get operational data about the given bridge domain from Honeycomb.
429 :param node: Honeycomb node.
430 :param name: The name of bridge domain.
433 :return: Operational data about the given bridge domain from Honeycomb.
437 br_domains = BridgeDomainKeywords.get_all_bds_oper_data(node)
438 for br_domain in br_domains:
439 if br_domain["name"] == name:
440 br_domain["name"] = br_domain["name"]
445 def get_all_bds_cfg_data(node, ignore_404=False):
446 """Get configuration data about all bridge domains from Honeycomb.
448 :param node: Honeycomb node.
449 :param ignore_404: If True, the error 404 is ignored.
451 :type ignore_404: bool
452 :return: Configuration data about all bridge domains from Honeycomb.
454 :raises HoneycombError: If it is not possible to get information about
458 status_code, resp = HcUtil.get_honeycomb_data(node,
459 "config_bridge_domain")
460 if status_code != HTTPCodes.OK:
461 if ignore_404 and status_code == HTTPCodes.NOT_FOUND:
463 logger.debug("Error 404 ignored")
465 raise HoneycombError(
466 "Not possible to get information about the bridge domains. "
467 "Status code: {0}.".format(status_code))
470 br_domains = HcUtil.parse_json_response(resp, ("bridge-domains",
477 def get_bd_cfg_data(node, name):
478 """Get configuration data about the given bridge domain from Honeycomb.
480 :param node: Honeycomb node.
481 :param name: The name of bridge domain.
484 :return: Configuration data about the given bridge domain from
489 br_domains = BridgeDomainKeywords.get_all_bds_cfg_data(node)
490 for br_domain in br_domains:
491 if br_domain["name"] == name:
496 def delete_all_bridge_domains(node):
497 """Delete all bridge domains on Honeycomb node.
499 :param node: Honeycomb node.
501 :return: Response from DELETE request.
503 :raises HoneycombError: If it is not possible to delete all bridge
507 status_code, resp = HcUtil.delete_honeycomb_data(node,
508 "config_bridge_domain")
509 if status_code != HTTPCodes.OK:
510 raise HoneycombError(
511 "Not possible to delete all bridge domains. "
512 "Status code: {0}.".format(status_code))
516 def remove_bridge_domain(node, name):
517 """Remove one bridge domain from Honeycomb.
519 :param node: Honeycomb node.
520 :param name: Name of the bridge domain to be removed.
523 :return: True if the bridge domain was removed.
525 :raises HoneycombError: If it is not possible to remove the bridge
529 br_domains = BridgeDomainKeywords.get_all_bds_cfg_data(node)
530 for br_domain in br_domains:
531 if br_domain["name"] == name:
532 br_domains.remove(br_domain)
533 brd_info = {"bridge-domains": {"bridge-domain": br_domains}}
534 status_code, _ = HcUtil.put_honeycomb_data(
535 node, "config_bridge_domain", dumps(brd_info))
536 if status_code != HTTPCodes.OK:
537 raise HoneycombError(
538 "Bridge domain '{0}' was not deleted. "
539 "Status code: {1}.".format(name, status_code))
542 raise HoneycombError("Not possible to delete bridge domain '{0}'. The "
543 "bridge domain was not found".format(name))