Add keyword to manipulate vhost-user parameters
[csit.git] / resources / libraries / python / honeycomb / HcAPIKwInterfaces.py
index 8f6819e..04800a2 100644 (file)
@@ -45,6 +45,8 @@ class InterfaceKeywords(object):
     VXLAN_PARAMS = ("src", "dst", "vni", "encap-vrf-id")
     L2_PARAMS = ("bridge-domain", "split-horizon-group",
                  "bridged-virtual-interface")
+    TAP_PARAMS = ("tap-name", "mac", "device-instance")
+    VHOST_USER_PARAMS = ("socket", "role")
 
     def __init__(self):
         pass
@@ -331,7 +333,7 @@ class InterfaceKeywords(object):
             node, interface, path, value)
 
     @staticmethod
-    def add_first_ipv4_address(node, interface, ip_addr, netmask):
+    def add_first_ipv4_address(node, interface, ip_addr, network):
         """Add the first IPv4 address.
 
         If there are any other addresses configured, they will be removed.
@@ -339,39 +341,51 @@ class InterfaceKeywords(object):
         :param node: Honeycomb node.
         :param interface: The name of interface.
         :param ip_addr: IPv4 address to be set.
-        :param netmask: Netmask.
+        :param network: Netmask or length of network prefix.
         :type node: dict
         :type interface: str
         :type ip_addr: str
-        :type netmask: str
+        :type network: str or int
         :return: Content of response.
         :rtype: bytearray
         """
 
         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4")
-        address = {"address": [{"ip": ip_addr, "netmask": netmask}, ]}
+        if isinstance(network, basestring):
+            address = {"address": [{"ip": ip_addr, "netmask": network}, ]}
+        elif isinstance(network, int) and (0 < network < 33):
+            address = {"address": [{"ip": ip_addr, "prefix-length": network}, ]}
+        else:
+            raise HoneycombError("Value {0} is not a valid netmask or network "
+                                 "prefix length.".format(network))
         return InterfaceKeywords._set_interface_properties(
             node, interface, path, address)
 
     @staticmethod
-    def add_ipv4_address(node, interface, ip_addr, netmask):
+    def add_ipv4_address(node, interface, ip_addr, network):
         """Add IPv4 address.
 
         :param node: Honeycomb node.
         :param interface: The name of interface.
         :param ip_addr: IPv4 address to be set.
-        :param netmask: Netmask.
+        :param network: Netmask or length of network prefix.
         :type node: dict
         :type interface: str
         :type ip_addr: str
-        :type netmask: str
+        :type network: str or int
         :return: Content of response.
         :rtype: bytearray
         """
 
         path = ("interfaces", ("interface", "name", interface), "ietf-ip:ipv4",
                 "address")
-        address = [{"ip": ip_addr, "prefix-length": netmask}, ]
+        if isinstance(network, basestring):
+            address = {"address": [{"ip": ip_addr, "netmask": network}, ]}
+        elif isinstance(network, int) and (0 < network < 33):
+            address = {"address": [{"ip": ip_addr, "prefix-length": network}, ]}
+        else:
+            raise HoneycombError("Value {0} is not a valid netmask or network "
+                                 "prefix length.".format(network))
         return InterfaceKeywords._set_interface_properties(
             node, interface, path, address)
 
@@ -662,30 +676,102 @@ class InterfaceKeywords(object):
             node, interface, path, value)
 
     @staticmethod
-    def configure_interface_vxlan(node, interface, param, value):
-        """Configure the VxLAN parameters of interface.
+    def create_vxlan_interface(node, interface, **kwargs):
+        """Create a new VxLAN interface.
 
         :param node: Honeycomb node.
         :param interface: The name of interface.
-        :param param: Parameter to configure (set, change, remove)
-        :param value: The value of parameter. If None, the parameter will be
-        removed.
+        :param kwargs: Parameters and their values. The accepted parameters are
+        defined in InterfaceKeywords.VXLAN_PARAMS.
         :type node: dict
         :type interface: str
