Automate workaround to trigger ETSI flow

Change-Id: Ib9bf3639acb16b7b403cb07ba71407711bd529ff
Issue-ID: SO-1955
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
diff --git a/plans/so/integration-etsi-testing/.gitignore b/plans/so/integration-etsi-testing/.gitignore
index 675e83c..44793d7 100644
--- a/plans/so/integration-etsi-testing/.gitignore
+++ b/plans/so/integration-etsi-testing/.gitignore
@@ -18,3 +18,5 @@
 /.metadata/
 temp/
 **/temp/**
+test_lab/
+**/test_lab/**
diff --git a/plans/so/integration-etsi-testing/config/apply-workarounds.sh b/plans/so/integration-etsi-testing/config/apply-workarounds.sh
new file mode 100755
index 0000000..f6bf906
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/apply-workarounds.sh
@@ -0,0 +1,116 @@
+#!/bin/bash
+#
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 Nordix Foundation.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+#
+
+# @author Waqas Ikram (waqas.ikram@est.tech)
+
+BUIDLING_BLOCK_TABLE_NAME="building_block_detail"
+ORCH_FLOW_TABLE_NAME="orchestration_flow_reference"
+NORTH_BOUND_TABLE_NAME="northbound_request_ref_lookup"
+TABLE_EXISTS_QUERY="select count(*) from information_schema.tables WHERE table_schema='$CATALOG_DB' AND table_name='$BUIDLING_BLOCK_TABLE_NAME';"
+BUILDING_BLOCK_COUNT_QUERY="select count(*) from $BUIDLING_BLOCK_TABLE_NAME;"
+FLY_WAY_MIGRATION_QUERY="SELECT COUNT(*) FROM flyway_schema_history WHERE script LIKE '%R__MacroData%' AND installed_on IS NOT NULL;"
+
+
+current_timestamp()
+{
+ date +"%Y-%m-%d %H:%M:%S"
+}
+
+wait_for_database_availability()
+{
+ echo "$(current_timestamp): Checking for database availability"
+ until echo '\q' | mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB; do
+     >&2 echo "$(current_timestamp): Database is unavailable - sleeping for 5 seconds"
+     sleep 5
+ done
+
+ echo "$(current_timestamp): Database is available now"
+}
+
+wait_container_to_create_table()
+{
+ while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$TABLE_EXISTS_QUERY") -eq "0" ] ; do
+     echo "$(current_timestamp): Waiting for so-catalog container to create tables - sleeping for 5 seconds"
+     sleep 5
+ done
+
+ while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$BUILDING_BLOCK_COUNT_QUERY") -eq "0" ] ; do
+     echo "$(current_timestamp): Waiting for so-catalog container to insert records in $BUIDLING_BLOCK_TABLE_NAME - sleeping for 5 seconds"
+     sleep 5
+ done
+
+ echo "$(current_timestamp): $CATALOG_DB tables available now . . ."
+}
+
+wait_for_flyway_migration_to_finish()
+{
+ while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$FLY_WAY_MIGRATION_QUERY") -ne "1" ] ; do
+     echo "$(current_timestamp): Waiting for flyway migration sql statement to finish with success - sleeping for 5 seconds"
+     sleep 5
+ done
+ echo "$(current_timestamp): flyway migration finished . . . "
+}
+
+
+apply_workaround()
+{
+ echo "$(current_timestamp): Applying workaround . . ."
+
+ wait_for_database_availability
+ wait_container_to_create_table
+ wait_for_flyway_migration_to_finish
+
+ echo "$(current_timestamp): Will insert data into $CATALOG_DB"
+mysql -h $DB_HOST -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB << EOF
+  BEGIN;
+  INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfInstantiateBB", "VNF", "ACTIVATE");
+  INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfDeleteBB", "VNF", "DEACTIVATE");
+ 
+  DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Create";
+
+  INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Create" AS COMPOSITE_ACTION, 1 AS SEQ_NO, "AssignVnfBB" AS FLOW_NAME, 1 AS FLOW_VERSION,  id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="createInstance";
+
+  INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Create" AS COMPOSITE_ACTION, 2 AS SEQ_NO, "EtsiVnfInstantiateBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="createInstance";
+
+  INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Create" AS COMPOSITE_ACTION, 3 AS SEQ_NO, "ActivateVnfBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="createInstance";
+
+ DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Delete";
+
+ INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Delete" AS COMPOSITE_ACTION, 1 AS SEQ_NO, "EtsiVnfDeleteBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="deleteInstance";
+
+ INSERT INTO $ORCH_FLOW_TABLE_NAME (COMPOSITE_ACTION,SEQ_NO,FLOW_NAME,FLOW_VERSION,NB_REQ_REF_LOOKUP_ID ) SELECT "VNF-Delete" AS COMPOSITE_ACTION, 2 AS SEQ_NO, "UnassignVnfBB" AS FLOW_NAME, 1 AS FLOW_VERSION, id AS NB_REQ_REF_LOOKUP_ID FROM $NORTH_BOUND_TABLE_NAME WHERE REQUEST_SCOPE='Vnf' AND IS_ALACARTE is true AND ACTION="deleteInstance";
+
+ UPDATE orchestration_status_state_transition_directive SET FLOW_DIRECTIVE='CONTINUE' WHERE RESOURCE_TYPE='VNF' AND ORCHESTRATION_STATUS='CREATED' AND TARGET_ACTION='ACTIVATE' AND FLOW_DIRECTIVE='FAIL';
+
+ COMMIT;
+EOF
+
+ if [ $? -ne 0 ]; then
+    echo "$(current_timestamp): Failed to execute workaround . . ."
+    exit 1
+ fi
+
+ echo "$(current_timestamp): Finished applying workaround . . ."
+}
+
+# main body
+apply_workaround
+
diff --git a/plans/so/integration-etsi-testing/config/env b/plans/so/integration-etsi-testing/config/env
new file mode 100644
index 0000000..f34f9c4
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/env
@@ -0,0 +1,2 @@
+NEXUS_DOCKER_REPO_MSO=nexus3.onap.org:10001
+TAG=1.4.0-STAGING-latest
diff --git a/plans/so/integration-etsi-testing/docker-compose.yml b/plans/so/integration-etsi-testing/docker-compose.yml
index d725596..a1421e3 100644
--- a/plans/so/integration-etsi-testing/docker-compose.yml
+++ b/plans/so/integration-etsi-testing/docker-compose.yml
@@ -1,6 +1,117 @@
 version: '3'
 services:
 ################################################################################
+  mariadb:
+    image: ${NEXUS_DOCKER_REPO_MSO}/mariadb:10.1.11
+    ports:
+      - "3306:3306"
+    volumes:
+      - ${TEST_LAB_DIR}/volumes/mariadb/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
+      - ${TEST_LAB_DIR}/volumes/mariadb/conf.d:/etc/mysql/conf.d
+    environment:
+      - MYSQL_ROOT_PASSWORD=password
+    hostname:
+      mariadb.so.testlab.onap.org
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "30m"
+        max-file: "5"
+################################################################################
+  catalog-db-adapter:
+    image: ${NEXUS_DOCKER_REPO_MSO}/onap/so/catalog-db-adapter:${TAG}
+    ports:
+      - "8082:8082"
+    volumes:
+      - ${TEST_LAB_DIR}/volumes/so/ca-certificates/onapheat:/app/ca-certificates
+      - ${TEST_LAB_DIR}/volumes/so/config/catalog-db-adapter/onapheat:/app/config
+    environment:
+      - APP=catalog-db-adapter
+      - JVM_ARGS=-Xms64m -Xmx512m
+      - DB_HOST=mariadb
+      - DB_PORT=3306
+      - DB_USERNAME=so_user
+      - DB_PASSWORD=so_User123
+      - DB_ADMIN_USERNAME=so_admin
+      - DB_ADMIN_PASSWORD=so_Admin123
+    hostname:
+      catalog-db-adapter.so.testlab.onap.org
+    depends_on:
+      - mariadb
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "30m"
+        max-file: "5"
+    command:
+      - /app/wait-for.sh
+      - -q
+      - -t
+      - "300"
+      - mariadb:3306
+      - --
+      - "/app/start-app.sh"
+################################################################################
+  request-db-adapter:
+    image: ${NEXUS_DOCKER_REPO_MSO}/onap/so/request-db-adapter:${TAG}
+    ports:
+      - "8083:8083"
+    volumes:
+      - ${TEST_LAB_DIR}/volumes/so/ca-certificates/onapheat:/app/ca-certificates
+      - ${TEST_LAB_DIR}/volumes/so/config/request-db-adapter/onapheat:/app/config
+    environment:
+      - APP=request-db-adapter
+      - JVM_ARGS=-Xms64m -Xmx512m
+      - DB_HOST=mariadb
+      - DB_PORT=3306
+      - DB_USERNAME=so_user
+      - DB_PASSWORD=so_User123
+      - DB_ADMIN_USERNAME=so_admin
+      - DB_ADMIN_PASSWORD=so_Admin123
+    hostname:
+      request-db-adapter.so.testlab.onap.org
+    depends_on:
+      - mariadb
+      - catalog-db-adapter
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "30m"
+        max-file: "5"
+    command:
+      - /app/wait-for.sh
+      - -q
+      - -t
+      - "300"
+      - catalog-db-adapter:8082
+      - --
+      - "/app/start-app.sh"
+################################################################################
+  VNFM-adapter:
+    image: ${NEXUS_DOCKER_REPO_MSO}/onap/so/vnfm-adapter:${TAG}
+    ports:
+      - "9092:9092"
+    environment:
+    - APP=VNFM-adapter
+    - JVM_ARGS=-Xms64m -Xmx512m
+    hostname:
+      vnfm-adapter-test
+    depends_on:
+      - request-db-adapter
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "30m"
+        max-file: "5"
+    command:
+      - /app/wait-for.sh
+      - -q
+      - -t
+      - "300"
+      - request-db-adapter:8083
+      - --
+      - "/app/start-app.sh"
+################################################################################
   sdc-simulator:
     image: simulators/sdc-simulator:latest
     ports:
@@ -10,25 +121,29 @@
     - JVM_ARGS=-Xms64m -Xmx512m
     hostname:
       sdc-simulator
+    depends_on:
+      - mariadb
+      - VNFM-adapter
     logging:
       driver: "json-file"
       options:
         max-size: "30m"
         max-file: "5"
 ################################################################################
-  VNFM-adapter:
-    image: nexus3.onap.org:10001/onap/so/vnfm-adapter:1.4.4
+  workaround-config:
+    image: jobs/workaround-job-container:latest
     ports:
-      - "9092:9092"
+       - "9990:9990"
+    volumes:
+      - ${CONFIG_DIR_PATH}/apply-workarounds.sh:/config/apply-workarounds.sh
     environment:
-    - APP=VNFM-adapter
-    - JVM_ARGS=-Xms64m -Xmx512m
-    hostname:
-      vnfm-adapter-test
-    logging:
-      driver: "json-file"
-      options:
-        max-size: "30m"
-        max-file: "5"
+      - MYSQL_ROOT_PASSWORD=password
+      - DB_HOST=mariadb
+      - DB_PORT=3306
+      - CATALOG_DB=catalogdb
+    depends_on:
+      - mariadb
+      - VNFM-adapter
+    command: 
+      - "/config/apply-workarounds.sh"
 ################################################################################
-
diff --git a/plans/so/integration-etsi-testing/setup.sh b/plans/so/integration-etsi-testing/setup.sh
index 25e7ee7..75e7527 100755
--- a/plans/so/integration-etsi-testing/setup.sh
+++ b/plans/so/integration-etsi-testing/setup.sh
@@ -26,7 +26,11 @@
 
 SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 SCRIPT_NAME=$(basename $0)
+CONFIG_DIR=$SCRIPT_HOME/config
+ENV_FILE=$CONFIG_DIR/env
 TEMP_DIR_PATH=$SCRIPT_HOME/temp
+TEST_LAB_DIR_PATH=$TEMP_DIR_PATH/test_lab
+DOCKER_COMPOSE_FILE_PATH=$SCRIPT_HOME/docker-compose.yml
 
 MAVEN_DIR=$TEMP_DIR_PATH/maven
 INSTALLED_MAVEN_DIR=$MAVEN_DIR/$MAVEN_VERSION_DIR
@@ -38,6 +42,8 @@
 
 echo "Running $SCRIPT_HOME/$SCRIPT_NAME ..."
 
+export $(egrep -v '^#' $ENV_FILE | xargs)
+
 if [[ ! "$TEMP_DIR_PATH" || ! -d "$TEMP_DIR_PATH" ]]; then
         echo "Creating temporary directory $TEMP_DIR_PATH"
         mkdir $TEMP_DIR_PATH
@@ -99,13 +105,30 @@
 echo "Will build simulator project using $MVN_CLEAN_INSTALL -f $SIMULATOR_MAVEN_PROJECT_POM --settings $MVN_SETTINGS_XML"
 $MVN_CLEAN_INSTALL -f $SIMULATOR_MAVEN_PROJECT_POM --settings $MVN_SETTINGS_XML
 
-export NEXUS_DOCKER_REPO_MSO=nexus3.onap.org:10001
-export TAG=1.4.0-STAGING-latest
+if [ $? -ne 0 ]; then
+        echo "Maven build failed"
+        exit 1
+fi
 
-docker-compose up -d
+echo "Will clone docker-config project ... "
 
-echo "Sleeping for 2m"
-sleep 2m
+
+if [[ -d "$TEST_LAB_DIR_PATH" ]]; then
+       echo "$TEST_LAB_DIR_PATH already exists"
+       echo "Removing $TEST_LAB_DIR_PATH directory ..."
+       rm -rf $TEST_LAB_DIR_PATH
+fi
+
+git clone http://gerrit.onap.org/r/so/docker-config.git $TEST_LAB_DIR_PATH
+
+
+export TEST_LAB_DIR=$TEST_LAB_DIR_PATH
+export CONFIG_DIR_PATH=$CONFIG_DIR
+
+docker-compose -f $DOCKER_COMPOSE_FILE_PATH up -d 
+
+echo "Sleeping for 3m"
+sleep 3m
 
 REPO_IP='127.0.0.1'
 ROBOT_VARIABLES="-v REPO_IP:${REPO_IP}"
diff --git a/plans/so/integration-etsi-testing/so-simulators/package/docker/pom.xml b/plans/so/integration-etsi-testing/so-simulators/package/docker/pom.xml
index 873fa2a..1753f4b 100644
--- a/plans/so/integration-etsi-testing/so-simulators/package/docker/pom.xml
+++ b/plans/so/integration-etsi-testing/so-simulators/package/docker/pom.xml
@@ -27,6 +27,14 @@
 
                     <images>
                         <image>
+                            <name>jobs/workaround-job-container</name>
+                            <build>
+                                <cleanup>try</cleanup>
+                                <dockerFileDir>docker-files</dockerFileDir>
+                                <dockerFile>Dockerfile.workaround-job-container</dockerFile>
+                            </build>
+                        </image>
+                        <image>
                             <name>simulators/sdc-simulator</name>
                             <build>
                                 <cleanup>try</cleanup>
diff --git a/plans/so/integration-etsi-testing/so-simulators/package/docker/src/main/docker/docker-files/Dockerfile.workaround-job-container b/plans/so/integration-etsi-testing/so-simulators/package/docker/src/main/docker/docker-files/Dockerfile.workaround-job-container
new file mode 100644
index 0000000..faf8492
--- /dev/null
+++ b/plans/so/integration-etsi-testing/so-simulators/package/docker/src/main/docker/docker-files/Dockerfile.workaround-job-container
@@ -0,0 +1,27 @@
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 Nordix Foundation.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+#      http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# 
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+#
+
+# @author Waqas Ikram (waqas.ikram@est.tech)
+
+FROM docker.io/alpine
+
+# Install packages
+RUN apk update && apk upgrade && apk add mysql-client && apk add bash
+
+RUN apk --no-cache add curl netcat-openbsd sudo nss
diff --git a/plans/so/integration-etsi-testing/teardown.sh b/plans/so/integration-etsi-testing/teardown.sh
index 8b778a5..6014a0d 100755
--- a/plans/so/integration-etsi-testing/teardown.sh
+++ b/plans/so/integration-etsi-testing/teardown.sh
@@ -19,4 +19,22 @@
 # ============LICENSE_END=========================================================
 #
 
-docker-compose down
+# @author Waqas Ikram (waqas.ikram@est.tech)
+
+SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+SCRIPT_NAME=$(basename $0)
+CONFIG_DIR=$SCRIPT_HOME/config
+ENV_FILE=$CONFIG_DIR/env
+TEMP_DIR_PATH=$SCRIPT_HOME/temp
+TEST_LAB_DIR_PATH=$TEMP_DIR_PATH/test_lab
+DOCKER_COMPOSE_FILE_PATH=$SCRIPT_HOME/docker-compose.yml
+
+echo "Running $SCRIPT_HOME/$SCRIPT_NAME ..."
+export $(egrep -v '^#' $ENV_FILE | xargs)
+export TEST_LAB_DIR=$TEST_LAB_DIR_PATH
+export CONFIG_DIR_PATH=$CONFIG_DIR
+
+
+docker-compose -f $DOCKER_COMPOSE_FILE_PATH down
+
+echo "Finished executing $SCRIPT_HOME/$SCRIPT_NAME"