Merge "Integrated gateway and updated kube support"
[nonrtric.git] / test / common / controller_api_functions.sh
1 #!/bin/bash
2
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
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 #  ============LICENSE_END=================================================
18 #
19
20 # This is a script that contains container/service management functions and test functions for A1 Controller API
21
22 ################ Test engine functions ################
23
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
27 __SDNC_imagesetup() {
28
29         sdnc_suffix_tag=$1
30
31         for oia_name in $ONAP_IMAGES_APP_NAMES; do
32                 if [ "$oia_name" == "SDNC" ]; then
33                         sdnc_suffix_tag="REMOTE_RELEASE_ONAP"
34                 fi
35         done
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"
38
39 }
40
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'
46 __SDNC_imagepull() {
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
49 }
50
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
54 __SDNC_imagebuild() {
55         echo -e $RED" Image for app SDNC shall never be built"$ERED
56 }
57
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>
60 __SDNC_image_data() {
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
63 }
64
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
70 }
71
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"
76 }
77
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
82 }
83
84 # Store docker logs
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
89 }
90
91 #######################################################
92
93
94 SDNC_HTTPX="http"
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
101     #presume correct
102         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
103         #test
104         #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
105 else
106         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
107 fi
108
109 use_sdnc_http() {
110         echo -e $BOLD"SDNC NB protocol setting"$EBOLD
111         echo -e " Using $BOLD http $EBOLD towards SDNC"
112         SDNC_HTTPX="http"
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
116                 #presume correct
117                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
118                 #test
119                 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
120         else
121                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
122         fi
123         echo ""
124 }
125
126 use_sdnc_https() {
127         echo -e $BOLD"SDNC NB protocol setting"$EBOLD
128         echo -e " Using $BOLD https $EBOLD towards SDNC"
129         SDNC_HTTPX="https"
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
133                 #presume correct
134                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
135                 #test
136                 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
137         else
138                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_SECURE_PORT
139         fi
140         echo ""
141 }
142
143 ##################
144 ### SDNC functions
145 ##################
146
147 # Start the SDNC A1 Controller
148 # args: -
149 # (Function for test scripts)
150 start_sdnc() {
151
152         echo -e $BOLD"Starting $SDNC_DISPLAY_NAME"$EBOLD
153
154         if [ $RUNMODE == "KUBE" ]; then
155
156                 # Check if app shall be fully managed by the test script
157                 __check_included_image "SDNC"
158                 retcode_i=$?
159
160                 # Check if app shall only be used by the testscipt
161                 __check_prestarted_image "SDNC"
162                 retcode_p=$?
163
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
167                         exit
168                 fi
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
172                         exit
173                 fi
174
175
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
180                 fi
181
182                                 # Check if app shall be fully managed by the test script
183                 if [ $retcode_i -eq 0 ]; then
184
185                         echo -e " Creating $SDNC_APP_NAME app and expose service"
186
187                         #Check if nonrtric namespace exists, if not create it
188                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
189
190                         export KUBE_NONRTRIC_NAMESPACE
191                         export SDNC_APP_NAME
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
199                         export SDNC_DB_IMAGE
200                         export SDNC_USER
201                         export SDNC_PWD
202
203                         # Create service
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
207
208                         # Create app
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
212
213                 fi
214
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")
219
220                 echo " Host IP, http port, https port: $SDNC_HOST_NAME $SDNC_EXTERNAL_PORT $SDNC_EXTERNAL_SECURE_PORT"
221
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
225             #presume correct
226                         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
227                         #test
228                         #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
229                 else
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
232             #presume correct
233                         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
234                         #test
235                         #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
236                 fi
237
238                 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
239         else
240
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
245                         exit
246                 fi
247
248                 export SDNC_DB_APP_NAME
249         export SDNC_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
257                 export SDNC_USER
258                 export SDNC_PWD
259
260                 __start_container $SDNC_COMPOSE_DIR $SDNC_COMPOSE_FILE NODOCKERARGS 1 $SDNC_APP_NAME
261
262                 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
263         fi
264     echo ""
265     return 0
266 }
267
268 # Check the agent logs for WARNINGs and ERRORs
269 # args: -
270 # (Function for test scripts)
271 check_sdnc_logs() {
272         __check_container_logs "SDNC A1 Controller" $SDNC_APP_NAME $SDNC_KARAF_LOG WARN ERROR
273 }
274
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
283                 ((RES_CONF_FAIL++))
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
287         echo "000"
288         return 1
289     fi
290     if [ $# -eq 2 ]; then
291         json='{"input":{"near-rt-ric-url":"'$2'"}}'
292     else
293         # Escape quotes in the body
294         body=$(echo "$3" | sed 's/"/\\"/g')
295         json='{"input":{"near-rt-ric-url":"'$2'","body":"'"$body"'"}}'
296     fi
297         payload="./tmp/.sdnc.payload.json"
298     echo "$json" > $payload
299     echo "  FILE ($payload) : $json"  >> $HTTPLOG
300         proxyflag=""
301         if [ $RUNMODE == "KUBE" ]; then
302                 if [ ! -z "$CLUSTER_KUBE_PROXY_NODEPORT" ]; then
303                         proxyflag=" --proxy http://localhost:$CLUSTER_KUBE_PROXY_NODEPORT"
304                 fi
305         fi
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
308     res=$($curlString)
309     retcode=$?
310     echo "  RESP: "$res >> $HTTPLOG
311     if [ $retcode -ne 0 ]; then
312         echo "  RETCODE: "$retcode >> $HTTPLOG
313         echo "000"
314         return 1
315     fi
316
317         status=${res:${#res}-3}
318
319     if [ $status -ne 200 ]; then
320         echo "000"
321         return 1
322     fi
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
329     echo "$res"
330     return 0
331 }
332
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() {
337         __log_test_start $@
338
339         ric_id=$3
340         if [ $RUNMODE == "KUBE" ]; then
341                 ric_id=$(get_kube_sim_host $3)
342         fi
343     paramError=1
344     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
345         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies"
346                 paramError=0
347     elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
348         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies"
349         paramError=0
350         fi
351
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>]* ] )" $@
354                 return 1
355         fi
356
357     res=$(__do_curl_to_controller getA1Policy "$url")
358     retcode=$?
359     status=${res:${#res}-3}
360
361     if [ $retcode -ne 0 ]; then
362                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
363                 return 1
364         fi
365
366         if [ $status -ne $1 ]; then
367                 __log_test_fail_status_code $1 $status
368                 return 1
369         fi
370     body=${res:0:${#res}-3}
371
372         targetJson="["
373     start=4
374     if [ $2 == "OSC" ]; then
375         start=5
376     fi
377     for pid in ${@:$start} ; do
378         if [ "$targetJson" != "[" ]; then
379             targetJson=$targetJson","
380         fi
381         targetJson=$targetJson"\"$UUID$pid\""
382     done
383     targetJson=$targetJson"]"
384
385         echo " TARGET JSON: $targetJson" >> $HTTPLOG
386
387         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
388
389         if [ $res -ne 0 ]; then
390                 __log_test_fail_body
391                 return 1
392         fi
393
394         __log_test_pass
395         return 0
396 }
397
398
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() {
403         __log_test_start $@
404
405         ric_id=$3
406         if [ $RUNMODE == "KUBE" ]; then
407                 ric_id=$(get_kube_sim_host $3)
408         fi
409     paramError=1
410     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
411         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4"
412                 paramError=0
413         fi
414
415     if [ $paramError -ne 0 ]; then
416                 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
417                 return 1
418         fi
419
420     res=$(__do_curl_to_controller getA1PolicyType "$url")
421     retcode=$?
422     status=${res:${#res}-3}
423
424     if [ $retcode -ne 0 ]; then
425                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
426                 return 1
427         fi
428
429         if [ $status -ne $1 ]; then
430                 __log_test_fail_status_code $1 $status
431                 return 1
432         fi
433     body=${res:0:${#res}-3}
434
435         if [ $# -eq 5 ]; then
436
437                 body=${res:0:${#res}-3}
438
439                 targetJson=$(< $5)
440                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
441                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
442
443                 if [ $res -ne 0 ]; then
444                         __log_test_fail_body
445                         return 1
446                 fi
447         fi
448
449         __log_test_pass
450         return 0
451 }
452
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() {
457         __log_test_start $@
458
459         ric_id=$3
460         if [ $RUNMODE == "KUBE" ]; then
461                 ric_id=$(get_kube_sim_host $3)
462         fi
463     paramError=1
464     if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
465         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
466                 paramError=0
467     elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
468         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
469         paramError=0
470         fi
471
472     if [ $paramError -ne 0 ]; then
473                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
474                 return 1
475         fi
476
477     res=$(__do_curl_to_controller deleteA1Policy "$url")
478     retcode=$?
479     status=${res:${#res}-3}
480
481     if [ $retcode -ne 0 ]; then
482                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
483                 return 1
484         fi
485
486         if [ $status -ne $1 ]; then
487                 __log_test_fail_status_code $1 $status
488                 return 1
489         fi
490
491         __log_test_pass
492         return 0
493 }
494
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() {
499         __log_test_start $@
500
501         ric_id=$3
502         if [ $RUNMODE == "KUBE" ]; then
503                 ric_id=$(get_kube_sim_host $3)
504         fi
505     paramError=1
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)
509
510                 paramError=0
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)
514         paramError=0
515         fi
516
517     if [ $paramError -ne 0 ]; then
518                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
519                 return 1
520         fi
521
522     res=$(__do_curl_to_controller putA1Policy "$url" "$body")
523     retcode=$?
524     status=${res:${#res}-3}
525
526     if [ $retcode -ne 0 ]; then
527                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
528                 return 1
529         fi
530
531         if [ $status -ne $1 ]; then
532                 __log_test_fail_status_code $1 $status
533                 return 1
534         fi
535
536         __log_test_pass
537         return 0
538 }
539
540
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() {
545         __log_test_start $@
546
547         ric_id=$3
548         if [ $RUNMODE == "KUBE" ]; then
549                 ric_id=$(get_kube_sim_host $3)
550         fi
551     targetJson=""
552     paramError=1
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\":\"????\"}"
559         fi
560                 paramError=0
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\""
567             fi
568             targetJson=$targetJson"}"
569         fi
570         paramError=0
571         fi
572
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>)" $@
575                 return 1
576         fi
577
578     res=$(__do_curl_to_controller getA1PolicyStatus "$url")
579     retcode=$?
580     status=${res:${#res}-3}
581
582     if [ $retcode -ne 0 ]; then
583                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
584                 return 1
585         fi
586
587         if [ $status -ne $1 ]; then
588                 __log_test_fail_status_code $1 $status
589                 return 1
590         fi
591
592         if [ ! -z "$targetJson" ]; then
593
594                 body=${res:0:${#res}-3}
595                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
596                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
597
598                 if [ $res -ne 0 ]; then
599                         __log_test_fail_body
600                         return 1
601                 fi
602         fi
603
604         __log_test_pass
605         return 0
606 }