Merge "[VOL-4363] Waiting for adapters."
diff --git a/jjb/pipeline/voltha/master/physical-build.groovy b/jjb/pipeline/voltha/master/physical-build.groovy
index d9f75b3..2107f21 100644
--- a/jjb/pipeline/voltha/master/physical-build.groovy
+++ b/jjb/pipeline/voltha/master/physical-build.groovy
@@ -127,8 +127,13 @@
             // adding user specified helm flags at the end so they'll have priority over everything else
             localHelmFlags = localHelmFlags + " ${extraHelmFlags}"
 
+            def numberOfAdaptersToWait = 2
+
             if(openoltAdapterChart != "onf/voltha-adapter-openolt") {
               localHelmFlags = localHelmFlags + " --set voltha-adapter-openolt.enabled=false"
+              // We skip waiting for adapters in the volthaDeploy step because it's already waiting for
+              // both of them after the deployment of the custom olt adapter. See line 156.
+              numberOfAdaptersToWait = 0
             }
 
             volthaDeploy([
@@ -141,12 +146,16 @@
               kafkaReplica: params.NumOfKafka,
               etcdReplica: params.NumOfEtcd,
               bbsimReplica: bbsimReplicas.toInteger(),
+              adaptersToWait: numberOfAdaptersToWait,
               ])
 
-              if(openoltAdapterChart != "onf/voltha-adapter-openolt"){
-                extraHelmFlags = extraHelmFlags + " --set global.log_level=${logLevel}"
-                deploy_custom_oltAdapterChart(volthaNamespace, oltAdapterReleaseName, openoltAdapterChart, extraHelmFlags)
-              }
+            if(openoltAdapterChart != "onf/voltha-adapter-openolt"){
+              extraHelmFlags = extraHelmFlags + " --set global.log_level=${logLevel}"
+              deploy_custom_oltAdapterChart(volthaNamespace, oltAdapterReleaseName, openoltAdapterChart, extraHelmFlags)
+              waitForAdapters([
+                adaptersToWait: 2
+              ])
+            }
           }
           sh """
           JENKINS_NODE_COOKIE="dontKillMe" _TAG="voltha-api" bash -c "while true; do kubectl port-forward --address 0.0.0.0 -n ${volthaNamespace} svc/voltha-voltha-api 55555:55555; done"&
diff --git a/jjb/pipeline/voltha/voltha-2.8/physical-build.groovy b/jjb/pipeline/voltha/voltha-2.8/physical-build.groovy
index 41a4df2..ed43acc 100644
--- a/jjb/pipeline/voltha/voltha-2.8/physical-build.groovy
+++ b/jjb/pipeline/voltha/voltha-2.8/physical-build.groovy
@@ -127,8 +127,13 @@
             // adding user specified helm flags at the end so they'll have priority over everything else
             localHelmFlags = localHelmFlags + " ${extraHelmFlags}"
 
+            def numberOfAdaptersToWait = 2
+
             if(openoltAdapterChart != "onf/voltha-adapter-openolt") {
               localHelmFlags = localHelmFlags + " --set voltha-adapter-openolt.enabled=false"
+              // We skip waiting for adapters in the volthaDeploy step because it's already waiting for
+              // both of them after the deployment of the custom olt adapter. See line 156.
+              numberOfAdaptersToWait = 0
             }
 
             volthaDeploy([
@@ -141,12 +146,16 @@
               kafkaReplica: params.NumOfKafka,
               etcdReplica: params.NumOfEtcd,
               bbsimReplica: bbsimReplicas.toInteger(),
+              adaptersToWait: numberOfAdaptersToWait,
               ])
 
-              if(openoltAdapterChart != "onf/voltha-adapter-openolt"){
-                extraHelmFlags = extraHelmFlags + " --set global.log_level=${logLevel}"
-                deploy_custom_oltAdapterChart(volthaNamespace, oltAdapterReleaseName, openoltAdapterChart, extraHelmFlags)
-              }
+            if(openoltAdapterChart != "onf/voltha-adapter-openolt"){
+              extraHelmFlags = extraHelmFlags + " --set global.log_level=${logLevel}"
+              deploy_custom_oltAdapterChart(volthaNamespace, oltAdapterReleaseName, openoltAdapterChart, extraHelmFlags)
+              waitForAdapters([
+                adaptersToWait: 2
+              ])
+            }
           }
           sh """
           JENKINS_NODE_COOKIE="dontKillMe" _TAG="voltha-api" bash -c "while true; do kubectl port-forward --address 0.0.0.0 -n ${volthaNamespace} svc/voltha-voltha-api 55555:55555; done"&
diff --git a/vars/volthaStackDeploy.groovy b/vars/volthaStackDeploy.groovy
index a49cd35..0f96796 100644
--- a/vars/volthaStackDeploy.groovy
+++ b/vars/volthaStackDeploy.groovy
@@ -11,6 +11,7 @@
       extraHelmFlags: "",
       localCharts: false,
       onosReplica: 1,
+      adaptersToWait: 2,
     ]
 
     if (!config) {
@@ -84,65 +85,7 @@
         done
     """
 
-    println "Wait for adapters to be registered"
-
-    // guarantee that at least two adapters are registered with VOLTHA before proceeding
-    // 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
-        adapters=\$(voltctl adapter list -q | wc -l)
-        while [[ \$adapters -lt 2 ]]; do
-          sleep 5
-          adapters=\$(voltctl adapter list -q | wc -l)
-        done
-    """
-
-    // 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
-    """
+    waitForAdapters(cfg)
 
     // also make sure that the ONOS config is loaded
     // NOTE that this is only required for VOLTHA-2.8
diff --git a/vars/waitForAdapters.groovy b/vars/waitForAdapters.groovy
index ab5f22b..661e089 100644
--- a/vars/waitForAdapters.groovy
+++ b/vars/waitForAdapters.groovy
@@ -11,6 +11,12 @@
 
     def cfg = defaultConfig + config
 
+    if (cfg.adaptersToWait == 0){
+       //no need to wait
+       println "No need to wait for adapters to be registered"
+       return
+    }
+
     println "Wait for adapters to be registered"
 
     // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding