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