1 # Copyright (c) 2018 Bell Canada, Pantheon Technologies 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 FIB tables using
15 Honeycomb REST API."""
17 from robot.api import logger
19 from resources.libraries.python.HTTPRequest import HTTPCodes
20 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
21 from resources.libraries.python.honeycomb.HoneycombUtil \
22 import DataRepresentation
23 from resources.libraries.python.honeycomb.HoneycombUtil \
24 import HoneycombUtil as HcUtil
27 class FibKeywords(object):
28 """Implementation of keywords which make it possible to:
29 - add/remove FIB tables,
30 - add/remove FIB table entries
31 - get operational data about FIB tables,
38 def _set_fib_table_properties(node, path, data=None):
39 """Set FIB table properties and check the return code.
41 :param node: Honeycomb node.
42 :param path: Path which is added to the base path to identify the data.
43 :param data: The new data to be set. If None, the item will be removed.
47 :returns: Content of response.
49 :raises HoneycombError: If the status code in response is not
54 status_code, resp = HcUtil. \
55 put_honeycomb_data(node, "config_fib_table", data, path,
56 data_representation=DataRepresentation.JSON)
58 status_code, resp = HcUtil. \
59 delete_honeycomb_data(node, "config_fib_table", path)
61 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
62 if data is None and '"error-tag":"data-missing"' in resp:
63 logger.debug("data does not exist in path.")
66 "The configuration of FIB table was not successful. "
67 "Status code: {0}.".format(status_code))
71 def configure_fib_table(node, ip_version, vrf=1):
72 """Configure a FIB table according to the data provided.
74 :param node: Honeycomb node.
75 :param ip_version: IP protocol version, ipv4 or ipv6.
76 :param vrf: vrf-id to attach configuration to.
80 :returns: Content of response.
84 "vpp-fib-table-management:table": [
87 "address-family": "vpp-fib-table-management:{0}"
89 "name": "{0}-VRF:{1}".format(ip_version, vrf)
93 path = "/table/{0}/vpp-fib-table-management:{1}".format(vrf, ip_version)
94 return FibKeywords._set_fib_table_properties(node, path, full_data)
97 def delete_fib_table(node, ip_version, vrf=1):
98 """Delete the specified FIB table from configuration data.
100 :param node: Honeycomb node.
101 :param ip_version: IP protocol version, ipv4 or ipv6.
102 :param vrf: vrf-id to attach configuration to.
104 :type ip_version: str
106 :returns: Content of response.
110 path = "/table/{0}/vpp-fib-table-management:{1}".format(vrf, ip_version)
111 return FibKeywords._set_fib_table_properties(node, path)
114 def get_fib_table_oper(node, ip_version, vrf=1):
115 """Retrieve operational data about the specified FIB table.
117 :param node: Honeycomb node.
118 :param ip_version: IP protocol version, ipv4 or ipv6.
119 :param vrf: vrf-id to attach configuration to.
121 :type ip_version: str
123 :returns: FIB table operational data.
125 :raises HoneycombError: If the operation fails.
128 path = "/table/{0}/vpp-fib-table-management:{1}".format(vrf, ip_version)
129 status_code, resp = HcUtil. \
130 get_honeycomb_data(node, "oper_fib_table", path)
132 if status_code != HTTPCodes.OK:
133 raise HoneycombError(
134 "Not possible to get operational information about the "
135 "FIB tables. Status code: {0}.".format(status_code))
137 data = resp['vpp-fib-table-management:table'][0]