HC2VPP-331: Fix Honeycomb fails to assign VRF to interface
[csit.git] / resources / libraries / python / honeycomb / FIB.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """This module implements keywords to manipulate FIB tables using
15 Honeycomb REST API."""
16
17 from robot.api import logger
18
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
25
26
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,
32     """
33
34     def __init__(self):
35         pass
36
37     @staticmethod
38     def _set_fib_table_properties(node, path, data=None):
39         """Set FIB table properties and check the return code.
40
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.
44         :type node: dict
45         :type path: str
46         :type data: dict
47         :returns: Content of response.
48         :rtype: bytearray
49         :raises HoneycombError: If the status code in response is not
50         200 = OK.
51         """
52
53         if data:
54             status_code, resp = HcUtil. \
55                 put_honeycomb_data(node, "config_fib_table", data, path,
56                                    data_representation=DataRepresentation.JSON)
57         else:
58             status_code, resp = HcUtil. \
59                 delete_honeycomb_data(node, "config_fib_table", path)
60
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.")
64             else:
65                 raise HoneycombError(
66                     "The configuration of FIB table was not successful. "
67                     "Status code: {0}.".format(status_code))
68         return resp
69
70     @staticmethod
71     def configure_fib_table(node, ip_version, vrf=1):
72         """Configure a FIB table according to the data provided.
73
74         :param node: Honeycomb node.
75         :param ip_version: IP protocol version, ipv4 or ipv6.
76         :param vrf: vrf-id to attach configuration to.
77         :type node: dict
78         :type ip_version: str
79         :type vrf: int
80         :returns: Content of response.
81         :rtype: bytearray
82         """
83         full_data = {
84             "vpp-fib-table-management:table": [
85                 {
86                     "table-id": vrf,
87                     "address-family": "vpp-fib-table-management:{0}"
88                     .format(ip_version),
89                     "name": "{0}-VRF:{1}".format(ip_version, vrf)
90                 }
91             ]
92         }
93         path = "/table/{0}/vpp-fib-table-management:{1}".format(vrf, ip_version)
94         return FibKeywords._set_fib_table_properties(node, path, full_data)
95
96     @staticmethod
97     def delete_fib_table(node, ip_version, vrf=1):
98         """Delete the specified FIB table from configuration data.
99
100         :param node: Honeycomb node.
101         :param ip_version: IP protocol version, ipv4 or ipv6.
102         :param vrf: vrf-id to attach configuration to.
103         :type node: dict
104         :type ip_version: str
105         :type vrf: int
106         :returns: Content of response.
107         :rtype: bytearray
108         """
109
110         path = "/table/{0}/vpp-fib-table-management:{1}".format(vrf, ip_version)
111         return FibKeywords._set_fib_table_properties(node, path)
112
113     @staticmethod
114     def get_fib_table_oper(node, ip_version, vrf=1):
115         """Retrieve operational data about the specified FIB table.
116
117         :param node: Honeycomb node.
118         :param ip_version: IP protocol version, ipv4 or ipv6.
119         :param vrf: vrf-id to attach configuration to.
120         :type node: dict
121         :type ip_version: str
122         :type vrf: int
123         :returns: FIB table operational data.
124         :rtype: list
125         :raises HoneycombError: If the operation fails.
126         """
127
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)
131
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))
136
137         data = resp['vpp-fib-table-management:table'][0]
138
139         return data