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