-        :type param: str
-        :type value: str
+        :type kwargs: dict
         :return: Content of response.
         :rtype: bytearray
         :raises HoneycombError: If the parameter is not valid.
         """
 
-        if param not in InterfaceKeywords.VXLAN_PARAMS:
-            raise HoneycombError("The parameter {0} is invalid.".format(param))
+        new_vx_lan = {
+            "name": interface,
+            "type": "v3po:vxlan-tunnel",
+            "v3po:vxlan": {}
+        }
+        for param, value in kwargs.items():
+            if param not in InterfaceKeywords.VXLAN_PARAMS:
+                raise HoneycombError("The parameter {0} is invalid.".
+                                     format(param))
+            new_vx_lan["v3po:vxlan"][param] = value
+
+        path = ("interfaces", "interface")
+        vx_lan_structure = [new_vx_lan, ]
+        return InterfaceKeywords._set_interface_properties(
+            node, interface, path, vx_lan_structure)
 
-        path = ("interfaces", ("interface", "name", interface), "v3po:vxlan",
-                param)
+    @staticmethod
+    def delete_interface(node, interface):
+        """Delete an interface.
+
+        :param node: Honeycomb node.
+        :param interface: The name of interface.
+        :type node: dict
+        :type interface: str
+        :return: Content of response.
+        :rtype: bytearray
+        :raises HoneycombError: If it is not possible to get information about
+        interfaces or it is not possible to delete the interface.
+        """
+
+        path = ("interfaces", ("interface", "name", interface))
+
+        status_code, resp = HcUtil.\
+            get_honeycomb_data(node, "config_vpp_interfaces")
+        if status_code != HTTPCodes.OK:
+            raise HoneycombError(
+                "Not possible to get configuration information about the "
+                "interfaces. Status code: {0}.".format(status_code))
+
+        new_data = HcUtil.remove_item(resp, path)
+        status_code, resp = HcUtil.\
+            put_honeycomb_data(node, "config_vpp_interfaces", new_data)
+        if status_code != HTTPCodes.OK:
+            raise HoneycombError("Not possible to remove interface {0}. "
+                                 "Status code: {1}.".
+                                 format(interface, status_code))
+        return resp
+
+    @staticmethod
+    def configure_interface_vxlan(node, interface, **kwargs):
+        """Configure VxLAN on the interface.
+
+        The keyword configures VxLAN parameters on the given interface. The type
+        of interface must be set to "v3po:vxlan-tunnel".
+        The new VxLAN parameters overwrite the current configuration. If a
+        parameter in new configuration is missing, it is removed from VxLAN
+        configuration.
+        If the dictionary kwargs is empty, VxLAN configuration is removed.
+
+        :param node: Honeycomb node.
+        :param interface: The name of interface.
+        :param kwargs: Parameters and their values. The accepted parameters are
+        defined in InterfaceKeywords.VXLAN_PARAMS.
+        :type node: dict
+        :type interface: str
+        :type kwargs: dict
+        :return: Content of response.
+        :rtype: bytearray
+        :raises HoneycombError: If the parameter is not valid.
+        """
+
+        vx_lan_structure = dict()
+        for param, value in kwargs.items():
+            if param not in InterfaceKeywords.VXLAN_PARAMS:
+                raise HoneycombError("The parameter {0} is invalid.".
+                                     format(param))
+            vx_lan_structure[param] = value
+
+        path = ("interfaces", ("interface", "name", interface), "v3po:vxlan")
         return InterfaceKeywords._set_interface_properties(
-            node, interface, path, value)
+            node, interface, path, vx_lan_structure)
 
     @staticmethod
     def configure_interface_l2(node, interface, param, value):
@@ -711,3 +797,136 @@ class InterfaceKeywords(object):
                 param)
         return InterfaceKeywords._set_interface_properties(
             node, interface, path, value)
+
+    @staticmethod
+    def create_tap_interface(node, interface, **kwargs):
+        """Create a new TAP interface.
+
+        :param node: Honeycomb node.
+        :param interface: The name of interface.
+        :param kwargs: Parameters and their values. The accepted parameters are
+        defined in InterfaceKeywords.TAP_PARAMS.
+        :type node: dict
+        :type interface: str
+        :type kwargs: dict
+        :return: Content of response.
+        :rtype: bytearray
+        :raises HoneycombError: If the parameter is not valid.
+        """
+
+        new_tap = {
+            "name": interface,
+            "type": "v3po:tap",
+            "v3po:tap": {}
+        }
+        for param, value in kwargs.items():
+            if param not in InterfaceKeywords.TAP_PARAMS:
+                raise HoneycombError("The parameter {0} is invalid.".
+                                     format(param))
+            new_tap["v3po:tap"][param] = value
+
+        path = ("interfaces", "interface")
+        new_tap_structure = [new_tap, ]
+        return InterfaceKeywords._set_interface_properties(
+            node, interface, path, new_tap_structure)
+
+    @staticmethod
+    def configure_interface_tap(node, interface, **kwargs):
+        """Configure TAP on the interface.
+
+        The keyword configures TAP parameters on the given interface. The type
+        of interface must be set to "v3po:tap".
+        The new TAP parameters overwrite the current configuration. If a
+        parameter in new configuration is missing, it is removed from TAP
+        configuration.
+        If the dictionary kwargs is empty, TAP configuration is removed.
+
+        :param node: Honeycomb node.
+        :param interface: The name of interface.
+        :param kwargs: Parameters and their values. The accepted parameters are
+        defined in InterfaceKeywords.TAP_PARAMS.
+        :type node: dict
+        :type interface: str
+        :type kwargs: dict
+        :return: Content of response.
+        :rtype: bytearray
+        :raises HoneycombError: If the parameter is not valid.
+        """
+
+        tap_structure = dict()
+        for param, value in kwargs.items():
+            if param not in InterfaceKeywords.TAP_PARAMS:
+                raise HoneycombError("The parameter {0} is invalid.".
+                                     format(param))
+            tap_structure[param] = value
+
+        path = ("interfaces", ("interface", "name", interface), "v3po:tap")
+        return InterfaceKeywords._set_interface_properties(
+            node, interface, path, tap_structure)
+
+    @staticmethod
+    def configure_interface_vhost_user(node, interface, **kwargs):
+        """Configure vhost-user on the interface.
+
+        The keyword configures vhost-user parameters on the given interface.
+        The type of interface must be set to "v3po:vhost-user".
+        The new vhost-user parameters overwrite the current configuration. If a
+        parameter in new configuration is missing, it is removed from vhost-user
+        configuration.
+        If the dictionary kwargs is empty, vhost-user configuration is removed.
+
+        :param node: Honeycomb node.
+        :param interface: The name of interface.
+        :param kwargs: Parameters and their values. The accepted parameters are
+        defined in InterfaceKeywords.VHOST_USER_PARAMS.
+        :type node: dict
+        :type interface: str
+        :type kwargs: dict
+        :return: Content of response.
+        :rtype: bytearray
+        :raises HoneycombError: If the parameter is not valid.
+        """
+
+        vhost_structure = dict()
+        for param, value in kwargs.items():
+            if param not in InterfaceKeywords.VHOST_USER_PARAMS:
+                raise HoneycombError("The parameter {0} is invalid.".
+                                     format(param))
+            vhost_structure[param] = value
+
+        path = ("interfaces", ("interface", "name", interface),
+                "v3po:vhost-user")
+        return InterfaceKeywords._set_interface_properties(
+            node, interface, path, vhost_structure)
+
+    @staticmethod
+    def create_vhost_user_interface(node, interface, **kwargs):
+        """Create a new vhost-user interface.
+
+        :param node: Honeycomb node.
+        :param interface: The name of interface.
+        :param kwargs: Parameters and their values. The accepted parameters are
+        defined in InterfaceKeywords.VHOST_USER_PARAMS.
+        :type node: dict
+        :type interface: str
+        :type kwargs: dict
+        :return: Content of response.
+        :rtype: bytearray
+        :raises HoneycombError: If the parameter is not valid.
+        """
+
+        new_vhost = {
+            "name": interface,
+            "type": "v3po:vhost-user",
+            "v3po:vhost-user": {}
+        }
+        for param, value in kwargs.items():
+            if param not in InterfaceKeywords.VHOST_USER_PARAMS:
+                raise HoneycombError("The parameter {0} is invalid.".
+                                     format(param))
+            new_vhost["v3po:vhost-user"][param] = value
+
+        path = ("interfaces", "interface")
+        new_vhost_structure = [new_vhost, ]
+        return InterfaceKeywords._set_interface_properties(
+            node, interface, path, new_vhost_structure)