Parameterize xconnect creation

Change-Id: Idc96e7d4749e57833128f2364d2a5d8ae9f32db1
diff --git a/Jenkinsfile-voltha-build b/Jenkinsfile-voltha-build
index 61c5cfe..5d5db04 100644
--- a/Jenkinsfile-voltha-build
+++ b/Jenkinsfile-voltha-build
@@ -25,7 +25,7 @@
                 deployment_config = readYaml file: "${configBaseDir}/${configDeploymentDir}/${configFileName}.yaml"
             }
             stage('Clean up') {
-              timeout(10) {
+                timeout(10) {
                     sh returnStdout: true, script: """
                     export KUBECONFIG=$WORKSPACE/${configBaseDir}/${configKubernetesDir}/${configFileName}.conf
                     for hchart in \$(helm list -q | grep -E -v 'docker-registry|mavenrepo|ponnet');
@@ -122,23 +122,31 @@
                 }
             }
             if ( deployment_config.fabric_switches.size() > 0 ) {
-              stage('Switch Configurations in ONOS') {
-                timeout(1) {
-                    netcfg_out = sh returnStatus: true, script: """
-                    curl -sSL --user karaf:karaf -X POST -H Content-Type:application/json http://${deployment_config.nodes[0].ip}:30120/onos/v1/network/configuration --data @$WORKSPACE/${configBaseDir}/${configToscaDir}/voltha/${configFileName}-onos-netcfg-switch.json
-                    curl -sSL --user karaf:karaf -X POST http://${deployment_config.nodes[0].ip}:30120/onos/v1/applications/org.onosproject.segmentrouting/active
-                    """
-                    return netcfg_out == 0
+                stage('Switch Configurations in ONOS') {
+                    timeout(1) {
+                        netcfg_out = sh returnStatus: true, script: """
+                        curl -sSL --user karaf:karaf -X POST -H Content-Type:application/json http://${deployment_config.nodes[0].ip}:30120/onos/v1/network/configuration --data @$WORKSPACE/${configBaseDir}/${configToscaDir}/voltha/${configFileName}-onos-netcfg-switch.json
+                        curl -sSL --user karaf:karaf -X POST http://${deployment_config.nodes[0].ip}:30120/onos/v1/applications/org.onosproject.segmentrouting/active
+                        """
+                        return netcfg_out == 0
+                    }
+                    timeout(1) {
+                        waitUntil {
+                            sr_active_out = sh returnStatus: true, script: """
+                            curl -sSL --user karaf:karaf -X GET http://${deployment_config.nodes[0].ip}:30120/onos/v1/applications/org.onosproject.segmentrouting | jq '.state' | grep ACTIVE
+                            """
+                            return sr_active_out == 0
+                        }
+                    }
+                    timeout(1) {
+                        // FIXME support multiple OLTs
+                        for(int i=0; i < deployment_config.hosts.src.size(); i++) {
+                            xconnect_out = sh returnStatus: true, script: """
+                            curl -X POST --user karaf:karaf --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"deviceId": "${deployment_config.fabric_switches[0].device_id}", "vlanId": "${deployment_config.hosts.src[i].s_tag}", "ports": [${deployment_config.fabric_switches[0].bngPort},${deployment_config.fabric_switches[0].oltPort}]}' 'http://${deployment_config.nodes[0].ip}:30120/onos/segmentrouting/xconnect'
+                            """
+                        }
+                    }
                 }
-                timeout(1) {
-                  // FIXME iterate over the hosts section to create multiple xconnect if needed
-                  // FIXME read the device id from deployment_config.fabric_switches
-                  xconnect_out = sh returnStatus: true, script: """
-                  curl -X POST --user karaf:karaf --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"deviceId": "of:0000000000000001", "vlanId": "111", "ports": [3,10]}' 'http://10.192.20.45:30120/onos/segmentrouting/xconnect'
-                  """
-                  return xconnect_out == 0
-                }
-              }
             }
             if ( params.configurePod ) {
                 stage('Push Tech-Profile') {
@@ -152,20 +160,20 @@
                         return out_push_tp == 0
                     }
                     timeout(1) {
-                      out_get_tp = sh returnStatus: true, script: """
-                      etcd_container=\$(kubectl get pods -n voltha | grep voltha-etcd-cluster | awk 'NR==1{print \$1}')
-                      get_result=\$(kubectl exec -it \$etcd_container -n voltha -- /bin/sh -c 'ETCDCTL_API=3 etcdctl get --prefix service/voltha/technology_profiles/xgspon/64')
-                      """
-                      return out_get_tp == 0
+                        out_get_tp = sh returnStatus: true, script: """
+                        etcd_container=\$(kubectl get pods -n voltha | grep voltha-etcd-cluster | awk 'NR==1{print \$1}')
+                        get_result=\$(kubectl exec -it \$etcd_container -n voltha -- /bin/sh -c 'ETCDCTL_API=3 etcdctl get --prefix service/voltha/technology_profiles/xgspon/64')
+                        """
+                        return out_get_tp == 0
                     }
                 }
                 stage('Push Sadis-config') {
-                  timeout(1) {
-                    sadis_out = sh returnStatus: true, script: """
-                    curl -sSL --user karaf:karaf -X POST -H Content-Type:application/json http://${deployment_config.nodes[0].ip}:30120/onos/v1/network/configuration --data @$WORKSPACE/voltha-system-tests/tests/data/${configFileName}-sadis.json
-                    """
-                    return sadis_out == 0
-                  }
+                    timeout(1) {
+                        sadis_out = sh returnStatus: true, script: """
+                        curl -sSL --user karaf:karaf -X POST -H Content-Type:application/json http://${deployment_config.nodes[0].ip}:30120/onos/v1/network/configuration --data @$WORKSPACE/voltha-system-tests/tests/data/${configFileName}-sadis.json
+                        """
+                        return sadis_out == 0
+                    }
                 }
             }
             currentBuild.result = 'SUCCESS'