vpnaas: Wait for site connection status

Related-Prod: PRODX-29215
Change-Id: I973505ca0b5d34fcbae75983b541e7d0468e0e3a
diff --git a/neutron_tempest_plugin/vpnaas/api/base_vpnaas.py b/neutron_tempest_plugin/vpnaas/api/base_vpnaas.py
index 0e54380..3ccf48b 100644
--- a/neutron_tempest_plugin/vpnaas/api/base_vpnaas.py
+++ b/neutron_tempest_plugin/vpnaas/api/base_vpnaas.py
@@ -14,7 +14,10 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import time
+
 from tempest.lib.common.utils import data_utils
+from tempest.lib import exceptions as lib_exc
 
 from neutron_tempest_plugin.api import base
 from neutron_tempest_plugin import config
@@ -138,6 +141,31 @@
         return ipsec_site_connection
 
     @classmethod
+    def wait_ipsec_site_connection_status(cls, site_id, status, timeout=None,
+                                          interval=None):
+        start_time = int(time.time())
+        timeout = timeout or cls.client.build_timeout
+        interval = interval or cls.client.build_interval
+        current_status = ""
+        while True:
+            if current_status.lower() == status.lower():
+                return
+
+            timed_out = int(time.time()) - start_time >= timeout
+            if timed_out:
+                message = ('IPSec site connection %(site_id)s failed to reach '
+                           'desired status %(desired_status)s, current state '
+                           '%(current_status)s in %(timeout)d' %
+                           {"site_id": site_id,
+                            "desired_status": status,
+                            "current_status": current_status,
+                            "timeout": timeout})
+                raise lib_exc.TimeoutException(message)
+            current_status = cls.client.show_ipsec_site_connection(
+                site_id)['ipsec_site_connection']['status']
+            time.sleep(interval)
+
+    @classmethod
     def create_endpoint_group(cls, name, type, endpoints):
         """Wrapper utility that returns a test ipsec policy."""
         body = cls.client.create_endpoint_group(
diff --git a/neutron_tempest_plugin/vpnaas/scenario/test_vpnaas.py b/neutron_tempest_plugin/vpnaas/scenario/test_vpnaas.py
index ca9b6bd..41cd72f 100644
--- a/neutron_tempest_plugin/vpnaas/scenario/test_vpnaas.py
+++ b/neutron_tempest_plugin/vpnaas/scenario/test_vpnaas.py
@@ -214,6 +214,7 @@
             site['vpnservice'] = self.create_vpnservice(
                 site['subnet']['id'], site['router']['id'],
                 name=data_utils.rand_name('%s-vpnservice' % site['name']))
+        site_connections = []
         for i in range(0, 2):
             site = sites[i]
             vpnservice = site['vpnservice']
@@ -225,7 +226,7 @@
                     raise self.skipException(msg)
             else:
                 peer_address = peer['vpnservice']['external_v4_ip']
-            self.create_ipsec_site_connection(
+            site_connection = self.create_ipsec_site_connection(
                 self.ikepolicy['id'],
                 self.ipsecpolicy['id'],
                 vpnservice['id'],
@@ -235,6 +236,10 @@
                 psk=psk,
                 name=data_utils.rand_name(
                     '%s-ipsec-site-connection' % site['name']))
+            site_connections.append(site_connection)
+        for site_connection in site_connections:
+            self.wait_ipsec_site_connection_status(site_connection['id'],
+                                                   status="ACTIVE")
 
     def _get_ip_on_subnet_for_port(self, port, subnet_id):
         for fixed_ip in port['fixed_ips']: