blob: 4200a361f34519ccb1284320105124f23d006859 [file] [log] [blame]
waqas.ikram7d753292019-07-18 16:41:17 +00001#!/bin/bash
2#
3# ============LICENSE_START=======================================================
4# Copyright (C) 2019 Nordix Foundation.
5# ================================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# SPDX-License-Identifier: Apache-2.0
19# ============LICENSE_END=========================================================
20#
21
22# @author Waqas Ikram (waqas.ikram@est.tech)
23
24BUIDLING_BLOCK_TABLE_NAME="building_block_detail"
25ORCH_FLOW_TABLE_NAME="orchestration_flow_reference"
26NORTH_BOUND_TABLE_NAME="northbound_request_ref_lookup"
27TABLE_EXISTS_QUERY="select count(*) from information_schema.tables WHERE table_schema='$CATALOG_DB' AND table_name='$BUIDLING_BLOCK_TABLE_NAME';"
28BUILDING_BLOCK_COUNT_QUERY="select count(*) from $BUIDLING_BLOCK_TABLE_NAME;"
29FLY_WAY_MIGRATION_QUERY="SELECT COUNT(*) FROM flyway_schema_history WHERE script LIKE '%R__MacroData%' AND installed_on IS NOT NULL;"
waqas.ikrambfdec072019-07-19 16:41:37 +000030SLEEP_TIME=5
31TIME_OUT_DEFAULT_VALUE_SEC=1200 #20 mins
waqas.ikram5fee4482019-07-22 13:56:13 +000032SCRIPT_NAME=$(basename $0)
waqas.ikram7d753292019-07-18 16:41:17 +000033
34current_timestamp()
35{
36 date +"%Y-%m-%d %H:%M:%S"
37}
38
39wait_for_database_availability()
40{
waqas.ikram5fee4482019-07-22 13:56:13 +000041 echo "$SCRIPT_NAME $(current_timestamp): Checking for database availability"
waqas.ikram7d753292019-07-18 16:41:17 +000042 until echo '\q' | mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB; do
waqas.ikram5fee4482019-07-22 13:56:13 +000043 >&2 echo "$SCRIPT_NAME $(current_timestamp): Database is unavailable - sleeping for ${SLEEP_TIME} seconds"
waqas.ikrambfdec072019-07-19 16:41:37 +000044 isTimeOut
45 sleep ${SLEEP_TIME}
waqas.ikram7d753292019-07-18 16:41:17 +000046 done
47
waqas.ikram5fee4482019-07-22 13:56:13 +000048 echo "$SCRIPT_NAME $(current_timestamp): Database is available now"
waqas.ikram7d753292019-07-18 16:41:17 +000049}
50
51wait_container_to_create_table()
52{
53 while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$TABLE_EXISTS_QUERY") -eq "0" ] ; do
waqas.ikram5fee4482019-07-22 13:56:13 +000054 echo "$SCRIPT_NAME $(current_timestamp): Waiting for so-catalog container to create tables - sleeping for ${SLEEP_TIME} seconds"
waqas.ikrambfdec072019-07-19 16:41:37 +000055 isTimeOut
56 sleep ${SLEEP_TIME}
waqas.ikram7d753292019-07-18 16:41:17 +000057 done
58
59 while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$BUILDING_BLOCK_COUNT_QUERY") -eq "0" ] ; do
waqas.ikram5fee4482019-07-22 13:56:13 +000060 echo "$SCRIPT_NAME $(current_timestamp): Waiting for so-catalog container to insert records in $BUIDLING_BLOCK_TABLE_NAME - sleeping for ${SLEEP_TIME} seconds"
waqas.ikrambfdec072019-07-19 16:41:37 +000061 isTimeOut
62 sleep ${SLEEP_TIME}
waqas.ikram7d753292019-07-18 16:41:17 +000063 done
64
waqas.ikram5fee4482019-07-22 13:56:13 +000065 echo "$SCRIPT_NAME $(current_timestamp): $CATALOG_DB tables available now . . ."
waqas.ikram7d753292019-07-18 16:41:17 +000066}
67
68wait_for_flyway_migration_to_finish()
69{
70 while [ $(mysql -h $DB_HOST -P $DB_PORT -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB -sse "$FLY_WAY_MIGRATION_QUERY") -ne "1" ] ; do
waqas.ikram5fee4482019-07-22 13:56:13 +000071 echo "$SCRIPT_NAME $(current_timestamp): Waiting for flyway migration sql statement to finish with success - sleeping for ${SLEEP_TIME} seconds"
waqas.ikrambfdec072019-07-19 16:41:37 +000072 isTimeOut
73 sleep ${SLEEP_TIME}
waqas.ikram7d753292019-07-18 16:41:17 +000074 done
waqas.ikram5fee4482019-07-22 13:56:13 +000075 echo "$SCRIPT_NAME $(current_timestamp): flyway migration finished . . . "
waqas.ikram7d753292019-07-18 16:41:17 +000076}
77
78
79apply_workaround()
80{
waqas.ikram5fee4482019-07-22 13:56:13 +000081 echo "$SCRIPT_NAME $(current_timestamp): Applying workaround . . ."
waqas.ikram7d753292019-07-18 16:41:17 +000082
83 wait_for_database_availability
84 wait_container_to_create_table
85 wait_for_flyway_migration_to_finish
86
waqas.ikram5fee4482019-07-22 13:56:13 +000087 echo "$SCRIPT_NAME $(current_timestamp): Will insert data into $CATALOG_DB"
waqas.ikram7d753292019-07-18 16:41:17 +000088mysql -h $DB_HOST -uroot -p$MYSQL_ROOT_PASSWORD $CATALOG_DB << EOF
89 BEGIN;
90 INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfInstantiateBB", "VNF", "ACTIVATE");
91 INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfDeleteBB", "VNF", "DEACTIVATE");
92
93 DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Create";
94
95 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";
96
97 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";
98
99 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";
100
101 DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Delete";
102
103 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";
104
105 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";
106
107 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';
108
109 COMMIT;
110EOF
111
112 if [ $? -ne 0 ]; then
waqas.ikram5fee4482019-07-22 13:56:13 +0000113 echo "$SCRIPT_NAME $(current_timestamp): Failed to execute workaround . . ."
waqas.ikram7d753292019-07-18 16:41:17 +0000114 exit 1
115 fi
116
waqas.ikram5fee4482019-07-22 13:56:13 +0000117 echo "$SCRIPT_NAME $(current_timestamp): Finished applying workaround . . ."
waqas.ikram7d753292019-07-18 16:41:17 +0000118}
119
waqas.ikrambfdec072019-07-19 16:41:37 +0000120isTimeOut()
121{
waqas.ikram5fee4482019-07-22 13:56:13 +0000122 if [ `date +%s` -gt $TIME_OUT_END_TIME_IN_SECONDS ]; then
123 echo "$SCRIPT_NAME $(current_timestamp): workaround script timed out . . ."
waqas.ikrambfdec072019-07-19 16:41:37 +0000124 exit 1;
125 fi
126}
127
waqas.ikram7d753292019-07-18 16:41:17 +0000128# main body
waqas.ikrambfdec072019-07-19 16:41:37 +0000129if [ -z "$TIME_OUT_IN_SECONDS"]; then
waqas.ikram5fee4482019-07-22 13:56:13 +0000130 echo "$SCRIPT_NAME $(current_timestamp): TIME_OUT_IN_SECONDS attribute is empty will use default val: $TIME_OUT_DEFAULT_VALUE_SEC"
waqas.ikrambfdec072019-07-19 16:41:37 +0000131 TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC
132fi
133
134DIGITS_REGEX='^[0-9]+$'
135if ! [[ $TIME_OUT_IN_SECONDS =~ $DIGIT_REGEX ]] ; then
waqas.ikram5fee4482019-07-22 13:56:13 +0000136 echo "$SCRIPT_NAME $(current_timestamp): TIME_OUT_IN_SECONDS attribute Must be number: $TIME_OUT_IN_SECONDS, will use default val: $TIME_OUT_DEFAULT_VALUE_SEC"
waqas.ikrambfdec072019-07-19 16:41:37 +0000137 TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC
138fi
139
waqas.ikram5fee4482019-07-22 13:56:13 +0000140START_TIME_IN_SECONDS=`date +%s`
141TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_IN_SECONDS));
142echo "$SCRIPT_NAME $(current_timestamp): Workaround script will time out at `date -d @$TIME_OUT_END_TIME_IN_SECONDS`"
waqas.ikrambfdec072019-07-19 16:41:37 +0000143
waqas.ikram7d753292019-07-18 16:41:17 +0000144apply_workaround
145