aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/honeycomb/BGP.py
blob: 976e41d37993bea9f3cbd2777c71b025c7e50929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Copyright (c) 2018 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Keywords to manipulate BGP configuration using Honeycomb REST API."""

from resources.libraries.python.Constants import Constants as Const
from resources.libraries.python.HTTPRequest import HTTPCodes
from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
from resources.libraries.python.honeycomb.HoneycombUtil \
    import HoneycombUtil as HcUtil


class BGPKeywords(object):
    """Keywords to manipulate BGP configuration.

    Implements keywords which read configuration and operational data for
    the BGP feature, and configure BGP parameters using Honeycomb REST API.
    """

    def __init__(self):
        """Initializer."""
        pass

    @staticmethod
    def _configure_bgp_peer(node, path, data=None):
        """Send BGP peer configuration data and check the response.

        :param node: Honeycomb node.
        :param path: Additional path to append to the base BGP config path.
        :param data: Configuration data to be sent in PUT request.
        :type node: dict
        :type path: str
        :type data: dict
        :returns: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If the status code in response to PUT is not
            200 = OK or 201 = ACCEPTED.
        """

        if data is None:
            status_code, resp = HcUtil. \
                delete_honeycomb_data(node, "config_bgp_peer", path)
        else:
            status_code, resp = HcUtil.\
                put_honeycomb_data(node, "config_bgp_peer", data, path)
        if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
            raise HoneycombError(
                "The configuration of BGP peer was not successful. "
                "Status code: {0}.".format(status_code))
        return resp

    @staticmethod
    def _configure_bgp_route(node, path, data=None):
        """Send BGP route configuration data and check the response.

        :param node: Honeycomb node.
        :param path: Additional path to append to the base BGP config path.
        :param data: Configuration data to be sent in PUT request.
        :type node: dict
        :type path: str
        :type data: dict
        :returns: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If the status code in response to PUT is not
            200 = OK or 201 = ACCEPTED.
        """

        if data is None:
            status_code, resp = HcUtil. \
                delete_honeycomb_data(node, "config_bgp_route", path)
        else:
            status_code, resp = HcUtil. \
                put_honeycomb_data(node, "config_bgp_route", data, path)
        if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
            raise HoneycombError(
                "The configuration of BGP route was not successful. "
                "Status code: {0}.".format(status_code))
        return resp

    @staticmethod
    def get_full_bgp_configuration(node):
        """Get BGP configuration from the node.

        :param node: Honeycomb node.
        :type node: dict
        :returns: BGP configuration data.
        :rtype: dict
        :raises HoneycombError: If the status code in response is not 200 = OK.
        """

        status_code, resp = HcUtil. \
            get_honeycomb_data(node, "config_bgp_peer")
        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get configuration information about BGP."
                " Status code: {0}.".format(status_code))
        return resp

    @staticmethod
    def get_bgp_peer(node, address, datastore='config'):
        """Get BGP configuration of the specified peer from the node.

        :param node: Honeycomb node.
        :param address: IP address of the peer.
        :param datastore: Get data from config or operational datastore.
        :type node: dict
        :type address: str
        :type datastore: str
        :returns: BGP peer configuration data.
        :rtype: dict
        :raises HoneycombError: If the status code in response is not 200 = OK.
        """

        path = "bgp-openconfig-extensions:neighbors/" \
               "neighbor/{0}".format(address)
        if datastore != "operational":
            url = "config_bgp_peer"
        else:
            url = "oper_bgp"
            path = "peer/bgp:%2F%2F{0}".format(address)
        status_code, resp = HcUtil. \
            get_honeycomb_data(node, url, path)
        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get configuration information about the BGP"
                " peer. Status code: {0}.".format(status_code))
        return resp

    @staticmethod
    def add_bgp_peer(node, address, data):
        """Configure a BGP peer on the node.

        :param node: Honeycomb node.
        :param address: IP address of the peer.
        :param data: Peer configuration data.
        :type node: dict
        :type address: str
        :type data: dict
        :returns: Content of response.
        :rtype: bytearray
        """

        path = "bgp-openconfig-extensions:neighbors/neighbor/{address}".format(
            address=address)
        return BGPKeywords._configure_bgp_peer(node, path, data)

    @staticmethod
    def remove_bgp_peer(node, address):
        """Remove a BGP peer from the configuration.

        :param node: Honeycomb node.
        :param address: IP address of the peer.
        :type node: dict
        :type address: str
        :returns: Content of response.
        :rtype: bytearray
        """

        path = "bgp-openconfig-extensions:neighbors/neighbor/{address}".format(
            address=address)
        return BGPKeywords._configure_bgp_peer(node, path)

    @staticmethod
    def configure_bgp_route(node, peer_address, data, route_address,
                            index, ip_version):
        """Configure a route for the BGP peer specified by peer IP address.

        :param node: Honeycomb node.
        :param peer_address: IP address of the BGP peer.
        :param data: Route configuration data.
        :param route_address: IP address of the route.
        :param index: Index number of the route within specified peer.
        :param ip_version: IP protocol version. ipv4 or ipv6
        :type node: dict
        :type peer_address: str
        :type data: dict
        :type route_address: str
        :type index: int
        :type ip_version: str
        :returns: Content of response.
        :rtype: bytearray
        """

        route_address = route_address.replace("/", "%2F")

        if ip_version.lower() == "ipv4":
            path = "{0}/tables/bgp-types:ipv4-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv4-routes/ipv4-route/{1}/{2}" \
                .format(peer_address, route_address, index)
        else:
            path = "{0}/tables/bgp-types:ipv6-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv6-routes/ipv6-route/{1}/{2}" \
                .format(peer_address, route_address, index)

        return BGPKeywords._configure_bgp_route(node, path, data)

    @staticmethod
    def get_bgp_route(node, peer_address, route_address, index, ip_version):
        """Get all BGP peers from operational data.

        :param node: Honeycomb node.
        :param peer_address: IP address of the BGP peer.
        :param route_address: IP address of the route.
        :param index: Index number of the route within specified peer.
        :param ip_version: IP protocol version. ipv4 or ipv6
        :type node: dict
        :type peer_address: str
        :type route_address: str
        :type index: int
        :type ip_version: str
        :returns: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If the status code in response is not 200 = OK.
        """

        route_address = route_address.replace("/", "%2F")

        if ip_version.lower() == "ipv4":
            path = "{0}/tables/bgp-types:ipv4-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv4-routes/ipv4-route/{1}/{2}" \
                .format(peer_address, route_address, index)
        else:
            path = "{0}/tables/bgp-types:ipv6-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv6-routes/ipv6-route/{1}/{2}" \
                .format(peer_address, route_address, index)
        status_code, resp = HcUtil. \
            get_honeycomb_data(node, "config_bgp_route", path)
        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get configuration information about the BGP"
                " route. Status code: {0}.".format(status_code))

        return resp

    @staticmethod
    def get_all_peer_routes(node, peer_address, ip_version):
        """Get all configured routes for the given BGP peer.

        :param node: Honeycomb node.
        :param peer_address: IP address of the peer.
        :param ip_version: IP protocol version. ipv4 or ipv6
        :type node: dict
        :type peer_address: str
        :type ip_version: str
        :returns: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If the status code in response is not 200 = OK.
        """

        if ip_version.lower() == "ipv4":
            path = "{0}/tables/bgp-types:ipv4-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv4-routes".format(peer_address)
        else:
            path = "{0}/tables/bgp-types:ipv6-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv6-routes".format(peer_address)
        status_code, resp = HcUtil. \
            get_honeycomb_data(node, "config_bgp_route", path)
        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get configuration information about BGP"
                " routes. Status code: {0}.".format(status_code))

        return resp

    @staticmethod
    def remove_bgp_route(node, peer_address, route_address, index, ip_version):
        """Remove the specified BGP route from configuration.

        :param node: Honeycomb node.
        :param peer_address: IP address of the BGP peer.
        :param route_address: IP address of the route.
        :param index: Index number of the route within specified peer.
        :param ip_version: IP protocol version. ipv4 or ipv6
        :type node: dict
        :type peer_address: str
        :type route_address: str
        :type index: int
        :type ip_version: str
        :returns: Content of response.
        :rtype: bytearray
        """

        route_address = route_address.replace("/", "%2F")

        if ip_version.lower() == "ipv4":
            path = "{0}/tables/bgp-types:ipv4-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv4-routes/ipv4-route/{1}/{2}" \
                .format(peer_address, route_address, index)
        else:
            path = "{0}/tables/bgp-types:ipv6-address-family/" \
                   "bgp-types:unicast-subsequent-address-family/" \
                   "bgp-inet:ipv6-routes/ipv6-route/{1}/{2}" \
                .format(peer_address, route_address, index)

        return BGPKeywords._configure_bgp_route(node, path)

    @staticmethod
    def get_bgp_local_rib(node):
        """Get local RIB table from the Honeycomb node.

        :param node: Honeycomb node.
        :type node: dict
        :returns: RIB operational data.
        :rtype: dict
        :raises HoneycombError: If the status code in response is not 200 = OK.
        """

        path = "loc-rib"

        status_code, resp = HcUtil. \
            get_honeycomb_data(node, "oper_bgp", path)

        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get operational data from BGP local RIB."
                " Status code: {0}.".format(status_code))

        return resp

    @staticmethod
    def configure_bgp_base(node, ip_address, port, as_number):
        """Modify BGP config file. Requires a restart of Honeycomb to take
        effect.

        :param node: Honeycomb node.
        :param ip_address: BGP peer identifier/binding address.
        :param port: BGP binding port.
        :param as_number: Autonomous System ID number.
        :type node: dict
        :type ip_address: str
        :type port: int
        :type as_number: int
        :raises HoneycombError: If modifying the configuration fails.
        """

        from resources.libraries.python.ssh import SSH

        config = {
            '\\"bgp-binding-address\\"': '\\"{0}\\"'.format(ip_address),
            '\\"bgp-port\\"': port,
            '\\"bgp-as-number\\"': as_number}

        path = "{0}/config/bgp.json".format(Const.REMOTE_HC_DIR)

        for key, value in config.items():
            find = key
            replace = '"{0}": "{1}",'.format(key, value)

            argument = '"/{0}/c\\ {1}"'.format(find, replace)
            command = "sed -i {0} {1}".format(argument, path)

            ssh = SSH()
            ssh.connect(node)
            (ret_code, _, stderr) = ssh.exec_command_sudo(command)
            if ret_code != 0:
                raise HoneycombError("Failed to modify configuration on "
                                     "node {0}, {1}".format(node, stderr))

    @staticmethod
    def compare_rib_tables(data, ref):
        """Compare provided RIB table with reference. All reference entries must
        be present in data. Data entries not present in reference are ignored.

        :param data: Data from Honeycomb node.
        :param ref: Reference data to compare against.
        :type data: dict
        :type ref: dict
        :raises HoneycombError: If the tables do not match.
        """

        # Remove runtime attributes from data
        for item in data:
            item.pop("attributes", "")

        for item in ref:
            if item not in data:
                raise HoneycombError(
                    "RIB entry {0} not found in operational data {1}."
                    .format(item, data))