Merge "Waiting for adapters to communicate with the core before starting tests"
diff --git a/jjb/voltha-e2e.yaml b/jjb/voltha-e2e.yaml
index 2887786..6339d0d 100755
--- a/jjb/voltha-e2e.yaml
+++ b/jjb/voltha-e2e.yaml
@@ -49,6 +49,8 @@
           code-branch: 'master'
           volthaHelmChartsChange: refs/changes/69/26569/11
           volthaSystemTestsChange: refs/changes/32/27532/5
+          gerritProject: voltctl
+          gerritRefspec: refs/changes/83/26983/4
           extraHelmFlags: >
             --set global.image_tag=master
             --set onos-classic.image.repository=andreacampanella/voltha-onos --set onos-classic.image.tag=meter-fixes-simple
@@ -244,6 +246,8 @@
           code-branch: 'master'
           volthaHelmChartsChange: refs/changes/69/26569/11
           volthaSystemTestsChange: refs/changes/32/27532/5
+          gerritProject: voltctl
+          gerritRefspec: refs/changes/83/26983/4
           extraHelmFlags: >
             --set global.image_tag=master
             --set onos-classic.image.repository=andreacampanella/voltha-onos --set onos-classic.image.tag=meter-fixes-simple
diff --git a/vars/volthaStackDeploy.groovy b/vars/volthaStackDeploy.groovy
index 594be03..a49cd35 100644
--- a/vars/volthaStackDeploy.groovy
+++ b/vars/volthaStackDeploy.groovy
@@ -87,17 +87,61 @@
     println "Wait for adapters to be registered"
 
     // guarantee that at least two adapters are registered with VOLTHA before proceeding
-    // this is potentially open to issue if we'll run test with multiple adapter pairs (eg: adtran + open)
-    // untill then it is safe to assume we'll be ready once we have two adapters in the system
+    // this is potentially prone to issues if we'll run test with multiple adapter pairs (eg: adtran + open)
+    // until then it is safe to assume we'll be ready once we have two adapters in the system
+    sh """
+      set +x
+      _TAG="voltha-voltha-api" bash -c "while true; do kubectl port-forward --address 0.0.0.0 -n ${cfg.volthaNamespace} svc/voltha-voltha-api 55555:55555; done"&
+    """
     sh """
         set +x
-        _TAG="voltha-voltha-api" bash -c "while true; do kubectl port-forward --address 0.0.0.0 -n ${cfg.volthaNamespace} svc/voltha-voltha-api 55555:55555; done"&
         adapters=\$(voltctl adapter list -q | wc -l)
         while [[ \$adapters -lt 2 ]]; do
           sleep 5
           adapters=\$(voltctl adapter list -q | wc -l)
         done
-        ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
+    """
+
+    // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s
+    // as that means the core can talk to the adapters
+    // if voltctl can't read LastCommunication we skip this check
+    def done = false;
+
+    while (!done) {
+      sleep 1
+      def adapters = ""
+      try {
+        adapters = sh (
+          script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"',
+          returnStdout: true,
+        ).trim()
+      } catch (err) {
+        // in some older versions of voltctl the command results in
+        // ERROR: Unexpected error while attempting to format results as table : template: output:1: function "gosince" not defined
+        // if that's the case we won't be able to check the timestamp so it's useless to wait
+        println("voltctl can't parse LastCommunication, skip waiting")
+        done = true
+        break
+      }
+
+      def waitingOn = adapters.split( '\n' ).find{since ->
+        since = since.replaceAll('s','') //remove seconds from the string
+
+        // it has to be a single digit
+        if (since.length() > 1) {
+            return true
+        }
+        if ((since as Integer) > 5) {
+            return true
+        }
+        return false
+      }
+      done = (waitingOn == null || waitingOn.length() == 0)
+    }
+
+    sh """
+      set +x
+      ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
     """
 
     // also make sure that the ONOS config is loaded