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 A1 Controller API
22 ################ Test engine functions ################
24 # Create the image var used during the test
25 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
26 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
31 for oia_name in $ONAP_IMAGES_APP_NAMES; do
32 if [ "$oia_name" == "SDNC" ]; then
33 sdnc_suffix_tag="REMOTE_RELEASE_ONAP"
36 __check_and_create_image_var SDNC "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_IMAGE_BASE" "SDNC_A1_CONTROLLER_IMAGE_TAG" $sdnc_suffix_tag "$SDNC_DISPLAY_NAME"
37 __check_and_create_image_var SDNC "SDNC_DB_IMAGE" "SDNC_DB_IMAGE_BASE" "SDNC_DB_IMAGE_TAG" REMOTE_PROXY "SDNC DB"
41 # Pull image from remote repo or use locally built image
42 # arg: <pull-policy-override> <pull-policy-original>
43 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
44 # <pull-policy-original> Shall be used for images that does not allow overriding
45 # Both var may contain: 'remote', 'remote-remove' or 'local'
47 __check_and_pull_image $1 "$SDNC_DISPLAY_NAME" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
48 __check_and_pull_image $2 "SDNC DB" $SDNC_APP_NAME $SDNC_DB_IMAGE
51 # Build image (only for simulator or interfaces stubs owned by the test environment)
52 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
53 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
55 echo -e $RED" Image for app SDNC shall never be built"$ERED
58 # Generate a string for each included image using the app display name and a docker images format string
59 # arg: <docker-images-format-string> <file-to-append>
61 echo -e "$SDNC_DISPLAY_NAME\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE)" >> $2
62 echo -e "SDNC DB\t$(docker images --format $1 $SDNC_DB_IMAGE)" >> $2
65 # Scale kubernetes resources to zero
66 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
67 # This function is called for apps fully managed by the test script
68 __SDNC_kube_scale_zero() {
69 __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest SDNC
72 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
73 # This function is called for prestarted apps not managed by the test script.
74 __SDNC_kube_scale_zero_and_wait() {
75 echo -e " SDNC replicas kept as is"
78 # Delete all kube resouces for the app
79 # This function is called for apps managed by the test script.
80 __SDNC_kube_delete_all() {
81 __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest SDNC
85 # This function is called for apps managed by the test script.
86 # args: <log-dir> <file-prexix>
87 __SDNC_store_docker_logs() {
88 docker exec -t $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
91 #######################################################
95 SDNC_HOST_NAME=$LOCALHOST_NAME
96 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
97 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
98 #Docker/Kube internal path
99 if [ $RUNMODE == "KUBE" ]; then
100 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
102 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
104 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
106 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
110 echo -e $BOLD"SDNC NB protocol setting"$EBOLD
111 echo -e " Using $BOLD http $EBOLD towards SDNC"
113 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
114 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
115 if [ $RUNMODE == "KUBE" ]; then
117 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
119 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
121 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
127 echo -e $BOLD"SDNC NB protocol setting"$EBOLD
128 echo -e " Using $BOLD https $EBOLD towards SDNC"
130 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT
131 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT$SDNC_API_URL
132 if [ $RUNMODE == "KUBE" ]; then
134 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
136 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
138 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_SECURE_PORT
147 # Start the SDNC A1 Controller
149 # (Function for test scripts)
152 echo -e $BOLD"Starting $SDNC_DISPLAY_NAME"$EBOLD
154 if [ $RUNMODE == "KUBE" ]; then
156 # Check if app shall be fully managed by the test script
157 __check_included_image "SDNC"
160 # Check if app shall only be used by the testscipt
161 __check_prestarted_image "SDNC"
164 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
165 echo -e $RED"The $SDNC_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
166 echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
169 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
170 echo -e $RED"The $SDNC_APP_NAME app is included both as managed and prestarted in this test script"$ERED
171 echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
176 if [ $retcode_p -eq 0 ]; then
177 echo -e " Using existing $SDNC_APP_NAME deployment and service"
178 echo " Setting SDNC replicas=1"
179 __kube_scale deployment $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
182 # Check if app shall be fully managed by the test script
183 if [ $retcode_i -eq 0 ]; then
185 echo -e " Creating $SDNC_APP_NAME app and expose service"
187 #Check if nonrtric namespace exists, if not create it
188 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
190 export KUBE_NONRTRIC_NAMESPACE
192 export SDNC_A1_CONTROLLER_IMAGE
193 export SDNC_INTERNAL_PORT
194 export SDNC_EXTERNAL_PORT
195 export SDNC_INTERNAL_SECURE_PORT
196 export SDNC_EXTERNAL_SECURE_PORT
197 export SDNC_A1_TRUSTSTORE_PASSWORD
198 export SDNC_DB_APP_NAME
204 input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"svc.yaml
205 output_yaml=$PWD/tmp/sdnc_svc.yaml
206 __kube_create_instance service $SDNC_APP_NAME $input_yaml $output_yaml
209 input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"$SDNC_KUBE_APP_FILE
210 output_yaml=$PWD/tmp/sdnc_app.yaml
211 __kube_create_instance app $SDNC_APP_NAME $input_yaml $output_yaml
215 echo " Retrieving host and ports for service..."
216 SDNC_HOST_NAME=$(__kube_get_service_host $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
217 SDNC_EXTERNAL_PORT=$(__kube_get_service_port $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
218 SDNC_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
220 echo " Host IP, http port, https port: $SDNC_HOST_NAME $SDNC_EXTERNAL_PORT $SDNC_EXTERNAL_SECURE_PORT"
222 if [ $SDNC_HTTPX == "http" ]; then
223 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
224 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
226 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
228 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
230 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT
231 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT$SDNC_API_URL
233 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
235 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
238 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
241 __check_included_image 'SDNC'
242 if [ $? -eq 1 ]; then
243 echo -e $RED"The SDNC A1 Controller app is not included in this test script"$ERED
244 echo -e $RED"The Policy Agent will not be started"$ERED
248 export SDNC_DB_APP_NAME
250 export SDNC_INTERNAL_PORT
251 export SDNC_EXTERNAL_PORT
252 export SDNC_INTERNAL_SECURE_PORT
253 export SDNC_EXTERNAL_SECURE_PORT
254 export SDNC_A1_TRUSTSTORE_PASSWORD
255 export DOCKER_SIM_NWNAME
256 export SDNC_DISPLAY_NAME
260 __start_container $SDNC_COMPOSE_DIR $SDNC_COMPOSE_FILE NODOCKERARGS 1 $SDNC_APP_NAME
262 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
268 # Check the agent logs for WARNINGs and ERRORs
270 # (Function for test scripts)
272 __check_container_logs "SDNC A1 Controller" $SDNC_APP_NAME $SDNC_KARAF_LOG WARN ERROR
275 # Generic function to query the RICs via the A1-controller API.
276 # args: <operation> <url> [<body>]
277 # <operation>: getA1Policy,putA1Policy,getA1PolicyType,deleteA1Policy,getA1PolicyStatus
278 # response: <json-body><3-digit-response-code>
279 # (Not for test scripts)
280 __do_curl_to_controller() {
281 echo " (${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
282 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
284 echo "-Incorrect number of parameters to __do_curl_to_controller " $@ >> $HTTPLOG
285 echo "-Expected: <operation> <url> [<body>]" >> $HTTPLOG
286 echo "-Returning response 000" >> $HTTPLOG
290 if [ $# -eq 2 ]; then
291 json='{"input":{"near-rt-ric-url":"'$2'"}}'
293 # Escape quotes in the body
294 body=$(echo "$3" | sed 's/"/\\"/g')
295 json='{"input":{"near-rt-ric-url":"'$2'","body":"'"$body"'"}}'
297 payload="./tmp/.sdnc.payload.json"
298 echo "$json" > $payload
299 echo " FILE ($payload) : $json" >> $HTTPLOG
301 if [ $RUNMODE == "KUBE" ]; then
302 if [ ! -z "$CLUSTER_KUBE_PROXY_NODEPORT" ]; then
303 proxyflag=" --proxy http://localhost:$CLUSTER_KUBE_PROXY_NODEPORT"
306 curlString="curl -skw %{http_code} $proxyflag -X POST $SDNC_API_PATH$1 -H accept:application/json -H Content-Type:application/json --data-binary @$payload"
307 echo " CMD: "$curlString >> $HTTPLOG
310 echo " RESP: "$res >> $HTTPLOG
311 if [ $retcode -ne 0 ]; then
312 echo " RETCODE: "$retcode >> $HTTPLOG
317 status=${res:${#res}-3}
319 if [ $status -ne 200 ]; then
323 body=${res:0:${#res}-3}
324 echo " JSON: "$body >> $HTTPLOG
325 reply="./tmp/.sdnc-reply.json"
326 echo "$body" > $reply
327 res=$(python3 ../common/extract_sdnc_reply.py $SDNC_RESPONSE_JSON_KEY $reply)
328 echo " EXTRACED BODY+CODE: "$res >> $HTTPLOG
333 # Controller API Test function: getA1Policy (return ids only)
334 # arg: <response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )
335 # (Function for test scripts)
336 controller_api_get_A1_policy_ids() {
340 if [ $RUNMODE == "KUBE" ]; then
341 ric_id=$(get_kube_sim_host $3)
344 if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
345 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies"
347 elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
348 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies"
352 if [ $paramError -ne 0 ]; then
353 __print_err "<response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )" $@
357 res=$(__do_curl_to_controller getA1Policy "$url")
359 status=${res:${#res}-3}
361 if [ $retcode -ne 0 ]; then
362 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
366 if [ $status -ne $1 ]; then
367 __log_test_fail_status_code $1 $status
370 body=${res:0:${#res}-3}
374 if [ $2 == "OSC" ]; then
377 for pid in ${@:$start} ; do
378 if [ "$targetJson" != "[" ]; then
379 targetJson=$targetJson","
381 targetJson=$targetJson"\"$UUID$pid\""
383 targetJson=$targetJson"]"
385 echo " TARGET JSON: $targetJson" >> $HTTPLOG
387 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
389 if [ $res -ne 0 ]; then
399 # Controller API Test function: getA1PolicyType
400 # arg: <response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]
401 # (Function for test scripts)
402 controller_api_get_A1_policy_type() {
406 if [ $RUNMODE == "KUBE" ]; then
407 ric_id=$(get_kube_sim_host $3)
410 if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
411 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4"
415 if [ $paramError -ne 0 ]; then
416 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
420 res=$(__do_curl_to_controller getA1PolicyType "$url")
422 status=${res:${#res}-3}
424 if [ $retcode -ne 0 ]; then
425 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
429 if [ $status -ne $1 ]; then
430 __log_test_fail_status_code $1 $status
433 body=${res:0:${#res}-3}
435 if [ $# -eq 5 ]; then
437 body=${res:0:${#res}-3}
440 echo " TARGET JSON: $targetJson" >> $HTTPLOG
441 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
443 if [ $res -ne 0 ]; then
453 # Controller API Test function: deleteA1Policy
454 # arg: <response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)
455 # (Function for test scripts)
456 controller_api_delete_A1_policy() {
460 if [ $RUNMODE == "KUBE" ]; then
461 ric_id=$(get_kube_sim_host $3)
464 if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
465 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
467 elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
468 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
472 if [ $paramError -ne 0 ]; then
473 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
477 res=$(__do_curl_to_controller deleteA1Policy "$url")
479 status=${res:${#res}-3}
481 if [ $retcode -ne 0 ]; then
482 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
486 if [ $status -ne $1 ]; then
487 __log_test_fail_status_code $1 $status
495 # Controller API Test function: putA1Policy
496 # arg: <response-code> (STD <ric-id> <policy-id> <template-file> ) | (OSC <ric-id> <policy-type-id> <policy-id> <template-file>)
497 # (Function for test scripts)
498 controller_api_put_A1_policy() {
502 if [ $RUNMODE == "KUBE" ]; then
503 ric_id=$(get_kube_sim_host $3)
506 if [ $# -eq 6 ] && [ $2 == "OSC" ]; then
507 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
508 body=$(sed 's/XXX/'${5}'/g' $6)
511 elif [ $# -eq 5 ] && [ $2 == "STD" ]; then
512 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
513 body=$(sed 's/XXX/'${4}'/g' $5)
517 if [ $paramError -ne 0 ]; then
518 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
522 res=$(__do_curl_to_controller putA1Policy "$url" "$body")
524 status=${res:${#res}-3}
526 if [ $retcode -ne 0 ]; then
527 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
531 if [ $status -ne $1 ]; then
532 __log_test_fail_status_code $1 $status
541 # Controller API Test function: getA1PolicyStatus
542 # arg: <response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)
543 # (Function for test scripts)
544 controller_api_get_A1_policy_status() {
548 if [ $RUNMODE == "KUBE" ]; then
549 ric_id=$(get_kube_sim_host $3)
553 if [ $# -ge 5 ] && [ $2 == "OSC" ]; then
554 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5/status"
555 if [ $# -gt 5 ]; then
556 targetJson="{\"instance_status\":\"$6\""
557 targetJson=$targetJson",\"has_been_deleted\":\"$7\""
558 targetJson=$targetJson",\"created_at\":\"????\"}"
561 elif [ $# -ge 4 ] && [ $2 == "STD" ]; then
562 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4/status"
563 if [ $# -gt 4 ]; then
564 targetJson="{\"enforceStatus\":\"$5\""
565 if [ $# -eq 6 ]; then
566 targetJson=$targetJson",\"reason\":\"$6\""
568 targetJson=$targetJson"}"
573 if [ $paramError -ne 0 ]; then
574 __print_err "<response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)" $@
578 res=$(__do_curl_to_controller getA1PolicyStatus "$url")
580 status=${res:${#res}-3}
582 if [ $retcode -ne 0 ]; then
583 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
587 if [ $status -ne $1 ]; then
588 __log_test_fail_status_code $1 $status
592 if [ ! -z "$targetJson" ]; then
594 body=${res:0:${#res}-3}
595 echo " TARGET JSON: $targetJson" >> $HTTPLOG
596 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
598 if [ $res -ne 0 ]; then