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