Populate AAI Simulator Docker Job

Change-Id: I91f8e5c7b21930974f0a7264c9735c4b0375026a
Issue-ID: SO-2182
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
diff --git a/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/customer.json b/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/customer.json
new file mode 100644
index 0000000..6c53c05
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/customer.json
@@ -0,0 +1,73 @@
+{
+    "global-customer-id": "DemoCustomer",
+    "subscriber-name": "DemoCustomer",
+    "subscriber-type": "INFRA",
+    "service-subscriptions": {
+        "service-subscription": [
+            {
+                "service-type": "vLB",
+                "relationship-list": {
+                    "relationship": [
+                        {
+                            "related-to": "tenant",
+                            "relationship-label": "org.onap.relationships.inventory.Uses",
+                            "related-link": "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/xyzcloud/tenants/tenant/693c7729b2364a26a3ca602e6f66187d",
+                            "relationship-data": [
+                                {
+                                    "relationship-key": "cloud-region.cloud-owner",
+                                    "relationship-value": "CloudOwner"
+                                },
+                                {
+                                    "relationship-key": "cloud-region.cloud-region-id",
+                                    "relationship-value": "xyzcloud"
+                                },
+                                {
+                                    "relationship-key": "tenant.tenant-id",
+                                    "relationship-value": "693c7729b2364a26a3ca602e6f66187d"
+                                }
+                            ],
+                            "related-to-property": [
+                                {
+                                    "property-key": "tenant.tenant-name",
+                                    "property-value": "admin"
+                                }
+                            ]
+                        }
+                    ]
+                }
+            },
+            {
+                "service-type": "vCPE",
+                "relationship-list": {
+                    "relationship": [
+                        {
+                            "related-to": "tenant",
+                            "relationship-label": "org.onap.relationships.inventory.Uses",
+                            "related-link": "/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/xyzcloud/tenants/tenant/693c7729b2364a26a3ca602e6f66187d",
+                            "relationship-data": [
+                                {
+                                    "relationship-key": "cloud-region.cloud-owner",
+                                    "relationship-value": "CloudOwner"
+                                },
+                                {
+                                    "relationship-key": "cloud-region.cloud-region-id",
+                                    "relationship-value": "xyzcloud"
+                                },
+                                {
+                                    "relationship-key": "tenant.tenant-id",
+                                    "relationship-value": "693c7729b2364a26a3ca602e6f66187d"
+                                }
+                            ],
+                            "related-to-property": [
+                                {
+                                    "property-key": "tenant.tenant-name",
+                                    "property-value": "admin"
+                                }
+                            ]
+                        }
+                    ]
+                }
+            }
+        ]
+    }
+}
diff --git a/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/owning-entity.json b/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/owning-entity.json
new file mode 100644
index 0000000..7248d41
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/owning-entity.json
@@ -0,0 +1,4 @@
+{
+    "owning-entity-id": "oe_1",
+    "owning-entity-name": "oe_2"
+}
diff --git a/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/project.json b/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/project.json
new file mode 100644
index 0000000..146a1e2
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/aai-simulator-populate-data/project.json
@@ -0,0 +1,4 @@
+{
+    "project-name": "etsiCsitProject"
+}
+
diff --git a/plans/so/integration-etsi-testing/config/populate-aai-simulator.sh b/plans/so/integration-etsi-testing/config/populate-aai-simulator.sh
new file mode 100755
index 0000000..1a00f71
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/populate-aai-simulator.sh
@@ -0,0 +1,99 @@
+#!/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 Gareth Roper (gareth.roper@est.tech)
+# @auther Waqas Ikram (waqas.ikram@est.tech)
+
+SCRIPT_NAME=$(basename $0)
+SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+WAIT_FOR_SCRIPT=$SCRIPT_HOME/wait-for.sh
+
+current_timestamp()
+{
+ date +"%Y-%m-%d %H:%M:%S"
+}
+
+populate_aai_simulator()
+{
+ $WAIT_FOR_SCRIPT -t "$TIMEOUT_IN_SECONDS" -h "$AAI_SIMULATOR_HOST" -p "$AAI_SIMULATOR_PORT"
+
+ if [ $?  -eq 0 ]
+ then
+     echo "$SCRIPT_NAME $(current_timestamp): AAI Simulator is Running."
+ else
+     echo "$SCRIPT_NAME $(current_timestamp): AAI Simulator could not be found. Exiting..."
+     exit 1
+ fi
+
+ BASE_URL="https://$AAI_SIMULATOR_HOST:$AAI_SIMULATOR_PORT/aai/v15"
+ BASIC_AUTHORIZATION_HEADER="Authorization: Basic YWFpOmFhaS5vbmFwLm9yZzpkZW1vMTIzNDU2IQ=="
+ APPICATION_JSON="application/json"
+ ACCEPT_HEADER="Accept: $APPICATION_JSON"
+ CONTENT_TYPE_HEADER="Content-Type: $APPICATION_JSON"
+ CURL_COMMAND="curl -k -H $BASIC_AUTHORIZATION_HEADER -H $ACCEPT_HEADER -H $CONTENT_TYPE_HEADER"
+
+ AAI_SIMULATOR_DATA_DIR=$SCRIPT_HOME/aai-simulator-populate-data
+ CUSTOMER_JSON_FILE=$AAI_SIMULATOR_DATA_DIR/customer.json
+ PROJECT_JSON_FILE=$AAI_SIMULATOR_DATA_DIR/project.json
+ OWNING_ENTITY_JSON_FILE=$AAI_SIMULATOR_DATA_DIR/owning-entity.json
+ STATUS_CODE_ACCEPTED="202"
+
+ echo "$SCRIPT_NAME $(current_timestamp): checking health of AAI Simulator"
+ response=$(curl -k $BASE_URL/healthcheck)
+
+ if [[ "$response" -ne "healthy" ]] ; then
+       echo "$SCRIPT_NAME $(current_timestamp) ERROR: AAI Simulator health check failed. Response: $response"
+       exit 1
+ fi
+
+ echo "$SCRIPT_NAME $(current_timestamp): AAI Simulator is healthy"
+
+ echo "$SCRIPT_NAME $(current_timestamp): Populating AAI Simulator"
+
+ echo "$SCRIPT_NAME $(current_timestamp): Adding Cloud-Customer Data"
+ status_code=$(curl -k --write-out %{http_code} --silent --output /dev/null -H "$BASIC_AUTHORIZATION_HEADER" -H "$ACCEPT_HEADER" -H "$CONTENT_TYPE_HEADER" $BASE_URL/business/customers/customer/DemoCustomer -X PUT -d @"$CUSTOMER_JSON_FILE")
+
+ if [[ "$status_code" -ne "$STATUS_CODE_ACCEPTED" ]] ; then
+     echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to put customer data in AAI Simulator. Status code received: $status_code"
+     exit 1
+ fi
+
+ echo "$SCRIPT_NAME $(current_timestamp): Adding Project"
+ status_code=$(curl -k --write-out %{http_code} --silent --output /dev/null -H "$BASIC_AUTHORIZATION_HEADER" -H "$ACCEPT_HEADER" -H "$CONTENT_TYPE_HEADER" $BASE_URL/business/projects/project/etsiCsitProject -X PUT -d @"$PROJECT_JSON_FILE")
+
+ if [[ "$status_code" -ne "$STATUS_CODE_ACCEPTED" ]] ; then
+     echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to put project data in AAI Simulator. Status code received: $status_code"
+     exit 1
+ fi
+
+  echo "$SCRIPT_NAME $(current_timestamp): Adding Owning-Entity"
+  status_code=$(curl -k --write-out %{http_code} --silent --output /dev/null -H "$BASIC_AUTHORIZATION_HEADER" -H "$ACCEPT_HEADER" -H "$CONTENT_TYPE_HEADER" $BASE_URL/business/owning-entities/owning-entity/oe_1 -X PUT -d @$"$OWNING_ENTITY_JSON_FILE")
+
+  if [[ "$status_code" -ne "$STATUS_CODE_ACCEPTED" ]] ; then
+     echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to put owning entity data in AAI Simulator. Status code received: $status_code"
+     exit 1
+ fi
+
+ echo "$SCRIPT_NAME $(current_timestamp): AAI Simulator Populated Successfully"
+}
+
+# main body
+populate_aai_simulator
diff --git a/plans/so/integration-etsi-testing/config/wait-for-aai-config-job.sh b/plans/so/integration-etsi-testing/config/wait-for-aai-config-job.sh
new file mode 100755
index 0000000..58a0781
--- /dev/null
+++ b/plans/so/integration-etsi-testing/config/wait-for-aai-config-job.sh
@@ -0,0 +1,84 @@
+#!/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)
+
+SLEEP_TIME=5
+SUCCESSFUL_TEXT="AAI Simulator Populated Successfully"
+FAILURE_TEXT="ERROR:"
+TIME_OUT_TEXT="Time out"
+CONTAINER_NAME=$(docker ps -aqf "name=populate-aai-config" --format "{{.Names}}")
+SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+SCRIPT_NAME=$(basename $0)
+
+current_timestamp()
+{
+ date +"%Y-%m-%d %H:%M:%S"
+}
+
+# main body
+if [ -z $TIME_OUT_DEFAULT_VALUE_SEC ]; then
+    echo "$SCRIPT_NAME $(current_timestamp): ERROR: Undefined value for TIME_OUT_DEFAULT_VALUE_SEC attribute"
+    exit 1
+fi
+
+if [ -z $CONTAINER_NAME ]; then
+   echo "$SCRIPT_NAME $(current_timestamp): Unable to find docker container id "
+   exit 1
+fi
+
+START_TIME_IN_SECONDS=`date +%s`
+TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_DEFAULT_VALUE_SEC));
+
+
+echo echo "$SCRIPT_NAME $(current_timestamp): $SCRIPT_NAME script Start Time `date -d @$START_TIME_IN_SECONDS`"
+echo echo "$SCRIPT_NAME $(current_timestamp): $SCRIPT_NAME will time out at `date -d @$TIME_OUT_END_TIME_IN_SECONDS`"
+
+while [ `date +%s` -lt "$TIME_OUT_END_TIME_IN_SECONDS" ]; do
+    echo "$(current_timestamp): Waiting for $CONTAINER_NAME to finish ..."
+
+    result=$(docker logs $CONTAINER_NAME 2>&1 | grep -E "$SUCCESSFUL_TEXT|$FAILURE_TEXT|$TIME_OUT_TEXT")
+    if [ ! -z "$result" ]; then
+        echo "$SCRIPT_NAME $(current_timestamp): Found result: $result"
+        break;
+    fi
+    echo "$(current_timestamp): Sleeping for ${SLEEP_TIME} seconds"
+    sleep ${SLEEP_TIME}
+done
+
+if [ -z "$result" ]; then
+   echo "$SCRIPT_NAME $(current_timestamp): ERROR: failed to populate AAI Simulator . . . "
+   echo "-------------- $CONTAINER_NAME logs -------------"
+   docker logs $CONTAINER_NAME
+   echo "------------------------------------------------------------"
+   exit 1
+fi
+
+if echo "$result" | grep -E "$FAILURE_TEXT|$TIME_OUT_TEXT"; then
+    echo "$SCRIPT_NAME $(current_timestamp): populate-aai-simulator.sh failed"
+    echo "-------------- $CONTAINER_NAME logs -------------"
+    docker logs $CONTAINER_NAME
+    echo "------------------------------------------------------------"
+    exit 1
+fi
+
+echo "$SCRIPT_NAME $(current_timestamp): Successfully populated AAI Simulator . . ."
+exit 0
diff --git a/plans/so/integration-etsi-testing/config/wait-for-workaround-job.sh b/plans/so/integration-etsi-testing/config/wait-for-workaround-job.sh
index ae6c2f5..035c5e2 100755
--- a/plans/so/integration-etsi-testing/config/wait-for-workaround-job.sh
+++ b/plans/so/integration-etsi-testing/config/wait-for-workaround-job.sh
@@ -1,4 +1,4 @@
-
+#!/bin/bash
 #
 # ============LICENSE_START=======================================================
 #  Copyright (C) 2019 Nordix Foundation.
