3 # ============LICENSE_START===============================================
4 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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 # ============LICENSE_END=================================================
20 # This is a script that contains container/service management functions and test functions for Consul/CBS
22 CONSUL_PATH="http://$LOCALHOST:$CONSUL_EXTERNAL_PORT"
28 # Function to load config from a file into consul for the Policy Agent
29 # arg: <json-config-file>
30 # (Function for test scripts)
33 echo -e $BOLD"Configuring Consul"$EBOLD
37 __print_err "need one arg, <json-config-file>" $@
41 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
43 curlString="$LOCALHOST_HTTP:${CONSUL_EXTERNAL_PORT}/v1/kv/${POLICY_AGENT_CONFIG_KEY}?dc=dc1 -X PUT -H Accept:application/json -H Content-Type:application/json -H X-Requested-With:XMLHttpRequest --data-binary @"$1
44 result=$(__do_curl "$curlString")
46 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
50 body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_CONFIG_KEY)"
51 echo $body > "./tmp/.output"$1
54 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
59 targetJson="{\"config\":"$targetJson"}"
60 echo "TARGET JSON: $targetJson" >> $HTTPLOG
61 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
62 if [ $res -ne 0 ]; then
63 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
67 echo -e $GREEN" Config loaded ok to consul"$EGREEN
75 # Function to perpare the consul configuration according to the current simulator configuration
76 # args: SDNC|NOSDNC <output-file>
77 # (Function for test scripts)
78 prepare_consul_config() {
79 echo -e $BOLD"Prepare Consul config"$EBOLD
81 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
85 __print_err "need two args, SDNC|NOSDNC <output-file>" $@
89 if [ $1 == "SDNC" ]; then
90 echo -e " Config$BOLD including SDNC$EBOLD configuration"
91 elif [ $1 == "NOSDNC" ]; then
92 echo -e " Config$BOLD excluding SDNC$EBOLD configuration"
95 __print_err "need two args, SDNC|NOSDNC <output-file>" $@
100 if [ $1 == "SDNC" ]; then
101 config_json=$config_json"\n \"controller\": ["
102 config_json=$config_json"\n {"
103 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
104 config_json=$config_json"\n \"baseUrl\": \"$SDNC_SERVICE_PATH\","
105 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
106 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
107 config_json=$config_json"\n }"
108 config_json=$config_json"\n ],"
111 config_json=$config_json"\n \"streams_publishes\": {"
112 config_json=$config_json"\n \"dmaap_publisher\": {"
113 config_json=$config_json"\n \"type\": \"message-router\","
114 config_json=$config_json"\n \"dmaap_info\": {"
115 config_json=$config_json"\n \"topic_url\": \"$MR_SERVICE_PATH$MR_WRITE_URL\""
116 config_json=$config_json"\n }"
117 config_json=$config_json"\n }"
118 config_json=$config_json"\n },"
119 config_json=$config_json"\n \"streams_subscribes\": {"
120 config_json=$config_json"\n \"dmaap_subscriber\": {"
121 config_json=$config_json"\n \"type\": \"message-router\","
122 config_json=$config_json"\n \"dmaap_info\": {"
123 config_json=$config_json"\n \"topic_url\": \"$MR_SERVICE_PATH$MR_READ_URL\""
124 config_json=$config_json"\n }"
125 config_json=$config_json"\n }"
126 config_json=$config_json"\n },"
128 config_json=$config_json"\n \"ric\": ["
130 if [ $RUNMODE == "KUBE" ]; then
131 result=$(kubectl get pods -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
134 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
135 for im in $result; do
136 if [[ $im != *"-0" ]]; then
137 ric_subdomain=$(kubectl get pod $im -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.spec.subdomain}')
138 rics=$rics" "$im"."$ric_subdomain".nonrtric"
139 let ric_cntr=ric_cntr+1
143 if [ $ric_cntr -eq 0 ]; then
144 echo $YELLOW"Warning: No rics found for the configuration"$EYELLOW
147 rics=$(docker ps | grep $RIC_SIM_PREFIX | awk '{print $NF}')
149 if [ $? -ne 0 ] || [ -z "$rics" ]; then
150 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
157 if [ $cntr -gt 0 ]; then
158 config_json=$config_json"\n ,"
160 config_json=$config_json"\n {"
161 if [ $RUNMODE == "KUBE" ]; then
162 ric_id=${ric%.*.*} #extract pod id from full hosthame
163 ric_id=$(echo "$ric_id" | tr '-' '_')
167 config_json=$config_json"\n \"name\": \"$ric_id\","
168 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
169 if [ $1 == "SDNC" ]; then
170 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
172 config_json=$config_json"\n \"managedElementIds\": ["
173 config_json=$config_json"\n \"me1_$ric_id\","
174 config_json=$config_json"\n \"me2_$ric_id\""
175 config_json=$config_json"\n ]"
176 config_json=$config_json"\n }"
180 config_json=$config_json"\n ]"
181 config_json=$config_json"\n}"
183 if [ $RUNMODE == "KUBE" ]; then
184 config_json="{\"config\":"$config_json"}"
187 printf "$config_json">$2
192 # Start Consul and CBS
194 # (Function for test scripts)
197 echo -e $BOLD"Starting $CONSUL_DISPLAY_NAME and $CBS_DISPLAY_NAME"$EBOLD
198 __check_included_image 'CONSUL'
199 if [ $? -eq 1 ]; then
200 echo -e $RED"The Consul image has not been checked for this test run due to arg to the test script"$ERED
201 echo -e $RED"Consul will not be started"$ERED
204 export CONSUL_APP_NAME
205 export CONSUL_INTERNAL_PORT
206 export CONSUL_EXTERNAL_PORT
208 export CBS_INTERNAL_PORT
209 export CBS_EXTERNAL_PORT
212 __start_container $CONSUL_CBS_COMPOSE_DIR NODOCKERARGS 2 $CONSUL_APP_NAME $CBS_APP_NAME
214 __check_service_start $CONSUL_APP_NAME "http://"$LOCALHOST_NAME":"$CONSUL_EXTERNAL_PORT$CONSUL_ALIVE_URL
215 __check_service_start $CBS_APP_NAME "http://"$LOCALHOST_NAME":"$CBS_EXTERNAL_PORT$CBS_ALIVE_URL