blob: 21a3aeb84d4d2722658041f65823c559026ac5f4 [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;
waqas.ikramc44a7532019-08-08 15:57:14 +000090
91 UPDATE $NORTH_BOUND_TABLE_NAME SET SERVICE_TYPE="*";
92
waqas.ikram7d753292019-07-18 16:41:17 +000093 INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfInstantiateBB", "VNF", "ACTIVATE");
94 INSERT INTO $BUIDLING_BLOCK_TABLE_NAME (BUILDING_BLOCK_NAME,RESOURCE_TYPE,TARGET_ACTION) values ("EtsiVnfDeleteBB", "VNF", "DEACTIVATE");
95
96 DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Create";
97
98 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";
99
100 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";
101
102 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";
103
104 DELETE FROM $ORCH_FLOW_TABLE_NAME where COMPOSITE_ACTION = "VNF-Delete";
105
106 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";
107
108 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";
109
110 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';
111
112 COMMIT;
113EOF
114
115 if [ $? -ne 0 ]; then
waqas.ikram5fee4482019-07-22 13:56:13 +0000116 echo "$SCRIPT_NAME $(current_timestamp): Failed to execute workaround . . ."
waqas.ikram7d753292019-07-18 16:41:17 +0000117 exit 1
118 fi
119
waqas.ikram5fee4482019-07-22 13:56:13 +0000120 echo "$SCRIPT_NAME $(current_timestamp): Finished applying workaround . . ."
waqas.ikram7d753292019-07-18 16:41:17 +0000121}
122
waqas.ikrambfdec072019-07-19 16:41:37 +0000123isTimeOut()
124{
waqas.ikram5fee4482019-07-22 13:56:13 +0000125 if [ `date +%s` -gt $TIME_OUT_END_TIME_IN_SECONDS ]; then
126 echo "$SCRIPT_NAME $(current_timestamp): workaround script timed out . . ."
waqas.ikrambfdec072019-07-19 16:41:37 +0000127 exit 1;
128 fi
129}
130
waqas.ikram7d753292019-07-18 16:41:17 +0000131# main body
waqas.ikrambfdec072019-07-19 16:41:37 +0000132if [ -z "$TIME_OUT_IN_SECONDS"]; then
waqas.ikram5fee4482019-07-22 13:56:13 +0000133 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 +0000134 TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC
135fi
136
137DIGITS_REGEX='^[0-9]+$'
138if ! [[ $TIME_OUT_IN_SECONDS =~ $DIGIT_REGEX ]] ; then
waqas.ikram5fee4482019-07-22 13:56:13 +0000139 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 +0000140 TIME_OUT_IN_SECONDS=$TIME_OUT_DEFAULT_VALUE_SEC
141fi
142
waqas.ikram5fee4482019-07-22 13:56:13 +0000143START_TIME_IN_SECONDS=`date +%s`
144TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_IN_SECONDS));
145echo "$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 +0000146
waqas.ikram7d753292019-07-18 16:41:17 +0000147apply_workaround
148