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