Step versions on master
[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                         if [ "$PMS_VERSION" == "V2" ]; then
39                                 __var_test "Policy Agent" $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT"/v2/" $1 "=" $2 $3
40                         else
41                                 __var_test "Policy Agent" $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT"/" $1 "=" $2 $3
42                         fi
43                         return 0
44                 fi
45         fi
46         __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
47         return 1
48 }
49
50 # API Test function: GET /policies and V2 GET /v2/policy-instances
51 # args: <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>]*]
52 # args(V2): <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <policy-type-id>|NOTYPE [ NOID | [<policy-id> <ric-id> <service-id> EMPTY|<policy-type-id> <transient> <notification-url> <template-file>]*]
53 # (Function for test scripts)
54 api_get_policies() {
55         __log_test_start $@
56
57         if [ "$PMS_VERSION" == "V2" ]; then
58                 paramError=0
59                 variableParams=$(($#-4))
60                 if [ $# -lt 4 ]; then
61                         paramError=1
62                 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
63                         paramError=1
64                 elif [ $# -gt 5 ] && [ $(($variableParams%7)) -ne 0 ]; then
65                         paramError=1
66                 fi
67
68                 if [ $paramError -ne 0 ]; then
69                         __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> <transient> <notification-url> <template-file>]*]" $@
70                         return 1
71                 fi
72         else
73                 paramError=0
74                 variableParams=$(($#-4))
75                 if [ $# -lt 4 ]; then
76                         paramError=1
77                 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
78                         paramError=1
79                 elif [ $# -gt 5 ] && [ $(($variableParams%5)) -ne 0 ]; then
80                         paramError=1
81                 fi
82
83                 if [ $paramError -ne 0 ]; then
84                         __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>]*]" $@
85                         return 1
86                 fi
87         fi
88
89         queryparams=""
90         if [ "$PMS_VERSION" == "V2" ]; then
91                 if [ $2 != "NORIC" ]; then
92                         queryparams="?ric_id="$2
93                 fi
94                 if [ $3 != "NOSERVICE" ]; then
95                         if [ -z $queryparams ]; then
96                                 queryparams="?service_id="$3
97                         else
98                                 queryparams=$queryparams"&service_id="$3
99                         fi
100                 fi
101                 if [ $4 != "NOTYPE" ]; then
102                         if [ -z $queryparams ]; then
103                                 queryparams="?policytype_id="$4
104                         else
105                                 queryparams=$queryparams"&policytype_id="$4
106                         fi
107                 fi
108
109                 query="/v2/policy-instances"$queryparams
110                 res="$(__do_curl_to_api PA GET $query)"
111                 status=${res:${#res}-3}
112
113                 if [ $status -ne $1 ]; then
114                         __log_test_fail_status_code $1 $status
115                         return 1
116                 fi
117
118                 if [ $# -gt 4 ]; then
119                         body=${res:0:${#res}-3}
120                         if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
121                                 targetJson="["
122                         else
123                                 targetJson="["
124                                 arr=(${@:5})
125
126                                 for ((i=0; i<$(($#-4)); i=i+7)); do
127
128                                         if [ "$targetJson" != "[" ]; then
129                                                 targetJson=$targetJson","
130                                         fi
131                                         targetJson=$targetJson"{\"policy_id\":\"$UUID${arr[$i]}\",\"ric_id\":\"${arr[$i+1]}\",\"service_id\":\"${arr[$i+2]}\",\"policytype_id\":"
132                                         if [ "${arr[$i+3]}" == "EMPTY" ]; then
133                                                 targetJson=$targetJson"\"\","
134                                         else
135                                                 targetJson=$targetJson"\"${arr[$i+3]}\","
136                                         fi
137                                         targetJson=$targetJson"\"transient\":${arr[$i+4]},\"status_notification_uri\":\"${arr[$i+5]}\","
138                                         file="./tmp/.p.json"
139                                         sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+6]} > $file
140                                         json=$(cat $file)
141                                         targetJson=$targetJson"\"policy_data\":"$json"}"
142                                 done
143                         fi
144
145                         targetJson=$targetJson"]"
146                         targetJson="{\"policies\": $targetJson}"
147                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
148                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
149
150                         if [ $res -ne 0 ]; then
151                                 __log_test_fail_body
152                                 return 1
153                         fi
154                 fi
155         else
156                 if [ $2 != "NORIC" ]; then
157                         queryparams="?ric="$2
158                 fi
159                 if [ $3 != "NOSERVICE" ]; then
160                         if [ -z $queryparams ]; then
161                                 queryparams="?service="$3
162                         else
163                                 queryparams=$queryparams"&service="$3
164                         fi
165                 fi
166                 if [ $4 != "NOTYPE" ]; then
167                         if [ -z $queryparams ]; then
168                                 queryparams="?type="$4
169                         else
170                                 queryparams=$queryparams"&type="$4
171                         fi
172                 fi
173
174                 query="/policies"$queryparams
175                 res="$(__do_curl_to_api PA GET $query)"
176                 status=${res:${#res}-3}
177
178                 if [ $status -ne $1 ]; then
179                         __log_test_fail_status_code $1 $status
180                         return 1
181                 fi
182
183                 if [ $# -gt 4 ]; then
184                         if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
185                                 targetJson="["
186                         else
187                                 body=${res:0:${#res}-3}
188                                 targetJson="["
189                                 arr=(${@:5})
190
191                                 for ((i=0; i<$(($#-4)); i=i+5)); do
192
193                                         if [ "$targetJson" != "[" ]; then
194                                                 targetJson=$targetJson","
195                                         fi
196                                         targetJson=$targetJson"{\"id\":\"$UUID${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
197                                         if [ "${arr[$i+3]}" == "EMPTY" ]; then
198                                                 targetJson=$targetJson"\"\","
199                                         else
200                                                 targetJson=$targetJson"\"${arr[$i+3]}\","
201                                         fi
202                                         file="./tmp/.p.json"
203                                         sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
204                                         json=$(cat $file)
205                                         targetJson=$targetJson"\"json\":"$json"}"
206                                 done
207                         fi
208
209                         targetJson=$targetJson"]"
210                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
211                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
212
213                         if [ $res -ne 0 ]; then
214                                 __log_test_fail_body
215                                 return 1
216                         fi
217                 fi
218         fi
219
220         __log_test_pass
221         return 0
222
223 }
224
225
226 # API Test function: GET /policy and V2 GET /v2/policies/{policy_id}
227 # args: <response-code>  <policy-id> [<template-file>]
228 # args(V2): <response-code> <policy-id> [ <template-file> <service-name> <ric-id> <policytype-id>|NOTYPE <transient> <notification-url>|NOURL ]
229
230 # (Function for test scripts)
231 api_get_policy() {
232         __log_test_start $@
233
234
235         if [ "$PMS_VERSION" == "V2" ]; then
236                 if [ $# -ne 2 ] && [ $# -ne 8 ]; then
237                         __print_err "<response-code> <policy-id> [ <template-file> <service-name> <ric-id> <policytype-id>|NOTYPE <transient> <notification-url>|NOURL ]" $@
238                         return 1
239                 fi
240                 query="/v2/policies/$UUID$2"
241         else
242                 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
243                         __print_err "<response-code>  <policy-id> [<template-file>] " $@
244                         return 1
245                 fi
246                 query="/policy?id=$UUID$2"
247         fi
248         res="$(__do_curl_to_api PA GET $query)"
249         status=${res:${#res}-3}
250
251         if [ $status -ne $1 ]; then
252                 __log_test_fail_status_code $1 $status
253                 return 1
254         fi
255
256         if [ "$PMS_VERSION" == "V2" ]; then
257                 if [ $# -eq 8 ]; then
258
259                         #Create a policy json to compare with
260                         body=${res:0:${#res}-3}
261
262                         targetJson="\"ric_id\":\"$5\",\"policy_id\":\"$UUID$2\",\"service_id\":\"$4\""
263                         if [ $7 != "NOTRANSIENT" ]; then
264                                 targetJson=$targetJson", \"transient\":$7"
265                         fi
266                         if [ $6 != "NOTYPE" ]; then
267                                 targetJson=$targetJson", \"policytype_id\":\"$6\""
268                         else
269                                 targetJson=$targetJson", \"policytype_id\":\"\""
270                         fi
271                         if [ $8 != "NOURL" ]; then
272                                 targetJson=$targetJson", \"status_notification_uri\":\"$8\""
273                         fi
274
275                         data=$(sed 's/XXX/'${2}'/g' $3)
276                         targetJson=$targetJson", \"policy_data\":$data"
277                         targetJson="{$targetJson}"
278
279                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
280                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
281                         if [ $res -ne 0 ]; then
282                                 __log_test_fail_body
283                                 return 1
284                         fi
285                 fi
286         else
287                 if [ $# -eq 3 ]; then
288                         #Create a policy json to compare with
289                         body=${res:0:${#res}-3}
290                         file="./tmp/.p.json"
291                         sed 's/XXX/'${2}'/g' $3 > $file
292                         targetJson=$(< $file)
293                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
294                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
295                         if [ $res -ne 0 ]; then
296                                 __log_test_fail_body
297                         fi
298                 fi
299         fi
300
301         __log_test_pass
302         return 0
303 }
304
305 # API Test function: PUT /policy and V2 PUT /policies
306 # args: <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]
307 # args(V2): <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <notification-url>|NOURL <template-file> [<count>]
308 # (Function for test scripts)
309 api_put_policy() {
310         __log_test_start $@
311
312         if [ "$PMS_VERSION" == "V2" ]; then
313                 if [ $# -lt 8 ] || [ $# -gt 9 ]; then
314                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <notification-url>|NOURL <template-file> [<count>]" $@
315                         return 1
316                 fi
317         else
318                 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
319                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]" $@
320                         return 1
321                 fi
322         fi
323
324         count=0
325         max=1
326         serv=$2
327         ric=$3
328         pt=$4
329         pid=$5
330         trans=$6
331
332         if [ "$PMS_VERSION" == "V2" ]; then
333                 noti=$7
334                 temp=$8
335                 if [ $# -eq 9 ]; then
336                         max=$9
337                 fi
338         else
339                 temp=$7
340                 if [ $# -eq 8 ]; then
341                         max=$8
342                 fi
343         fi
344
345         while [ $count -lt $max ]; do
346                 if [ "$PMS_VERSION" == "V2" ]; then
347
348                         query="/v2/policies"
349
350                         inputJson="\"ric_id\":\"$ric\",\"policy_id\":\"$UUID$pid\",\"service_id\":\"$serv\""
351                         if [ $trans != "NOTRANSIENT" ]; then
352                                 inputJson=$inputJson", \"transient\":$trans"
353                         fi
354                         if [ $pt != "NOTYPE" ]; then
355                                 inputJson=$inputJson", \"policytype_id\":\"$pt\""
356                         else
357                                 inputJson=$inputJson", \"policytype_id\":\"\""
358                         fi
359                         if [ $noti != "NOURL" ]; then
360                                 inputJson=$inputJson", \"status_notification_uri\":\"$noti\""
361                         fi
362                         file="./tmp/.p.json"
363                         data=$(sed 's/XXX/'${pid}'/g' $temp)
364                         inputJson=$inputJson", \"policy_data\":$data"
365                         inputJson="{$inputJson}"
366                         echo $inputJson > $file
367                 else
368                         query="/policy?id=$UUID$pid&ric=$ric&service=$serv"
369
370                         if [ $pt != "NOTYPE" ]; then
371                                 query=$query"&type=$pt"
372                         fi
373
374                         if [ $trans != NOTRANSIENT ]; then
375                                 query=$query"&transient=$trans"
376                         fi
377
378                         file="./tmp/.p.json"
379                         sed 's/XXX/'${pid}'/g' $temp > $file
380                 fi
381         res="$(__do_curl_to_api PA PUT $query $file)"
382         status=${res:${#res}-3}
383                 echo -ne " Executing "$count"("$max")${SAMELINE}"
384                 if [ $status -ne $1 ]; then
385                         echo " Executed "$count"?("$max")"
386                         __log_test_fail_status_code $1 $status
387                         return 1
388                 fi
389
390                 let pid=$pid+1
391                 let count=$count+1
392                 echo -ne " Executed  "$count"("$max")${SAMELINE}"
393         done
394         echo ""
395
396         __log_test_pass
397         return 0
398 }
399
400 # API Test function: PUT /policy and V2 PUT /policies, to run in batch
401 # args: <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <template-file> [<count>]
402 # args(V2): <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <notification-url>|NOURL <template-file> [<count>]
403 # (Function for test scripts)
404
405 api_put_policy_batch() {
406         __log_test_start $@
407
408         if [ "$PMS_VERSION" == "V2" ]; then
409                 if [ $# -lt 8 ] || [ $# -gt 9 ]; then
410                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <notification-url>|NOURL <template-file> [<count>]" $@
411                         return 1
412                 fi
413         else
414                 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
415                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <template-file> [<count>]" $@
416                         return 1
417                 fi
418         fi
419
420         count=0
421         max=1
422         serv=$2
423         ric=$3
424         pt=$4
425         pid=$5
426         trans=$6
427         if [ "$PMS_VERSION" == "V2" ]; then
428                 noti=$7
429                 temp=$8
430                 if [ $# -eq 9 ]; then
431                         max=$9
432                 fi
433         else
434                 temp=$7
435                 if [ $# -eq 8 ]; then
436                         max=$8
437                 fi
438         fi
439
440         ARR=""
441         while [ $count -lt $max ]; do
442                 if [ "$PMS_VERSION" == "V2" ]; then
443                         query="/v2/policies"
444
445                         inputJson="\"ric_id\":\"$ric\",\"policy_id\":\"$UUID$pid\",\"service_id\":\"$serv\""
446                         if [ $trans != "NOTRANSIENT" ]; then
447                                 inputJson=$inputJson", \"transient\":$trans"
448                         fi
449                         if [ $pt != "NOTYPE" ]; then
450                                 inputJson=$inputJson", \"policytype_id\":\"$pt\""
451                         else
452                                 inputJson=$inputJson", \"policytype_id\":\"\""
453                         fi
454                         if [ $noti != "NOURL" ]; then
455                                 inputJson=$inputJson", \"status_notification_uri\":\"$noti\""
456                         fi
457                         file="./tmp/.p.json"
458                         data=$(sed 's/XXX/'${pid}'/g' $temp)
459                         inputJson=$inputJson", \"policy_data\":$data"
460                         inputJson="{$inputJson}"
461                         echo $inputJson > $file
462                 else
463                         query="/policy?id=$UUID$pid&ric=$ric&service=$serv"
464
465                         if [ $pt != "NOTYPE" ]; then
466                                 query=$query"&type=$pt"
467                         fi
468
469                         if [ $trans != NOTRANSIENT ]; then
470                                 query=$query"&transient=$trans"
471                         fi
472                         file="./tmp/.p.json"
473                         sed 's/XXX/'${pid}'/g' $temp > $file
474                 fi
475         res="$(__do_curl_to_api PA PUT_BATCH $query $file)"
476         status=${res:${#res}-3}
477                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
478
479                 if [ $status -ne 200 ]; then
480                         echo " Requested(batch) "$count"?("$max")"
481                         __log_test_fail_status_code 200 $status
482                         return 1
483                 fi
484                 cid=${res:0:${#res}-3}
485                 ARR=$ARR" "$cid
486                 let pid=$pid+1
487                 let count=$count+1
488                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
489         done
490
491         echo ""
492         count=0
493         for cid in $ARR; do
494
495         res="$(__do_curl_to_api PA RESPONSE $cid)"
496         status=${res:${#res}-3}
497                 echo -ne " Accepting(batch) "$count"("$max")${SAMELINE}"
498
499                 if [ $status -ne $1 ]; then
500                         echo " Accepted(batch) "$count"?("$max")"
501                         __log_test_fail_status_code $1 $status
502                         return 1
503                 fi
504
505                 let count=$count+1
506                 echo -ne " Accepted(batch)  "$count"("$max")${SAMELINE}"
507         done
508
509         echo ""
510
511         __log_test_pass
512         return 0
513 }
514
515 # API Test function: PUT /policy and V2 PUT /policies, to run in i parallel for a number of rics
516 # 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>
517 # args(V2): <response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <notification-url>|NOURL <template-file> <count-per-ric> <number-of-threads>
518 # (Function for test scripts)
519 api_put_policy_parallel() {
520         __log_test_start $@
521
522         if [ "$PMS_VERSION" == "V2" ]; then
523                 if [ $# -ne 11 ]; then
524                         __print_err "<response-code> <service-name> <ric-id-base> <number-of-rics> <policytype-id> <policy-start-id> <transient> <notification-url>|NOURL <template-file> <count-per-ric> <number-of-threads>" $@
525                         return 1
526                 fi
527         else
528                 if [ $# -ne 10 ]; then
529                         __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>" $@
530                         return 1
531                 fi
532         fi
533         resp_code=$1; shift;
534         serv=$1; shift
535         ric_base=$1; shift;
536         num_rics=$1; shift;
537         type=$1; shift;
538         start_id=$1; shift;
539         transient=$1; shift;
540         if [ "$PMS_VERSION" == "V2" ]; then
541                 noti=$1; shift;
542         else
543                 noti=""
544         fi
545         template=$1; shift;
546         count=$1; shift;
547         pids=$1; shift;
548
549         if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
550                 echo " Info - api_put_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         if [ "$PMS_VERSION" == "V2" ]; then
554                 if [ $serv == "NOSERVICE" ]; then
555                         serv=""
556                 fi
557                 query="/v2/policies"
558         else
559                 if [ $serv == "NOSERVICE" ]; then
560                         serv=""
561                 fi
562                 query="/policy?service=$serv"
563
564                 if [ $type != "NOTYPE" ]; then
565                         query=$query"&type=$type"
566                 fi
567
568                 if [ $transient != NOTRANSIENT ]; then
569                         query=$query"&transient=$transient"
570                 fi
571         fi
572
573         urlbase=${ADAPTER}${query}
574
575         for ((i=1; i<=$pids; i++))
576         do
577                 uuid=$UUID
578                 if [ -z "$uuid" ]; then
579                         uuid="NOUUID"
580                 fi
581                 echo "" > "./tmp/.pid${i}.res.txt"
582                 if [ "$PMS_VERSION" == "V2" ]; then
583                         echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $serv $type $transient $noti $template $count $pids $i > "./tmp/.pid${i}.txt"
584                 else
585                         echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $template $count $pids $i > "./tmp/.pid${i}.txt"
586                 fi
587                 echo $i
588         done  | xargs -n 1 -I{} -P $pids bash -c '{
589                 arg=$(echo {})
590                 echo " Parallel process $arg started"
591                 tmp=$(< "./tmp/.pid${arg}.txt")
592                 python3 ../common/create_policies_process.py $tmp > ./tmp/.pid${arg}.res.txt
593         }'
594         msg=""
595         for ((i=1; i<=$pids; i++))
596         do
597                 file="./tmp/.pid${i}.res.txt"
598                 tmp=$(< $file)
599                 if [ -z "$tmp" ]; then
600                         echo " Process $i : unknown result (result file empty"
601                         msg="failed"
602                 else
603                         res=${tmp:0:1}
604                         if [ $res == "0" ]; then
605                                 echo " Process $i : OK - "${tmp:1}
606                         else
607                                 echo " Process $i : failed - "${tmp:1}
608                                 msg="failed"
609                         fi
610                 fi
611         done
612         if [ -z $msg ]; then
613                 __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
614                 return 0
615         fi
616
617         __log_test_fail_general "One of more processes failed to execute"
618         return 1
619 }
620
621 # API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}
622 # args: <response-code> <policy-id> [count]
623 # (Function for test scripts)
624 api_delete_policy() {
625         __log_test_start $@
626
627     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
628         __print_err "<response-code> <policy-id> [count]" $@
629         return 1
630     fi
631
632         count=0
633         max=1
634
635         if [ $# -eq 3 ]; then
636                 max=$3
637         fi
638
639         pid=$2
640
641         while [ $count -lt $max ]; do
642                 if [ "$PMS_VERSION" == "V2" ]; then
643                         query="/v2/policies/"$UUID$pid
644                 else
645                         query="/policy?id="$UUID$pid
646                 fi
647                 res="$(__do_curl_to_api PA DELETE $query)"
648                 status=${res:${#res}-3}
649                 echo -ne " Executing "$count"("$max")${SAMELINE}"
650
651                 if [ $status -ne $1 ]; then
652                         echo " Executed "$count"?("$max")"
653                         __log_test_fail_status_code $1 $status
654                         return 1
655                 fi
656                 let pid=$pid+1
657                 let count=$count+1
658                 echo -ne " Executed  "$count"("$max")${SAMELINE}"
659         done
660         echo ""
661
662         __log_test_pass
663         return 0
664 }
665
666 # API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}, to run in batch
667 # args: <response-code> <policy-id> [count]
668 # (Function for test scripts)
669 api_delete_policy_batch() {
670         __log_test_start $@
671
672     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
673         __print_err "<response-code> <policy-id> [count]" $@
674         return 1
675     fi
676
677         count=0
678         max=1
679
680         if [ $# -eq 3 ]; then
681                 max=$3
682         fi
683
684         pid=$2
685         ARR=""
686         while [ $count -lt $max ]; do
687                 if [ "$PMS_VERSION" == "V2" ]; then
688                         query="/v2/policies/"$UUID$pid
689                 else
690                         query="/policy?id="$UUID$pid
691                 fi
692                 res="$(__do_curl_to_api PA DELETE_BATCH $query)"
693                 status=${res:${#res}-3}
694                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
695
696                 if [ $status -ne 200 ]; then
697                         echo " Requested(batch) "$count"?("$max")"
698                         __log_test_fail_status_code 200 $status
699                         return 1
700                 fi
701                 cid=${res:0:${#res}-3}
702                 ARR=$ARR" "$cid
703                 let pid=$pid+1
704                 let count=$count+1
705                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
706         done
707
708         echo ""
709
710         count=0
711         for cid in $ARR; do
712
713         res="$(__do_curl_to_api PA RESPONSE $cid)"
714         status=${res:${#res}-3}
715                 echo -ne " Deleting(batch) "$count"("$max")${SAMELINE}"
716
717                 if [ $status -ne $1 ]; then
718                         echo " Deleted(batch) "$count"?("$max")"
719                         __log_test_fail_status_code $1 $status
720                         return 1
721                 fi
722
723                 let count=$count+1
724                 echo -ne " Deleted(batch)  "$count"("$max")${SAMELINE}"
725         done
726
727         echo ""
728
729         __log_test_pass
730         return 0
731 }
732
733 # API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}, to run in i parallel for a number of rics
734 # args: <response-code> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>
735 # (Function for test scripts)
736 api_delete_policy_parallel() {
737         __log_test_start $@
738
739     if [ $# -ne 5 ]; then
740         __print_err " <response-code> <ric-id-base> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>" $@
741         return 1
742     fi
743         resp_code=$1; shift;
744         num_rics=$1; shift;
745         start_id=$1; shift;
746         count=$1; shift;
747         pids=$1; shift;
748
749         if [ $ADAPTER != $RESTBASE ] && [ $ADAPTER != $RESTBASE_SECURE ]; then
750                 echo " Info - api_delete_policy_parallel uses only the agent REST interface - create over dmaap in parallel is not supported"
751                 echo " Info - will execute over agent REST"
752         fi
753
754         if [ "$PMS_VERSION" == "V2" ]; then
755                 query="/v2/policies/"
756         else
757                 query="/policy"
758         fi
759
760         urlbase=${ADAPTER}${query}
761
762         for ((i=1; i<=$pids; i++))
763         do
764                 uuid=$UUID
765                 if [ -z "$uuid" ]; then
766                         uuid="NOUUID"
767                 fi
768                 echo "" > "./tmp/.pid${i}.del.res.txt"
769                 echo $resp_code $urlbase $num_rics $uuid $start_id $count $pids $i > "./tmp/.pid${i}.del.txt"
770                 echo $i
771         done  | xargs -n 1 -I{} -P $pids bash -c '{
772                 arg=$(echo {})
773                 echo " Parallel process $arg started"
774                 tmp=$(< "./tmp/.pid${arg}.del.txt")
775                 python3 ../common/delete_policies_process.py $tmp > ./tmp/.pid${arg}.del.res.txt
776         }'
777         msg=""
778         for ((i=1; i<=$pids; i++))
779         do
780                 file="./tmp/.pid${i}.del.res.txt"
781                 tmp=$(< $file)
782                 if [ -z "$tmp" ]; then
783                         echo " Process $i : unknown result (result file empty"
784                         msg="failed"
785                 else
786                         res=${tmp:0:1}
787                         if [ $res == "0" ]; then
788                                 echo " Process $i : OK - "${tmp:1}
789                         else
790                                 echo " Process $i : failed - "${tmp:1}
791                                 msg="failed"
792                         fi
793                 fi
794         done
795         if [ -z $msg ]; then
796                 __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
797                 return 0
798         fi
799
800         __log_test_fail_general "One of more processes failed to execute"
801         return 1
802 }
803
804 # API Test function: GET /policy_ids and V2 GET /v2/policies
805 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
806 # (Function for test scripts)
807 api_get_policy_ids() {
808         __log_test_start $@
809
810     if [ $# -lt 4 ]; then
811                 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
812                 return 1
813         fi
814
815         queryparams=""
816
817         if [ "$PMS_VERSION" == "V2" ]; then
818                 if [ $2 != "NORIC" ]; then
819                         queryparams="?ric_id="$2
820                 fi
821
822                 if [ $3 != "NOSERVICE" ]; then
823                         if [ -z $queryparams ]; then
824                                 queryparams="?service_id="$3
825                         else
826                                 queryparams=$queryparams"&service_id="$3
827                         fi
828                 fi
829                 if [ $4 != "NOTYPE" ]; then
830                         if [ -z $queryparams ]; then
831                                 queryparams="?policytype_id="$4
832                         else
833                                 queryparams=$queryparams"&policytype_id="$4
834                         fi
835                 fi
836
837                 query="/v2/policies"$queryparams
838         else
839                 if [ $2 != "NORIC" ]; then
840                         queryparams="?ric="$2
841                 fi
842
843                 if [ $3 != "NOSERVICE" ]; then
844                         if [ -z $queryparams ]; then
845                                 queryparams="?service="$3
846                         else
847                                 queryparams=$queryparams"&service="$3
848                         fi
849                 fi
850                 if [ $4 != "NOTYPE" ]; then
851                         if [ -z $queryparams ]; then
852                                 queryparams="?type="$4
853                         else
854                                 queryparams=$queryparams"&type="$4
855                         fi
856                 fi
857
858                 query="/policy_ids"$queryparams
859         fi
860
861     res="$(__do_curl_to_api PA GET $query)"
862     status=${res:${#res}-3}
863
864         if [ $status -ne $1 ]; then
865                 __log_test_fail_status_code $1 $status
866                 return 1
867         fi
868
869         if [ $# -gt 4 ]; then
870                 body=${res:0:${#res}-3}
871                 targetJson="["
872
873                 for pid in ${@:5} ; do
874                         if [ "$targetJson" != "[" ]; then
875                                 targetJson=$targetJson","
876                         fi
877                         if [ $pid != "NOID" ]; then
878                                 targetJson=$targetJson"\"$UUID$pid\""
879                         fi
880                 done
881
882                 targetJson=$targetJson"]"
883                 if [ "$PMS_VERSION" == "V2" ]; then
884                         targetJson="{\"policy_ids\": $targetJson}"
885                 fi
886                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
887                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
888
889                 if [ $res -ne 0 ]; then
890                         __log_test_fail_body
891                         return 1
892                 fi
893         fi
894
895         __log_test_pass
896         return 0
897 }
898
899 # API Test function: V2 GET /v2/policy-types/{policyTypeId}
900 # args(V2): <response-code> <policy-type-id> [<schema-file>]
901 # (Function for test scripts)
902 api_get_policy_type() {
903         __log_test_start $@
904
905         if [ "$PMS_VERSION" != "V2" ]; then
906                 __log_test_fail_not_supported
907                 return 1
908         fi
909
910     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
911         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
912         return 1
913     fi
914         query="/v2/policy-types/$2"
915
916         res="$(__do_curl_to_api PA GET $query)"
917         status=${res:${#res}-3}
918
919         if [ $status -ne $1 ]; then
920                 __log_test_fail_status_code $1 $status
921                 return 1
922         fi
923
924         if [ $# -eq 3 ]; then
925
926                 body=${res:0:${#res}-3}
927
928                 targetJson=$(< $3)
929                 targetJson="{\"policy_schema\":$targetJson}"
930                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
931                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
932
933                 if [ $res -ne 0 ]; then
934                         __log_test_fail_body
935                         return 1
936                 fi
937         fi
938
939         __log_test_pass
940         return 0
941 }
942
943 # API Test function: GET /policy_schema
944 # args: <response-code> <policy-type-id> [<schema-file>]
945 # (Function for test scripts)
946 api_get_policy_schema() {
947         __log_test_start $@
948
949         if [ "$PMS_VERSION" == "V2" ]; then
950                 __log_test_fail_not_supported
951                 return 1
952         fi
953
954     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
955         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
956         return 1
957     fi
958         query="/policy_schema?id=$2"
959         res="$(__do_curl_to_api PA GET $query)"
960         status=${res:${#res}-3}
961
962         if [ $status -ne $1 ]; then
963                 __log_test_fail_status_code $1 $status
964                 return 1
965         fi
966
967         if [ $# -eq 3 ]; then
968
969                 body=${res:0:${#res}-3}
970
971                 targetJson=$(< $3)
972
973                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
974                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
975
976                 if [ $res -ne 0 ]; then
977                         __log_test_fail_body
978                         return 1
979                 fi
980         fi
981
982         __log_test_pass
983         return 0
984 }
985
986 # API Test function: GET /policy_schemas
987 # args: <response-code>  <ric-id>|NORIC [<schema-file>|NOFILE]*
988 # args(V2): <response-code>
989 # (Function for test scripts)
990 api_get_policy_schemas() {
991         __log_test_start $@
992
993         if [ "$PMS_VERSION" == "V2" ]; then
994                 if [ $# -ne 1 ]; then
995                         __print_err "<response-code>" $@
996                         return 1
997                 fi
998         else
999                 if [ $# -lt 2 ]; then
1000                         __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
1001                         return 1
1002                 fi
1003         fi
1004         if [ "$PMS_VERSION" == "V2" ]; then
1005                 query="/v2/policy-schemas"
1006         else
1007                 query="/policy_schemas"
1008                 if [ $2 != "NORIC" ]; then
1009                         query=$query"?ric="$2
1010                 fi
1011         fi
1012
1013         res="$(__do_curl_to_api PA GET $query)"
1014         status=${res:${#res}-3}
1015
1016         if [ $status -ne $1 ]; then
1017                 __log_test_fail_status_code $1 $status
1018                 return 1
1019         fi
1020
1021         if [ $# -gt 2 ]; then
1022                 body=${res:0:${#res}-3}
1023                 targetJson="["
1024
1025                 for file in ${@:3} ; do
1026                         if [ "$targetJson" != "[" ]; then
1027                                 targetJson=$targetJson","
1028                         fi
1029                         if [ $file == "NOFILE" ]; then
1030                                 targetJson=$targetJson"{}"
1031                         else
1032                                 targetJson=$targetJson$(< $file)
1033                         fi
1034                 done
1035
1036                 targetJson=$targetJson"]"
1037                 if [ "$PMS_VERSION" == "V2" ]; then
1038                         targetJson="{\"policy_schemas\": $targetJson }"
1039                 fi
1040                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1041                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1042
1043                 if [ $res -ne 0 ]; then
1044                         __log_test_fail_body
1045                         return 1
1046                 fi
1047         fi
1048
1049         __log_test_pass
1050         return 0
1051 }
1052
1053 # API Test function: GET /policy_status and V2 GET /policies/{policy_id}/status
1054 # arg: <response-code> <policy-id> (STD|STD2 <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)
1055 # (Function for test scripts)
1056 api_get_policy_status() {
1057         __log_test_start $@
1058
1059     if [ $# -lt 4 ] || [ $# -gt 5 ]; then
1060                 __print_err "<response-code> <policy-id> (STD <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)" $@
1061                 return 1
1062         fi
1063
1064         targetJson=""
1065
1066         if [ $3 == "STD" ]; then
1067                 targetJson="{\"enforceStatus\":\"$4\""
1068                 if [ $# -eq 5 ]; then
1069                         targetJson=$targetJson",\"reason\":\"$5\""
1070                 fi
1071                 targetJson=$targetJson"}"
1072         elif [ $3 == "STD2" ]; then
1073                 if [ $4 == "EMPTY" ]; then
1074                         targetJson="{\"enforceStatus\":\"\""
1075                 else
1076                         targetJson="{\"enforceStatus\":\"$4\""
1077                 fi
1078                 if [ $# -eq 5 ]; then
1079                         if [ $5 == "EMPTY" ]; then
1080                                 targetJson=$targetJson",\"enforceReason\":\"\""
1081                         else
1082                                 targetJson=$targetJson",\"enforceReason\":\"$5\""
1083                         fi
1084                 fi
1085                 targetJson=$targetJson"}"
1086         elif [ $3 == "OSC" ]; then
1087                 targetJson="{\"instance_status\":\"$4\""
1088                 if [ $# -eq 5 ]; then
1089                         targetJson=$targetJson",\"has_been_deleted\":\"$5\""
1090                 fi
1091                 targetJson=$targetJson",\"created_at\":\"????\"}"
1092         else
1093                 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
1094                 return 1
1095         fi
1096
1097         if [ "$PMS_VERSION" == "V2" ]; then
1098                 query="/v2/policies/$UUID$2/status"
1099                 targetJson="{\"last_modified\":\"????\",\"status\":$targetJson}"
1100         else
1101                 query="/policy_status?id="$UUID$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                 __log_test_fail_status_code $1 $status
1109                 return 1
1110         fi
1111
1112         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1113         body=${res:0:${#res}-3}
1114         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1115
1116         if [ $res -ne 0 ]; then
1117                 __log_test_fail_body
1118                 return 1
1119         fi
1120
1121         __log_test_pass
1122         return 0
1123 }
1124
1125 # API Test function: GET /policy_types and V2 GET /v2/policy-types
1126 # args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
1127 # (Function for test scripts)
1128 api_get_policy_types() {
1129         __log_test_start $@
1130
1131     if [ $# -lt 1 ]; then
1132                 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
1133                 return 1
1134         fi
1135
1136         if [ "$PMS_VERSION" == "V2" ]; then
1137                 if [ $# -eq 1 ]; then
1138                         query="/v2/policy-types"
1139                 elif [ $2 == "NORIC" ]; then
1140                         query="/v2/policy-types"
1141                 else
1142                         query="/v2/policy-types?ric_id=$2"
1143                 fi
1144         else
1145                 if [ $# -eq 1 ]; then
1146                         query="/policy_types"
1147                 elif [ $2 == "NORIC" ]; then
1148                         query="/policy_types"
1149                 else
1150                         query="/policy_types?ric=$2"
1151                 fi
1152         fi
1153
1154     res="$(__do_curl_to_api PA GET $query)"
1155     status=${res:${#res}-3}
1156
1157         if [ $status -ne $1 ]; then
1158                 __log_test_fail_status_code $1 $status
1159                 return 1
1160         fi
1161
1162         if [ $# -gt 2 ]; then
1163                 body=${res:0:${#res}-3}
1164                 targetJson="["
1165
1166                 for pid in ${@:3} ; do
1167                         if [ "$targetJson" != "[" ]; then
1168                                 targetJson=$targetJson","
1169                         fi
1170                         if [ $pid == "EMPTY" ]; then
1171                                 pid=""
1172                         fi
1173                         targetJson=$targetJson"\"$pid\""
1174                 done
1175
1176                 targetJson=$targetJson"]"
1177                 if [ "$PMS_VERSION" == "V2" ]; then
1178                         targetJson="{\"policytype_ids\": $targetJson }"
1179                 fi
1180                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1181                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1182
1183                 if [ $res -ne 0 ]; then
1184                         __log_test_fail_body
1185                         return 1
1186                 fi
1187         fi
1188
1189         __log_test_pass
1190         return 0
1191 }
1192
1193 #########################################################
1194 #### Test case functions Health check
1195 #########################################################
1196
1197 # API Test function: GET /status and V2 GET /status
1198 # args: <response-code>
1199 # (Function for test scripts)
1200 api_get_status() {
1201         __log_test_start $@
1202     if [ $# -ne 1 ]; then
1203                 __print_err "<response-code>" $@
1204                 return 1
1205         fi
1206         if [ "$PMS_VERSION" == "V2" ]; then
1207                 query="/v2/status"
1208         else
1209                 query="/status"
1210         fi
1211     res="$(__do_curl_to_api PA GET $query)"
1212     status=${res:${#res}-3}
1213
1214         if [ $status -ne $1 ]; then
1215                 __log_test_fail_status_code $1 $status
1216                 return 1
1217         fi
1218
1219         __log_test_pass
1220         return 0
1221 }
1222
1223 #########################################################
1224 #### Test case functions RIC Repository
1225 #########################################################
1226
1227 # API Test function: GET /ric and V2 GET /v2/rics/ric
1228 # args: <reponse-code> <management-element-id> [<ric-id>]
1229 # (V2) args: <reponse-code> <management-element-id>|NOME <ric-id>|<NORIC> [<string-of-ricinfo>]
1230 # (V2) example of <string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4"
1231 # (V2) format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1232
1233
1234 # (Function for test scripts)
1235 api_get_ric() {
1236         __log_test_start $@
1237
1238         if [ "$PMS_VERSION" == "V2" ]; then
1239                 if [ $# -lt 3 ]; then
1240                         __print_err "<reponse-code> <management-element-id>|NOME <ric-id>|<NORIC> [string-of-ricinfo>]" $@
1241                         return 1
1242                 fi
1243                 search=""
1244                 if [ $2 != "NOME" ]; then
1245                         search="?managed_element_id="$2
1246                 fi
1247                 if [ $3 != "NORIC" ]; then
1248                         if [ -z $search ]; then
1249                                 search="?ric_id="$3
1250                         else
1251                                 search=$search"&ric_id="$3
1252                         fi
1253                 fi
1254                 query="/v2/rics/ric"$search
1255
1256                 res="$(__do_curl_to_api PA GET $query)"
1257                 status=${res:${#res}-3}
1258
1259                 if [ $status -ne $1 ]; then
1260                         __log_test_fail_status_code $1 $status
1261                         return 1
1262                 fi
1263
1264                 if [ $# -gt 3 ]; then
1265                         body=${res:0:${#res}-3}
1266                         res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V2" "$4" )
1267                         if [ $res -ne 0 ]; then
1268                                 __log_test_fail_general "Could not create target ric info json"
1269                                 return 1
1270                         fi
1271
1272                         targetJson=$(<./tmp/.tmp_rics.json)
1273                         targetJson=${targetJson:1:${#targetJson}-2} #remove array brackets
1274                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
1275                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1276                         if [ $res -ne 0 ]; then
1277                                 __log_test_fail_body
1278                                 return 1
1279                         fi
1280                 fi
1281         else
1282                 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1283                         __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
1284                         return 1
1285                 fi
1286
1287                 query="/ric?managedElementId="$2
1288
1289                 res="$(__do_curl_to_api PA GET $query)"
1290                 status=${res:${#res}-3}
1291
1292                 if [ $status -ne $1 ]; then
1293                         __log_test_fail_status_code $1 $status
1294                         return 1
1295                 fi
1296
1297                 if [ $# -eq 3 ]; then
1298                         body=${res:0:${#res}-3}
1299                         if [ "$body" != "$3" ]; then
1300                                 __log_test_fail_body
1301                                 return 1
1302                         fi
1303                 fi
1304         fi
1305         __log_test_pass
1306         return 0
1307 }
1308
1309 # API test function: GET /rics and V2 GET /v2/rics
1310 # args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
1311 # 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_........."
1312 # format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1313 # (Function for test scripts)
1314 api_get_rics() {
1315         __log_test_start $@
1316
1317     if [ $# -lt 2 ]; then
1318                 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
1319                 return 1
1320         fi
1321
1322         if [ "$PMS_VERSION" == "V2" ]; then
1323                 query="/v2/rics"
1324                 if [ $2 != "NOTYPE" ]; then
1325                         query="/v2/rics?policytype_id="$2
1326                 fi
1327         else
1328                 query="/rics"
1329                 if [ $2 != "NOTYPE" ]; then
1330                         query="/rics?policyType="$2
1331                 fi
1332         fi
1333
1334     res="$(__do_curl_to_api PA GET $query)"
1335     status=${res:${#res}-3}
1336
1337         if [ $status -ne $1 ]; then
1338                 __log_test_fail_status_code $1 $status
1339                 return 1
1340         fi
1341
1342         if [ $# -gt 2 ]; then
1343                 body=${res:0:${#res}-3}
1344                 if [ "$PMS_VERSION" == "V2" ]; then
1345                         res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V2" "$3" )
1346                 else
1347                         res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V1" "$3" )
1348                 fi
1349                 if [ $res -ne 0 ]; then
1350                         __log_test_fail_general "Could not create target ric info json"
1351                         return 1
1352                 fi
1353
1354                 targetJson=$(<./tmp/.tmp_rics.json)
1355                 if [ "$PMS_VERSION" == "V2" ]; then
1356                         targetJson="{\"rics\": $targetJson }"
1357                 fi
1358         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1359                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1360                 if [ $res -ne 0 ]; then
1361                         __log_test_fail_body
1362                         return 1
1363                 fi
1364         fi
1365
1366         __log_test_pass
1367         return 0
1368 }
1369
1370 ##################################################################
1371 #### API Test case functions Service registry and supervision ####
1372 ##################################################################
1373
1374 # API test function: PUT /service and V2 PUT /service
1375 # args: <response-code>  <service-name> <keepalive-timeout> <callbackurl>
1376 # (Function for test scripts)
1377 api_put_service() {
1378         __log_test_start $@
1379     if [ $# -ne 4 ]; then
1380         __print_err "<response-code>  <service-name> <keepalive-timeout> <callbackurl>" $@
1381         return 1
1382     fi
1383
1384         if [ "$PMS_VERSION" == "V2" ]; then
1385                 query="/v2/services"
1386                 json="{\"callback_url\": \""$4"\",\"keep_alive_interval_seconds\": \""$3"\",\"service_id\": \""$2"\"}"
1387         else
1388                 query="/service"
1389                 json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
1390         fi
1391     file="./tmp/.tmp.json"
1392         echo "$json" > $file
1393
1394     res="$(__do_curl_to_api PA PUT $query $file)"
1395     status=${res:${#res}-3}
1396
1397         if [ $status -ne $1 ]; then
1398                 __log_test_fail_status_code $1 $status
1399                 return 1
1400         fi
1401
1402         __log_test_pass
1403         return 0
1404 }
1405
1406 # API test function: GET /services and V2 GET /v2/services
1407 #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>]* )]
1408 # (Function for test scripts)
1409 api_get_services() {
1410         __log_test_start $@
1411         #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
1412         paramError=1
1413         if [ $# -eq 1 ]; then
1414                 paramError=0
1415         elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
1416                 paramError=0
1417         elif [ $# -eq 5 ]; then
1418                 paramError=0
1419         elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
1420                 argLen=$(($#-2))
1421                 if [ $(($argLen%3)) -eq 0 ]; then
1422                         paramError=0
1423                 fi
1424         fi
1425
1426     if [ $paramError -ne 0 ]; then
1427                 __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>]* )]" $@
1428                 return 1
1429         fi
1430
1431         if [ "$PMS_VERSION" == "V2" ]; then
1432                 query="/v2/services"
1433
1434                 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
1435                         query="/v2/services?service_id="$2
1436                 fi
1437         else
1438                 query="/services"
1439
1440                 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
1441                         query="/services?name="$2
1442                 fi
1443         fi
1444     res="$(__do_curl_to_api PA GET $query)"
1445     status=${res:${#res}-3}
1446
1447         if [ $status -ne $1 ]; then
1448                 __log_test_fail_status_code $1 $status
1449                 return 1
1450         fi
1451
1452         if [ $# -gt 2 ]; then
1453                 variableArgCount=$(($#-2))
1454                 body=${res:0:${#res}-3}
1455         targetJson="["
1456                 shift; shift;
1457                 cntr=0
1458                 while [ $cntr -lt $variableArgCount ]; do
1459                         servicename=$1; shift;
1460                         timeout=$1; shift;
1461                         callback=$1; shift;
1462                         if [ $cntr -gt 0 ]; then
1463                                 targetJson=$targetJson","
1464                         fi
1465                         # timeSinceLastActivitySeconds value cannot be checked since value varies
1466                         if [ "$PMS_VERSION" == "V2" ]; then
1467                                 targetJson=$targetJson"{\"service_id\": \""$servicename"\",\"keep_alive_interval_seconds\": "$timeout",\"time_since_last_activity_seconds\":\"????\",\"callback_url\": \""$callback"\"}"
1468                         else
1469                                 targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
1470                         fi
1471                         let cntr=cntr+3
1472                 done
1473                 targetJson=$targetJson"]"
1474                 if [ "$PMS_VERSION" == "V2" ]; then
1475                         targetJson="{\"service_list\": $targetJson }"
1476                 fi
1477                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1478                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1479                 if [ $res -ne 0 ]; then
1480                         __log_test_fail_body
1481                         return 1
1482                 fi
1483         fi
1484
1485         __log_test_pass
1486         return 0
1487 }
1488
1489 # API test function: GET /services V2 GET /v2/services -  (only checking service names)
1490 # args: <response-code> [<service-name>]*"
1491 # (Function for test scripts)
1492 api_get_service_ids() {
1493         __log_test_start $@
1494
1495     if [ $# -lt 1 ]; then
1496                 __print_err "<response-code> [<service-name>]*" $@
1497                 return 1
1498         fi
1499
1500         if [ "$PMS_VERSION" == "V2" ]; then
1501             query="/v2/services"
1502         else
1503         query="/services"
1504         fi
1505     res="$(__do_curl_to_api PA GET $query)"
1506     status=${res:${#res}-3}
1507
1508         if [ $status -ne $1 ]; then
1509                 __log_test_fail_status_code $1 $status
1510                 return 1
1511         fi
1512
1513         body=${res:0:${#res}-3}
1514         targetJson="["
1515         for rapp in ${@:2} ; do
1516                 if [ "$targetJson" != "[" ]; then
1517                         targetJson=$targetJson","
1518                 fi
1519                 if [ "$PMS_VERSION" == "V2" ]; then
1520                         targetJson=$targetJson"{\"callback_url\":\"????\",\"keep_alive_interval_seconds\":\"????\",\"service_id\":\""$rapp"\",\"time_since_last_activity_seconds\":\"????\"}"
1521                 else
1522                         targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
1523                 fi
1524         done
1525
1526         targetJson=$targetJson"]"
1527         if [ "$PMS_VERSION" == "V2" ]; then
1528                 targetJson="{\"service_list\": $targetJson }"
1529         fi
1530         echo "TARGET JSON: $targetJson" >> $HTTPLOG
1531         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1532
1533         if [ $res -ne 0 ]; then
1534                 __log_test_fail_body
1535                 return 1
1536         fi
1537
1538         __log_test_pass
1539         return 0
1540 }
1541
1542 # API test function: DELETE /services and V2 DELETE /v2/services/{serviceId}
1543 # args: <response-code> <service-name>
1544 # (Function for test scripts)
1545 api_delete_services() {
1546         __log_test_start $@
1547
1548     if [ $# -ne 2 ]; then
1549                 __print_err "<response-code> <service-name>" $@
1550                 return 1
1551         fi
1552         if [ "$PMS_VERSION" == "V2" ]; then
1553                 query="/v2/services/"$2
1554         else
1555                 query="/services?name="$2
1556         fi
1557     res="$(__do_curl_to_api PA DELETE $query)"
1558     status=${res:${#res}-3}
1559
1560         if [ $status -ne $1 ]; then
1561                 __log_test_fail_status_code $1 $status
1562                 return 1
1563         fi
1564
1565         __log_test_pass
1566         return 0
1567 }
1568
1569 # API test function: PUT /services/keepalive and V2 PUT /v2/services/{service_id}/keepalive
1570 # args: <response-code> <service-name>
1571 # (Function for test scripts)
1572 api_put_services_keepalive() {
1573         __log_test_start $@
1574
1575     if [ $# -ne 2 ]; then
1576                 __print_err "<response-code> <service-name>" $@
1577                 return 1
1578         fi
1579         if [ "$PMS_VERSION" == "V2" ]; then
1580                 query="/v2/services/$2/keepalive"
1581         else
1582         query="/services/keepalive?name="$2
1583         fi
1584
1585     res="$(__do_curl_to_api PA PUT $query)"
1586     status=${res:${#res}-3}
1587
1588         if [ $status -ne $1 ]; then
1589                 __log_test_fail_status_code $1 $status
1590                 return 1
1591         fi
1592
1593         __log_test_pass
1594         return 0
1595 }
1596
1597 ##################################################################
1598 #### API Test case functions Configuration                    ####
1599 ##################################################################
1600
1601 # API Test function: PUT /v2/configuration
1602 # args: <response-code> <config-file>
1603 # (Function for test scripts)
1604 api_put_configuration() {
1605         __log_test_start $@
1606
1607         if [ "$PMS_VERSION" != "V2" ]; then
1608                 __log_test_fail_not_supported
1609                 return 1
1610         fi
1611
1612     if [ $# -ne 2 ]; then
1613         __print_err "<response-code> <config-file>" $@
1614         return 1
1615     fi
1616         if [ ! -f $2 ]; then
1617                 _log_test_fail_general "Config file "$2", does not exist"
1618                 return 1
1619         fi
1620         inputJson=$(< $2)
1621         inputJson="{\"config\":"$inputJson"}"
1622         file="./tmp/.config.json"
1623         echo $inputJson > $file
1624         query="/v2/configuration"
1625         res="$(__do_curl_to_api PA PUT $query $file)"
1626         status=${res:${#res}-3}
1627
1628         if [ $status -ne $1 ]; then
1629                 __log_test_fail_status_code $1 $status
1630                 return 1
1631         fi
1632
1633         __log_test_pass
1634         return 0
1635 }
1636
1637 # API Test function: GET /v2/configuration
1638 # args: <response-code> [<config-file>]
1639 # (Function for test scripts)
1640 api_get_configuration() {
1641         __log_test_start $@
1642
1643         if [ "$PMS_VERSION" != "V2" ]; then
1644                 __log_test_fail_not_supported
1645                 return 1
1646         fi
1647
1648     if [ $# -lt 1 ] || [ $# -gt 2 ]; then
1649         __print_err "<response-code> [<config-file>]" $@
1650         return 1
1651     fi
1652         if [ ! -f $2 ]; then
1653                 _log_test_fail_general "Config file "$2" for comparison, does not exist"
1654                 return 1
1655         fi
1656
1657         query="/v2/configuration"
1658         res="$(__do_curl_to_api PA GET $query)"
1659         status=${res:${#res}-3}
1660
1661         if [ $status -ne $1 ]; then
1662                 __log_test_fail_status_code $1 $status
1663                 return 1
1664         fi
1665
1666         if [ $# -eq 2 ]; then
1667
1668                 body=${res:0:${#res}-3}
1669
1670                 targetJson=$(< $2)
1671                 targetJson="{\"config\":"$targetJson"}"
1672                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1673                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1674
1675                 if [ $res -ne 0 ]; then
1676                         __log_test_fail_body
1677                         return 1
1678                 fi
1679         fi
1680
1681         __log_test_pass
1682         return 0
1683 }