Add timeout_409 parameter (tempest configuration)

We still face the issue with deletion of SG in case of Tungsten
Fabric on slow envs. To resolve this issue and achieve control
and flexibility for different environments we should have an
ability to set the timeout to wait for the successful deletion of
Security Group in case we get 409 HTTP status code (Conflict).

Related-PROD: PRODX-4003
Change-Id: I5d6c55c1f9e581a6d4f4c03161de44757cd8dc7e
diff --git a/tempest/common/credentials_factory.py b/tempest/common/credentials_factory.py
index c6e5dcb..28a775b 100644
--- a/tempest/common/credentials_factory.py
+++ b/tempest/common/credentials_factory.py
@@ -87,7 +87,8 @@
         ('create_networks', (CONF.auth.create_isolated_networks and not
                              CONF.network.shared_physical_network)),
         ('resource_prefix', 'tempest'),
-        ('identity_admin_endpoint_type', endpoint_type)
+        ('identity_admin_endpoint_type', endpoint_type),
+        ('networking_timeout_409', CONF.network.timeout_409)
     ]))
 
 
diff --git a/tempest/config.py b/tempest/config.py
index df1aa55..6823874 100644
--- a/tempest/config.py
+++ b/tempest/config.py
@@ -727,6 +727,10 @@
                 default=False,
                 help="The environment does not support network separation "
                      "between tenants."),
+    cfg.IntOpt('timeout_409',
+               default=120,
+               help="Total time in seconds to keep retrying a request that "
+                    "returns HTTP 409 (Conflict)."),
 ]
 
 network_feature_group = cfg.OptGroup(name='network-feature-enabled',
diff --git a/tempest/lib/common/dynamic_creds.py b/tempest/lib/common/dynamic_creds.py
index 259a13a..6dab040 100644
--- a/tempest/lib/common/dynamic_creds.py
+++ b/tempest/lib/common/dynamic_creds.py
@@ -74,7 +74,8 @@
                  neutron_available=False, create_networks=True,
                  project_network_cidr=None, project_network_mask_bits=None,
                  public_network_id=None, resource_prefix=None,
-                 identity_admin_endpoint_type='public', identity_uri=None):
+                 identity_admin_endpoint_type='public', identity_uri=None,
+                 networking_timeout_409=120):
         super(DynamicCredentialProvider, self).__init__(
             identity_version=identity_version, identity_uri=identity_uri,
             admin_role=admin_role, name=name,
@@ -119,6 +120,7 @@
             self.roles_admin_client,
             self.domains_admin_client,
             self.creds_domain_name)
+        self.networking_timeout_409 = networking_timeout_409
 
     def _get_admin_clients(self, endpoint_type):
         """Returns a tuple with instances of the following admin clients
@@ -404,8 +406,8 @@
         secgroups_to_delete = resp_body['security_groups']
         for secgroup in secgroups_to_delete:
             # Workaround for PRODX-4003
-            attempts = 10
-            for i in range(attempts):
+            start_time = time.time()
+            while True:
                 try:
                     nsg_client.delete_security_group(secgroup['id'])
                     break
@@ -416,11 +418,11 @@
                 except lib_exc.Conflict:
                     LOG.warning('Conflict with state of security group %s, '
                                 'id %s.', secgroup['name'], secgroup['id'])
-                    if i == attempts - 1:
+                    if (time.time() - self.networking_timeout_409) > \
+                            start_time:
                         raise
                     else:
                         time.sleep(5)
-                        continue
 
     def _clear_isolated_net_resources(self):
         client = self.routers_admin_client