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