4 Vhosts setup test
[csit.git] / resources / libraries / python / honeycomb / HcAPIKwInterfaces.py
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:
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 """Keywords to manipulate interface configuration using Honeycomb REST API.
15
16 The keywords make possible to put and get configuration data and to get
17 operational data.
18 """
19
20 from resources.libraries.python.HTTPRequest import HTTPCodes
21 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
22 from resources.libraries.python.honeycomb.HoneycombUtil \
23     import DataRepresentation
24 from resources.libraries.python.honeycomb.HoneycombUtil \
25     import HoneycombUtil as HcUtil
26
27
28 # pylint: disable=too-many-public-methods
29 # pylint: disable=too-many-lines
30 class InterfaceKeywords(object):
31     """Keywords for Interface manipulation.
32
33     Implements keywords which get configuration and operational data about
34     vpp interfaces and set the interface's parameters using Honeycomb REST API.
35     """
36
37     INTF_PARAMS = ("name", "description", "type", "enabled",
38                    "link-up-down-trap-enable", "v3po:l2")
39     IPV4_PARAMS = ("enabled", "forwarding", "mtu")
40     IPV6_PARAMS = ("enabled", "forwarding", "mtu", "dup-addr-detect-transmits")
41     IPV6_AUTOCONF_PARAMS = ("create-global-addresses",
42                             "create-temporary-addresses",
43                             "temporary-valid-lifetime",
44                             "temporary-preferred-lifetime")
45     ETH_PARAMS = ("mtu", )
46     ROUTING_PARAMS = ("vrf-id", )
47     VXLAN_PARAMS = ("src", "dst", "vni", "encap-vrf-id")
48     L2_PARAMS = ("bridge-domain", "split-horizon-group",
49                  "bridged-virtual-interface")
50     L2_REWRITE_TAG_PARAMS = ("rewrite-operation",
51                              "first-pushed",
52                              "tag1",
53                              "tag2")
54     TAP_PARAMS = ("tap-name", "mac", "device-instance")
55     VHOST_USER_PARAMS = ("socket", "role")
56     SUB_INTF_PARAMS = ("super-interface",
57                        "identifier",
58                        "vlan-type",
59                        "number-of-tags",
60                        "outer-id",
61                        "inner-id",
62                        "match-any-outer-id",
63                        "match-any-inner-id",
64                        "exact-match",
65                        "default-subif")
66
67     def __init__(self):
68         pass
69
70     @staticmethod
71     def _configure_interface(node, interface, data,
72                              data_representation=DataRepresentation.JSON):
73         """Send interface configuration data and check the response.
74
75         :param node: Honeycomb node.
76         :param interface: The name of interface.
77         :param data: Configuration data to be sent in PUT request.
78         :param data_representation: How the data is represented.
79         :type node: dict
80         :type interface: str
81         :type data: dict
82         :type data_representation: DataRepresentation
83         :return: Content of response.
84         :rtype: bytearray
85         :raises HoneycombError: If the status code in response on PUT is not
86         200 = OK.
87         """
88
89         status_code, resp = HcUtil.\
90             put_honeycomb_data(node, "config_vpp_interfaces", data,
91                                data_representation=data_representation)
92         if status_code != HTTPCodes.OK:
93             raise HoneycombError(
94                 "The configuration of interface '{0}' was not successful. "
95                 "Status code: {1}.".format(interface, status_code))
96         return resp
97
98     @staticmethod
99     def get_all_interfaces_cfg_data(node):
100         """Get configuration data about all interfaces from Honeycomb.
101
102         :param node: Honeycomb node.
103         :type node: dict
104         :return: Configuration data about all interfaces from Honeycomb.
105         :rtype: list
106         :raises HoneycombError: If it is not possible to get configuration data.
107         """
108
109         status_code, resp = HcUtil.\
110             get_honeycomb_data(node, "config_vpp_interfaces")
111         if status_code != HTTPCodes.OK:
112             raise HoneycombError(
113                 "Not possible to get configuration information about the "
114                 "interfaces. Status code: {0}.".format(status_code))
115         try:
116             return resp["interfaces"]["interface"]
117
118         except (KeyError, TypeError):
119             return []
120
121     @staticmethod
122     def get_interface_cfg_data(node, interface):
123         """Get configuration data about the given interface from Honeycomb.
124
125         :param node: Honeycomb node.
126         :param interface: The name of interface.
127         :type node: dict
128         :type interface: str
129         :return: Configuration data about the given interface from Honeycomb.
130         :rtype: dict
131         """
132
133         intfs = InterfaceKeywords.get_all_interfaces_cfg_data(node)
134         for intf in intfs:
135             if intf["name"] == interface:
136                 return intf
137         return {}
138
139     @staticmethod
140     def get_all_interfaces_oper_data(node):
141         """Get operational data about all interfaces from Honeycomb.
142
143         :param node: Honeycomb node.
144         :type node: dict
145         :return: Operational data about all interfaces from Honeycomb.
146         :rtype: list
147         :raises HoneycombError: If it is not possible to get operational data.
148         """
149
150         status_code, resp = HcUtil.\
151             get_honeycomb_data(node, "oper_vpp_interfaces")
152         if status_code != HTTPCodes.OK:
153             raise HoneycombError(
154                 "Not possible to get operational information about the "
155                 "interfaces. Status code: {0}.".format(status_code))
156         try:
157             return resp["interfaces-state"]["interface"]
158
159         except (KeyError, TypeError):
160             return []
161
162     @staticmethod
163     def get_interface_oper_data(node, interface):
164         """Get operational data about the given interface from Honeycomb.
165
166         :param node: Honeycomb node.
167         :param interface: The name of interface.
168         :type node: dict
169         :type interface: str
170         :return: Operational data about the given interface from Honeycomb.
171         :rtype: dict
172         """
173
174         intfs = InterfaceKeywords.get_all_interfaces_oper_data(node)
175         for intf in intfs:
176             if intf["name"] == interface:
177                 return intf
178         return {}
179
180     @staticmethod
181     def _set_interface_properties(node, interface, path, new_value=None):
182         """Set interface properties.
183
184         This method reads interface configuration data, creates, changes or
185         removes the requested data and puts it back to Honeycomb.
186
187         :param node: Honeycomb node.
188         :param interface: The name of interface.
189         :param path:  Path to data we want to change / create / remove.
190         :param new_value: The new value to be set. If None, the item will be
191         removed.
192         :type node: dict
193         :type interface: str
194         :type path: tuple
195         :type new_value: str, dict or list
196         :return: Content of response.
197         :rtype: bytearray
198         :raises HoneycombError: If it is not possible to get or set the data.
199         """
200
201         status_code, resp = HcUtil.\
202             get_honeycomb_data(node, "config_vpp_interfaces")
203         if status_code != HTTPCodes.OK:
204             raise HoneycombError(
205                 "Not possible to get configuration information about the "
206                 "interfaces. Status code: {0}.".format(status_code))
207
208         if new_value:
209             new_data = HcUtil.set_item_value(resp, path, new_value)
210         else:
211             new_data = HcUtil.remove_item(resp, path)
212         return InterfaceKeywords._configure_interface(node, interface, new_data)
213
214     @staticmethod
215     def set_interface_state(node, interface, state="up"):
216         """Set VPP interface state.
217
218         The keyword changes the administration state of interface to up or down
219         depending on the parameter "state".
220
221         :param node: Honeycomb node.
222         :param interface: The name of interface.
223         :param state: The requested state, only "up" and "down" are valid
224         values.
225         :type node: dict
226         :type interface: str
227         :type state: str
228         :return: Content of response.
229         :rtype: bytearray
230         :raises KeyError: If the argument "state" is nor "up" or "down".
231         :raises HoneycombError: If the interface is not present on the node.
232         """
233
234         intf_state = {"up": "true",
235                       "down": "false"}
236
237         path = ("interfaces", ("interface", "name", str(interface)), "enabled")
238         return InterfaceKeywords._set_interface_properties(
239             node, interface, path, intf_state[state.lower()])
240
241     @staticmethod
242     def set_interface_up(node, interface):
243         """Set the administration state of VPP interface to up.
244
245         :param node: Honeycomb node.
246         :param interface: The name of interface.
247         :type node: dict
248         :type interface: str
249         :return: Content of response
250         :rtype: bytearray
251         """
252
253         return InterfaceKeywords.set_interface_state(node, interface, "up")
254
255     @staticmethod
256     def set_interface_down(node, interface):
257         """Set the administration state of VPP interface to down.
258
259         :param node: Honeycomb node.
260         :param interface: The name of interface.
261         :type node: dict
262         :type interface: str
263         :return: Content of response.
264         :rtype: bytearray
265         """
266
267         return InterfaceKeywords.set_interface_state(node, interface, "down")
268
269     @staticmethod
270     def add_bridge_domain_to_interface(node, interface, bd_name,
271                                        split_horizon_group=None, bvi=None):
272         """Add a new bridge domain to an interface and set its parameters.
273
274         :param node: Honeycomb node.
275         :param interface: The name of interface.
276         :param bd_name: Bridge domain name.
277         :param split_horizon_group: Split-horizon group name.
278         :param bvi: The bridged virtual interface.
279         :type node: dict
280         :type interface: str
281         :type bd_name: str
282         :type split_horizon_group: str
283         :type bvi: str
284         :return: Content of response.
285         :rtype: bytearray
286         :raises HoneycombError: If the interface is not present on the node.
287         """
288
289         v3po_l2 = {"bridge-domain": str(bd_name)}
290         if split_horizon_group:
291             v3po_l2["split-horizon-group"] = str(split_horizon_group)
292         if bvi:
293             v3po_l2["bridged-virtual-interface"] = str(bvi)
294
295         path = ("interfaces", ("interface", "name", str(interface)), "v3po:l2")
296
297         return InterfaceKeywords._set_interface_properties(
298             node, interface, path, v3po_l2)
299
300     @staticmethod
301     def configure_interface_base(node, interface, param, value):
302         """Configure the base parameters of interface.
303
304         :param node: Honeycomb node.
305         :param interface: The name of interface.
306         :param param: Parameter to configure (set, change, remove)
307         :param value: The value of parameter. If None, the parameter will be
308         removed.
309         :type node: dict
310         :type interface: str
311         :type param: str
312         :type value: str
313         :return: Content of response.
314         :rtype: bytearray
315         :raises HoneycombError: If the parameter is not valid.
316         """
317
318         if param not in InterfaceKeywords.INTF_PARAMS:
319             raise HoneycombError("The parameter {0} is invalid.".format(param))
320
321         path = ("interfaces", ("interface", "name", interface), param)
322         return InterfaceKeywords._set_interface_properties(
323             node, interface, path, value)
324
325     @staticmethod
326     def configure_interface_ipv4(node, interface, param, value):
327         """Configure IPv4 parameters of interface
328
329         :param node: Honeycomb node.
330         :param interface: The name of interface.
331         :param param: Parameter to configure (set, change, remove)
332         :param value: The value of parameter. If None, the parameter will be
333         removed.
334         :type node: dict
335         :type interface: str
336         :type param: str
337         :type value: str
338         :return: Content of response.
339         :rtype: bytearray
340         :raises HoneycombError: If the parameter is not valid.
341         """
342
343         if param not in InterfaceKeywords.IPV4_PARAMS:
344             raise HoneycombError("The parameter {0} is invalid.".format(param))
345
346         path = ("interfaces", ("interface", "name", interface),
347                 "ietf-ip:ipv4", param)
348         return InterfaceKeywords._set_interface_properties(
349             node, interface, path, value)
350
351     @staticmethod
352     def add_first_ipv4_address(node, interface, ip_addr, network):
353         """Add the first IPv4 address.
354
355         If there are any other addresses configured, they will be removed.
356
357         :param node: Honeycomb node.
358         :param interface: The name of interface.
359         :param ip_addr: IPv4 address to be set.
360         :param network: Netmask or length of network prefix.
361         :type node: dict
362         :type interface: str
363         :type ip_addr: str
364         :type network: str or int
365         :return: Content of response.
366         :rtype: bytearray
367         """
368
369         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4")
370         if isinstance(network, basestring):
371             address = {"address": [{"ip": ip_addr, "netmask": network}, ]}
372         elif isinstance(network, int) and (0 < network < 33):
373             address = {"address": [{"ip": ip_addr, "prefix-length": network}, ]}
374         else:
375             raise HoneycombError("Value {0} is not a valid netmask or network "
376                                  "prefix length.".format(network))
377         return InterfaceKeywords._set_interface_properties(
378             node, interface, path, address)
379
380     @staticmethod
381     def add_ipv4_address(node, interface, ip_addr, network):
382         """Add IPv4 address.
383
384         :param node: Honeycomb node.
385         :param interface: The name of interface.
386         :param ip_addr: IPv4 address to be set.
387         :param network: Netmask or length of network prefix.
388         :type node: dict
389         :type interface: str
390         :type ip_addr: str
391         :type network: str or int
392         :return: Content of response.
393         :rtype: bytearray
394         """
395
396         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4",
397                 "address")
398         if isinstance(network, basestring):
399             address = {"address": [{"ip": ip_addr, "netmask": network}, ]}
400         elif isinstance(network, int) and (0 < network < 33):
401             address = {"address": [{"ip": ip_addr, "prefix-length": network}, ]}
402         else:
403             raise HoneycombError("Value {0} is not a valid netmask or network "
404                                  "prefix length.".format(network))
405         return InterfaceKeywords._set_interface_properties(
406             node, interface, path, address)
407
408     @staticmethod
409     def remove_all_ipv4_addresses(node, interface):
410         """Remove all IPv4 addresses from interface.
411
412         :param node: Honeycomb node.
413         :param interface: The name of interface.
414         :type node: dict
415         :type interface: str
416         :return: Content of response.
417         :rtype: bytearray
418         """
419
420         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4",
421                 "address")
422         return InterfaceKeywords._set_interface_properties(
423             node, interface, path, None)
424
425     @staticmethod
426     def add_first_ipv4_neighbor(node, interface, ip_addr, link_layer_address):
427         """Add the first IPv4 neighbour.
428
429         If there are any other neighbours configured, they will be removed.
430
431         :param node: Honeycomb node.
432         :param interface: The name of interface.
433         :param ip_addr: IPv4 address of neighbour to be set.
434         :param link_layer_address: Link layer address.
435         :type node: dict
436         :type interface: str
437         :type ip_addr: str
438         :type link_layer_address: str
439         :return: Content of response.
440         :rtype: bytearray
441         """
442
443         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4")
444         neighbor = {"neighbor": [{"ip": ip_addr,
445                                   "link-layer-address": link_layer_address}, ]}
446         return InterfaceKeywords._set_interface_properties(
447             node, interface, path, neighbor)
448
449     @staticmethod
450     def add_ipv4_neighbor(node, interface, ip_addr, link_layer_address):
451         """Add the IPv4 neighbour.
452
453         :param node: Honeycomb node.
454         :param interface: The name of interface.
455         :param ip_addr: IPv4 address of neighbour to be set.
456         :param link_layer_address: Link layer address.
457         :type node: dict
458         :type interface: str
459         :type ip_addr: str
460         :type link_layer_address: str
461         :return: Content of response.
462         :rtype: bytearray
463         """
464
465         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4",
466                 "neighbor")
467         neighbor = [{"ip": ip_addr, "link-layer-address": link_layer_address}, ]
468         return InterfaceKeywords._set_interface_properties(
469             node, interface, path, neighbor)
470
471     @staticmethod
472     def remove_all_ipv4_neighbors(node, interface):
473         """Remove all IPv4 neighbours.
474
475         :param node: Honeycomb node.
476         :param interface: The name of interface.
477         :type node: dict
478         :type interface: str
479         :return: Content of response.
480         :rtype: bytearray
481         """
482
483         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4",
484                 "neighbor")
485         return InterfaceKeywords._set_interface_properties(
486             node, interface, path, None)
487
488     @staticmethod
489     def configure_interface_ipv6(node, interface, param, value):
490         """Configure IPv6 parameters of interface
491
492         :param node: Honeycomb node.
493         :param interface: The name of interface.
494         :param param: Parameter to configure (set, change, remove)
495         :param value: The value of parameter. If None, the parameter will be
496         removed.
497         :type node: dict
498         :type interface: str
499         :type param: str
500         :type value: str
501         :return: Content of response.
502         :rtype: bytearray
503         :raises HoneycombError: If the parameter is not valid.
504         """
505
506         if param in InterfaceKeywords.IPV6_PARAMS:
507             path = ("interfaces", ("interface", "name", interface),
508                     "ietf-ip:ipv6", param)
509         elif param in InterfaceKeywords.IPV6_AUTOCONF_PARAMS:
510             path = ("interfaces", ("interface", "name", interface),
511                     "ietf-ip:ipv6", "autoconf", param)
512         else:
513             raise HoneycombError("The parameter {0} is invalid.".format(param))
514
515         return InterfaceKeywords._set_interface_properties(
516             node, interface, path, value)
517
518     @staticmethod
519     def add_first_ipv6_address(node, interface, ip_addr, prefix_len):
520         """Add the first IPv6 address.
521
522         If there are any other addresses configured, they will be removed.
523
524         :param node: Honeycomb node.
525         :param interface: The name of interface.
526         :param ip_addr: IPv6 address to be set.
527         :param prefix_len: Prefix length.
528         :type node: dict
529         :type interface: str
530         :type ip_addr: str
531         :type prefix_len: str
532         :return: Content of response.
533         :rtype: bytearray
534         """
535
536         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv6")
537         address = {"address": [{"ip": ip_addr, "prefix-length": prefix_len}, ]}
538         return InterfaceKeywords._set_interface_properties(
539             node, interface, path, address)
540
541     @staticmethod
542     def add_ipv6_address(node, interface, ip_addr, prefix_len):
543         """Add IPv6 address.
544
545         :param node: Honeycomb node.
546         :param interface: The name of interface.
547         :param ip_addr: IPv6 address to be set.
548         :param prefix_len: Prefix length.
549         :type node: dict
550         :type interface: str
551         :type ip_addr: str
552         :type prefix_len: str
553         :return: Content of response.
554         :rtype: bytearray
555         """
556
557         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv6",
558                 "address")
559         address = [{"ip": ip_addr, "prefix-length": prefix_len}, ]
560         return InterfaceKeywords._set_interface_properties(
561             node, interface, path, address)
562
563     @staticmethod
564     def remove_all_ipv6_addresses(node, interface):
565         """Remove all IPv6 addresses from interface.
566
567         :param node: Honeycomb node.
568         :param interface: The name of interface.
569         :type node: dict
570         :type interface: str
571         :return: Content of response.
572         :rtype: bytearray
573         """
574
575         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv6",
576                 "address")
577         return InterfaceKeywords._set_interface_properties(
578             node, interface, path, None)
579
580     @staticmethod
581     def add_first_ipv6_neighbor(node, interface, ip_addr, link_layer_address):
582         """Add the first IPv6 neighbour.
583
584         If there are any other neighbours configured, they will be removed.
585
586         :param node: Honeycomb node.
587         :param interface: The name of interface.
588         :param ip_addr: IPv6 address of neighbour to be set.
589         :param link_layer_address: Link layer address.
590         :type node: dict
591         :type interface: str
592         :type ip_addr: str
593         :type link_layer_address: str
594         :return: Content of response.
595         :rtype: bytearray
596         """
597
598         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv6")
599         neighbor = {"neighbor": [{"ip": ip_addr,
600                                   "link-layer-address": link_layer_address}, ]}
601         return InterfaceKeywords._set_interface_properties(
602             node, interface, path, neighbor)
603
604     @staticmethod
605     def add_ipv6_neighbor(node, interface, ip_addr, link_layer_address):
606         """Add the IPv6 neighbour.
607
608         :param node: Honeycomb node.
609         :param interface: The name of interface.
610         :param ip_addr: IPv6 address of neighbour to be set.
611         :param link_layer_address: Link layer address.
612         :type node: dict
613         :type interface: str
614         :type ip_addr: str
615         :type link_layer_address: str
616         :return: Content of response.
617         :rtype: bytearray
618         """
619
620         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv6",
621                 "neighbor")
622         neighbor = [{"ip": ip_addr, "link-layer-address": link_layer_address}, ]
623         return InterfaceKeywords._set_interface_properties(
624             node, interface, path, neighbor)
625
626     @staticmethod
627     def remove_all_ipv6_neighbors(node, interface):
628         """Remove all IPv6 neighbours.
629
630         :param node: Honeycomb node.
631         :param interface: The name of interface.
632         :type node: dict
633         :type interface: str
634         :return: Content of response.
635         :rtype: bytearray
636         """
637
638         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv6",
639                 "neighbor")
640         return InterfaceKeywords._set_interface_properties(
641             node, interface, path, None)
642
643     @staticmethod
644     def configure_interface_ethernet(node, interface, param, value):
645         """Configure the ethernet parameters of interface.
646
647         :param node: Honeycomb node.
648         :param interface: The name of interface.
649         :param param: Parameter to configure (set, change, remove)
650         :param value: The value of parameter. If None, the parameter will be
651         removed.
652         :type node: dict
653         :type interface: str
654         :type param: str
655         :type value: str
656         :return: Content of response.
657         :rtype: bytearray
658         :raises HoneycombError: If the parameter is not valid.
659         """
660
661         if param not in InterfaceKeywords.ETH_PARAMS:
662             raise HoneycombError("The parameter {0} is invalid.".format(param))
663         path = ("interfaces", ("interface", "name", interface), "v3po:ethernet",
664                 param)
665         return InterfaceKeywords._set_interface_properties(
666             node, interface, path, value)
667
668     @staticmethod
669     def configure_interface_routing(node, interface, param, value):
670         """Configure the routing parameters of interface.
671
672         :param node: Honeycomb node.
673         :param interface: The name of interface.
674         :param param: Parameter to configure (set, change, remove)
675         :param value: The value of parameter. If None, the parameter will be
676         removed.
677         :type node: dict
678         :type interface: str
679         :type param: str
680         :type value: str
681         :return: Content of response.
682         :rtype: bytearray
683         :raises HoneycombError: If the parameter is not valid.
684         """
685
686         if param not in InterfaceKeywords.ROUTING_PARAMS:
687             raise HoneycombError("The parameter {0} is invalid.".format(param))
688
689         path = ("interfaces", ("interface", "name", interface), "v3po:routing",
690                 param)
691         return InterfaceKeywords._set_interface_properties(
692             node, interface, path, value)
693
694     @staticmethod
695     def create_vxlan_interface(node, interface, **kwargs):
696         """Create a new VxLAN interface.
697
698         :param node: Honeycomb node.
699         :param interface: The name of interface.
700         :param kwargs: Parameters and their values. The accepted parameters are
701         defined in InterfaceKeywords.VXLAN_PARAMS.
702         :type node: dict
703         :type interface: str
704         :type kwargs: dict
705         :return: Content of response.
706         :rtype: bytearray
707         :raises HoneycombError: If the parameter is not valid.
708         """
709
710         new_vx_lan = {
711             "name": interface,
712             "type": "v3po:vxlan-tunnel",
713             "v3po:vxlan": {}
714         }
715         for param, value in kwargs.items():
716             if param not in InterfaceKeywords.VXLAN_PARAMS:
717                 raise HoneycombError("The parameter {0} is invalid.".
718                                      format(param))
719             new_vx_lan["v3po:vxlan"][param] = value
720
721         path = ("interfaces", "interface")
722         vx_lan_structure = [new_vx_lan, ]
723         return InterfaceKeywords._set_interface_properties(
724             node, interface, path, vx_lan_structure)
725
726     @staticmethod
727     def delete_interface(node, interface):
728         """Delete an interface.
729
730         :param node: Honeycomb node.
731         :param interface: The name of interface.
732         :type node: dict
733         :type interface: str
734         :return: Content of response.
735         :rtype: bytearray
736         :raises HoneycombError: If it is not possible to get information about
737         interfaces or it is not possible to delete the interface.
738         """
739
740         path = ("interfaces", ("interface", "name", interface))
741
742         status_code, resp = HcUtil.\
743             get_honeycomb_data(node, "config_vpp_interfaces")
744         if status_code != HTTPCodes.OK:
745             raise HoneycombError(
746                 "Not possible to get configuration information about the "
747                 "interfaces. Status code: {0}.".format(status_code))
748
749         new_data = HcUtil.remove_item(resp, path)
750         status_code, resp = HcUtil.\
751             put_honeycomb_data(node, "config_vpp_interfaces", new_data)
752         if status_code != HTTPCodes.OK:
753             raise HoneycombError("Not possible to remove interface {0}. "
754                                  "Status code: {1}.".
755                                  format(interface, status_code))
756         return resp
757
758     @staticmethod
759     def configure_interface_vxlan(node, interface, **kwargs):
760         """Configure VxLAN on the interface.
761
762         The keyword configures VxLAN parameters on the given interface. The type
763         of interface must be set to "v3po:vxlan-tunnel".
764         The new VxLAN parameters overwrite the current configuration. If a
765         parameter in new configuration is missing, it is removed from VxLAN
766         configuration.
767         If the dictionary kwargs is empty, VxLAN configuration is removed.
768
769         :param node: Honeycomb node.
770         :param interface: The name of interface.
771         :param kwargs: Parameters and their values. The accepted parameters are
772         defined in InterfaceKeywords.VXLAN_PARAMS.
773         :type node: dict
774         :type interface: str
775         :type kwargs: dict
776         :return: Content of response.
777         :rtype: bytearray
778         :raises HoneycombError: If the parameter is not valid.
779         """
780
781         vx_lan_structure = dict()
782         for param, value in kwargs.items():
783             if param not in InterfaceKeywords.VXLAN_PARAMS:
784                 raise HoneycombError("The parameter {0} is invalid.".
785                                      format(param))
786             vx_lan_structure[param] = value
787
788         path = ("interfaces", ("interface", "name", interface), "v3po:vxlan")
789         return InterfaceKeywords._set_interface_properties(
790             node, interface, path, vx_lan_structure)
791
792     @staticmethod
793     def configure_interface_l2(node, interface, param, value):
794         """Configure the L2 parameters of interface.
795
796         :param node: Honeycomb node.
797         :param interface: The name of interface.
798         :param param: Parameter to configure (set, change, remove)
799         :param value: The value of parameter. If None, the parameter will be
800         removed.
801         :type node: dict
802         :type interface: str
803         :type param: str
804         :type value: str
805         :return: Content of response.
806         :rtype: bytearray
807         :raises HoneycombError: If the parameter is not valid.
808         """
809
810         if param not in InterfaceKeywords.L2_PARAMS:
811             raise HoneycombError("The parameter {0} is invalid.".format(param))
812         path = ("interfaces", ("interface", "name", interface), "v3po:l2",
813                 param)
814         return InterfaceKeywords._set_interface_properties(
815             node, interface, path, value)
816
817     @staticmethod
818     def create_tap_interface(node, interface, **kwargs):
819         """Create a new TAP interface.
820
821         :param node: Honeycomb node.
822         :param interface: The name of interface.
823         :param kwargs: Parameters and their values. The accepted parameters are
824         defined in InterfaceKeywords.TAP_PARAMS.
825         :type node: dict
826         :type interface: str
827         :type kwargs: dict
828         :return: Content of response.
829         :rtype: bytearray
830         :raises HoneycombError: If the parameter is not valid.
831         """
832
833         new_tap = {
834             "name": interface,
835             "type": "v3po:tap",
836             "v3po:tap": {}
837         }
838         for param, value in kwargs.items():
839             if param not in InterfaceKeywords.TAP_PARAMS:
840                 raise HoneycombError("The parameter {0} is invalid.".
841                                      format(param))
842             new_tap["v3po:tap"][param] = value
843
844         path = ("interfaces", "interface")
845         new_tap_structure = [new_tap, ]
846         return InterfaceKeywords._set_interface_properties(
847             node, interface, path, new_tap_structure)
848
849     @staticmethod
850     def configure_interface_tap(node, interface, **kwargs):
851         """Configure TAP on the interface.
852
853         The keyword configures TAP parameters on the given interface. The type
854         of interface must be set to "v3po:tap".
855         The new TAP parameters overwrite the current configuration. If a
856         parameter in new configuration is missing, it is removed from TAP
857         configuration.
858         If the dictionary kwargs is empty, TAP configuration is removed.
859
860         :param node: Honeycomb node.
861         :param interface: The name of interface.
862         :param kwargs: Parameters and their values. The accepted parameters are
863         defined in InterfaceKeywords.TAP_PARAMS.
864         :type node: dict
865         :type interface: str
866         :type kwargs: dict
867         :return: Content of response.
868         :rtype: bytearray
869         :raises HoneycombError: If the parameter is not valid.
870         """
871
872         tap_structure = dict()
873         for param, value in kwargs.items():
874             if param not in InterfaceKeywords.TAP_PARAMS:
875                 raise HoneycombError("The parameter {0} is invalid.".
876                                      format(param))
877             tap_structure[param] = value
878
879         path = ("interfaces", ("interface", "name", interface), "v3po:tap")
880         return InterfaceKeywords._set_interface_properties(
881             node, interface, path, tap_structure)
882
883     @staticmethod
884     def configure_interface_vhost_user(node, interface, **kwargs):
885         """Configure vhost-user on the interface.
886
887         The keyword configures vhost-user parameters on the given interface.
888         The type of interface must be set to "v3po:vhost-user".
889         The new vhost-user parameters overwrite the current configuration. If a
890         parameter in new configuration is missing, it is removed from vhost-user
891         configuration.
892         If the dictionary kwargs is empty, vhost-user configuration is removed.
893
894         :param node: Honeycomb node.
895         :param interface: The name of interface.
896         :param kwargs: Parameters and their values. The accepted parameters are
897         defined in InterfaceKeywords.VHOST_USER_PARAMS.
898         :type node: dict
899         :type interface: str
900         :type kwargs: dict
901         :return: Content of response.
902         :rtype: bytearray
903         :raises HoneycombError: If the parameter is not valid.
904         """
905
906         vhost_structure = dict()
907         for param, value in kwargs.items():
908             if param not in InterfaceKeywords.VHOST_USER_PARAMS:
909                 raise HoneycombError("The parameter {0} is invalid.".
910                                      format(param))
911             vhost_structure[param] = value
912
913         path = ("interfaces", ("interface", "name", interface),
914                 "v3po:vhost-user")
915         return InterfaceKeywords._set_interface_properties(
916             node, interface, path, vhost_structure)
917
918     @staticmethod
919     def create_vhost_user_interface(node, interface, **kwargs):
920         """Create a new vhost-user interface.
921
922         :param node: Honeycomb node.
923         :param interface: The name of interface.
924         :param kwargs: Parameters and their values. The accepted parameters are
925         defined in InterfaceKeywords.VHOST_USER_PARAMS.
926         :type node: dict
927         :type interface: str
928         :type kwargs: dict
929         :return: Content of response.
930         :rtype: bytearray
931         :raises HoneycombError: If the parameter is not valid.
932         """
933
934         new_vhost = {
935             "name": interface,
936             "type": "v3po:vhost-user",
937             "v3po:vhost-user": {}
938         }
939         for param, value in kwargs.items():
940             if param not in InterfaceKeywords.VHOST_USER_PARAMS:
941                 raise HoneycombError("The parameter {0} is invalid.".
942                                      format(param))
943             new_vhost["v3po:vhost-user"][param] = value
944
945         path = ("interfaces", "interface")
946         new_vhost_structure = [new_vhost, ]
947         return InterfaceKeywords._set_interface_properties(
948             node, interface, path, new_vhost_structure)
949
950     @staticmethod
951     def create_sub_interface(node, super_interface, identifier, **kwargs):
952         """Create a new sub-interface.
953
954         :param node: Honeycomb node.
955         :param super_interface: The name of super interface.
956         :param identifier: sub-interface identifier.
957         :param kwargs: Parameters and their values. The accepted parameters are
958         defined in InterfaceKeywords.SUB_INTF_PARAMS.
959         :type node: dict
960         :type super_interface: str
961         :type identifier: int
962         :type kwargs: dict
963         :return: Content of response.
964         :rtype: bytearray
965         :raises HoneycombError: If the parameter is not valid.
966         """
967
968         # These parameters are empty types (in JSON represented as empty
969         # dictionary) but ODL internally represents them as Booleans. If the
970         # value is an empty dictionary, it is True, if the parameter is
971         # missing, it is False.
972         empty_types = ("match-any-outer-id",
973                        "match-any-inner-id",
974                        "exact-match",
975                        "default-subif")
976
977         sub_interface_name = "{0}.{1}".format(super_interface, str(identifier))
978         new_sub_interface = {
979             "name": sub_interface_name,
980             "type": "v3po:sub-interface",
981             "enabled": "false",
982             "sub-interface": {
983                 "super-interface": super_interface,
984                 "identifier": identifier
985             }
986         }
987         for param, value in kwargs.items():
988             if param in InterfaceKeywords.INTF_PARAMS:
989                 new_sub_interface[param] = value
990             elif param in InterfaceKeywords.SUB_INTF_PARAMS:
991                 if param in empty_types:
992                     if value:
993                         new_sub_interface["sub-interface"][param] = dict()
994                 else:
995                     new_sub_interface["sub-interface"][param] = value
996             else:
997                 raise HoneycombError("The parameter {0} is invalid.".
998                                      format(param))
999
1000         path = ("interfaces", "interface")
1001         new_sub_interface_structure = [new_sub_interface, ]
1002         return InterfaceKeywords._set_interface_properties(
1003             node, sub_interface_name, path, new_sub_interface_structure)
1004
1005     @staticmethod
1006     def add_vlan_tag_rewrite_to_sub_interface(node, sub_interface, **kwargs):
1007         """Add vlan tag rewrite to a sub-interface.
1008
1009         :param node: Honeycomb node.
1010         :param sub_interface: The name of sub-interface.
1011         :param kwargs: Parameters and their values. The accepted parameters are
1012         defined in InterfaceKeywords.L2_REWRITE_TAG_PARAMS.
1013         :type node: dict
1014         :type sub_interface: str
1015         :type kwargs: dict
1016         :return: Content of response.
1017         :rtype: bytearray
1018         :raises HoneycombError: If the parameter is not valid.
1019         """
1020
1021         new_rewrite = dict()
1022         for param, value in kwargs.items():
1023             if param in InterfaceKeywords.L2_REWRITE_TAG_PARAMS:
1024                 new_rewrite[param] = value
1025             else:
1026                 raise HoneycombError("The parameter {0} is invalid.".
1027                                      format(param))
1028
1029         path = ("interfaces", ("interface", "name", sub_interface), "v3po:l2",
1030                 "vlan-tag-rewrite")
1031         return InterfaceKeywords._set_interface_properties(
1032             node, sub_interface, path, new_rewrite)
1033
1034     @staticmethod
1035     def remove_vlan_tag_rewrite_from_sub_interface(node, sub_interface):
1036         """Remove vlan tag rewrite from a sub-interface.
1037
1038         :param node: Honeycomb node.
1039         :param sub_interface: The name of sub-interface.
1040         :type node: dict
1041         :type sub_interface: str
1042         :rtype: bytearray
1043         :raises HoneycombError: If the parameter is not valid.
1044         """
1045
1046         path = ("interfaces", ("interface", "name", sub_interface), "v3po:l2",
1047                 "vlan-tag-rewrite")
1048         return InterfaceKeywords._set_interface_properties(
1049             node, sub_interface, path, None)