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