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