Merge "Fix typos in documentation"
[nonrtric.git] / test / common / agent_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 specific test functions for Policy Agent API
21
22 ### API functiond towards the Policy Agent
23
24 . ../common/api_curl.sh
25
26 #########################################################
27 #### Test case functions A1 Policy management service
28 #########################################################
29
30 # This function compare the size, towards a target value, of a json array returned from <url> of the Policy Agent.
31 # This is done immediately by setting PASS or FAIL or wait up to and optional timeout before setting PASS or FAIL
32 # args: json:<url> <target-value> [<timeout-in-seconds]
33 # (Function for test scripts)
34 api_equal() {
35     echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
36         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
37                 if [[ $1 == "json:"* ]]; then
38                         __var_test "Policy Agent" $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT"/" $1 "=" $2 $3
39                         return 0
40                 fi
41         fi
42
43         ((RES_CONF_FAIL++))
44         __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
45         return 1
46 }
47
48 # API Test function: GET /policies
49 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-ype-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]
50 # (Function for test scripts)
51 api_get_policies() {
52         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
53     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
54         ((RES_TEST++))
55         paramError=0
56         if [ $# -lt 4 ]; then
57                 paramError=1
58         elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
59                 paramError=1
60         elif [ $# -gt 4 ] && [ $(($#%5)) -ne 4 ]; then
61                 paramError=1
62         fi
63
64     if [ $paramError -ne 0 ]; then
65         __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-type-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <template-file>]*]" $@
66         return 1
67     fi
68         queryparams=""
69         if [ $2 != "NORIC" ]; then
70                 queryparams="?ric="$2
71         fi
72         if [ $3 != "NOSERVICE" ]; then
73                 if [ -z $queryparams ]; then
74                         queryparams="?service="$3
75                 else
76                         queryparams=$queryparams"&service="$3
77                 fi
78         fi
79         if [ $4 != "NOTYPE" ]; then
80                 if [ -z $queryparams ]; then
81                         queryparams="?type="$4
82                 else
83                         queryparams=$queryparams"&type="$4
84                 fi
85         fi
86
87         query="/policies"$queryparams
88     res="$(__do_curl_to_api PA GET $query)"
89     status=${res:${#res}-3}
90
91         if [ $status -ne $1 ]; then
92                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
93                 ((RES_FAIL++))
94                 __check_stop_at_error
95                 return 1
96         fi
97
98         if [ $# -gt 4 ]; then
99                 if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
100                         targetJson="["
101                 else
102                         body=${res:0:${#res}-3}
103                         targetJson="["
104                         arr=(${@:5})
105
106                         for ((i=0; i<$(($#-4)); i=i+5)); do
107
108                                 if [ "$targetJson" != "[" ]; then
109                                         targetJson=$targetJson","
110                                 fi
111                                 targetJson=$targetJson"{\"id\":\"$UUID${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
112                                 if [ "${arr[$i+3]}" == "EMPTY" ]; then
113                                         targetJson=$targetJson"\"\","
114                                 else
115                                         targetJson=$targetJson"\"${arr[$i+3]}\","
116                                 fi
117                                 file="./tmp/.p.json"
118                                 sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
119                                 json=$(cat $file)
120                                 targetJson=$targetJson"\"json\":"$json"}"
121                         done
122                 fi
123
124                 targetJson=$targetJson"]"
125                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
126                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
127
128                 if [ $res -ne 0 ]; then
129                         echo -e $RED" FAIL, returned body not correct"$ERED
130                         ((RES_FAIL++))
131                         __check_stop_at_error
132                         return 1
133                 fi
134         fi
135
136         ((RES_PASS++))
137         echo -e $GREEN" PASS"$EGREEN
138         return 0
139
140 }
141
142 # API Test function: GET /policy
143 #args: <response-code>  <policy-id> [<template-file>]
144 # (Function for test scripts)
145 api_get_policy() {
146         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
147     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
148         ((RES_TEST++))
149
150     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
151         __print_err "<response-code>  <policy-id> [<template-file>] " $@
152         return 1
153     fi
154
155         query="/policy?id=$UUID$2"
156         res="$(__do_curl_to_api PA GET $query)"
157         status=${res:${#res}-3}
158
159         if [ $status -ne $1 ]; then
160                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
161                 ((RES_FAIL++))
162                 __check_stop_at_error
163                 return 1
164         fi
165
166         if [ $# -eq 3 ]; then
167                 #Create a policy json to compare with
168                 body=${res:0:${#res}-3}
169                 file="./tmp/.p.json"
170                 sed 's/XXX/'${2}'/g' $3 > $file
171                 targetJson=$(< $file)
172                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
173                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
174                 if [ $res -ne 0 ]; then
175                         echo -e $RED" FAIL, returned body not correct"$ERED
176                         ((RES_FAIL++))
177                         __check_stop_at_error
178                         return 1
179                 fi
180         fi
181
182         ((RES_PASS++))
183         echo -e $GREEN" PASS"$EGREEN
184         return 0
185 }
186
187 # API Test function: PUT /policy
188 # args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]
189 # (Function for test scripts)
190 api_put_policy() {
191         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
192     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
193         ((RES_TEST++))
194
195     if [ $# -lt 7 ] || [ $# -gt 8 ]; then
196         __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]" $@
197         return 1
198     fi
199
200         ric=$3
201         count=0
202         max=1
203
204         if [ $# -eq 8 ]; then
205                 max=$8
206         fi
207
208         pid=$5
209         file=$7
210
211         while [ $count -lt $max ]; do
212                 query="/policy?id=$UUID$pid&ric=$ric&service=$2"
213
214                 if [ $4 != "NOTYPE" ]; then
215                         query=$query"&type=$4"
216                 fi
217
218                 if [ $6 != NOTRANSIENT ]; then
219                         query=$query"&transient=$6"
220                 fi
221
222                 file="./tmp/.p.json"
223                 sed 's/XXX/'${pid}'/g' $7 > $file
224         res="$(__do_curl_to_api PA PUT $query $file)"
225         status=${res:${#res}-3}
226                 echo -ne " Executing "$count"("$max")${SAMELINE}"
227                 if [ $status -ne $1 ]; then
228                         echo " Executed "$count"?("$max")"
229                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
230                         ((RES_FAIL++))
231                         __check_stop_at_error
232                         return 1
233                 fi
234
235                 let pid=$pid+1
236                 let count=$count+1
237                 echo -ne " Executed  "$count"("$max")${SAMELINE}"
238         done
239         echo ""
240
241         ((RES_PASS++))
242         echo -e $GREEN" PASS"$EGREEN
243         return 0
244 }
245
246 # API Test function: PUT /policy to run in batch
247 # args: <response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]
248 # (Function for test scripts)
249 api_put_policy_batch() {
250         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
251     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
252         ((RES_TEST++))
253
254     if [ $# -lt 7 ] || [ $# -gt 8 ]; then
255         __print_err "<response-code> <service-name> <ric-id> <policytype-id> <policy-id> <transient> <template-file> [<count>]" $@
256         return 1
257     fi
258
259         ric=$3
260         count=0
261         max=1
262
263         if [ $# -eq 8 ]; then
264                 max=$8
265         fi
266
267         pid=$5
268         file=$7
269         ARR=""
270         while [ $count -lt $max ]; do
271                 query="/policy?id=$UUID$pid&ric=$ric&service=$2"
272
273                 if [ $4 != "NOTYPE" ]; then
274                         query=$query"&type=$4"
275                 fi
276
277                 if [ $6 != NOTRANSIENT ]; then
278                         query=$query"&transient=$6"
279                 fi
280
281                 file="./tmp/.p.json"
282                 sed 's/XXX/'${pid}'/g' $7 > $file
283         res="$(__do_curl_to_api PA PUT_BATCH $query $file)"
284         status=${res:${#res}-3}
285                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
286
287                 if [ $status -ne 200 ]; then
288                         echo " Requested(batch) "$count"?("$max")"
289                         echo -e $RED" FAIL. Exepected status 200 (in request), got "$status $ERED
290                         ((RES_FAIL++))
291                         __check_stop_at_error
292                         return 1
293                 fi
294                 cid=${res:0:${#res}-3}
295                 ARR=$ARR" "$cid
296                 let pid=$pid+1
297                 let count=$count+1
298                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
299         done
300
301         echo ""
302         count=0
303         for cid in $ARR; do
304
305         res="$(__do_curl_to_api PA RESPONSE $cid)"
306         status=${res:${#res}-3}
307                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
308
309                 if [ $status -ne $1 ]; then
310                         echo " Requested(batch) "$count"?("$max")"
311                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
312                         ((RES_FAIL++))
313                         __check_stop_at_error
314                         return 1
315                 fi
316
317                 let count=$count+1
318                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
319         done
320
321         echo ""
322
323         ((RES_PASS++))
324         echo -e $GREEN" PASS"$EGREEN
325         return 0
326 }
327
328 # API Test function: PUT /policy to run in i parallel for a number of rics
329 # args: <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>
330 # (Function for test scripts)
331 api_put_policy_parallel() {
332         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
333     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
334         ((RES_TEST++))
335
336     if [ $# -ne 10 ]; then
337         __print_err " <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <template-file> <count-per-ric> <number-of-threads>" $@
338         return 1
339     fi
340         resp_code=$1; shift;
341         serv=$1; shift
342         ric_base=$1; shift;
343         num_rics=$1; shift;
344         type=$1; shift;
345         start_id=$1; shift;
346         transient=$1; shift;
347         template=$1; shift;
348         count=$1; shift;
349         pids=$1; shift;
350
351         if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
352                 echo " Info - api_put_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
353                 echo " Info - will execute over agent REST"
354         fi
355
356         if [ $serv == "NOSERVICE" ]; then
357                 serv=""
358         fi
359         query="/policy?service=$serv"
360
361         if [ $type != "NOTYPE" ]; then
362                 query=$query"&type=$type"
363         fi
364
365         if [ $transient != NOTRANSIENT ]; then
366                 query=$query"&transient=$transient"
367         fi
368
369         urlbase=${ADAPTER}${query}
370
371         for ((i=1; i<=$pids; i++))
372         do
373                 uuid=$UUID
374                 if [ -z "$uuid" ]; then
375                         uuid="NOUUID"
376                 fi
377                 echo "" > "./tmp/.pid${i}.res.txt"
378                 echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $template $count $pids $i > "./tmp/.pid${i}.txt"
379                 echo $i
380         done  | xargs -n 1 -I{} -P $pids bash -c '{
381                 arg=$(echo {})
382                 echo " Parallel process $arg started"
383                 tmp=$(< "./tmp/.pid${arg}.txt")
384                 python3 ../common/create_policies_process.py $tmp > ./tmp/.pid${arg}.res.txt
385         }'
386         msg=""
387         for ((i=1; i<=$pids; i++))
388         do
389                 file="./tmp/.pid${i}.res.txt"
390                 tmp=$(< $file)
391                 if [ -z "$tmp" ]; then
392                         echo " Process $i : unknown result (result file empty"
393                         msg="failed"
394                 else
395                         res=${tmp:0:1}
396                         if [ $res == "0" ]; then
397                                 echo " Process $i : OK"
398                         else
399                                 echo " Process $i : failed - "${tmp:1}
400                                 msg="failed"
401                         fi
402                 fi
403         done
404         if [ -z $msg ]; then
405                 echo " $(($count*$num_rics)) policy request(s) executed"
406                 ((RES_PASS++))
407                 echo -e $GREEN" PASS"$EGREEN
408                 return 0
409         fi
410
411         echo -e $RED" FAIL. One of more processes failed to execute" $ERED
412         ((RES_FAIL++))
413         __check_stop_at_error
414         return 1
415 }
416
417 # API Test function: DELETE /policy
418 # args: <response-code> <policy-id> [count]
419 # (Function for test scripts)
420 api_delete_policy() {
421         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
422     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
423         ((RES_TEST++))
424
425     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
426         __print_err "<response-code> <policy-id> [count]" $@
427         return 1
428     fi
429
430         count=0
431         max=1
432
433         if [ $# -eq 3 ]; then
434                 max=$3
435         fi
436
437         pid=$2
438
439         while [ $count -lt $max ]; do
440                 query="/policy?id="$UUID$pid
441                 res="$(__do_curl_to_api PA DELETE $query)"
442                 status=${res:${#res}-3}
443                 echo -ne " Executing "$count"("$max")${SAMELINE}"
444
445                 if [ $status -ne $1 ]; then
446                         echo " Executed "$count"?("$max")"
447                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
448                         ((RES_FAIL++))
449                         __check_stop_at_error
450                         return 1
451                 fi
452                 let pid=$pid+1
453                 let count=$count+1
454                 echo -ne " Executed  "$count"("$max")${SAMELINE}"
455         done
456         echo ""
457
458         ((RES_PASS++))
459         echo -e $GREEN" PASS"$EGREEN
460         return 0
461 }
462
463 # API Test function: DELETE /policy to run in batch
464 # args: <response-code> <policy-id> [count]
465 # (Function for test scripts)
466 api_delete_policy_batch() {
467         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
468     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
469         ((RES_TEST++))
470
471     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
472         __print_err "<response-code> <policy-id> [count]" $@
473         return 1
474     fi
475
476         count=0
477         max=1
478
479         if [ $# -eq 3 ]; then
480                 max=$3
481         fi
482
483         pid=$2
484         ARR=""
485         while [ $count -lt $max ]; do
486                 query="/policy?id="$UUID$pid
487                 res="$(__do_curl_to_api PA DELETE_BATCH $query)"
488                 status=${res:${#res}-3}
489                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
490
491                 if [ $status -ne 200 ]; then
492                         echo " Requested(batch) "$count"?("$max")"
493                         echo -e $RED" FAIL. Exepected status 200 (in request), got "$status $ERED
494                         ((RES_FAIL++))
495                         __check_stop_at_error
496                         return 1
497                 fi
498                 cid=${res:0:${#res}-3}
499                 ARR=$ARR" "$cid
500                 let pid=$pid+1
501                 let count=$count+1
502                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
503         done
504
505         echo ""
506
507         count=0
508         for cid in $ARR; do
509
510         res="$(__do_curl_to_api PA RESPONSE $cid)"
511         status=${res:${#res}-3}
512                 echo -ne " Deleted(batch) "$count"("$max")${SAMELINE}"
513
514                 if [ $status -ne $1 ]; then
515                         echo " Deleted(batch) "$count"?("$max")"
516                         echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
517                         ((RES_FAIL++))
518                         __check_stop_at_error
519                         return 1
520                 fi
521
522                 let count=$count+1
523                 echo -ne " Deleted(batch)  "$count"("$max")${SAMELINE}"
524         done
525
526         ((RES_PASS++))
527         echo -e $GREEN" PASS"$EGREEN
528         return 0
529 }
530
531 # API Test function: DELETE /policy to run in i parallel for a number of rics
532 # args: <response-code> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>
533 # (Function for test scripts)
534 api_delete_policy_parallel() {
535         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
536     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
537         ((RES_TEST++))
538
539     if [ $# -ne 5 ]; then
540         __print_err " <response-code> <ric-id-base> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>" $@
541         return 1
542     fi
543         resp_code=$1; shift;
544         num_rics=$1; shift;
545         start_id=$1; shift;
546         count=$1; shift;
547         pids=$1; shift;
548
549         if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
550                 echo " Info - api_delete_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
551                 echo " Info - will execute over agent REST"
552         fi
553
554         query="/policy"
555
556         urlbase=${ADAPTER}${query}
557
558         for ((i=1; i<=$pids; i++))
559         do
560                 uuid=$UUID
561                 if [ -z "$uuid" ]; then
562                         uuid="NOUUID"
563                 fi
564                 echo "" > "./tmp/.pid${i}.del.res.txt"
565                 echo $resp_code $urlbase $num_rics $uuid $start_id $count $pids $i > "./tmp/.pid${i}.del.txt"
566                 echo $i
567         done  | xargs -n 1 -I{} -P $pids bash -c '{
568                 arg=$(echo {})
569                 echo " Parallel process $arg started"
570                 tmp=$(< "./tmp/pid${arg}.del.txt")
571                 python3 ../common/delete_policies_process.py $tmp > ./tmp/pid${arg}.del.res.txt
572         }'
573         msg=""
574         for ((i=1; i<=$pids; i++))
575         do
576                 file="./tmp/.pid${i}.del.res.txt"
577                 tmp=$(< $file)
578                 if [ -z "$tmp" ]; then
579                         echo " Process $i : unknown result (result file empty"
580                         msg="failed"
581                 else
582                         res=${tmp:0:1}
583                         if [ $res == "0" ]; then
584                                 echo " Process $i : OK"
585                         else
586                                 echo " Process $i : failed - "${tmp:1}
587                                 msg="failed"
588                         fi
589                 fi
590         done
591         if [ -z $msg ]; then
592                 echo " $(($count*$num_rics)) policy request(s) executed"
593                 ((RES_PASS++))
594                 echo -e $GREEN" PASS"$EGREEN
595                 return 0
596         fi
597
598         echo -e $RED" FAIL. One of more processes failed to execute" $ERED
599         ((RES_FAIL++))
600         __check_stop_at_error
601         return 1
602 }
603
604 # API Test function: GET /policy_ids
605 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
606 # (Function for test scripts)
607 api_get_policy_ids() {
608         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
609     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
610         ((RES_TEST++))
611
612     if [ $# -lt 4 ]; then
613                 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
614                 return 1
615         fi
616
617         queryparams=""
618
619         if [ $2 != "NORIC" ]; then
620                 queryparams="?ric="$2
621         fi
622
623         if [ $3 != "NOSERVICE" ]; then
624                 if [ -z $queryparams ]; then
625                         queryparams="?service="$3
626                 else
627                         queryparams=$queryparams"&service="$3
628                 fi
629         fi
630         if [ $4 != "NOTYPE" ]; then
631                 if [ -z $queryparams ]; then
632                         queryparams="?type="$4
633                 else
634                         queryparams=$queryparams"&type="$4
635                 fi
636         fi
637
638         query="/policy_ids"$queryparams
639     res="$(__do_curl_to_api PA GET $query)"
640     status=${res:${#res}-3}
641
642         if [ $status -ne $1 ]; then
643                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
644                 ((RES_FAIL++))
645                 __check_stop_at_error
646                 return 1
647         fi
648
649         if [ $# -gt 4 ]; then
650                 body=${res:0:${#res}-3}
651                 targetJson="["
652
653                 for pid in ${@:5} ; do
654                         if [ "$targetJson" != "[" ]; then
655                                 targetJson=$targetJson","
656                         fi
657                         if [ $pid != "NOID" ]; then
658                                 targetJson=$targetJson"\"$UUID$pid\""
659                         fi
660                 done
661
662                 targetJson=$targetJson"]"
663                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
664                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
665
666                 if [ $res -ne 0 ]; then
667                         echo -e $RED" FAIL, returned body not correct"$ERED
668                         ((RES_FAIL++))
669                         __check_stop_at_error
670                         return 1
671                 fi
672         fi
673
674         ((RES_PASS++))
675         echo -e $GREEN" PASS"$EGREEN
676         return 0
677 }
678
679 # API Test function: GET /policy_schema
680 # args: <response-code> <policy-type-id> [<schema-file>]
681 # (Function for test scripts)
682 api_get_policy_schema() {
683         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
684     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
685         ((RES_TEST++))
686
687     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
688         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
689         return 1
690     fi
691
692         query="/policy_schema?id=$2"
693         res="$(__do_curl_to_api PA GET $query)"
694         status=${res:${#res}-3}
695
696         if [ $status -ne $1 ]; then
697                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
698                 ((RES_FAIL++))
699                 __check_stop_at_error
700                 return 1
701         fi
702
703         if [ $# -eq 3 ]; then
704
705                 body=${res:0:${#res}-3}
706
707                 targetJson=$(< $3)
708                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
709                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
710
711                 if [ $res -ne 0 ]; then
712                         echo -e $RED" FAIL, returned body not correct"$ERED
713                         ((RES_FAIL++))
714                         __check_stop_at_error
715                         return 1
716                 fi
717         fi
718
719         ((RES_PASS++))
720         echo -e $GREEN" PASS"$EGREEN
721         return 0
722 }
723
724 # API Test function: GET /policy_schemas
725 # args: <response-code>  <ric-id>|NORIC [<schema-file>|NOFILE]*
726 # (Function for test scripts)
727 api_get_policy_schemas() {
728         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
729     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
730         ((RES_TEST++))
731
732     if [ $# -lt 2 ]; then
733         __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
734         return 1
735     fi
736
737         query="/policy_schemas"
738         if [ $2 != "NORIC" ]; then
739                 query=$query"?ric="$2
740         fi
741
742         res="$(__do_curl_to_api PA GET $query)"
743         status=${res:${#res}-3}
744
745         if [ $status -ne $1 ]; then
746                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
747                 ((RES_FAIL++))
748                 __check_stop_at_error
749                 return 1
750         fi
751
752         if [ $# -gt 2 ]; then
753                 body=${res:0:${#res}-3}
754                 targetJson="["
755
756                 for file in ${@:3} ; do
757                         if [ "$targetJson" != "[" ]; then
758                                 targetJson=$targetJson","
759                         fi
760                         if [ $file == "NOFILE" ]; then
761                                 targetJson=$targetJson"{}"
762                         else
763                                 targetJson=$targetJson$(< $file)
764                         fi
765                 done
766
767                 targetJson=$targetJson"]"
768                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
769                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
770
771                 if [ $res -ne 0 ]; then
772                         echo -e $RED" FAIL, returned body not correct"$ERED
773                         ((RES_FAIL++))
774                         __check_stop_at_error
775                         return 1
776                 fi
777         fi
778
779         ((RES_PASS++))
780         echo -e $GREEN" PASS"$EGREEN
781         return 0
782 }
783
784 # API Test function: GET /policy_status
785 # arg: <response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)
786 # (Function for test scripts)
787 api_get_policy_status() {
788         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
789     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
790         ((RES_TEST++))
791     if [ $# -lt 4 ] || [ $# -gt 5 ]; then
792                 __print_err "<response-code> <policy-id> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
793                 return 1
794         fi
795
796         targetJson=""
797
798         if [ $3 == "STD" ]; then
799                 targetJson="{\"enforceStatus\":\"$4\""
800                 if [ $# -eq 5 ]; then
801                         targetJson=$targetJson",\"reason\":\"$5\""
802                 fi
803                 targetJson=$targetJson"}"
804         elif [ $3 == "OSC" ]; then
805                 targetJson="{\"instance_status\":\"$4\""
806                 if [ $# -eq 5 ]; then
807                         targetJson=$targetJson",\"has_been_deleted\":\"$5\""
808                 fi
809                 targetJson=$targetJson",\"created_at\":\"????\"}"
810         else
811                 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
812                 return 1
813         fi
814
815         query="/policy_status?id="$UUID$2
816
817         res="$(__do_curl_to_api PA GET $query)"
818     status=${res:${#res}-3}
819
820         if [ $status -ne $1 ]; then
821                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
822                 ((RES_FAIL++))
823                 __check_stop_at_error
824                 return 1
825         fi
826
827         echo "TARGET JSON: $targetJson" >> $HTTPLOG
828         body=${res:0:${#res}-3}
829         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
830
831         if [ $res -ne 0 ]; then
832                 echo -e $RED" FAIL, returned body not correct"$ERED
833                 ((RES_FAIL++))
834                 __check_stop_at_error
835                 return 1
836         fi
837
838         ((RES_PASS++))
839         echo -e $GREEN" PASS"$EGREEN
840         return 0
841 }
842
843 # API Test function: GET /policy_types
844 # args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
845 # (Function for test scripts)
846 api_get_policy_types() {
847         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
848     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
849         ((RES_TEST++))
850
851     if [ $# -lt 1 ]; then
852                 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
853                 return 1
854         fi
855
856         if [ $# -eq 1 ]; then
857                 query="/policy_types"
858         elif [ $2 == "NORIC" ]; then
859                 query="/policy_types"
860         else
861                 query="/policy_types?ric=$2"
862         fi
863
864     res="$(__do_curl_to_api PA GET $query)"
865     status=${res:${#res}-3}
866
867         if [ $status -ne $1 ]; then
868                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
869                 ((RES_FAIL++))
870                 __check_stop_at_error
871                 return 1
872         fi
873
874         if [ $# -gt 2 ]; then
875                 body=${res:0:${#res}-3}
876                 targetJson="["
877
878                 for pid in ${@:3} ; do
879                         if [ "$targetJson" != "[" ]; then
880                                 targetJson=$targetJson","
881                         fi
882                         if [ $pid == "EMPTY" ]; then
883                                 pid=""
884                         fi
885                         targetJson=$targetJson"\"$pid\""
886                 done
887
888                 targetJson=$targetJson"]"
889                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
890                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
891
892                 if [ $res -ne 0 ]; then
893                         echo -e $RED" FAIL, returned body not correct"$ERED
894                         ((RES_FAIL++))
895                         __check_stop_at_error
896                         return 1
897                 fi
898         fi
899
900         ((RES_PASS++))
901         echo -e $GREEN" PASS"$EGREEN
902         return 0
903 }
904
905 #########################################################
906 #### Test case functions Health check
907 #########################################################
908
909 # API Test function: GET /status
910 # args: <response-code>
911 # (Function for test scripts)
912 api_get_status() {
913         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
914     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
915         ((RES_TEST++))
916     if [ $# -ne 1 ]; then
917                 __print_err "<response-code>" $@
918                 return 1
919         fi
920     query="/status"
921     res="$(__do_curl_to_api PA GET $query)"
922     status=${res:${#res}-3}
923
924         if [ $status -ne $1 ]; then
925                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
926                 ((RES_FAIL++))
927                 __check_stop_at_error
928                 return 1
929         fi
930
931         ((RES_PASS++))
932         echo -e $GREEN" PASS"$EGREEN
933         return 0
934 }
935
936 #########################################################
937 #### Test case functions RIC Repository
938 #########################################################
939
940 # API Test function: GET /ric
941 # args: <reponse-code> <management-element-id> [<ric-id>]
942 # (Function for test scripts)
943 api_get_ric() {
944         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
945     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
946         ((RES_TEST++))
947     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
948                 __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
949                 return 1
950         fi
951
952         query="/ric?managedElementId="$2
953
954     res="$(__do_curl_to_api PA GET $query)"
955     status=${res:${#res}-3}
956
957         if [ $status -ne $1 ]; then
958                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
959                 ((RES_FAIL++))
960                 __check_stop_at_error
961                 return 1
962         fi
963
964         if [ $# -eq 3 ]; then
965                 body=${res:0:${#res}-3}
966                 if [ "$body" != "$3" ]; then
967                         echo -e $RED" FAIL, returned body not correct"$ERED
968                         ((RES_FAIL++))
969                         __check_stop_at_error
970                         return 1
971                 fi
972         fi
973
974         ((RES_PASS++))
975         echo -e $GREEN" PASS"$EGREEN
976         return 0
977 }
978
979 # API test function: GET /rics
980 # args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
981 # example of <space-separate-string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4 ricsim_g1_1:me2_........."
982 # format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
983 # (Function for test scripts)
984 api_get_rics() {
985         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
986     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
987         ((RES_TEST++))
988
989     if [ $# -lt 2 ]; then
990                 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
991                 return 1
992         fi
993
994         query="/rics"
995         if [ $2 != "NOTYPE" ]; then
996         query="/rics?policyType="$2
997         fi
998
999     res="$(__do_curl_to_api PA GET $query)"
1000     status=${res:${#res}-3}
1001
1002         if [ $status -ne $1 ]; then
1003                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1004                 ((RES_FAIL++))
1005                 __check_stop_at_error
1006                 return 1
1007         fi
1008
1009         if [ $# -gt 2 ]; then
1010                 body=${res:0:${#res}-3}
1011                 res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "$3" )
1012                 if [ $res -ne 0 ]; then
1013                         echo -e $RED" FAIL, could not create target ric info json"$ERED
1014                         ((RES_FAIL++))
1015                         __check_stop_at_error
1016                         return 1
1017                 fi
1018
1019                 targetJson=$(<./tmp/.tmp_rics.json)
1020         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1021                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1022                 if [ $res -ne 0 ]; then
1023                         echo -e $RED" FAIL, returned body not correct"$ERED
1024                         ((RES_FAIL++))
1025                         __check_stop_at_error
1026                         return 1
1027                 fi
1028         fi
1029
1030         ((RES_PASS++))
1031         echo -e $GREEN" PASS"$EGREEN
1032         return 0
1033 }
1034
1035 ##################################################################
1036 #### API Test case functions Service registry and supervision ####
1037 ##################################################################
1038
1039 # API test function: PUT /service
1040 # args: <response-code>  <service-name> <keepalive-timeout> <callbackurl>
1041 # (Function for test scripts)
1042 api_put_service() {
1043         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1044     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1045         ((RES_TEST++))
1046     if [ $# -ne 4 ]; then
1047         __print_err "<response-code>  <service-name> <keepalive-timeout> <callbackurl>" $@
1048         return 1
1049     fi
1050
1051     query="/service"
1052     json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
1053     file="./tmp/.tmp.json"
1054         echo "$json" > $file
1055
1056     res="$(__do_curl_to_api PA PUT $query $file)"
1057     status=${res:${#res}-3}
1058
1059         if [ $status -ne $1 ]; then
1060                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1061                 ((RES_FAIL++))
1062                 __check_stop_at_error
1063                 return 1
1064         fi
1065
1066         ((RES_PASS++))
1067         echo -e $GREEN" PASS"$EGREEN
1068         return 0
1069 }
1070
1071 # API test function: GET /services
1072 #args: <response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]
1073 # (Function for test scripts)
1074 api_get_services() {
1075         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1076     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1077         ((RES_TEST++))
1078         #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
1079         paramError=1
1080         if [ $# -eq 1 ]; then
1081                 paramError=0
1082         elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
1083                 paramError=0
1084         elif [ $# -eq 5 ]; then
1085                 paramError=0
1086         elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
1087                 argLen=$(($#-2))
1088                 if [ $(($argLen%3)) -eq 0 ]; then
1089                         paramError=0
1090                 fi
1091         fi
1092
1093     if [ $paramError -ne 0 ]; then
1094                 __print_err "<response-code> [ (<query-service-name> <target-service-name> <keepalive-timeout> <callbackurl>) | (NOSERVICE <target-service-name> <keepalive-timeout> <callbackurl> [<target-service-name> <keepalive-timeout> <callbackurl>]* )]" $@
1095                 return 1
1096         fi
1097
1098     query="/services"
1099
1100     if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
1101         query="/services?name="$2
1102         fi
1103
1104     res="$(__do_curl_to_api PA GET $query)"
1105     status=${res:${#res}-3}
1106
1107         if [ $status -ne $1 ]; then
1108                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1109                 ((RES_FAIL++))
1110                 __check_stop_at_error
1111                 return 1
1112         fi
1113
1114         if [ $# -gt 2 ]; then
1115                 variableArgCount=$(($#-2))
1116                 body=${res:0:${#res}-3}
1117         targetJson="["
1118                 shift; shift;
1119                 cntr=0
1120                 while [ $cntr -lt $variableArgCount ]; do
1121                         servicename=$1; shift;
1122                         timeout=$1; shift;
1123                         callback=$1; shift;
1124                         if [ $cntr -gt 0 ]; then
1125                                 targetJson=$targetJson","
1126                         fi
1127                         # timeSinceLastActivitySeconds value cannot be checked since value varies
1128                         targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
1129                         let cntr=cntr+3
1130                 done
1131                 targetJson=$targetJson"]"
1132                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1133                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1134                 if [ $res -ne 0 ]; then
1135                         echo -e $RED" FAIL, returned body not correct"$ERED
1136                         ((RES_FAIL++))
1137                         __check_stop_at_error
1138                         return 1
1139                 fi
1140         fi
1141
1142         ((RES_PASS++))
1143         echo -e $GREEN" PASS"$EGREEN
1144         return 0
1145 }
1146
1147 # API test function: GET /services  (only checking service names)
1148 # args: <response-code> [<service-name>]*"
1149 # (Function for test scripts)
1150 api_get_service_ids() {
1151         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1152     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1153         ((RES_TEST++))
1154
1155     if [ $# -lt 1 ]; then
1156                 __print_err "<response-code> [<service-name>]*" $@
1157                 return 1
1158         fi
1159
1160     query="/services"
1161     res="$(__do_curl_to_api PA GET $query)"
1162     status=${res:${#res}-3}
1163
1164         if [ $status -ne $1 ]; then
1165                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1166                 ((RES_FAIL++))
1167                 __check_stop_at_error
1168                 return 1
1169         fi
1170
1171         body=${res:0:${#res}-3}
1172         targetJson="["
1173         for rapp in ${@:2} ; do
1174                 if [ "$targetJson" != "[" ]; then
1175                         targetJson=$targetJson","
1176                 fi
1177                 targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
1178         done
1179
1180         targetJson=$targetJson"]"
1181         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1182         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1183
1184         if [ $res -ne 0 ]; then
1185                 echo -e $RED" FAIL, returned body not correct"$ERED
1186                 ((RES_FAIL++))
1187                 __check_stop_at_error
1188                 return 1
1189         fi
1190
1191         ((RES_PASS++))
1192         echo -e $GREEN" PASS"$EGREEN
1193         return 0
1194 }
1195
1196 # API test function: DELETE /services
1197 # args: <response-code> <service-name>
1198 # (Function for test scripts)
1199 api_delete_services() {
1200         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1201     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1202         ((RES_TEST++))
1203
1204     if [ $# -ne 2 ]; then
1205                 __print_err "<response-code> <service-name>" $@
1206                 return 1
1207         fi
1208
1209     query="/services?name="$2
1210     res="$(__do_curl_to_api PA DELETE $query)"
1211     status=${res:${#res}-3}
1212
1213         if [ $status -ne $1 ]; then
1214                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1215                 ((RES_FAIL++))
1216                 __check_stop_at_error
1217                 return 1
1218         fi
1219
1220         ((RES_PASS++))
1221         echo -e $GREEN" PASS"$EGREEN
1222         return 0
1223 }
1224
1225 # API test function: PUT /services/keepalive
1226 # args: <response-code> <service-name>
1227 # (Function for test scripts)
1228 api_put_services_keepalive() {
1229         echo -e $BOLD"TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ $EBOLD
1230     echo "TEST(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
1231         ((RES_TEST++))
1232
1233     if [ $# -ne 2 ]; then
1234                 __print_err "<response-code> <service-name>" $@
1235                 return 1
1236         fi
1237
1238     query="/services/keepalive?name="$2
1239     res="$(__do_curl_to_api PA PUT $query)"
1240     status=${res:${#res}-3}
1241
1242         if [ $status -ne $1 ]; then
1243                 echo -e $RED" FAIL. Exepected status "$1", got "$status $ERED
1244                 ((RES_FAIL++))
1245                 __check_stop_at_error
1246                 return 1
1247         fi
1248
1249         ((RES_PASS++))
1250         echo -e $GREEN" PASS"$EGREEN
1251         return 0
1252 }
1253