[VOL-2778] Supporting required configuration for BBSim v1.0.0-dev

Change-Id: I4ce3b4f9731dda64c2e391a7945412633b9b77d5
diff --git a/README.md b/README.md
index ccf9f34..c45a2e6 100644
--- a/README.md
+++ b/README.md
@@ -100,6 +100,7 @@
 | `SADIS_SUBSCRIBERS`                     | http://bbsim0.voltha.svc:50074/v2/subscribers/%s      | URL for ONOS to use to query subsriber information if `CONFIG_SADIS` is set to `url` |
 | `SADIS_BANDWIDTH_PROFILES`              | http://bbsim0.voltha.svc:50074/v2/bandwidthprofiles/%s| URL for ONOS to use to query bandwidth profiles if `CONFIG_SADIS` is set to `url` |
 | `SADIS_CFG`                             | onos-files/onos-sadis-sample.json                     | SADIS Configuration File to push, if CONFIG_SADIS set |
+| `BBSIM_CFG`                             | configs/bbsim-sadis-att.yaml                          | Configuration for BBSim services |
 | `INSTALL_ONOS_APPS`                     | no                                                    | Replaces/installs ONOS OAR files in onos-files/onos-apps |
 | `INSTALL_KUBECTL`                       | yes                                                   | Should a copy of `kubectl` be installed locally? |
 | `INSTALL_HELM`                          | yes                                                   | Should a copy of `helm` be installed locallly? |
@@ -190,6 +191,15 @@
 | `bbsim`          | configure ONOS use use the SADIS servers that are part of BBSIM |
 | `external`       | an additional helm chart will be installed (`bbsim-sadis-server`) and ONOS will be configured to use that service for SADIS queries |
 
+### `BBSIM_CFG` Values
+
+`BBSIM_CFG` contains a description of the services that is needed to properly configure BBSim.
+Examples are available in the `configs` folder. It can be pointed to any valid BBSim service configuration.
+
+You'll note that the examples contain a `:TAG:` placeholder. That is used by `kind-voltha`
+to generate different `C/S_TAG` combinations when deploying multiple instances of BBSim and it's
+replaced with an incremental number starting from `900`.
+
 ### EFK Configuration for accessing Voltha component logs
 If EFK is selected for deployment with VOLTHA, `WITH_EFK=yes`, then a single node elasticsearch and kibana
 instance will be deployed and a fluentd-elasticsearch pod will be deployed on each node that allows workloads
diff --git a/configs/bbsim-sadis-att.yaml b/configs/bbsim-sadis-att.yaml
new file mode 100644
index 0000000..1eaa0c0
--- /dev/null
+++ b/configs/bbsim-sadis-att.yaml
@@ -0,0 +1,12 @@
+---
+servicesConfig:
+  workflow: att
+  services:
+    - name: hsia
+      c_tag: 900
+      c_tag_allocation: unique
+      s_tag: ":TAG:"
+      s_tag_allocation: shared
+      needs_eapol: true
+      needs_dhcp: true
+      tp_id: 64
diff --git a/configs/bbsim-sadis-dt.yaml b/configs/bbsim-sadis-dt.yaml
new file mode 100644
index 0000000..da64559
--- /dev/null
+++ b/configs/bbsim-sadis-dt.yaml
@@ -0,0 +1,11 @@
+---
+servicesConfig:
+  workflow: dt
+  services:
+    - name: hsia
+      uni_tag_match: 4096
+      c_tag: ":TAG:"
+      c_tag_allocation: shared
+      s_tag: 7
+      s_tag_allocation: unique
+      tp_id: 64
diff --git a/configs/bbsim-sadis-tt.yaml b/configs/bbsim-sadis-tt.yaml
new file mode 100644
index 0000000..5b4e0bd
--- /dev/null
+++ b/configs/bbsim-sadis-tt.yaml
@@ -0,0 +1,48 @@
+---
+servicesConfig:
+  workflow: tt
+  services:
+    - name: hsia
+      uni_tag_match: 35
+      c_tag: 900
+      c_tag_allocation: unique
+      s_tag: ":TAG:"
+      s_tag_allocation: shared
+      tp_id: 64
+    - name: voip
+      uni_tag_match: 65
+      c_tag: 444
+      c_tag_allocation: shared
+      s_tag: 333
+      s_tag_allocation: shared
+      needs_dhcp: true
+      tp_id: 65
+      configure_mac_address: true
+      us_pon_c_tag_priority: 7
+      us_pon_s_tag_priority: 7
+      ds_pon_c_tag_priority: 7
+      ds_pon_s_tag_priority: 7
+    - name: vod
+      uni_tag_match: 55
+      c_tag: 55
+      c_tag_allocation: shared
+      s_tag: 555
+      s_tag_allocation: shared
+      needs_dhcp: true
+      needs_igmp: true
+      tp_id: 66
+      configure_mac_address: true
+      us_pon_c_tag_priority: 5
+      us_pon_s_tag_priority: 5
+      ds_pon_c_tag_priority: 5
+      ds_pon_s_tag_priority: 5
+    # NOTE: the multicast service must be called MC unless you set
+    # multicastServiceName to something else in org.opencord.olt.impl.Olt
+    - name: MC
+      c_tag: 55
+      c_tag_allocation: shared
+      s_tag: 550
+      s_tag_allocation: shared
+      tp_id: 66
+      ds_pon_c_tag_priority: 5
+      ds_pon_s_tag_priority: 5
diff --git a/voltha b/voltha
index 2f856c2..ceb9154 100755
--- a/voltha
+++ b/voltha
@@ -95,6 +95,7 @@
 SCHEDULE_ON_CONTROL_NODES=${SCHEDULE_ON_CONTROL_NODES:-no}
 CONFIG_SADIS=${CONFIG_SADIS:-no} # yes | no | file | bbsim | external | URL
 SADIS_CFG=${SADIS_CFG:-onos-files/onos-sadis-sample.json}
