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