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