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