Avoid overlapping subnets in class NetworksTestJSON

When allow_overlapping_ips is False, some of the tests in
"NetworksTestJSON" class are failing.

This class creates a subnet 10.100.0.0/28 as part of resource_setup.
This subnet is used in test_(list/show) tests. Other kind of tests
in this class (i.e tests_create_) first try with 10.100.0.0/28 subnet,
it fails, then they try next subnet block i.e 10.100.0.16/28 but
this also fails as the parameters(ex, gateway, allocation_pools) with
which the tests try to create still belongs to 10.100.0.0/28.
Having overlapping ips in the same tenant is bad practice even when
allow_overlapping_ips is enabled.

So create the subnet for the class from the last subnet block
(10.100.255.240/28),so that the subnet in both the kind of tests won't
overlap. Also delete the subnets in "test_create_delete_ " tests
if they are not deleting, so that it won't fail when next test tries
to create same subnet.

Closes-bug: #1393564
Change-Id: I6861c7cf09a9a5e8765d78fe7913cfe9b49233ab
diff --git a/tempest/api/network/test_networks.py b/tempest/api/network/test_networks.py
index 1f827da..e70519e 100644
--- a/tempest/api/network/test_networks.py
+++ b/tempest/api/network/test_networks.py
@@ -66,7 +66,8 @@
         super(NetworksTestJSON, cls).resource_setup()
         cls.network = cls.create_network()
         cls.name = cls.network['name']
-        cls.subnet = cls.create_subnet(cls.network)
+        cls.subnet = cls._create_subnet_with_last_subnet_block(cls.network,
+                                                               cls._ip_version)
         cls.cidr = cls.subnet['cidr']
         cls._subnet_data = {6: {'gateway':
                                 str(cls._get_gateway_from_tempest_conf(6)),
@@ -96,6 +97,23 @@
                                 'new_dns_nameservers': ['7.8.8.8', '7.8.4.4']}}
 
     @classmethod
+    def _create_subnet_with_last_subnet_block(cls, network, ip_version):
+        """Derive last subnet CIDR block from tenant CIDR and
+           create the subnet with that derived CIDR
+        """
+        if ip_version == 4:
+            cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)
+            mask_bits = CONF.network.tenant_network_mask_bits
+        elif ip_version == 6:
+            cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)
+            mask_bits = CONF.network.tenant_network_v6_mask_bits
+
+        subnet_cidr = list(cidr.subnet(mask_bits))[-1]
+        gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)
+        return cls.create_subnet(network, gateway=gateway_ip,
+                                 cidr=subnet_cidr, mask_bits=mask_bits)
+
+    @classmethod
     def _get_gateway_from_tempest_conf(cls, ip_version):
         """Return first subnet gateway for configured CIDR """
         if ip_version == 4:
@@ -129,6 +147,15 @@
         self.assertThat(actual, custom_matchers.MatchesDictExceptForKeys(
                         expected, exclude_keys))
 
+    def _delete_network(self, network):
+        # Deleting network also deletes its subnets if exists
+        self.client.delete_network(network['id'])
+        if network in self.networks:
+            self.networks.remove(network)
+        for subnet in self.subnets:
+            if subnet['network_id'] == network['id']:
+                self.subnets.remove(subnet)
+
     def _create_verify_delete_subnet(self, cidr=None, mask_bits=None,
                                      **kwargs):
         network = self.create_network()
@@ -156,6 +183,7 @@
         # Create a network
         name = data_utils.rand_name('network-')
         network = self.create_network(network_name=name)
+        self.addCleanup(self._delete_network, network)
         net_id = network['id']
         self.assertEqual('ACTIVE', network['status'])
         # Verify network update
@@ -312,6 +340,7 @@
     @test.attr(type='smoke')
     def test_update_subnet_gw_dns_host_routes_dhcp(self):
         network = self.create_network()
+        self.addCleanup(self._delete_network, network)
 
         subnet = self.create_subnet(
             network, **self.subnet_dict(['gateway', 'host_routes',