aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/honeycomb/HcAPIKwBridgeDomain.py
blob: 29a3a0e6ad3eebfcb55225da03b713100c6078a8 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# Copyright (c) 2016 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 bridge domain configuration using Honeycomb REST API.

The keywords make possible to put and get configuration data and to get
operational data.
"""

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


class BridgeDomainKeywords(object):
    """Keywords to manipulate bridge domain configuration.

    Implements keywords which get configuration and operational data about
    bridge domains and put the bridge domains' parameters using Honeycomb REST
    API.
    """

    PARAMS = ("flood", "forward", "learn", "unknown-unicast-flood",
              "arp-termination")

    def __init__(self):
        pass

    @staticmethod
    def _configure_bd(node, bd_name, data,
                      data_representation=DataRepresentation.JSON):
        """Send bridge domain configuration data and check the response.

        :param node: Honeycomb node.
        :param bd_name: The name of bridge domain.
        :param data: Configuration data to be sent in PUT request.
        :param data_representation: How the data is represented.
        :type node: dict
        :type bd_name: str
        :type data: dict
        :type data_representation: DataRepresentation
        :return: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If the status code in response on PUT is not
        200 = OK.
        """

        status_code, resp = HcUtil.\
            put_honeycomb_data(node, "config_bridge_domain", data,
                               data_representation=data_representation)
        if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
            raise HoneycombError(
                "The configuration of bridge domain '{0}' was not successful. "
                "Status code: {1}.".format(bd_name, status_code))
        return resp

    @staticmethod
    def _set_bd_properties(node, bd_name, path, new_value=None):
        """Set bridge domain properties.

        This method reads bridge domain configuration data, creates, changes or
        removes the requested data and puts it back to Honeycomb.

        :param node: Honeycomb node.
        :param bd_name: The name of bridge domain.
        :param path:  Path to data we want to change, create or remove.
        :param new_value: The new value to be set. If None, the item will be
        removed.
        :type node: dict
        :type bd_name: str
        :type path: tuple
        :type new_value: str, dict or list
        :return: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If it is not possible to get or set the data.
        """

        status_code, resp = HcUtil.\
            get_honeycomb_data(node, "config_bridge_domain")
        if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
            raise HoneycombError(
                "Not possible to get configuration information about the "
                "bridge domains. Status code: {0}.".format(status_code))

        if new_value:
            new_data = HcUtil.set_item_value(resp, path, new_value)
        else:
            new_data = HcUtil.remove_item(resp, path)

        return BridgeDomainKeywords._configure_bd(node, bd_name, new_data)

    @staticmethod
    def _create_bd_structure(bd_name, **kwargs):
        """Create the bridge domain data structure as it is expected by
        Honeycomb REST API.

        :param bd_name: Bridge domain name.
        :param kwargs: Parameters and their values. The accepted parameters are
        defined in BridgeDomainKeywords.PARAMS.
        :type bd_name: str
        :type kwargs: dict
        :return: Bridge domain data structure.
        :rtype: dict
        """

        bd_structure = {"name": bd_name}

        for param, value in kwargs.items():
            if param not in BridgeDomainKeywords.PARAMS:
                raise HoneycombError("The parameter {0} is invalid.".
                                     format(param))
            bd_structure[param] = str(value)

        return bd_structure

    @staticmethod
    def get_all_bds_cfg_data(node):
        """Get configuration data about all bridge domains from Honeycomb.

        :param node: Honeycomb node.
        :type node: dict
        :return: Configuration data about all bridge domains from Honeycomb.
        :rtype: list
        :raises HoneycombError: If it is not possible to get configuration data.
        """

        status_code, resp = HcUtil.\
            get_honeycomb_data(node, "config_bridge_domain")
        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get configuration information about the "
                "bridge domains. Status code: {0}.".format(status_code))
        try:
            return resp["bridge-domains"]["bridge-domain"]

        except (KeyError, TypeError):
            return []

    @staticmethod
    def get_bd_cfg_data(node, bd_name):
        """Get configuration data about the given bridge domain from Honeycomb.

        :param node: Honeycomb node.
        :param bd_name: The name of bridge domain.
        :type node: dict
        :type bd_name: str
        :return: Configuration data about the given bridge domain from
        Honeycomb.
        :rtype: dict
        """

        intfs = BridgeDomainKeywords.get_all_bds_cfg_data(node)
        for intf in intfs:
            if intf["name"] == bd_name:
                return intf
        return {}

    @staticmethod
    def get_all_bds_oper_data(node):
        """Get operational data about all bridge domains from Honeycomb.

        :param node: Honeycomb node.
        :type node: dict
        :return: Operational data about all bridge domains from Honeycomb.
        :rtype: list
        :raises HoneycombError: If it is not possible to get operational data.
        """

        status_code, resp = HcUtil.\
            get_honeycomb_data(node, "oper_bridge_domains")
        if status_code != HTTPCodes.OK:
            raise HoneycombError(
                "Not possible to get operational information about the "
                "bridge domains. Status code: {0}.".format(status_code))
        try:
            return resp["bridge-domains"]["bridge-domain"]

        except (KeyError, TypeError):
            return []

    @staticmethod
    def get_bd_oper_data(node, bd_name):
        """Get operational data about the given bridge domain from Honeycomb.

        :param node: Honeycomb node.
        :param bd_name: The name of bridge domain.
        :type node: dict
        :type bd_name: str
        :return: Operational data about the given bridge domain from Honeycomb.
        :rtype: dict
        """

        intfs = BridgeDomainKeywords.get_all_bds_oper_data(node)
        for intf in intfs:
            if intf["name"] == bd_name:
                return intf
        return {}

    @staticmethod
    def add_first_bd(node, bd_name, **kwargs):
        """Add the first bridge domain.

        If there are any other bridge domains configured, they will be removed.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain name.
        :param kwargs: Parameters and their values. The accepted parameters are
        defined in BridgeDomainKeywords.PARAMS
        :type node: dict
        :type bd_name: str
        :type kwargs: dict
        :return: Bridge domain data structure.
        :rtype: dict
        """

        new_bd = BridgeDomainKeywords._create_bd_structure(bd_name, **kwargs)
        bridge_domain = {"bridge-domains": {"bridge-domain": [new_bd, ]}}
        return BridgeDomainKeywords._configure_bd(node, bd_name, bridge_domain)

    @staticmethod
    def add_bd(node, bd_name, **kwargs):
        """Add a bridge domain.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain name.
        :param kwargs: Parameters and their values. The accepted parameters are
        defined in BridgeDomainKeywords.PARAMS
        :type node: dict
        :type bd_name: str
        :type kwargs: dict
        :return: Bridge domain data structure.
        :rtype: dict
        """

        path = ("bridge-domains", "bridge-domain")
        new_bd = BridgeDomainKeywords._create_bd_structure(bd_name, **kwargs)
        bridge_domain = [new_bd, ]
        return BridgeDomainKeywords._set_bd_properties(node, bd_name, path,
                                                       bridge_domain)

    @staticmethod
    def remove_all_bridge_domains(node):
        """Remove all bridge domains.

        :param node: Honeycomb node.
        :type node: dict
        :return: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If it is not possible to remove all bridge
        domains.
        """

        data = {"bridge-domains": {"bridge-domain": []}}

        status_code, resp = HcUtil.\
            put_honeycomb_data(node, "config_bridge_domain", data)

        if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
            raise HoneycombError("Not possible to remove all bridge domains. "
                                 "Status code: {0}.".format(status_code))
        return resp

    @staticmethod
    def remove_bridge_domain(node, bd_name):
        """Remove a bridge domain.

        :param node:  Honeycomb node.
        :param bd_name: The name of bridge domain to be removed.
        :type node: dict
        :type bd_name: str
        :return: Content of response.
        :rtype: bytearray
        :raises HoneycombError:If it is not possible to remove the bridge
        domain.
        """

        path = ("bridge-domains", ("bridge-domain", "name", bd_name))

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

        new_data = HcUtil.remove_item(resp, path)
        status_code, resp = HcUtil.\
            put_honeycomb_data(node, "config_bridge_domain", new_data)
        if status_code != HTTPCodes.OK:
            raise HoneycombError("Not possible to remove bridge domain {0}. "
                                 "Status code: {1}.".
                                 format(bd_name, status_code))
        return resp

    @staticmethod
    def configure_bridge_domain(node, bd_name, param, value):
        """Configure a bridge domain.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain name.
        :param param: Parameter to set, change or remove. The accepted
        parameters are defined in BridgeDomainKeywords.PARAMS
        :param value: The new value to be set, change or remove. If None, the
        item will be removed.
        :type node: dict
        :type bd_name: str
        :type param: str
        :type value: str
        :return: Content of response.
        :rtype: bytearray
        """

        if param not in BridgeDomainKeywords.PARAMS:
            raise HoneycombError("The parameter {0} is invalid.".format(param))

        path = ("bridge-domains", ("bridge-domain", "name", bd_name), param)
        return BridgeDomainKeywords.\
            _set_bd_properties(node, bd_name, path, value)

    @staticmethod
    def add_l2_fib_entry(node, bd_name, l2_fib_entry):
        """Add an L2 FIB entry to the bridge domain's list of L2 FIB entries.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain's name.
        :param l2_fib_entry: L2 FIB entry to be added to the L2 FIB table.
        :type node: dict
        :type bd_name: str
        :type l2_fib_entry: dict
        :return: Content of response.
        :rtype: bytearray
        """

        path = ("bridge-domains",
                ("bridge-domain", "name", bd_name),
                "l2-fib-table",
                "l2-fib-entry")

        new_l2_fib_entry = [l2_fib_entry, ]
        return BridgeDomainKeywords._set_bd_properties(
            node, bd_name, path, new_l2_fib_entry)

    @staticmethod
    def modify_l2_fib_entry(node, bd_name, mac, param, value):
        """Modify an existing L2 FIB entry in the bridge domain's L2 FIB table.
        The L2 FIB entry is specified by MAC address.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain's name.
        :param mac: MAC address used as the key in L2 FIB data structure.
        :param param: The parameter to be modified.
        :param value: The new value of the parameter.
        :type node: dict
        :type bd_name: str
        :type mac: str
        :type param: str
        :type value: str or int
        :return: Content of response.
        :rtype: bytearray
        """

        path = ("bridge-domains",
                ("bridge-domain", "name", bd_name),
                "l2-fib-table",
                ("l2-fib-entry", "phys-address", mac),
                param)

        return BridgeDomainKeywords._set_bd_properties(
            node, bd_name, path, value)

    @staticmethod
    def remove_l2_fib_entry(node, bd_name, mac):
        """Remove an L2 FIB entry from bridge domain's L2 FIB table. The
        entry is specified by MAC address.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain's name.
        :param mac: MAC address used as the key in L2 FIB data structure.
        :type node: dict
        :type bd_name: str
        :type mac: str
        :return: Content of response.
        :rtype: bytearray
        :raises HoneycombError: If it is not possible to remove the specified
        entry.
        """

        path = ("bridge-domains",
                ("bridge-domain", "name", bd_name),
                "l2-fib-table",
                ("l2-fib-entry", "phys-address", str(mac)))

        status_code, resp = HcUtil.\
            get_honeycomb_data(node, "config_bridge_domain")
        if status_code != HTTPCodes.OK:
            raise HoneycombError("Not possible to get configuration information"
                                 " about the L2 FIB entry {0} from bridge "
                                 "domain {1}. Status code: {2}.".
                                 format(mac, bd_name, status_code))

        new_data = HcUtil.remove_item(resp, path)
        status_code, resp = HcUtil.\
            put_honeycomb_data(node, "config_bridge_domain", new_data)
        if status_code != HTTPCodes.OK:
            raise HoneycombError("Not possible to remove L2 FIB entry {0} from "
                                 "bridge domain {1}. Status code: {2}.".
                                 format(mac, bd_name, status_code))
        return resp


    @staticmethod
    def remove_all_l2_fib_entries(node, bd_name):
        """Remove all entries from the bridge domain's L2 FIB table.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain's name.
        :type node: dict
        :type bd_name: str
        :return: Content of response.
        :rtype: bytearray
        """

        path = ("bridge-domains",
                ("bridge-domain", "name", bd_name),
                "l2-fib-table")

        return BridgeDomainKeywords._set_bd_properties(
            node, bd_name, path, None)

    @staticmethod
    def get_all_l2_fib_entries(node, bd_name):
        """Retrieves all entries from the bridge domain's L2 FIB table.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain's name.
        :type node: dict
        :type bd_name: str
        :return: Bridge domain's L2 FIB table or empty list if the table does
        not exist or it is empty.
        :rtype: list
        """

        bd_data = BridgeDomainKeywords.get_bd_oper_data(node, bd_name)
        try:
            return bd_data["l2-fib-table"]["l2-fib-entry"]
        except KeyError:
            return []

    @staticmethod
    def get_l2_fib_entry(node, bd_name, mac):
        """Retrieves an entry from bridge domain's L2 FIB table. The entry is
        specified by MAC address.

        :param node: Honeycomb node.
        :param bd_name: Bridge domain's name.
        :param mac: MAC address used as the key in L2 FIB data structure.
        :type node: dict
        :type bd_name: str
        :type mac: str
        :return: The requested entry from bridge domain's L2 FIB table or empty
        dictionary if it does not exist in the L2 FIB table.
        :rtype: dict
        """

        l2_fib = BridgeDomainKeywords.get_all_l2_fib_entries(node, bd_name)
        for entry in l2_fib:
            if entry["phys-address"] == mac:
                return entry
        return {}