+BBSIM_CFG=${BBSIM_CFG:-configs/bbsim-sadis-att.yaml}
 INSTALL_ONOS_APPS=${INSTALL_ONOS_APPS:-no}
 JUST_K8S=${JUST_K8S:-no}
 DEPLOY_K8S=${DEPLOY_K8S:-yes}
@@ -109,7 +110,7 @@
 VOLTHA_CHART=${VOLTHA_CHART:-onf/voltha}
 VOLTHA_CHART_VERSION=${VOLTHA_CHART_VERSION:-latest}
 VOLTHA_BBSIM_CHART=${VOLTHA_BBSIM_CHART:-onf/bbsim}
-VOLTHA_BBSIM_CHART_VERSION=${VOLTHA_BBSIM_CHART_VERSION:-latest}
+VOLTHA_BBSIM_CHART_VERSION=${VOLTHA_BBSIM_CHART_VERSION:-4.0.0-dev}
 ELASTICSEARCH_CHART=${ELASTICSEARCH_CHART:-elastic/elasticsearch}
 ELASTICSEARCH_CHART_VERSION=${ELASTICSEARCH_CHART_VERSION:-latest}
 KIBANA_CHART=${KIBANA_CHART:-elastic/kibana}
@@ -203,6 +204,95 @@
     kubectl -n "$NS" get service "$NAME" -o json | jq -r '.spec.clusterIP + ":" + (.spec.ports[0].port|tostring)'
 }
 
