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