Upgraded test env with Kubernetes 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 SDNC_HTTPX="http"
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
29     #presume correct
30         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
31         #test
32         #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
33 else
34         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
35 fi
36
37 use_sdnc_http() {
38         echo -e $BOLD"SDNC NB protocol setting"$EBOLD
39         echo -e " Using $BOLD http $EBOLD towards SDNC"
40         SDNC_HTTPX="http"
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
44                 #presume correct
45                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
46                 #test
47                 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
48         else
49                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
50         fi
51         echo ""
52 }
53
54 use_sdnc_https() {
55         echo -e $BOLD"SDNC NB protocol setting"$EBOLD
56         echo -e " Using $BOLD https $EBOLD towards SDNC"
57         SDNC_HTTPX="https"
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
61                 #presume correct
62                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
63                 #test
64                 #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
65         else
66                 SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_SECURE_PORT
67         fi
68         echo ""
69 }
70
71 ##################
72 ### SDNC functions
73 ##################
74
75 # Start the SDNC A1 Controller
76 # args: -
77 # (Function for test scripts)
78 start_sdnc() {
79
80         echo -e $BOLD"Starting $SDNC_DISPLAY_NAME"$EBOLD
81
82         if [ $RUNMODE == "KUBE" ]; then
83
84                 # Check if app shall be fully managed by the test script
85                 __check_included_image "SDNC"
86                 retcode_i=$?
87
88                 # Check if app shall only be used by the testscipt
89                 __check_prestarted_image "SDNC"
90                 retcode_p=$?
91
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
95                         exit
96                 fi
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
100                         exit
101                 fi
102
103
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
108                 fi
109
110                                 # Check if app shall be fully managed by the test script
111                 if [ $retcode_i -eq 0 ]; then
112
113                         echo -e " Creating $SDNC_APP_NAME app and expose service"
114
115                         #Check if nonrtric namespace exists, if not create it
116                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
117
118                         export KUBE_NONRTRIC_NAMESPACE
119                         export SDNC_APP_NAME
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
127                         export SDNC_DB_IMAGE
128
129                         # Create service
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
133
134                         # Create app
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
138
139                 fi
140
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")
145
146                 echo " Host IP, http port, https port: $SDNC_HOST_NAME $SDNC_EXTERNAL_PORT $SDNC_EXTERNAL_SECURE_PORT"
147
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
151             #presume correct
152                         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
153                         #test
154                         #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
155                 else
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
158             #presume correct
159                         SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
160                         #test
161                         #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
162                 fi
163
164                 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
165         else
166
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
171                         exit
172                 fi
173
174                 export SDNC_DB_APP_NAME
175         export SDNC_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
182
183                 __start_container $SDNC_COMPOSE_DIR NODOCKERARGS 1 $SDNC_APP_NAME
184
185                 __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
186         fi
187     echo ""
188     return 0
189 }
190
191
192
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
201                 ((RES_CONF_FAIL++))
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
205         echo "000"
206         return 1
207     fi
208     if [ $# -eq 2 ]; then
209         json='{"input":{"near-rt-ric-url":"'$2'"}}'
210     else
211         # Escape quotes in the body
212         body=$(echo "$3" | sed 's/"/\\"/g')
213         json='{"input":{"near-rt-ric-url":"'$2'","body":"'"$body"'"}}'
214     fi
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
220     res=$($curlString)
221     retcode=$?
222     echo "  RESP: "$res >> $HTTPLOG
223     if [ $retcode -ne 0 ]; then
224         echo "  RETCODE: "$retcode >> $HTTPLOG
225         echo "000"
226         return 1
227     fi
228
229         status=${res:${#res}-3}
230
231     if [ $status -ne 200 ]; then
232         echo "000"
233         return 1
234     fi
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
241     echo "$res"
242     return 0
243 }
244
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() {
249         __log_test_start $@
250
251         ric_id=$3
252         if [ $RUNMODE == "KUBE" ]; then
253                 ric_id=$(get_kube_sim_host $3)
254         fi
255     paramError=1
256     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
257         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies"
258                 paramError=0
259     elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
260         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies"
261         paramError=0
262         fi
263
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>]* ] )" $@
266                 return 1
267         fi
268
269     res=$(__do_curl_to_controller getA1Policy "$url")
270     retcode=$?
271     status=${res:${#res}-3}
272
273     if [ $retcode -ne 0 ]; then
274                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
275                 return 1
276         fi
277
278         if [ $status -ne $1 ]; then
279                 __log_test_fail_status_code $1 $status
280                 return 1
281         fi
282     body=${res:0:${#res}-3}
283
284         targetJson="["
285     start=4
286     if [ $2 == "OSC" ]; then
287         start=5
288     fi
289     for pid in ${@:$start} ; do
290         if [ "$targetJson" != "[" ]; then
291             targetJson=$targetJson","
292         fi
293         targetJson=$targetJson"\"$UUID$pid\""
294     done
295     targetJson=$targetJson"]"
296
297         echo " TARGET JSON: $targetJson" >> $HTTPLOG
298
299         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
300
301         if [ $res -ne 0 ]; then
302                 __log_test_fail_body
303                 return 1
304         fi
305
306         __log_test_pass
307         return 0
308 }
309
310
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() {
315         __log_test_start $@
316
317         ric_id=$3
318         if [ $RUNMODE == "KUBE" ]; then
319                 ric_id=$(get_kube_sim_host $3)
320         fi
321     paramError=1
322     if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
323         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4"
324                 paramError=0
325         fi
326
327     if [ $paramError -ne 0 ]; then
328                 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
329                 return 1
330         fi
331
332     res=$(__do_curl_to_controller getA1PolicyType "$url")
333     retcode=$?
334     status=${res:${#res}-3}
335
336     if [ $retcode -ne 0 ]; then
337                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
338                 return 1
339         fi
340
341         if [ $status -ne $1 ]; then
342                 __log_test_fail_status_code $1 $status
343                 return 1
344         fi
345     body=${res:0:${#res}-3}
346
347         if [ $# -eq 5 ]; then
348
349                 body=${res:0:${#res}-3}
350
351                 targetJson=$(< $5)
352                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
353                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
354
355                 if [ $res -ne 0 ]; then
356                         __log_test_fail_body
357                         return 1
358                 fi
359         fi
360
361         __log_test_pass
362         return 0
363 }
364
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() {
369         __log_test_start $@
370
371         ric_id=$3
372         if [ $RUNMODE == "KUBE" ]; then
373                 ric_id=$(get_kube_sim_host $3)
374         fi
375     paramError=1
376     if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
377         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/a1-p/policytypes/$4/policies/$UUID$5"
378                 paramError=0
379     elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
380         url="$RIC_SIM_HTTPX://$ric_id:$RIC_SIM_PORT/A1-P/v1/policies/$UUID$4"
381         paramError=0
382         fi
383
384     if [ $paramError -ne 0 ]; then
385                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
386                 return 1
387         fi
388
389     res=$(__do_curl_to_controller deleteA1Policy "$url")
390     retcode=$?
391     status=${res:${#res}-3}
392
393     if [ $retcode -ne 0 ]; then
394                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
395                 return 1
396         fi
397
398         if [ $status -ne $1 ]; then
399                 __log_test_fail_status_code $1 $status
400                 return 1
401         fi
402
403         __log_test_pass
404         return 0
405 }
406
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() {
411         __log_test_start $@
412
413         ric_id=$3
414         if [ $RUNMODE == "KUBE" ]; then
415                 ric_id=$(get_kube_sim_host $3)
416         fi
417     paramError=1
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)
421
422                 paramError=0
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)
426         paramError=0
427         fi
428
429     if [ $paramError -ne 0 ]; then
430                 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
431                 return 1
432         fi
433
434     res=$(__do_curl_to_controller putA1Policy "$url" "$body")
435     retcode=$?
436     status=${res:${#res}-3}
437
438     if [ $retcode -ne 0 ]; then
439                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
440                 return 1
441         fi
442
443         if [ $status -ne $1 ]; then
444                 __log_test_fail_status_code $1 $status
445                 return 1
446         fi
447
448         __log_test_pass
449         return 0
450 }
451
452
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() {
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     targetJson=""
464     paramError=1
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\":\"????\"}"
471         fi
472                 paramError=0
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\""
479             fi
480             targetJson=$targetJson"}"
481         fi
482         paramError=0
483         fi
484
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>)" $@
487                 return 1
488         fi
489
490     res=$(__do_curl_to_controller getA1PolicyStatus "$url")
491     retcode=$?
492     status=${res:${#res}-3}
493
494     if [ $retcode -ne 0 ]; then
495                 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
496                 return 1
497         fi
498
499         if [ $status -ne $1 ]; then
500                 __log_test_fail_status_code $1 $status
501                 return 1
502         fi
503
504         if [ ! -z "$targetJson" ]; then
505
506                 body=${res:0:${#res}-3}
507                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
508                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
509
510                 if [ $res -ne 0 ]; then
511                         __log_test_fail_body
512                         return 1
513                 fi
514         fi
515
516         __log_test_pass
517         return 0
518 }