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