Merge "After tempest network resource do not be cleared"
diff --git a/manila_tempest_tests/tests/api/base.py b/manila_tempest_tests/tests/api/base.py
index b34c92a..12a986b 100644
--- a/manila_tempest_tests/tests/api/base.py
+++ b/manila_tempest_tests/tests/api/base.py
@@ -26,6 +26,7 @@
 from tempest import config
 from tempest.lib.common import dynamic_creds
 from tempest.lib.common.utils import data_utils
+from tempest.lib.common.utils import test_utils
 from tempest.lib import exceptions
 from tempest import test
 
@@ -126,6 +127,9 @@
     # Will be cleaned up in resource_cleanup
     class_resources = []
 
+    # Will be cleaned up in clear_net_resources
+    class_net_resources = []
+
     # Will be cleaned up in tearDown method
     method_resources = []
 
@@ -304,12 +308,14 @@
         super(BaseSharesTest, self).setUp()
         self.addCleanup(self.clear_isolated_creds)
         self.addCleanup(self.clear_resources)
+        self.addCleanup(self.clear_net_resources)
         verify_test_has_appropriate_tags(self)
 
     @classmethod
     def resource_cleanup(cls):
         cls.clear_resources(cls.class_resources)
         cls.clear_isolated_creds(cls.class_isolated_creds)
+        cls.clear_net_resources(cls.class_net_resources)
         super(BaseSharesTest, cls).resource_cleanup()
 
     @classmethod
@@ -387,6 +393,24 @@
                         network, subnet, router = net_data
                         net_id = network["id"]
                         subnet_id = subnet["id"]
+                        network_res = {
+                            "type": "network",
+                            "resource": network,
+                            "client": ic,
+                        }
+                        subnet_res = {
+                            "type": "subnet",
+                            "resource": subnet,
+                            "client": ic,
+                        }
+                        router_res = {
+                            "type": "router",
+                            "resource": router,
+                            "client": ic,
+                        }
+                        cls.class_net_resources.insert(0, network_res)
+                        cls.class_net_resources.insert(0, subnet_res)
+                        cls.class_net_resources.insert(0, router_res)
 
                     # Try get suitable share-network
                     share_networks = sc.list_share_networks_with_detail()
@@ -416,6 +440,58 @@
         return share_network_id
 
     @classmethod
+    def clear_net_resources(cls, resources=None):
+        if resources is None:
+            resources = cls.class_net_resources
+        for res in resources:
+            if "deleted" not in res.keys():
+                res["deleted"] = False
+            if not (res["deleted"]):
+                if res["type"] is "router":
+                    cls.clear_router(res['client'], res['resource'])
+                elif res["type"] is "subnet":
+                    cls.clear_subnet(res['client'], res['resource'])
+                elif res["type"] is "network":
+                    cls.clear_network(res['client'], res['resource'])
+                else:
+                    LOG.warning("Provided unsupported resource type for "
+                                "cleanup '%s'. Skipping." % res["type"])
+            res["deleted"] = True
+
+    @classmethod
+    def clear_router(cls, ic, router):
+        body = ic.ports_admin_client.list_ports(device_id=router['id'])
+        interfaces = body['ports']
+        for i in interfaces:
+            test_utils.call_and_ignore_notfound_exc(
+                ic.routers_admin_client.remove_router_interface, router['id'],
+                subnet_id=i['fixed_ips'][0]['subnet_id'])
+
+        try:
+            ic.routers_admin_client.delete_router(router['id'])
+        except exceptions.NotFound:
+            LOG.warning('router with name: %s not found for delete' %
+                        router['name'])
+
+    @classmethod
+    def clear_subnet(cls, ic, subnet):
+        client = ic.subnets_admin_client
+        try:
+            client.delete_subnet(subnet['id'])
+        except exceptions.NotFound:
+            LOG.warning('subnet with name: %s not found for delete' %
+                        subnet['name'])
+
+    @classmethod
+    def clear_network(cls, ic, network):
+        net_client = ic.networks_admin_client
+        try:
+            net_client.delete_network(network['id'])
+        except exceptions.NotFound:
+            LOG.warning('network with name: %s not found for delete' %
+                        network['name'])
+
+    @classmethod
     def _create_share(cls, share_protocol=None, size=None, name=None,
                       snapshot_id=None, description=None, metadata=None,
                       share_network_id=None, share_type_id=None,