[VOL-4363] Waiting for adapters.

Customising number of adapters required in the system.
Enabling deployment of custom adapter at a later time

Change-Id: Ia66505ee1676be52680a87647f888bb7706ef0cb
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