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
23 SDNC_HOST_NAME=$LOCALHOST_NAME
24 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
25 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
26 #Docker/Kube internal path
27 if [ $RUNMODE == "KUBE" ]; then
28 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
30 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
32 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
34 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
38 echo -e $BOLD"SDNC NB protocol setting"$EBOLD
39 echo -e " Using $BOLD http $EBOLD towards SDNC"
41 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
42 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
43 if [ $RUNMODE == "KUBE" ]; then
45 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
47 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
49 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
55 echo -e $BOLD"SDNC NB protocol setting"$EBOLD
56 echo -e " Using $BOLD https $EBOLD towards SDNC"
58 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT
59 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT$SDNC_API_URL
60 if [ $RUNMODE == "KUBE" ]; then
62 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
64 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
66 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_SECURE_PORT
75 # Start the SDNC A1 Controller
77 # (Function for test scripts)
80 echo -e $BOLD"Starting $SDNC_DISPLAY_NAME"$EBOLD
82 if [ $RUNMODE == "KUBE" ]; then
84 # Check if app shall be fully managed by the test script
85 __check_included_image "SDNC"
88 # Check if app shall only be used by the testscipt
89 __check_prestarted_image "SDNC"
92 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
93 echo -e $RED"The $SDNC_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
94 echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
97 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
98 echo -e $RED"The $SDNC_APP_NAME app is included both as managed and prestarted in this test script"$ERED
99 echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
104 if [ $retcode_p -eq 0 ]; then
105 echo -e " Using existing $SDNC_APP_NAME deployment and service"
106 echo " Setting SDNC replicas=1"
107 __kube_scale deployment $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
110 # Check if app shall be fully managed by the test script
111 if [ $retcode_i -eq 0 ]; then
113 echo -e " Creating $SDNC_APP_NAME app and expose service"
115 #Check if nonrtric namespace exists, if not create it
116 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
118 export KUBE_NONRTRIC_NAMESPACE
120 export SDNC_A1_CONTROLLER_IMAGE
121 export SDNC_INTERNAL_PORT
122 export SDNC_EXTERNAL_PORT
123 export SDNC_INTERNAL_SECURE_PORT
124 export SDNC_EXTERNAL_SECURE_PORT
125 export SDNC_A1_TRUSTSTORE_PASSWORD
126 export SDNC_DB_APP_NAME
130 input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"svc.yaml
131 output_yaml=$PWD/tmp/sdnc_svc.yaml
132 __kube_create_instance service $SDNC_APP_NAME $input_yaml $output_yaml
135 input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"app.yaml
136 output_yaml=$PWD/tmp/sdnc_app.yaml
137 __kube_create_instance app $SDNC_APP_NAME $input_yaml $output_yaml
141 echo " Retrieving host and ports for service..."
142 SDNC_HOST_NAME=$(__kube_get_service_host $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
143 SDNC_EXTERNAL_PORT=$(__kube_get_service_port $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
144 SDNC_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
146 echo " Host IP, http port, https port: $SDNC_HOST_NAME $SDNC_EXTERNAL_PORT $SDNC_EXTERNAL_SECURE_PORT"
148 if [ $SDNC_HTTPX == "http" ]; then
149 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
150 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
152 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
154 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
156 SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT
157 SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT$SDNC_API_URL
159 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
161 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
164 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
167 __check_included_image 'SDNC'
168 if [ $? -eq 1 ]; then
169 echo -e $RED"The SDNC A1 Controller app is not included in this test script"$ERED
170 echo -e $RED"The Policy Agent will not be started"$ERED
174 export SDNC_DB_APP_NAME
176 export SDNC_INTERNAL_PORT
177 export SDNC_EXTERNAL_PORT
178 export SDNC_INTERNAL_SECURE_PORT
179 export SDNC_EXTERNAL_SECURE_PORT
180 export SDNC_A1_TRUSTSTORE_PASSWORD
181 export DOCKER_SIM_NWNAME
183 __start_container $SDNC_COMPOSE_DIR NODOCKERARGS 1 $SDNC_APP_NAME
185 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
193 # Generic function to query the RICs via the A1-controller API.
194 # args: <operation> <url> [<body>]
195 # <operation>: getA1Policy,putA1Policy,getA1PolicyType,deleteA1Policy,getA1PolicyStatus
196 # response: <json-body><3-digit-response-code>
197 # (Not for test scripts)
198 __do_curl_to_controller() {
199 echo " (${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
200 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
202 echo "-Incorrect number of parameters to __do_curl_to_controller " $@ >> $HTTPLOG
203 echo "-Expected: <operation> <url> [<body>]" >> $HTTPLOG
204 echo "-Returning response 000" >> $HTTPLOG
208 if [ $# -eq 2 ]; then
209 json='{"input":{"near-rt-ric-url":"'$2'"}}'
211 # Escape quotes in the body
212 body=$(echo "$3" | sed 's/"/\\"/g')
213 json='{"input":{"near-rt-ric-url":"'$2'","body":"'"$body"'"}}'
215 payload="./tmp/.sdnc.payload.json"
216 echo "$json" > $payload
217 echo " FILE ($payload) : $json" >> $HTTPLOG
218 curlString="curl -skw %{http_code} -X POST $SDNC_API_PATH$1 -H accept:application/json -H Content-Type:application/json --data-binary @$payload"
219 echo " CMD: "$curlString >> $HTTPLOG
222 echo " RESP: "$res >> $HTTPLOG
223 if [ $retcode -ne 0 ]; then
224 echo " RETCODE: "$retcode >> $HTTPLOG
229 status=${res:${#res}-3}
231 if [ $status -ne 200 ]; then
235 body=${res:0:${#res}-3}
236 echo " JSON: "$body >> $HTTPLOG
237 reply="./tmp/.sdnc-reply.json"
238 echo "$body" > $reply
239 res=$(python3 ../common/extract_sdnc_reply.py $reply)
240 echo " EXTRACED BODY+CODE: "$res >> $HTTPLOG
245 # Controller API Test function: getA1Policy (return ids only)
246 # arg: <response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )
247 # (Function for test scripts)
248 controller_api_get_A1_policy_ids() {
252 if [ $RUNMODE == "KUBE" ]; then
253 ric_id=$(get_kube_sim_host $3)
256 if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
257 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies"
259 elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
260 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies"
264 if [ $paramError -ne 0 ]; then
265 __print_err "<response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )" $@
269 res=$(__do_curl_to_controller getA1Policy "$url")
271 status=${res:${#res}-3}
273 if [ $retcode -ne 0 ]; then
274 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
278 if [ $status -ne $1 ]; then
279 __log_test_fail_status_code $1 $status
282 body=${res:0:${#res}-3}
286 if [ $2 == "OSC" ]; then
289 for pid in ${@:$start} ; do
290 if [ "$targetJson" != "[" ]; then
291 targetJson=$targetJson","
293 targetJson=$targetJson"\"$UUID$pid\""
295 targetJson=$targetJson"]"
297 echo " TARGET JSON: $targetJson" >> $HTTPLOG
299 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
301 if [ $res -ne 0 ]; then
311 # Controller API Test function: getA1PolicyType
312 # arg: <response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]
313 # (Function for test scripts)
314 controller_api_get_A1_policy_type() {
318 if [ $RUNMODE == "KUBE" ]; then
319 ric_id=$(get_kube_sim_host $3)
322 if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
323 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4"
327 if [ $paramError -ne 0 ]; then
328 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
332 res=$(__do_curl_to_controller getA1PolicyType "$url")
334 status=${res:${#res}-3}
336 if [ $retcode -ne 0 ]; then
337 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
341 if [ $status -ne $1 ]; then
342 __log_test_fail_status_code $1 $status
345 body=${res:0:${#res}-3}
347 if [ $# -eq 5 ]; then
349 body=${res:0:${#res}-3}
352 echo " TARGET JSON: $targetJson" >> $HTTPLOG
353 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
355 if [ $res -ne 0 ]; then
365 # Controller API Test function: deleteA1Policy
366 # arg: <response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)
367 # (Function for test scripts)
368 controller_api_delete_A1_policy() {
372 if [ $RUNMODE == "KUBE" ]; then
373 ric_id=$(get_kube_sim_host $3)
376 if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
377 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
379 elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
380 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
384 if [ $paramError -ne 0 ]; then
385 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
389 res=$(__do_curl_to_controller deleteA1Policy "$url")
391 status=${res:${#res}-3}
393 if [ $retcode -ne 0 ]; then
394 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
398 if [ $status -ne $1 ]; then
399 __log_test_fail_status_code $1 $status
407 # Controller API Test function: putA1Policy
408 # arg: <response-code> (STD <ric-id> <policy-id> <template-file> ) | (OSC <ric-id> <policy-type-id> <policy-id> <template-file>)
409 # (Function for test scripts)
410 controller_api_put_A1_policy() {
414 if [ $RUNMODE == "KUBE" ]; then
415 ric_id=$(get_kube_sim_host $3)
418 if [ $# -eq 6 ] && [ $2 == "OSC" ]; then
419 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
420 body=$(sed 's/XXX/'${5}'/g' $6)
423 elif [ $# -eq 5 ] && [ $2 == "STD" ]; then
424 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
425 body=$(sed 's/XXX/'${4}'/g' $5)
429 if [ $paramError -ne 0 ]; then
430 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
434 res=$(__do_curl_to_controller putA1Policy "$url" "$body")
436 status=${res:${#res}-3}
438 if [ $retcode -ne 0 ]; then
439 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
443 if [ $status -ne $1 ]; then
444 __log_test_fail_status_code $1 $status
453 # Controller API Test function: getA1PolicyStatus
454 # arg: <response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)
455 # (Function for test scripts)
456 controller_api_get_A1_policy_status() {
460 if [ $RUNMODE == "KUBE" ]; then
461 ric_id=$(get_kube_sim_host $3)
465 if [ $# -ge 5 ] && [ $2 == "OSC" ]; then
466 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5/status"
467 if [ $# -gt 5 ]; then
468 targetJson="{\"instance_status\":\"$6\""
469 targetJson=$targetJson",\"has_been_deleted\":\"$7\""
470 targetJson=$targetJson",\"created_at\":\"????\"}"
473 elif [ $# -ge 4 ] && [ $2 == "STD" ]; then
474 url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4/status"
475 if [ $# -gt 4 ]; then
476 targetJson="{\"enforceStatus\":\"$5\""
477 if [ $# -eq 6 ]; then
478 targetJson=$targetJson",\"reason\":\"$6\""
480 targetJson=$targetJson"}"
485 if [ $paramError -ne 0 ]; then
486 __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>)" $@
490 res=$(__do_curl_to_controller getA1PolicyStatus "$url")
492 status=${res:${#res}-3}
494 if [ $retcode -ne 0 ]; then
495 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
499 if [ $status -ne $1 ]; then
500 __log_test_fail_status_code $1 $status
504 if [ ! -z "$targetJson" ]; then
506 body=${res:0:${#res}-3}
507 echo " TARGET JSON: $targetJson" >> $HTTPLOG
508 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
510 if [ $res -ne 0 ]; then