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 RICSIM A1 simulators
23 RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
24 RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
27 #Vars for A1 interface version and container count
40 ###########################
41 ### RIC Simulator functions
42 ###########################
44 use_simulator_http() {
45 echo -e $BOLD"RICSIM protocol setting"$EBOLD
46 echo -e " Using $BOLD http $EBOLD towards the simulators"
48 RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
49 RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
53 use_simulator_https() {
54 echo -e $BOLD"RICSIM protocol setting"$EBOLD
55 echo -e " Using $BOLD https $EBOLD towards the simulators"
57 RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
58 RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
62 # Start one group (ricsim_g1, ricsim_g2 .. ricsim_g5) with a number of RIC Simulators using a given A interface
63 # 'ricsim' may be set on command line to other prefix
64 # args: ricsim_g1|ricsim_g2|ricsim_g3|ricsim_g4|ricsim_g5 <count> <interface-id>
65 # (Function for test scripts)
66 start_ric_simulators() {
68 echo -e $BOLD"Starting $RIC_SIM_DISPLAY_NAME"$EBOLD
70 if [ $RUNMODE == "KUBE" ]; then
72 # Check if app shall be fully managed by the test script
73 __check_included_image "RICSIM"
76 # Check if app shall only be used by the testscipt
77 __check_prestarted_image "RICSIM"
80 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
81 echo -e $RED"The $1 app is not included as managed nor prestarted in this test script"$ERED
82 echo -e $RED"The $1 will not be started"$ERED
85 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
86 echo -e $RED"The $1 stub app is included both as managed and prestarted in this test script"$ERED
87 echo -e $RED"The $1 will not be started"$ERED
91 if [ $retcode_p -eq 0 ]; then
92 echo -e " Using existing $1 statefulset and service"
93 echo " Using existing simulator deployment and service for statefulset $1"
94 echo " Setting $1 replicas=$2"
95 __kube_scale statefulset $1 $KUBE_NONRTRIC_NAMESPACE $2
101 RIC1=$RIC_SIM_PREFIX"_g1"
102 RIC2=$RIC_SIM_PREFIX"_g2"
103 RIC3=$RIC_SIM_PREFIX"_g3"
104 RIC4=$RIC_SIM_PREFIX"_g4"
105 RIC5=$RIC_SIM_PREFIX"_g5"
109 __print_err "need three args, $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@
113 echo " $2 simulators using basename: $1 on interface: $3"
114 #Set env var for simulator count and A1 interface vesion for the given group
115 if [ $1 == "$RIC1" ]; then
118 elif [ $1 == "$RIC2" ]; then
121 elif [ $1 == "$RIC3" ]; then
124 elif [ $1 == "$RIC4" ]; then
127 elif [ $1 == "$RIC5" ]; then
132 __print_err "need three args, $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@
136 if [ $RUNMODE == "KUBE" ]; then
138 if [ $retcode_i -eq 0 ]; then
140 #export needed env var for statefulset
141 export RIC_SIM_SET_NAME=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names
142 export KUBE_NONRTRIC_NAMESPACE
144 #Adding 1 more instance, instance 0 is never used. This is done to keep test scripts compatible
145 # with docker that starts instance index on 1.....
146 export RIC_SIM_COUNT=$(($2+1))
148 export RIC_SIM_INTERNAL_PORT
149 export RIC_SIM_INTERNAL_SECURE_PORT
151 echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service"
153 #Check if nonrtric namespace exists, if not create it
154 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
157 input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"svc.yaml
158 output_yaml=$PWD/tmp/ric_${1}_svc.yaml
159 __kube_create_instance service $RIC_SIM_SET_NAME $input_yaml $output_yaml
162 input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"app.yaml
163 output_yaml=$PWD/tmp/pa_${1}_app.yaml
164 __kube_create_instance app $RIC_SIM_SET_NAME $input_yaml $output_yaml
166 #Using only instance from index 1 to keep compatability with docker
167 for (( count=1; count<${RIC_SIM_COUNT}; count++ )); do
168 host=$(__find_sim_host $RIC_SIM_SET_NAME"-"$count)
169 __check_service_start $RIC_SIM_SET_NAME"-"$count $host"/"
174 __check_included_image 'RICSIM'
175 if [ $? -eq 1 ]; then
176 echo -e $RED"The Near-RT RIC Simulator app is not included as managed in this test script"$ERED
177 echo -e $RED"Near-RT RIC Simulator will not be started"$ERED
181 # Create .env file to compose project, all ric container will get this prefix
182 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/$RIC_SIM_COMPOSE_DIR/.env
189 export RIC_SIM_INTERNAL_PORT
190 export RIC_SIM_INTERNAL_SECURE_PORT
191 export RIC_SIM_CERT_MOUNT_DIR
192 export DOCKER_SIM_NWNAME
194 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT --scale g4=$G4_COUNT --scale g5=$G5_COUNT"
197 while [ $cntr -le $2 ]; do
199 app_data="$app_data $app"
203 __start_container $RIC_SIM_COMPOSE_DIR "$docker_args" $2 $app_data
206 while [ $cntr -le $2 ]; do
210 while [ $localport -eq 0 ]; do
211 echo -ne " Waiting for container ${app} to publish its ports...${SAMELINE}"
212 localport=$(__find_sim_port $app)
214 echo -ne " Waiting for container ${app} to publish its ports...retrying....${SAMELINE}"
216 echo -e " Waiting for container ${app} to publish its ports...retrying....$GREEN OK $EGREEN"
217 __check_service_start $app $RIC_SIM_HOST":"$localport$RIC_SIM_ALIVE_URL
227 # Excute a curl cmd towards a ricsimulator and check the response code.
228 # args: <expected-response-code> <curl-cmd-string>
229 __execute_curl_to_sim() {
230 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
231 echo " CMD: $2" >> $HTTPLOG
233 echo " RESP: $res" >> $HTTPLOG
235 if [ $retcode -ne 0 ]; then
237 echo " RETCODE: "$retcode
238 echo -e $RED" FAIL - fatal error when executing curl."$ERED
241 status=${res:${#res}-3}
242 if [ $status -eq $1 ]; then
243 echo -e $GREEN" OK"$EGREEN
246 echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
251 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
252 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
253 # equal to the target or not.
254 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
255 # before setting pass or fail depending on if the variable value becomes equal to the target
257 # (Function for test scripts)
260 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
261 host=$(__find_sim_host $1)
262 __var_test $1 "$host/counter/" $2 "=" $3 $4
265 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
270 # Print a variable value from the RIC sim.
271 # args: <ric-id> <variable-name>
272 # (Function for test scripts)
276 __print_err "need two args, <ric-id> <sim-param>" $@
279 host=$(__find_sim_host $1)
280 echo -e $BOLD"INFO(${BASH_LINENO[0]}): $1, $2 = $(__do_curl $host/counter/$2)"$EBOLD
283 # Tests if a variable value in the RIC simulator contains the target string and and optional timeout
284 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
286 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
287 # before setting pass or fail depending on if the variable value contains the target
289 # (Function for test scripts)
292 if [ $# -eq 3 ] || [ $# -eq 4 ]; then
293 host=$(__find_sim_host $1)
294 __var_test $1 "$host/counter/" $2 "contain_str" $3 $4
297 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
302 # Simulator API: Put a policy type in a ric
303 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
304 # (Function for test scripts)
305 sim_put_policy_type() {
307 if [ $# -ne 4 ]; then
308 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
311 host=$(__find_sim_host $2)
312 curlString="curl -X PUT -skw %{http_code} "$host"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4
313 __execute_curl_to_sim $1 "$curlString"
317 # Simulator API: Delete a policy type in a ric
318 # <response-code> <ric-id> <policy-type-id>
319 # (Function for test scripts)
320 sim_delete_policy_type() {
322 if [ $# -ne 3 ]; then
323 __print_err "<response-code> <ric-id> <policy_type_id>" $@
326 host=$(__find_sim_host $2)
327 curlString="curl -X DELETE -skw %{http_code} "$host"/policytype?id="$3
328 __execute_curl_to_sim $1 "$curlString"
332 # Simulator API: Delete instances (and status), for one ric
333 # <response-code> <ric-id>
334 # (Function for test scripts)
335 sim_post_delete_instances() {
337 if [ $# -ne 2 ]; then
338 __print_err "<response-code> <ric-id>" $@
341 host=$(__find_sim_host $2)
342 curlString="curl -X POST -skw %{http_code} "$host"/deleteinstances"
343 __execute_curl_to_sim $1 "$curlString"
347 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
348 # <response-code> <ric-id>
349 # (Function for test scripts)
350 sim_post_delete_all() {
352 if [ $# -ne 3 ]; then
353 __print_err "<response-code> <numericic-id>" $@
356 host=$(__find_sim_host $2)
357 curlString="curl -X POST -skw %{http_code} "$host"/deleteall"
358 __execute_curl_to_sim $1 "$curlString"
362 # Simulator API: Set (or reset) response code for next A1 message, for one ric
363 # <response-code> <ric-id> [<forced_response_code>]
364 # (Function for test scripts)
365 sim_post_forcedresponse() {
367 if [ $# -ne 3 ]; then
368 __print_err "<response-code> <ric-id> <forced_response_code>" $@
371 host=$(__find_sim_host $2)
372 curlString="curl -X POST -skw %{http_code} "$host"/forceresponse"
373 if [ $# -eq 3 ]; then
374 curlString=$curlString"?code="$3
376 __execute_curl_to_sim $1 "$curlString"
380 # Simulator API: Set (or reset) A1 response delay, for one ric
381 # <response-code> <ric-id> [<delay-in-seconds>]
382 # (Function for test scripts)
383 sim_post_forcedelay() {
385 if [ $# -ne 3 ]; then
386 __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@
389 host=$(__find_sim_host $2)
390 curlString="curl -X POST -skw %{http_code} $host/forcedelay"
391 if [ $# -eq 3 ]; then
392 curlString=$curlString"?delay="$3
394 __execute_curl_to_sim $1 "$curlString"