Add create_trunk and delete_trunk methods.

Change-Id: I23f74551325a45147af054fe37b0f7e8b9bd3d5e
diff --git a/neutron_tempest_plugin/api/base.py b/neutron_tempest_plugin/api/base.py
index ae01d56..966b30d 100644
--- a/neutron_tempest_plugin/api/base.py
+++ b/neutron_tempest_plugin/api/base.py
@@ -128,10 +128,15 @@
         cls.log_objects = []
         cls.reserved_subnet_cidrs = set()
         cls.keypairs = []
+        cls.trunks = []
 
     @classmethod
     def resource_cleanup(cls):
         if CONF.service_available.neutron:
+            # Clean up trunks
+            for trunk in cls.trunks:
+                cls._try_delete_resource(cls.delete_trunk, trunk)
+
             # Clean up floating IPs
             for floating_ip in cls.floating_ips:
                 cls._try_delete_resource(cls.client.delete_floatingip,
@@ -680,6 +685,64 @@
                   cls.os_primary.keypairs_client)
         client.delete_keypair(keypair_name=keypair['name'])
 
+    @classmethod
+    def create_trunk(cls, port=None, subports=None, client=None, **kwargs):
+        """Create network trunk
+
+        :param port: dictionary containing parent port ID (port['id'])
+        :param client: client to be used for connecting to networking service
+        :param **kwargs: extra parameters to be forwarded to network service
+
+        :returns: dictionary containing created trunk details
+        """
+        client = client or cls.client
+
+        if port:
+            kwargs['port_id'] = port['id']
+
+        trunk = client.create_trunk(subports=subports, **kwargs)['trunk']
+        # Save client reference for later deletion
+        trunk['client'] = client
+        cls.trunks.append(trunk)
+        return trunk
+
+    @classmethod
+    def delete_trunk(cls, trunk, client=None):
+        """Delete network trunk
+
+        :param trunk: dictionary containing trunk ID (trunk['id'])
+
+        :param client: client to be used for connecting to networking service
+        """
+        client = client or trunk.get('client') or cls.client
+        trunk.update(client.show_trunk(trunk['id'])['trunk'])
+
+        if not trunk['admin_state_up']:
+            # Cannot touch trunk before admin_state_up is True
+            client.update_trunk(trunk['id'], admin_state_up=True)
+        if trunk['sub_ports']:
+            # Removes trunk ports before deleting it
+            cls._try_delete_resource(client.remove_subports, trunk['id'],
+                                     trunk['sub_ports'])
+
+        # we have to detach the interface from the server before
+        # the trunk can be deleted.
+        parent_port = {'id': trunk['port_id']}
+
+        def is_parent_port_detached():
+            parent_port.update(client.show_port(parent_port['id'])['port'])
+            return not parent_port['device_id']
+
+        if not is_parent_port_detached():
+            # this could probably happen when trunk is deleted and parent port
+            # has been assigned to a VM that is still running. Here we are
+            # assuming that device_id points to such VM.
+            cls.os_primary.compute.InterfacesClient().delete_interface(
+                parent_port['device_id'], parent_port['id'])
+            utils.wait_until_true(is_parent_port_detached)
+
+        client.delete_trunk(trunk['id'])
+
 
 class BaseAdminNetworkTest(BaseNetworkTest):
 
diff --git a/neutron_tempest_plugin/services/network/json/network_client.py b/neutron_tempest_plugin/services/network/json/network_client.py
index 930cbfd..b316ce4 100644
--- a/neutron_tempest_plugin/services/network/json/network_client.py
+++ b/neutron_tempest_plugin/services/network/json/network_client.py
@@ -745,26 +745,23 @@
         body = jsonutils.loads(body)
         return service_client.ResponseBody(resp, body)
 
-    def create_trunk(self, parent_port_id, subports,
+    def create_trunk(self, parent_port_id=None, subports=None,
                      tenant_id=None, name=None, admin_state_up=None,
-                     description=None):
+                     description=None, **kwargs):
         uri = '%s/trunks' % self.uri_prefix
-        post_data = {
-            'trunk': {
-                'port_id': parent_port_id,
-            }
-        }
+        if parent_port_id:
+            kwargs['port_id'] = parent_port_id
         if subports is not None:
-            post_data['trunk']['sub_ports'] = subports
+            kwargs['sub_ports'] = subports
         if tenant_id is not None:
-            post_data['trunk']['tenant_id'] = tenant_id
+            kwargs['tenant_id'] = tenant_id
         if name is not None:
-            post_data['trunk']['name'] = name
+            kwargs['name'] = name
         if description is not None:
-            post_data['trunk']['description'] = description
+            kwargs['description'] = description
         if admin_state_up is not None:
-            post_data['trunk']['admin_state_up'] = admin_state_up
-        resp, body = self.post(uri, self.serialize(post_data))
+            kwargs['admin_state_up'] = admin_state_up
+        resp, body = self.post(uri, self.serialize({'trunk': kwargs}))
         body = self.deserialize_single(body)
         self.expected_success(201, resp.status)
         return service_client.ResponseBody(resp, body)