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