diff --git a/plans/so/integration-etsi-testing/docker-compose.yml b/plans/so/integration-etsi-testing/docker-compose.yml
index 22fc221..69fb685 100644
--- a/plans/so/integration-etsi-testing/docker-compose.yml
+++ b/plans/so/integration-etsi-testing/docker-compose.yml
@@ -361,4 +361,20 @@
         max-size: "30m"
         max-file: "5"
 ################################################################################
-
+  populate-aai-config:
+    image: jobs/workaround-job-container:latest
+    ports:
+       - "9995:9995"
+    volumes:
+      - ${CONFIG_DIR_PATH}/populate-aai-simulator.sh:/config/populate-aai-simulator.sh
+      - ${CONFIG_DIR_PATH}/wait-for.sh:/config/wait-for.sh
+      - ${CONFIG_DIR_PATH}/aai-simulator-populate-data:/config/aai-simulator-populate-data/
+    environment:
+      - AAI_SIMULATOR_HOST=aai-simulator
+      - AAI_SIMULATOR_PORT=9993
+      - TIMEOUT_IN_SECONDS=300 #5 mins
+    depends_on:
+      - aai-simulator
+    command: 
+        - "/config/populate-aai-simulator.sh"
+################################################################################
diff --git a/plans/so/integration-etsi-testing/setup.sh b/plans/so/integration-etsi-testing/setup.sh
index 597bf2e..52ef26d 100755
--- a/plans/so/integration-etsi-testing/setup.sh
+++ b/plans/so/integration-etsi-testing/setup.sh
@@ -40,6 +40,7 @@
 MVN_CLEAN_INSTALL="$MVN clean install"
 SIMULATOR_MAVEN_PROJECT_POM="$SCRIPT_HOME/so-simulators/pom.xml"
 WAIT_FOR_WORKAROUND_SCRIPT=$CONFIG_DIR/"wait-for-workaround-job.sh"
+WAIT_FOR_POPULATE_AAI_SCRIPT=$CONFIG_DIR/"wait-for-aai-config-job.sh"
 
 echo "Running $SCRIPT_HOME/$SCRIPT_NAME ..."
 
@@ -140,6 +141,15 @@
    exit 1
 fi
 
+echo "Will execute $WAIT_FOR_POPULATE_AAI_SCRIPT script"
+$WAIT_FOR_POPULATE_AAI_SCRIPT
+
+if [ $? -ne 0 ]; then
+   echo "ERROR: $WAIT_FOR_POPULATE_AAI_SCRIPT failed"
+   echo "Will stop running docker containers . . ."
+   docker-compose -f $DOCKER_COMPOSE_FILE_PATH down
+   exit 1
+fi
 
 REPO_IP='127.0.0.1'
 ROBOT_VARIABLES="-v REPO_IP:${REPO_IP}"