+# returns the greater version (0 if equal)
+function semvercompare() {
+    if [[ "$1" == "$2" ]]
+    then
+        echo 0
+        return
+    fi
+
+    local tmp1 tmp2
+    tmp1=$(echo "${1//v/}" | awk -F'-' '{print $1}')
+    tmp2=$(echo "${2//v/}" | awk -F'-' '{print $1}')
+
+    local IFS=.
+    # shellcheck disable=SC2206
+    local i ver1=($tmp1) ver2=($tmp2)
+
+    # fill empty fields in ver1 with zeros
+    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
+    do
+        ver1[i]=0
+    done
+    for ((i=0; i<${#ver1[@]}; i++))
+    do
+        if [[ -z ${ver2[i]} ]]
+        then
+            # fill empty fields in ver2 with zeros
+            ver2[i]=0
+        fi
+        if ((10#${ver1[i]} > 10#${ver2[i]}))
+        then
+            echo 1
+            return
+        fi
+        if ((10#${ver1[i]} < 10#${ver2[i]}))
+        then
+            echo 2
+            return
+        fi
+    done
+    echo 0
+    return
+}
+
+# returns true if v1 is less than v2
+function semver_greater() {
+  local v1
+  local v2
+  v1=("$1") v2=("$2")
+
+  # shellcheck disable=SC2128
+  if [[ "$v1" == "latest" ]] || [[ "$v1" == "master" ]]; then
+    # if the version is latest or master we can't really compare,
+    # but we assume is greater
+    echo "true"
+    return
+  fi
+
+  # shellcheck disable=SC2128
+  res=$(semvercompare "$v1" "$v2")
+  if [[ $res == 1 ]]; then
+    echo "true"
+  else
+    echo "false"
+  fi
+}
+
+# returns true if v2 is less than v1
+function semver_lesser() {
+  local v1
+  local v2
+  v1=("$1") v2=("$2")
+
+  # shellcheck disable=SC2128
+  if [[ "$v1" == "latest" ]] || [[ "$v1" == "master" ]]; then
+    # if the version is latest or master we can't really compare,
+    # but we assume is greater
+    echo "false"
+    return
+  fi
+
+  # shellcheck disable=SC2128
+  res=$(semvercompare "$v1" "$v2")
+  if [[ $res == 2 ]]; then
+    echo "true"
+  else
+    echo "false"
+  fi
+}
+
 # Used to verify configuration values are set to "yes" or "no" value or convert
 # equivalents to "yes" or "no"
 YES="y,yes,t,true,1"
@@ -328,6 +418,7 @@
     ONOS_API_PORT \
     ONOS_SSH_PORT \
     SADIS_CFG \
+    BBSIM_CFG \
     VOLTHA_API_PORT \
     VOLTHA_SSH_PORT \
     VOLTHA_ETCD_PORT \
@@ -2354,18 +2445,32 @@
 
         if [ "$HELM_USE_UPGRADE" == "yes" ] || [ "$(helm_is_deployed "$BBSIM_NS" "^bbsim${instance_num}\$")" -ne 1 ]; then
             espin - "$NOT_VERIFIED"
-            S_TAG="$((900+instance))"
-            INTERNAL_EXTRA_HELM_INSTALL_ARGS="--set olt_id=$instance,s_tag=$S_TAG"
-            if [ "$WITH_EAPOL" == "yes" ]; then
-                INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set auth=true"
+
+            if [[ $(semver_greater "$VOLTHA_BBSIM_CHART_VERSION" "3.1.0") == "true" ]]; then
+              # this is the latest BBSim with support for TT
+              # when we deploy multiple BBSims we need to update the configuration
+              # to avoid overlapping tags.
+              _TAG="$((900+instance))"
+              BBSIM_TMP="$(mktemp -u)"
+              sed -e "s/\":TAG:\"/$_TAG/g" "$BBSIM_CFG" > "$BBSIM_TMP"
+
+              INTERNAL_EXTRA_HELM_INSTALL_ARGS="--set olt_id=$instance -f $BBSIM_TMP"
             else
-                INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set auth=false"
+              # these are older versio of BBSim
+              S_TAG="$((900+instance))"
+              INTERNAL_EXTRA_HELM_INSTALL_ARGS="--set olt_id=$instance,s_tag=$S_TAG"
+              if [ "$WITH_EAPOL" == "yes" ]; then
+                 INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set auth=true"
+              else
+                 INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set auth=false"
+              fi
+              if [ "$WITH_DHCP" == "yes" ]; then
+                 INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set dhcp=true"
+              else
+                 INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set dhcp=false"
+              fi
             fi
-            if [ "$WITH_DHCP" == "yes" ]; then
-                INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set dhcp=true"
-            else
-                INTERNAL_EXTRA_HELM_INSTALL_ARGS+=" --set dhcp=false"
-            fi
+
             if is_in "$WITH_KAFKA" "yes,external"; then
                 _HOST=kafka.$INFRA_NS.svc
                 _PORT=9092
@@ -2378,6 +2483,7 @@
             fi
             helm_install - "$BBSIM_NS" "bbsim${instance_num}" "$VOLTHA_BBSIM_CHART" "$VOLTHA_BBSIM_CHART_VERSION" "+bbsim" "$_HELM_DESC BBSIM${instance_num}"
             INTERNAL_EXTRA_HELM_INSTALL_ARGS=
+            rm -f "$BBSIM_TMP"
         else
             espin - "$VERIFIED"
         fi