ccc424e65df8a7972d7c95c327e97da53fa40c50
[nonrtric.git] / test / common / a1pms_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021 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 prestarted 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 resouces 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-prexix>
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 kubernets 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 prestarted apps.
98 # args: -
99 __A1PMS_statisics_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 testscipt
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 perpare the consul configuration according to the current simulator configuration
460 # args: SDNC|NOSDNC <output-file>
461 # (Function for test scripts)
462 prepare_a1pms_config() {
463         echo -e $BOLD"Prepare Consul config"$EBOLD
464
465         echo " Writing consul config for "$A1PMS_APP_NAME" to file: "$2
466
467         if [ $# != 2 ];  then
468                 ((RES_CONF_FAIL++))
469         __print_err "need two args,  SDNC|NOSDNC <output-file>" $@
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
495         config_json=$config_json"\n   \"streams_publishes\": {"
496         config_json=$config_json"\n                            \"dmaap_publisher\": {"
497         config_json=$config_json"\n                              \"type\": \"message-router\","
498         config_json=$config_json"\n                              \"dmaap_info\": {"
499         config_json=$config_json"\n                                \"topic_url\": \"$MR_SERVICE_PATH$MR_WRITE_URL\""
500         config_json=$config_json"\n                              }"
501         config_json=$config_json"\n                            }"
502         config_json=$config_json"\n   },"
503         config_json=$config_json"\n   \"streams_subscribes\": {"
504         config_json=$config_json"\n                             \"dmaap_subscriber\": {"
505         config_json=$config_json"\n                               \"type\": \"message-router\","
506         config_json=$config_json"\n                               \"dmaap_info\": {"
507         config_json=$config_json"\n                                   \"topic_url\": \"$MR_SERVICE_PATH$MR_READ_URL\""
508         config_json=$config_json"\n                                 }"
509         config_json=$config_json"\n                               }"
510         config_json=$config_json"\n   },"
511
512         config_json=$config_json"\n   \"ric\": ["
513
514         if [ $RUNMODE == "KUBE" ]; then
515                 result=$(kubectl $KUBECONF get pods -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
516                 rics=""
517                 ric_cntr=0
518                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
519                         for im in $result; do
520                                 if [[ $im != *"-0" ]]; then
521                                         ric_subdomain=$(kubectl $KUBECONF get pod $im -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.spec.subdomain}')
522                                         rics=$rics" "$im"."$ric_subdomain"."$KUBE_A1SIM_NAMESPACE
523                                         let ric_cntr=ric_cntr+1
524                                 fi
525                         done
526                 fi
527                 if [ $ric_cntr -eq 0 ]; then
528                         echo $YELLOW"Warning: No rics found for the configuration"$EYELLOW
529                 fi
530         else
531                 rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
532                 if [ $? -ne 0 ] || [ -z "$rics" ]; then
533                         echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
534                         ((RES_CONF_FAIL++))
535                         return 1
536                 fi
537         fi
538         cntr=0
539         for ric in $rics; do
540                 if [ $cntr -gt 0 ]; then
541                         config_json=$config_json"\n          ,"
542                 fi
543                 config_json=$config_json"\n          {"
544                 if [ $RUNMODE == "KUBE" ]; then
545                         ric_id=${ric%.*.*} #extract pod id from full hosthame
546                         ric_id=$(echo "$ric_id" | tr '-' '_')
547                 else
548                         if [ $DOCKER_COMPOSE_VERSION == "V1" ]; then
549                                 ric_id=$ric
550                         else
551                                 ric_id=$(echo "$ric" | tr '-' '_')  #ric id still needs underscore as it is different from the container name
552                         fi
553                 fi
554                 echo " Found a1 sim: "$ric_id
555                 config_json=$config_json"\n            \"name\": \"$ric_id\","
556                 config_json=$config_json"\n            \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
557                 if [ $1 == "SDNC" ]; then
558                         config_json=$config_json"\n            \"controller\": \"$SDNC_APP_NAME\","
559                 fi
560                 config_json=$config_json"\n            \"managedElementIds\": ["
561                 config_json=$config_json"\n              \"me1_$ric_id\","
562                 config_json=$config_json"\n              \"me2_$ric_id\""
563                 config_json=$config_json"\n            ]"
564                 config_json=$config_json"\n          }"
565                 let cntr=cntr+1
566         done
567
568         config_json=$config_json"\n           ]"
569         config_json=$config_json"\n}"
570
571         config_json="{\"config\":"$config_json"}"
572
573         printf "$config_json">$2
574
575         echo ""
576 }
577
578 # Load the the appl config for the a1pms into a config map
579 a1pms_load_config() {
580         echo -e $BOLD"A1PMS - load config from "$EBOLD$1
581         data_json=$PWD/tmp/$A1PMS_DATA_FILE
582         cp $1 $data_json
583         output_yaml=$PWD/tmp/a1pms-cfd.yaml
584         __kube_create_configmap $A1PMS_APP_NAME"-data" $KUBE_NONRTRIC_NAMESPACE autotest A1PMS $data_json $output_yaml
585         echo ""
586 }
587
588
589 # Turn on debug level tracing in the a1pms
590 # args: -
591 # (Function for test scripts)
592 set_a1pms_debug() {
593         echo -e $BOLD"Setting a1pms debug logging"$EBOLD
594         curlString="$A1PMS_SERVICE_PATH$A1PMS_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
595         result=$(__do_curl "$curlString")
596         if [ $? -ne 0 ]; then
597                 __print_err "could not set debug mode" $@
598                 ((RES_CONF_FAIL++))
599                 return 1
600         fi
601         echo ""
602         return 0
603 }
604
605 # Turn on trace level tracing in the a1pms
606 # args: -
607 # (Function for test scripts)
608 set_a1pms_trace() {
609         echo -e $BOLD"Setting a1pms trace logging"$EBOLD
610         curlString="$A1PMS_SERVICE_PATH$A1PMS_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
611         result=$(__do_curl "$curlString")
612         if [ $? -ne 0 ]; then
613                 __print_err "could not set trace mode" $@
614                 ((RES_CONF_FAIL++))
615                 return 1
616         fi
617         echo ""
618         return 0
619 }
620
621 # Perform curl retries when making direct call to the a1pms for the specified http response codes
622 # Speace separated list of http response codes
623 # args: [<response-code>]*
624 use_a1pms_retries() {
625         echo -e $BOLD"Do curl retries to the a1pms REST inteface for these response codes:$@"$EBOLD
626         AGENT_RETRY_CODES=$@
627         echo ""
628         return
629 }
630
631 # Check the a1pms logs for WARNINGs and ERRORs
632 # args: -
633 # (Function for test scripts)
634 check_a1pms_logs() {
635         __check_container_logs "A1PMS" $A1PMS_APP_NAME $A1PMS_LOGPATH WARN ERR
636 }
637
638 #########################################################
639 #### Test case functions A1 Policy management service
640 #########################################################
641
642 # This function compare the size, towards a target value, of a json array returned from <url> of the A1PMS.
643 # This is done immediately by setting PASS or FAIL or wait up to and optional timeout before setting PASS or FAIL
644 # args: json:<url> <target-value> [<timeout-in-seconds]
645 # (Function for test scripts)
646 a1pms_equal() {
647     echo "(${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
648         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
649                 if [[ $1 == "json:"* ]]; then
650                         if [ "$A1PMS_VERSION" == "V2" ]; then
651                                 __var_test "A1PMS" $A1PMS_SERVICE_PATH$A1PMS_API_PREFIX"/v2/" $1 "=" $2 $3
652                         else
653                                 __var_test "A1PMS" $A1PMS_SERVICE_PATH"/" $1 "=" $2 $3
654                         fi
655                         return 0
656                 fi
657         fi
658         __print_err "needs two or three args: json:<json-array-param> <target-value> [ timeout ]" $@
659         return 1
660 }
661
662 # API Test function: GET /policies and V2 GET /v2/policy-instances
663 # 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>]*]
664 # 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>]*]
665 # (Function for test scripts)
666 a1pms_api_get_policies() {
667         __log_test_start $@
668
669         if [ "$A1PMS_VERSION" == "V2" ]; then
670                 paramError=0
671                 variableParams=$(($#-4))
672                 if [ $# -lt 4 ]; then
673                         paramError=1
674                 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
675                         paramError=1
676                 elif [ $# -gt 5 ] && [ $(($variableParams%7)) -ne 0 ]; then
677                         paramError=1
678                 fi
679
680                 if [ $paramError -ne 0 ]; then
681                         __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>]*]" $@
682                         return 1
683                 fi
684         else
685                 paramError=0
686                 variableParams=$(($#-4))
687                 if [ $# -lt 4 ]; then
688                         paramError=1
689                 elif [ $# -eq 5 ] && [ $5 != "NOID" ]; then
690                         paramError=1
691                 elif [ $# -gt 5 ] && [ $(($variableParams%5)) -ne 0 ]; then
692                         paramError=1
693                 fi
694
695                 if [ $paramError -ne 0 ]; then
696                         __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>]*]" $@
697                         return 1
698                 fi
699         fi
700
701         queryparams=""
702         if [ "$A1PMS_VERSION" == "V2" ]; then
703                 if [ $2 != "NORIC" ]; then
704                         queryparams="?ric_id="$2
705                 fi
706                 if [ $3 != "NOSERVICE" ]; then
707                         if [ -z $queryparams ]; then
708                                 queryparams="?service_id="$3
709                         else
710                                 queryparams=$queryparams"&service_id="$3
711                         fi
712                 fi
713                 if [ $4 != "NOTYPE" ]; then
714                         if [ -z $queryparams ]; then
715                                 queryparams="?policytype_id="$4
716                         else
717                                 queryparams=$queryparams"&policytype_id="$4
718                         fi
719                 fi
720
721                 query="/v2/policy-instances"$queryparams
722                 res="$(__do_curl_to_api A1PMS GET $query)"
723                 status=${res:${#res}-3}
724
725                 if [ $status -ne $1 ]; then
726                         __log_test_fail_status_code $1 $status
727                         return 1
728                 fi
729
730                 if [ $# -gt 4 ]; then
731                         body=${res:0:${#res}-3}
732                         if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
733                                 targetJson="["
734                         else
735                                 targetJson="["
736                                 arr=(${@:5})
737
738                                 for ((i=0; i<$(($#-4)); i=i+7)); do
739
740                                         if [ "$targetJson" != "[" ]; then
741                                                 targetJson=$targetJson","
742                                         fi
743                                         targetJson=$targetJson"{\"policy_id\":\"$UUID${arr[$i]}\",\"ric_id\":\"${arr[$i+1]}\",\"service_id\":\"${arr[$i+2]}\",\"policytype_id\":"
744                                         if [ "${arr[$i+3]}" == "EMPTY" ]; then
745                                                 targetJson=$targetJson"\"\","
746                                         else
747                                                 targetJson=$targetJson"\"${arr[$i+3]}\","
748                                         fi
749                                         targetJson=$targetJson"\"transient\":${arr[$i+4]},\"status_notification_uri\":\"${arr[$i+5]}\","
750                                         file="./tmp/.p.json"
751                                         sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+6]} > $file
752                                         json=$(cat $file)
753                                         targetJson=$targetJson"\"policy_data\":"$json"}"
754                                 done
755                         fi
756
757                         targetJson=$targetJson"]"
758                         targetJson="{\"policies\": $targetJson}"
759                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
760                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
761
762                         if [ $res -ne 0 ]; then
763                                 __log_test_fail_body
764                                 return 1
765                         fi
766                 fi
767         else
768                 if [ $2 != "NORIC" ]; then
769                         queryparams="?ric="$2
770                 fi
771                 if [ $3 != "NOSERVICE" ]; then
772                         if [ -z $queryparams ]; then
773                                 queryparams="?service="$3
774                         else
775                                 queryparams=$queryparams"&service="$3
776                         fi
777                 fi
778                 if [ $4 != "NOTYPE" ]; then
779                         if [ -z $queryparams ]; then
780                                 queryparams="?type="$4
781                         else
782                                 queryparams=$queryparams"&type="$4
783                         fi
784                 fi
785
786                 query="/policies"$queryparams
787                 res="$(__do_curl_to_api A1PMS GET $query)"
788                 status=${res:${#res}-3}
789
790                 if [ $status -ne $1 ]; then
791                         __log_test_fail_status_code $1 $status
792                         return 1
793                 fi
794
795                 if [ $# -gt 4 ]; then
796                         if [ $# -eq 5 ] && [ $5 == "NOID" ]; then
797                                 targetJson="["
798                         else
799                                 body=${res:0:${#res}-3}
800                                 targetJson="["
801                                 arr=(${@:5})
802
803                                 for ((i=0; i<$(($#-4)); i=i+5)); do
804
805                                         if [ "$targetJson" != "[" ]; then
806                                                 targetJson=$targetJson","
807                                         fi
808                                         targetJson=$targetJson"{\"id\":\"$UUID${arr[$i]}\",\"lastModified\":\"????\",\"ric\":\"${arr[$i+1]}\",\"service\":\"${arr[$i+2]}\",\"type\":"
809                                         if [ "${arr[$i+3]}" == "EMPTY" ]; then
810                                                 targetJson=$targetJson"\"\","
811                                         else
812                                                 targetJson=$targetJson"\"${arr[$i+3]}\","
813                                         fi
814                                         file="./tmp/.p.json"
815                                         sed 's/XXX/'${arr[$i]}'/g' ${arr[$i+4]} > $file
816                                         json=$(cat $file)
817                                         targetJson=$targetJson"\"json\":"$json"}"
818                                 done
819                         fi
820
821                         targetJson=$targetJson"]"
822                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
823                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
824
825                         if [ $res -ne 0 ]; then
826                                 __log_test_fail_body
827                                 return 1
828                         fi
829                 fi
830         fi
831         __collect_endpoint_stats "A1PMS" 00 "GET" $A1PMS_API_PREFIX"/v2/policy-instances" $status
832         __log_test_pass
833         return 0
834
835 }
836
837
838 # API Test function: GET /policy and V2 GET /v2/policies/{policy_id}
839 # args: <response-code>  <policy-id> [<template-file>]
840 # args(V2): <response-code> <policy-id> [ <template-file> <service-name> <ric-id> <policytype-id>|NOTYPE <transient> <notification-url>|NOURL ]
841
842 # (Function for test scripts)
843 a1pms_api_get_policy() {
844         __log_test_start $@
845
846
847         if [ "$A1PMS_VERSION" == "V2" ]; then
848                 if [ $# -ne 2 ] && [ $# -ne 8 ]; then
849                         __print_err "<response-code> <policy-id> [ <template-file> <service-name> <ric-id> <policytype-id>|NOTYPE <transient> <notification-url>|NOURL ]" $@
850                         return 1
851                 fi
852                 query="/v2/policies/$UUID$2"
853         else
854                 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
855                         __print_err "<response-code>  <policy-id> [<template-file>] " $@
856                         return 1
857                 fi
858                 query="/policy?id=$UUID$2"
859         fi
860         res="$(__do_curl_to_api A1PMS GET $query)"
861         status=${res:${#res}-3}
862
863         if [ $status -ne $1 ]; then
864                 __log_test_fail_status_code $1 $status
865                 return 1
866         fi
867
868         if [ "$A1PMS_VERSION" == "V2" ]; then
869                 if [ $# -eq 8 ]; then
870
871                         #Create a policy json to compare with
872                         body=${res:0:${#res}-3}
873
874                         targetJson="\"ric_id\":\"$5\",\"policy_id\":\"$UUID$2\",\"service_id\":\"$4\""
875                         if [ $7 != "NOTRANSIENT" ]; then
876                                 targetJson=$targetJson", \"transient\":$7"
877                         fi
878                         if [ $6 != "NOTYPE" ]; then
879                                 targetJson=$targetJson", \"policytype_id\":\"$6\""
880                         else
881                                 targetJson=$targetJson", \"policytype_id\":\"\""
882                         fi
883                         if [ $8 != "NOURL" ]; then
884                                 targetJson=$targetJson", \"status_notification_uri\":\"$8\""
885                         fi
886
887                         data=$(sed 's/XXX/'${2}'/g' $3)
888                         targetJson=$targetJson", \"policy_data\":$data"
889                         targetJson="{$targetJson}"
890
891                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
892                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
893                         if [ $res -ne 0 ]; then
894                                 __log_test_fail_body
895                                 return 1
896                         fi
897                 fi
898         else
899                 if [ $# -eq 3 ]; then
900                         #Create a policy json to compare with
901                         body=${res:0:${#res}-3}
902                         file="./tmp/.p.json"
903                         sed 's/XXX/'${2}'/g' $3 > $file
904                         targetJson=$(< $file)
905                         echo "TARGET JSON: $targetJson" >> $HTTPLOG
906                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
907                         if [ $res -ne 0 ]; then
908                                 __log_test_fail_body
909                         fi
910                 fi
911         fi
912
913         __collect_endpoint_stats "A1PMS" 01 "GET" $A1PMS_API_PREFIX"/v2/policies/{policy_id}" $status
914         __log_test_pass
915         return 0
916 }
917
918 # API Test function: PUT /policy and V2 PUT /policies
919 # args: <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]
920 # args(V2): <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <notification-url>|NOURL <template-file> [<count>]
921 # (Function for test scripts)
922 a1pms_api_put_policy() {
923         __log_test_start $@
924
925         if [ "$A1PMS_VERSION" == "V2" ]; then
926                 if [ $# -lt 8 ] || [ $# -gt 9 ]; then
927                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <notification-url>|NOURL <template-file> [<count>]" $@
928                         return 1
929                 fi
930         else
931                 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
932                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient>|NOTRANSIENT <template-file> [<count>]" $@
933                         return 1
934                 fi
935         fi
936
937         count=0
938         max=1
939         serv=$2
940         ric=$3
941         pt=$4
942         pid=$5
943         trans=$6
944
945         if [ "$A1PMS_VERSION" == "V2" ]; then
946                 noti=$7
947                 temp=$8
948                 if [ $# -eq 9 ]; then
949                         max=$9
950                 fi
951         else
952                 temp=$7
953                 if [ $# -eq 8 ]; then
954                         max=$8
955                 fi
956         fi
957
958         while [ $count -lt $max ]; do
959                 if [ "$A1PMS_VERSION" == "V2" ]; then
960
961                         query="/v2/policies"
962
963                         inputJson="\"ric_id\":\"$ric\",\"policy_id\":\"$UUID$pid\",\"service_id\":\"$serv\""
964                         if [ $trans != "NOTRANSIENT" ]; then
965                                 inputJson=$inputJson", \"transient\":$trans"
966                         fi
967                         if [ $pt != "NOTYPE" ]; then
968                                 inputJson=$inputJson", \"policytype_id\":\"$pt\""
969                         else
970                                 inputJson=$inputJson", \"policytype_id\":\"\""
971                         fi
972                         if [ $noti != "NOURL" ]; then
973                                 inputJson=$inputJson", \"status_notification_uri\":\"$noti\""
974                         fi
975                         file="./tmp/.p.json"
976                         data=$(sed 's/XXX/'${pid}'/g' $temp)
977                         inputJson=$inputJson", \"policy_data\":$data"
978                         inputJson="{$inputJson}"
979                         echo $inputJson > $file
980                 else
981                         query="/policy?id=$UUID$pid&ric=$ric&service=$serv"
982
983                         if [ $pt != "NOTYPE" ]; then
984                                 query=$query"&type=$pt"
985                         fi
986
987                         if [ $trans != NOTRANSIENT ]; then
988                                 query=$query"&transient=$trans"
989                         fi
990
991                         file="./tmp/.p.json"
992                         sed 's/XXX/'${pid}'/g' $temp > $file
993                 fi
994         res="$(__do_curl_to_api A1PMS PUT $query $file)"
995         status=${res:${#res}-3}
996                 echo -ne " Executing "$count"("$max")${SAMELINE}"
997                 if [ $status -ne $1 ]; then
998                         echo " Executed "$count"?("$max")"
999                         __log_test_fail_status_code $1 $status
1000                         return 1
1001                 fi
1002                 let pid=$pid+1
1003                 let count=$count+1
1004                 echo -ne " Executed  "$count"("$max")${SAMELINE}"
1005         done
1006         __collect_endpoint_stats "A1PMS" 02 "PUT" $A1PMS_API_PREFIX"/v2/policies" $status $max
1007         echo ""
1008
1009         __log_test_pass
1010         return 0
1011 }
1012
1013 # API Test function: PUT /policy and V2 PUT /policies, to run in batch
1014 # args: <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <template-file> [<count>]
1015 # args(V2): <response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <notification-url>|NOURL <template-file> [<count>]
1016 # (Function for test scripts)
1017
1018 a1pms_api_put_policy_batch() {
1019         __log_test_start $@
1020
1021         if [ "$A1PMS_VERSION" == "V2" ]; then
1022                 if [ $# -lt 8 ] || [ $# -gt 9 ]; then
1023                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <notification-url>|NOURL <template-file> [<count>]" $@
1024                         return 1
1025                 fi
1026         else
1027                 if [ $# -lt 7 ] || [ $# -gt 8 ]; then
1028                         __print_err "<response-code> <service-name> <ric-id> <policytype-id>|NOTYPE <policy-id> <transient> <template-file> [<count>]" $@
1029                         return 1
1030                 fi
1031         fi
1032
1033         count=0
1034         max=1
1035         serv=$2
1036         ric=$3
1037         pt=$4
1038         pid=$5
1039         trans=$6
1040         if [ "$A1PMS_VERSION" == "V2" ]; then
1041                 noti=$7
1042                 temp=$8
1043                 if [ $# -eq 9 ]; then
1044                         max=$9
1045                 fi
1046         else
1047                 temp=$7
1048                 if [ $# -eq 8 ]; then
1049                         max=$8
1050                 fi
1051         fi
1052
1053         ARR=""
1054         while [ $count -lt $max ]; do
1055                 if [ "$A1PMS_VERSION" == "V2" ]; then
1056                         query="/v2/policies"
1057
1058                         inputJson="\"ric_id\":\"$ric\",\"policy_id\":\"$UUID$pid\",\"service_id\":\"$serv\""
1059                         if [ $trans != "NOTRANSIENT" ]; then
1060                                 inputJson=$inputJson", \"transient\":$trans"
1061                         fi
1062                         if [ $pt != "NOTYPE" ]; then
1063                                 inputJson=$inputJson", \"policytype_id\":\"$pt\""
1064                         else
1065                                 inputJson=$inputJson", \"policytype_id\":\"\""
1066                         fi
1067                         if [ $noti != "NOURL" ]; then
1068                                 inputJson=$inputJson", \"status_notification_uri\":\"$noti\""
1069                         fi
1070                         file="./tmp/.p.json"
1071                         data=$(sed 's/XXX/'${pid}'/g' $temp)
1072                         inputJson=$inputJson", \"policy_data\":$data"
1073                         inputJson="{$inputJson}"
1074                         echo $inputJson > $file
1075                 else
1076                         query="/policy?id=$UUID$pid&ric=$ric&service=$serv"
1077
1078                         if [ $pt != "NOTYPE" ]; then
1079                                 query=$query"&type=$pt"
1080                         fi
1081
1082                         if [ $trans != NOTRANSIENT ]; then
1083                                 query=$query"&transient=$trans"
1084                         fi
1085                         file="./tmp/.p.json"
1086                         sed 's/XXX/'${pid}'/g' $temp > $file
1087                 fi
1088         res="$(__do_curl_to_api A1PMS PUT_BATCH $query $file)"
1089         status=${res:${#res}-3}
1090                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
1091
1092                 if [ $status -ne 200 ]; then
1093                         echo " Requested(batch) "$count"?("$max")"
1094                         __log_test_fail_status_code 200 $status
1095                         return 1
1096                 fi
1097                 cid=${res:0:${#res}-3}
1098                 ARR=$ARR" "$cid
1099                 let pid=$pid+1
1100                 let count=$count+1
1101                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
1102         done
1103
1104         echo ""
1105         count=0
1106         for cid in $ARR; do
1107
1108         res="$(__do_curl_to_api A1PMS RESPONSE $cid)"
1109         status=${res:${#res}-3}
1110                 echo -ne " Accepting(batch) "$count"("$max")${SAMELINE}"
1111
1112                 if [ $status -ne $1 ]; then
1113                         echo " Accepted(batch) "$count"?("$max")"
1114                         __log_test_fail_status_code $1 $status
1115                         return 1
1116                 fi
1117
1118                 let count=$count+1
1119                 echo -ne " Accepted(batch)  "$count"("$max")${SAMELINE}"
1120         done
1121         __collect_endpoint_stats "A1PMS" 02 "PUT" $A1PMS_API_PREFIX"/v2/policies" $1 $max
1122
1123         echo ""
1124
1125         __log_test_pass
1126         return 0
1127 }
1128
1129 # API Test function: PUT /policy and V2 PUT /policies, to run in i parallel for a number of rics
1130 # 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>
1131 # 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>
1132 # (Function for test scripts)
1133 a1pms_api_put_policy_parallel() {
1134         __log_test_start $@
1135
1136         if [ "$A1PMS_VERSION" == "V2" ]; then
1137                 if [ $# -ne 11 ]; then
1138                         __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>" $@
1139                         return 1
1140                 fi
1141         else
1142                 if [ $# -ne 10 ]; then
1143                         __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>" $@
1144                         return 1
1145                 fi
1146         fi
1147         resp_code=$1; shift;
1148         serv=$1; shift
1149         ric_base=$1; shift;
1150         num_rics=$1; shift;
1151         type=$1; shift;
1152         start_id=$1; shift;
1153         transient=$1; shift;
1154         if [ "$A1PMS_VERSION" == "V2" ]; then
1155                 noti=$1; shift;
1156         else
1157                 noti=""
1158         fi
1159         template=$1; shift;
1160         count=$1; shift;
1161         pids=$1; shift;
1162
1163         #if [ $A1PMS_ADAPTER != $RESTBASE ] && [ $A1PMS_ADAPTER != $RESTBASE_SECURE ]; then
1164         if [ $A1PMS_ADAPTER_TYPE != "REST" ]; then
1165                 echo " Info - a1pms_api_put_policy_parallel uses only the a1pms REST interface - create over dmaap in parallel is not supported"
1166                 echo " Info - will execute over a1pms REST"
1167         fi
1168         if [ "$A1PMS_VERSION" == "V2" ]; then
1169                 if [ $serv == "NOSERVICE" ]; then
1170                         serv=""
1171                 fi
1172                 query="$A1PMS_API_PREFIX/v2/policies"
1173         else
1174                 if [ $serv == "NOSERVICE" ]; then
1175                         serv=""
1176                 fi
1177                 query="/policy?service=$serv"
1178
1179                 if [ $type != "NOTYPE" ]; then
1180                         query=$query"&type=$type"
1181                 fi
1182
1183                 if [ $transient != NOTRANSIENT ]; then
1184                         query=$query"&transient=$transient"
1185                 fi
1186         fi
1187
1188         urlbase=${A1PMS_ADAPTER}${query}
1189
1190         httpproxy="NOPROXY"
1191         if [ ! -z "$KUBE_PROXY_PATH" ]; then
1192                 httpproxy=$KUBE_PROXY_PATH
1193         fi
1194
1195         for ((i=1; i<=$pids; i++))
1196         do
1197                 uuid=$UUID
1198                 if [ -z "$uuid" ]; then
1199                         uuid="NOUUID"
1200                 fi
1201                 echo "" > "./tmp/.pid${i}.res.txt"
1202                 if [ "$A1PMS_VERSION" == "V2" ]; then
1203                         echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $serv $type $transient $noti $template $count $pids $i $httpproxy > "./tmp/.pid${i}.txt"
1204                 else
1205                         echo $resp_code $urlbase $ric_base $num_rics $uuid $start_id $template $count $pids $i $httpproxy > "./tmp/.pid${i}.txt"
1206                 fi
1207                 echo $i
1208         done  | xargs -n 1 -I{} -P $pids bash -c '{
1209                 arg=$(echo {})
1210                 echo " Parallel process $arg started"
1211                 tmp=$(< "./tmp/.pid${arg}.txt")
1212                 python3 ../common/create_policies_process.py $tmp > ./tmp/.pid${arg}.res.txt
1213         }'
1214         msg=""
1215         for ((i=1; i<=$pids; i++))
1216         do
1217                 file="./tmp/.pid${i}.res.txt"
1218                 tmp=$(< $file)
1219                 if [ -z "$tmp" ]; then
1220                         echo " Process $i : unknown result (result file empty"
1221                         msg="failed"
1222                 else
1223                         res=${tmp:0:1}
1224                         if [ $res == "0" ]; then
1225                                 echo " Process $i : OK - "${tmp:1}
1226                         else
1227                                 echo " Process $i : failed - "${tmp:1}
1228                                 msg="failed"
1229                         fi
1230                 fi
1231         done
1232         if [ -z $msg ]; then
1233                 __collect_endpoint_stats "A1PMS" 02 "PUT" $A1PMS_API_PREFIX"/v2/policies" $resp_code $(($count*$num_rics))
1234                 __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
1235                 return 0
1236         fi
1237
1238         __log_test_fail_general "One of more processes failed to execute"
1239         return 1
1240 }
1241
1242 # API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}
1243 # args: <response-code> <policy-id> [count]
1244 # (Function for test scripts)
1245 a1pms_api_delete_policy() {
1246         __log_test_start $@
1247
1248     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1249         __print_err "<response-code> <policy-id> [count]" $@
1250         return 1
1251     fi
1252
1253         count=0
1254         max=1
1255
1256         if [ $# -eq 3 ]; then
1257                 max=$3
1258         fi
1259
1260         pid=$2
1261
1262         while [ $count -lt $max ]; do
1263                 if [ "$A1PMS_VERSION" == "V2" ]; then
1264                         query="/v2/policies/"$UUID$pid
1265                 else
1266                         query="/policy?id="$UUID$pid
1267                 fi
1268                 res="$(__do_curl_to_api A1PMS DELETE $query)"
1269                 status=${res:${#res}-3}
1270                 echo -ne " Executing "$count"("$max")${SAMELINE}"
1271
1272                 if [ $status -ne $1 ]; then
1273                         echo " Executed "$count"?("$max")"
1274                         __log_test_fail_status_code $1 $status
1275                         return 1
1276                 fi
1277
1278                 let pid=$pid+1
1279                 let count=$count+1
1280                 echo -ne " Executed  "$count"("$max")${SAMELINE}"
1281         done
1282         __collect_endpoint_stats "A1PMS" 03 "DELETE" $A1PMS_API_PREFIX"/v2/policies/{policy_id}" $status $max
1283         echo ""
1284
1285         __log_test_pass
1286         return 0
1287 }
1288
1289 # API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}, to run in batch
1290 # args: <response-code> <policy-id> [count]
1291 # (Function for test scripts)
1292 a1pms_api_delete_policy_batch() {
1293         __log_test_start $@
1294
1295     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1296         __print_err "<response-code> <policy-id> [count]" $@
1297         return 1
1298     fi
1299
1300         count=0
1301         max=1
1302
1303         if [ $# -eq 3 ]; then
1304                 max=$3
1305         fi
1306
1307         pid=$2
1308         ARR=""
1309         while [ $count -lt $max ]; do
1310                 if [ "$A1PMS_VERSION" == "V2" ]; then
1311                         query="/v2/policies/"$UUID$pid
1312                 else
1313                         query="/policy?id="$UUID$pid
1314                 fi
1315                 res="$(__do_curl_to_api A1PMS DELETE_BATCH $query)"
1316                 status=${res:${#res}-3}
1317                 echo -ne " Requesting(batch) "$count"("$max")${SAMELINE}"
1318
1319                 if [ $status -ne 200 ]; then
1320                         echo " Requested(batch) "$count"?("$max")"
1321                         __log_test_fail_status_code 200 $status
1322                         return 1
1323                 fi
1324                 cid=${res:0:${#res}-3}
1325                 ARR=$ARR" "$cid
1326                 let pid=$pid+1
1327                 let count=$count+1
1328                 echo -ne " Requested(batch)  "$count"("$max")${SAMELINE}"
1329         done
1330
1331         echo ""
1332
1333         count=0
1334         for cid in $ARR; do
1335
1336         res="$(__do_curl_to_api A1PMS RESPONSE $cid)"
1337         status=${res:${#res}-3}
1338                 echo -ne " Deleting(batch) "$count"("$max")${SAMELINE}"
1339
1340                 if [ $status -ne $1 ]; then
1341                         echo " Deleted(batch) "$count"?("$max")"
1342                         __log_test_fail_status_code $1 $status
1343                         return 1
1344                 fi
1345
1346                 let count=$count+1
1347                 echo -ne " Deleted(batch)  "$count"("$max")${SAMELINE}"
1348         done
1349         __collect_endpoint_stats "A1PMS" 03 "DELETE" $A1PMS_API_PREFIX"/v2/policies/{policy_id}" $1 $max
1350
1351         echo ""
1352
1353         __log_test_pass
1354         return 0
1355 }
1356
1357 # API Test function: DELETE /policy and V2 DELETE /v2/policies/{policy_id}, to run in i parallel for a number of rics
1358 # args: <response-code> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>
1359 # (Function for test scripts)
1360 a1pms_api_delete_policy_parallel() {
1361         __log_test_start $@
1362
1363     if [ $# -ne 5 ]; then
1364         __print_err " <response-code> <ric-id-base> <number-of-rics> <policy-start-id> <count-per-ric> <number-of-threads>" $@
1365         return 1
1366     fi
1367         resp_code=$1; shift;
1368         num_rics=$1; shift;
1369         start_id=$1; shift;
1370         count=$1; shift;
1371         pids=$1; shift;
1372
1373         #if [ $A1PMS_ADAPTER != $RESTBASE ] && [ $A1PMS_ADAPTER != $RESTBASE_SECURE ]; then
1374         if [ $A1PMS_ADAPTER_TYPE != "REST" ]; then
1375                 echo " Info - a1pms_api_delete_policy_parallel uses only the a1pms REST interface - create over dmaap in parallel is not supported"
1376                 echo " Info - will execute over a1pms REST"
1377         fi
1378
1379         if [ "$A1PMS_VERSION" == "V2" ]; then
1380                 query="$A1PMS_API_PREFIX/v2/policies/"
1381         else
1382                 query="/policy"
1383         fi
1384
1385         urlbase=${A1PMS_ADAPTER}${query}
1386
1387         httpproxy="NOPROXY"
1388         if [ ! -z "$KUBE_PROXY_PATH" ]; then
1389                 httpproxy=$KUBE_PROXY_PATH
1390         fi
1391
1392         for ((i=1; i<=$pids; i++))
1393         do
1394                 uuid=$UUID
1395                 if [ -z "$uuid" ]; then
1396                         uuid="NOUUID"
1397                 fi
1398                 echo "" > "./tmp/.pid${i}.del.res.txt"
1399                 echo $resp_code $urlbase $num_rics $uuid $start_id $count $pids $i $httpproxy> "./tmp/.pid${i}.del.txt"
1400                 echo $i
1401         done  | xargs -n 1 -I{} -P $pids bash -c '{
1402                 arg=$(echo {})
1403                 echo " Parallel process $arg started"
1404                 tmp=$(< "./tmp/.pid${arg}.del.txt")
1405                 python3 ../common/delete_policies_process.py $tmp > ./tmp/.pid${arg}.del.res.txt
1406         }'
1407         msg=""
1408         for ((i=1; i<=$pids; i++))
1409         do
1410                 file="./tmp/.pid${i}.del.res.txt"
1411                 tmp=$(< $file)
1412                 if [ -z "$tmp" ]; then
1413                         echo " Process $i : unknown result (result file empty"
1414                         msg="failed"
1415                 else
1416                         res=${tmp:0:1}
1417                         if [ $res == "0" ]; then
1418                                 echo " Process $i : OK - "${tmp:1}
1419                         else
1420                                 echo " Process $i : failed - "${tmp:1}
1421                                 msg="failed"
1422                         fi
1423                 fi
1424         done
1425         if [ -z $msg ]; then
1426                 __collect_endpoint_stats "A1PMS" 03 "DELETE" $A1PMS_API_PREFIX"/v2/policies/{policy_id}" $resp_code $(($count*$num_rics))
1427                 __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
1428                 return 0
1429         fi
1430
1431         __log_test_fail_general "One of more processes failed to execute"
1432         return 1
1433 }
1434
1435 # API Test function: GET /policy_ids and V2 GET /v2/policies
1436 # args: <response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)
1437 # (Function for test scripts)
1438 a1pms_api_get_policy_ids() {
1439         __log_test_start $@
1440
1441     if [ $# -lt 4 ]; then
1442                 __print_err "<response-code> <ric-id>|NORIC <service-id>|NOSERVICE <type-id>|NOTYPE ([<policy-instance-id]*|NOID)" $@
1443                 return 1
1444         fi
1445
1446         queryparams=""
1447
1448         if [ "$A1PMS_VERSION" == "V2" ]; then
1449                 if [ $2 != "NORIC" ]; then
1450                         queryparams="?ric_id="$2
1451                 fi
1452
1453                 if [ $3 != "NOSERVICE" ]; then
1454                         if [ -z $queryparams ]; then
1455                                 queryparams="?service_id="$3
1456                         else
1457                                 queryparams=$queryparams"&service_id="$3
1458                         fi
1459                 fi
1460                 if [ $4 != "NOTYPE" ]; then
1461                         if [ -z $queryparams ]; then
1462                                 queryparams="?policytype_id="$4
1463                         else
1464                                 queryparams=$queryparams"&policytype_id="$4
1465                         fi
1466                 fi
1467
1468                 query="/v2/policies"$queryparams
1469         else
1470                 if [ $2 != "NORIC" ]; then
1471                         queryparams="?ric="$2
1472                 fi
1473
1474                 if [ $3 != "NOSERVICE" ]; then
1475                         if [ -z $queryparams ]; then
1476                                 queryparams="?service="$3
1477                         else
1478                                 queryparams=$queryparams"&service="$3
1479                         fi
1480                 fi
1481                 if [ $4 != "NOTYPE" ]; then
1482                         if [ -z $queryparams ]; then
1483                                 queryparams="?type="$4
1484                         else
1485                                 queryparams=$queryparams"&type="$4
1486                         fi
1487                 fi
1488
1489                 query="/policy_ids"$queryparams
1490         fi
1491
1492     res="$(__do_curl_to_api A1PMS GET $query)"
1493     status=${res:${#res}-3}
1494
1495         if [ $status -ne $1 ]; then
1496                 __log_test_fail_status_code $1 $status
1497                 return 1
1498         fi
1499
1500         if [ $# -gt 4 ]; then
1501                 body=${res:0:${#res}-3}
1502                 targetJson="["
1503
1504                 for pid in ${@:5} ; do
1505                         if [ "$targetJson" != "[" ]; then
1506                                 targetJson=$targetJson","
1507                         fi
1508                         if [ $pid != "NOID" ]; then
1509                                 targetJson=$targetJson"\"$UUID$pid\""
1510                         fi
1511                 done
1512
1513                 targetJson=$targetJson"]"
1514                 if [ "$A1PMS_VERSION" == "V2" ]; then
1515                         targetJson="{\"policy_ids\": $targetJson}"
1516                 fi
1517                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1518                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1519
1520                 if [ $res -ne 0 ]; then
1521                         __log_test_fail_body
1522                         return 1
1523                 fi
1524         fi
1525
1526         __collect_endpoint_stats "A1PMS" 04 "GET" $A1PMS_API_PREFIX"/v2/policies" $status
1527         __log_test_pass
1528         return 0
1529 }
1530
1531 # API Test function: V2 GET /v2/policy-types/{policyTypeId}
1532 # args(V2): <response-code> <policy-type-id> [<schema-file>]
1533 # (Function for test scripts)
1534 a1pms_api_get_policy_type() {
1535         __log_test_start $@
1536
1537         if [ "$A1PMS_VERSION" != "V2" ]; then
1538                 __log_test_fail_not_supported
1539                 return 1
1540         fi
1541
1542     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1543         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
1544         return 1
1545     fi
1546         query="/v2/policy-types/$2"
1547
1548         res="$(__do_curl_to_api A1PMS GET $query)"
1549         status=${res:${#res}-3}
1550
1551         if [ $status -ne $1 ]; then
1552                 __log_test_fail_status_code $1 $status
1553                 return 1
1554         fi
1555
1556         if [ $# -eq 3 ]; then
1557
1558                 body=${res:0:${#res}-3}
1559
1560                 targetJson=$(< $3)
1561                 targetJson="{\"policy_schema\":$targetJson}"
1562                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1563                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1564
1565                 if [ $res -ne 0 ]; then
1566                         __log_test_fail_body
1567                         return 1
1568                 fi
1569         fi
1570
1571         __collect_endpoint_stats "A1PMS" 05 "GET" $A1PMS_API_PREFIX"/v2/policy-types/{policyTypeId}" $status
1572         __log_test_pass
1573         return 0
1574 }
1575
1576 # API Test function: GET /policy_schema
1577 # args: <response-code> <policy-type-id> [<schema-file>]
1578 # (Function for test scripts)
1579 a1pms_api_get_policy_schema() {
1580         __log_test_start $@
1581
1582         if [ "$A1PMS_VERSION" == "V2" ]; then
1583                 __log_test_fail_not_supported
1584                 return 1
1585         fi
1586
1587     if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1588         __print_err "<response-code> <policy-type-id> [<schema-file>]" $@
1589         return 1
1590     fi
1591         query="/policy_schema?id=$2"
1592         res="$(__do_curl_to_api A1PMS GET $query)"
1593         status=${res:${#res}-3}
1594
1595         if [ $status -ne $1 ]; then
1596                 __log_test_fail_status_code $1 $status
1597                 return 1
1598         fi
1599
1600         if [ $# -eq 3 ]; then
1601
1602                 body=${res:0:${#res}-3}
1603
1604                 targetJson=$(< $3)
1605
1606                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1607                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1608
1609                 if [ $res -ne 0 ]; then
1610                         __log_test_fail_body
1611                         return 1
1612                 fi
1613         fi
1614
1615         __collect_endpoint_stats "A1PMS" 06 "GET" $A1PMS_API_PREFIX"/v2/policy_schema" $status
1616         __log_test_pass
1617         return 0
1618 }
1619
1620 # API Test function: GET /policy_schemas
1621 # args: <response-code>  <ric-id>|NORIC [<schema-file>|NOFILE]*
1622 # args(V2): <response-code>
1623 # (Function for test scripts)
1624 a1pms_api_get_policy_schemas() {
1625         __log_test_start $@
1626
1627         if [ "$A1PMS_VERSION" == "V2" ]; then
1628                 if [ $# -ne 1 ]; then
1629                         __print_err "<response-code>" $@
1630                         return 1
1631                 fi
1632         else
1633                 if [ $# -lt 2 ]; then
1634                         __print_err "<response-code> <ric-id>|NORIC [<schema-file>|NOFILE]*" $@
1635                         return 1
1636                 fi
1637         fi
1638         if [ "$A1PMS_VERSION" == "V2" ]; then
1639                 query="/v2/policy-schemas"
1640         else
1641                 query="/policy_schemas"
1642                 if [ $2 != "NORIC" ]; then
1643                         query=$query"?ric="$2
1644                 fi
1645         fi
1646
1647         res="$(__do_curl_to_api A1PMS GET $query)"
1648         status=${res:${#res}-3}
1649
1650         if [ $status -ne $1 ]; then
1651                 __log_test_fail_status_code $1 $status
1652                 return 1
1653         fi
1654
1655         if [ $# -gt 2 ]; then
1656                 body=${res:0:${#res}-3}
1657                 targetJson="["
1658
1659                 for file in ${@:3} ; do
1660                         if [ "$targetJson" != "[" ]; then
1661                                 targetJson=$targetJson","
1662                         fi
1663                         if [ $file == "NOFILE" ]; then
1664                                 targetJson=$targetJson"{}"
1665                         else
1666                                 targetJson=$targetJson$(< $file)
1667                         fi
1668                 done
1669
1670                 targetJson=$targetJson"]"
1671                 if [ "$A1PMS_VERSION" == "V2" ]; then
1672                         targetJson="{\"policy_schemas\": $targetJson }"
1673                 fi
1674                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1675                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1676
1677                 if [ $res -ne 0 ]; then
1678                         __log_test_fail_body
1679                         return 1
1680                 fi
1681         fi
1682
1683         __collect_endpoint_stats "A1PMS" 07 "GET" $A1PMS_API_PREFIX"/v2/policy-schemas" $status
1684         __log_test_pass
1685         return 0
1686 }
1687
1688 # API Test function: GET /policy_status and V2 GET /policies/{policy_id}/status
1689 # arg: <response-code> <policy-id> [ (STD|STD2 <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>) ]
1690 # (Function for test scripts)
1691 a1pms_api_get_policy_status() {
1692         __log_test_start $@
1693
1694     if [ $# -lt 2 ] || [ $# -gt 5 ]; then
1695                 __print_err "<response-code> <policy-id> [(STD <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)]" $@
1696                 return 1
1697         fi
1698
1699         targetJson=""
1700         if [ $# -eq 2 ]; then
1701                 :
1702         elif [ "$3" == "STD" ]; then
1703                 targetJson="{\"enforceStatus\":\"$4\""
1704                 if [ $# -eq 5 ]; then
1705                         targetJson=$targetJson",\"reason\":\"$5\""
1706                 fi
1707                 targetJson=$targetJson"}"
1708         elif [ "$3" == "STD2" ]; then
1709                 if [ $4 == "EMPTY" ]; then
1710                         targetJson="{\"enforceStatus\":\"\""
1711                 else
1712                         targetJson="{\"enforceStatus\":\"$4\""
1713                 fi
1714                 if [ $# -eq 5 ]; then
1715                         if [ $5 == "EMPTY" ]; then
1716                                 targetJson=$targetJson",\"enforceReason\":\"\""
1717                         else
1718                                 targetJson=$targetJson",\"enforceReason\":\"$5\""
1719                         fi
1720                 fi
1721                 targetJson=$targetJson"}"
1722         elif [ "$3" == "OSC" ]; then
1723                 targetJson="{\"instance_status\":\"$4\""
1724                 if [ $# -eq 5 ]; then
1725                         targetJson=$targetJson",\"has_been_deleted\":\"$5\""
1726                 fi
1727                 targetJson=$targetJson",\"created_at\":\"????\"}"
1728         else
1729                 __print_err "<response-code> (STD <enforce-status> [<reason>])|(OSC <instance-status> <has-been-deleted>)" $@
1730                 return 1
1731         fi
1732
1733         if [ "$A1PMS_VERSION" == "V2" ]; then
1734                 query="/v2/policies/$UUID$2/status"
1735                 targetJson="{\"last_modified\":\"????\",\"status\":$targetJson}"
1736         else
1737                 query="/policy_status?id="$UUID$2
1738         fi
1739
1740         res="$(__do_curl_to_api A1PMS GET $query)"
1741     status=${res:${#res}-3}
1742
1743         if [ $status -ne $1 ]; then
1744                 __log_test_fail_status_code $1 $status
1745                 return 1
1746         fi
1747         if [ $# -gt 2 ]; then
1748                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1749                 body=${res:0:${#res}-3}
1750                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1751
1752                 if [ $res -ne 0 ]; then
1753                         __log_test_fail_body
1754                         return 1
1755                 fi
1756         fi
1757         __collect_endpoint_stats "A1PMS" 08 "GET" $A1PMS_API_PREFIX"/v2/policies/{policy_id}/status" $status
1758         __log_test_pass
1759         return 0
1760 }
1761
1762 # API Test function: GET /policy_types and V2 GET /v2/policy-types
1763 # args: <response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]
1764 # (Function for test scripts)
1765 a1pms_api_get_policy_types() {
1766         __log_test_start $@
1767
1768     if [ $# -lt 1 ]; then
1769                 __print_err "<response-code> [<ric-id>|NORIC [<policy-type-id>|EMPTY [<policy-type-id>]*]]" $@
1770                 return 1
1771         fi
1772
1773         if [ "$A1PMS_VERSION" == "V2" ]; then
1774                 if [ $# -eq 1 ]; then
1775                         query="/v2/policy-types"
1776                 elif [ $2 == "NORIC" ]; then
1777                         query="/v2/policy-types"
1778                 else
1779                         query="/v2/policy-types?ric_id=$2"
1780                 fi
1781         else
1782                 if [ $# -eq 1 ]; then
1783                         query="/policy_types"
1784                 elif [ $2 == "NORIC" ]; then
1785                         query="/policy_types"
1786                 else
1787                         query="/policy_types?ric=$2"
1788                 fi
1789         fi
1790
1791     res="$(__do_curl_to_api A1PMS GET $query)"
1792     status=${res:${#res}-3}
1793
1794         if [ $status -ne $1 ]; then
1795                 __log_test_fail_status_code $1 $status
1796                 return 1
1797         fi
1798
1799         if [ $# -gt 2 ]; then
1800                 body=${res:0:${#res}-3}
1801                 targetJson="["
1802
1803                 for pid in ${@:3} ; do
1804                         if [ "$targetJson" != "[" ]; then
1805                                 targetJson=$targetJson","
1806                         fi
1807                         if [ $pid == "EMPTY" ]; then
1808                                 pid=""
1809                         fi
1810                         targetJson=$targetJson"\"$pid\""
1811                 done
1812
1813                 targetJson=$targetJson"]"
1814                 if [ "$A1PMS_VERSION" == "V2" ]; then
1815                         targetJson="{\"policytype_ids\": $targetJson }"
1816                 fi
1817                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1818                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1819
1820                 if [ $res -ne 0 ]; then
1821                         __log_test_fail_body
1822                         return 1
1823                 fi
1824         fi
1825
1826         __collect_endpoint_stats "A1PMS" 09 "GET" $A1PMS_API_PREFIX"/v2/policy-types" $status
1827         __log_test_pass
1828         return 0
1829 }
1830
1831 #########################################################
1832 #### Test case functions Health check
1833 #########################################################
1834
1835 # API Test function: GET /status and V2 GET /status
1836 # args: <response-code>
1837 # (Function for test scripts)
1838 a1pms_api_get_status() {
1839         __log_test_start $@
1840     if [ $# -ne 1 ]; then
1841                 __print_err "<response-code>" $@
1842                 return 1
1843         fi
1844         if [ "$A1PMS_VERSION" == "V2" ]; then
1845                 query="/v2/status"
1846         else
1847                 query="/status"
1848         fi
1849     res="$(__do_curl_to_api A1PMS GET $query)"
1850     status=${res:${#res}-3}
1851
1852         if [ $status -ne $1 ]; then
1853                 __log_test_fail_status_code $1 $status
1854                 return 1
1855         fi
1856
1857         __collect_endpoint_stats "A1PMS" 10 "GET" $A1PMS_API_PREFIX"/v2/status" $status
1858         __log_test_pass
1859         return 0
1860 }
1861
1862 # API Test function: GET /status (root) without api prefix
1863 # args: <response-code>
1864 # (Function for test scripts)
1865 a1pms_api_get_status_root() {
1866         __log_test_start $@
1867     if [ $# -ne 1 ]; then
1868                 __print_err "<response-code>" $@
1869                 return 1
1870         fi
1871         query="/status"
1872         TMP_PREFIX=$A1PMS_API_PREFIX
1873         A1PMS_API_PREFIX=""
1874     res="$(__do_curl_to_api A1PMS GET $query)"
1875         A1PMS_API_PREFIX=$TMP_PREFIX
1876     status=${res:${#res}-3}
1877
1878         if [ $status -ne $1 ]; then
1879                 __log_test_fail_status_code $1 $status
1880                 return 1
1881         fi
1882
1883         __collect_endpoint_stats "A1PMS" 19 "GET" "/status" $status
1884         __log_test_pass
1885         return 0
1886 }
1887
1888 #########################################################
1889 #### Test case functions RIC Repository
1890 #########################################################
1891
1892 # API Test function: GET /ric and V2 GET /v2/rics/ric
1893 # args: <reponse-code> <management-element-id> [<ric-id>]
1894 # (V2) args: <reponse-code> <management-element-id>|NOME <ric-id>|<NORIC> [<string-of-ricinfo>]
1895 # (V2) example of <string-of-ricinfo> = "ricsim_g1_1:me1_ricsim_g1_1,me2_ricsim_g1_1:1,2,4"
1896 # (V2) format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1897
1898
1899 # (Function for test scripts)
1900 a1pms_api_get_ric() {
1901         __log_test_start $@
1902
1903         if [ "$A1PMS_VERSION" == "V2" ]; then
1904                 if [ $# -lt 3 ]; then
1905                         __print_err "<reponse-code> <management-element-id>|NOME <ric-id>|<NORIC> [string-of-ricinfo>]" $@
1906                         return 1
1907                 fi
1908                 search=""
1909                 if [ $2 != "NOME" ]; then
1910                         search="?managed_element_id="$2
1911                 fi
1912                 if [ $3 != "NORIC" ]; then
1913                         if [ -z $search ]; then
1914                                 search="?ric_id="$3
1915                         else
1916                                 search=$search"&ric_id="$3
1917                         fi
1918                 fi
1919                 query="/v2/rics/ric"$search
1920
1921                 res="$(__do_curl_to_api A1PMS GET $query)"
1922                 status=${res:${#res}-3}
1923
1924                 if [ $status -ne $1 ]; then
1925                         __log_test_fail_status_code $1 $status
1926                         return 1
1927                 fi
1928
1929                 if [ $# -gt 3 ]; then
1930                         body=${res:0:${#res}-3}
1931                         res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V2" "$4" )
1932                         if [ $res -ne 0 ]; then
1933                                 __log_test_fail_general "Could not create target ric info json"
1934                                 return 1
1935                         fi
1936
1937                         targetJson=$(<./tmp/.tmp_rics.json)
1938                         targetJson=${targetJson:1:${#targetJson}-2} #remove array brackets
1939                         echo " TARGET JSON: $targetJson" >> $HTTPLOG
1940                         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1941                         if [ $res -ne 0 ]; then
1942                                 __log_test_fail_body
1943                                 return 1
1944                         fi
1945                 fi
1946         else
1947                 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
1948                         __print_err "<reponse-code> <management-element-id> [<ric-id>]" $@
1949                         return 1
1950                 fi
1951
1952                 query="/ric?managedElementId="$2
1953
1954                 res="$(__do_curl_to_api A1PMS GET $query)"
1955                 status=${res:${#res}-3}
1956
1957                 if [ $status -ne $1 ]; then
1958                         __log_test_fail_status_code $1 $status
1959                         return 1
1960                 fi
1961
1962                 if [ $# -eq 3 ]; then
1963                         body=${res:0:${#res}-3}
1964                         if [ "$body" != "$3" ]; then
1965                                 __log_test_fail_body
1966                                 return 1
1967                         fi
1968                 fi
1969         fi
1970
1971         __collect_endpoint_stats "A1PMS" 11 "GET" $A1PMS_API_PREFIX"/v2/rics/ric" $status
1972         __log_test_pass
1973         return 0
1974 }
1975
1976 # API test function: GET /rics and V2 GET /v2/rics
1977 # args: <reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]
1978 # 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_........."
1979 # format of ric-info:  <ric-id>:<list-of-mes>:<list-of-policy-type-ids>
1980 # (Function for test scripts)
1981 a1pms_api_get_rics() {
1982         __log_test_start $@
1983
1984     if [ $# -lt 2 ]; then
1985                 __print_err "<reponse-code> <policy-type-id>|NOTYPE [<space-separate-string-of-ricinfo>]" $@
1986                 return 1
1987         fi
1988
1989         if [ "$A1PMS_VERSION" == "V2" ]; then
1990                 query="/v2/rics"
1991                 if [ $2 != "NOTYPE" ]; then
1992                         query="/v2/rics?policytype_id="$2
1993                 fi
1994         else
1995                 query="/rics"
1996                 if [ $2 != "NOTYPE" ]; then
1997                         query="/rics?policyType="$2
1998                 fi
1999         fi
2000
2001     res="$(__do_curl_to_api A1PMS GET $query)"
2002     status=${res:${#res}-3}
2003
2004         if [ $status -ne $1 ]; then
2005                 __log_test_fail_status_code $1 $status
2006                 return 1
2007         fi
2008
2009         if [ $# -gt 2 ]; then
2010                 body=${res:0:${#res}-3}
2011                 if [ "$A1PMS_VERSION" == "V2" ]; then
2012                         res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V2" "$3" )
2013                 else
2014                         res=$(python3 ../common/create_rics_json.py "./tmp/.tmp_rics.json" "V1" "$3" )
2015                 fi
2016                 if [ $res -ne 0 ]; then
2017                         __log_test_fail_general "Could not create target ric info json"
2018                         return 1
2019                 fi
2020
2021                 targetJson=$(<./tmp/.tmp_rics.json)
2022                 if [ "$A1PMS_VERSION" == "V2" ]; then
2023                         targetJson="{\"rics\": $targetJson }"
2024                 fi
2025         echo "TARGET JSON: $targetJson" >> $HTTPLOG
2026                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2027                 if [ $res -ne 0 ]; then
2028                         __log_test_fail_body
2029                         return 1
2030                 fi
2031         fi
2032
2033         __collect_endpoint_stats "A1PMS" 12 "GET" $A1PMS_API_PREFIX"/v2/rics" $status
2034         __log_test_pass
2035         return 0
2036 }
2037
2038 ##################################################################
2039 #### API Test case functions Service registry and supervision ####
2040 ##################################################################
2041
2042 # API test function: PUT /service and V2 PUT /service
2043 # args: <response-code>  <service-name> <keepalive-timeout> <callbackurl>
2044 # (Function for test scripts)
2045 a1pms_api_put_service() {
2046         __log_test_start $@
2047     if [ $# -ne 4 ]; then
2048         __print_err "<response-code>  <service-name> <keepalive-timeout> <callbackurl>" $@
2049         return 1
2050     fi
2051
2052         if [ "$A1PMS_VERSION" == "V2" ]; then
2053                 query="/v2/services"
2054                 json="{\"callback_url\": \""$4"\",\"keep_alive_interval_seconds\": \""$3"\",\"service_id\": \""$2"\"}"
2055         else
2056                 query="/service"
2057                 json="{\"callbackUrl\": \""$4"\",\"keepAliveIntervalSeconds\": \""$3"\",\"serviceName\": \""$2"\"}"
2058         fi
2059     file="./tmp/.tmp.json"
2060         echo "$json" > $file
2061
2062     res="$(__do_curl_to_api A1PMS PUT $query $file)"
2063     status=${res:${#res}-3}
2064
2065         if [ $status -ne $1 ]; then
2066                 __log_test_fail_status_code $1 $status
2067                 return 1
2068         fi
2069
2070         __collect_endpoint_stats "A1PMS" 13 "PUT" $A1PMS_API_PREFIX"/v2/service" $status
2071         __log_test_pass
2072         return 0
2073 }
2074
2075 # API test function: GET /services and V2 GET /v2/services
2076 #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>]* )]
2077 # (Function for test scripts)
2078 a1pms_api_get_services() {
2079         __log_test_start $@
2080         #Number of accepted parameters: 1, 2, 4, 7, 10, 13,...
2081         paramError=1
2082         if [ $# -eq 1 ]; then
2083                 paramError=0
2084         elif [ $# -eq 2 ] && [ $2 != "NOSERVICE" ]; then
2085                 paramError=0
2086         elif [ $# -eq 5 ]; then
2087                 paramError=0
2088         elif [ $# -gt 5 ] && [ $2 == "NOSERVICE" ]; then
2089                 argLen=$(($#-2))
2090                 if [ $(($argLen%3)) -eq 0 ]; then
2091                         paramError=0
2092                 fi
2093         fi
2094
2095     if [ $paramError -ne 0 ]; then
2096                 __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>]* )]" $@
2097                 return 1
2098         fi
2099
2100         if [ "$A1PMS_VERSION" == "V2" ]; then
2101                 query="/v2/services"
2102
2103                 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
2104                         query="/v2/services?service_id="$2
2105                 fi
2106         else
2107                 query="/services"
2108
2109                 if [ $# -gt 1 ] && [ $2 != "NOSERVICE" ]; then
2110                         query="/services?name="$2
2111                 fi
2112         fi
2113     res="$(__do_curl_to_api A1PMS GET $query)"
2114     status=${res:${#res}-3}
2115
2116         if [ $status -ne $1 ]; then
2117                 __log_test_fail_status_code $1 $status
2118                 return 1
2119         fi
2120
2121         if [ $# -gt 2 ]; then
2122                 variableArgCount=$(($#-2))
2123                 body=${res:0:${#res}-3}
2124         targetJson="["
2125                 shift; shift;
2126                 cntr=0
2127                 while [ $cntr -lt $variableArgCount ]; do
2128                         servicename=$1; shift;
2129                         timeout=$1; shift;
2130                         callback=$1; shift;
2131                         if [ $cntr -gt 0 ]; then
2132                                 targetJson=$targetJson","
2133                         fi
2134                         # timeSinceLastActivitySeconds value cannot be checked since value varies
2135                         if [ "$A1PMS_VERSION" == "V2" ]; then
2136                                 targetJson=$targetJson"{\"service_id\": \""$servicename"\",\"keep_alive_interval_seconds\": "$timeout",\"time_since_last_activity_seconds\":\"????\",\"callback_url\": \""$callback"\"}"
2137                         else
2138                                 targetJson=$targetJson"{\"serviceName\": \""$servicename"\",\"keepAliveIntervalSeconds\": "$timeout",\"timeSinceLastActivitySeconds\":\"????\",\"callbackUrl\": \""$callback"\"}"
2139                         fi
2140                         let cntr=cntr+3
2141                 done
2142                 targetJson=$targetJson"]"
2143                 if [ "$A1PMS_VERSION" == "V2" ]; then
2144                         targetJson="{\"service_list\": $targetJson }"
2145                 fi
2146                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
2147                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2148                 if [ $res -ne 0 ]; then
2149                         __log_test_fail_body
2150                         return 1
2151                 fi
2152         fi
2153
2154         __collect_endpoint_stats "A1PMS" 14 "GET" $A1PMS_API_PREFIX"/v2/services" $status
2155         __log_test_pass
2156         return 0
2157 }
2158
2159 # API test function: GET /services V2 GET /v2/services -  (only checking service names)
2160 # args: <response-code> [<service-name>]*"
2161 # (Function for test scripts)
2162 a1pms_api_get_service_ids() {
2163         __log_test_start $@
2164
2165     if [ $# -lt 1 ]; then
2166                 __print_err "<response-code> [<service-name>]*" $@
2167                 return 1
2168         fi
2169
2170         if [ "$A1PMS_VERSION" == "V2" ]; then
2171             query="/v2/services"
2172         else
2173         query="/services"
2174         fi
2175     res="$(__do_curl_to_api A1PMS GET $query)"
2176     status=${res:${#res}-3}
2177
2178         if [ $status -ne $1 ]; then
2179                 __log_test_fail_status_code $1 $status
2180                 return 1
2181         fi
2182
2183         body=${res:0:${#res}-3}
2184         targetJson="["
2185         for rapp in ${@:2} ; do
2186                 if [ "$targetJson" != "[" ]; then
2187                         targetJson=$targetJson","
2188                 fi
2189                 if [ "$A1PMS_VERSION" == "V2" ]; then
2190                         targetJson=$targetJson"{\"callback_url\":\"????\",\"keep_alive_interval_seconds\":\"????\",\"service_id\":\""$rapp"\",\"time_since_last_activity_seconds\":\"????\"}"
2191                 else
2192                         targetJson=$targetJson"{\"callbackUrl\":\"????\",\"keepAliveIntervalSeconds\":\"????\",\"serviceName\":\""$rapp"\",\"timeSinceLastActivitySeconds\":\"????\"}"
2193                 fi
2194         done
2195
2196         targetJson=$targetJson"]"
2197         if [ "$A1PMS_VERSION" == "V2" ]; then
2198                 targetJson="{\"service_list\": $targetJson }"
2199         fi
2200         echo "TARGET JSON: $targetJson" >> $HTTPLOG
2201         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2202
2203         if [ $res -ne 0 ]; then
2204                 __log_test_fail_body
2205                 return 1
2206         fi
2207
2208         __collect_endpoint_stats "A1PMS" 14 "GET" $A1PMS_API_PREFIX"/v2/services" $status
2209         __log_test_pass
2210         return 0
2211 }
2212
2213 # API test function: DELETE /services and V2 DELETE /v2/services/{serviceId}
2214 # args: <response-code> <service-name>
2215 # (Function for test scripts)
2216 a1pms_api_delete_services() {
2217         __log_test_start $@
2218
2219     if [ $# -ne 2 ]; then
2220                 __print_err "<response-code> <service-name>" $@
2221                 return 1
2222         fi
2223         if [ "$A1PMS_VERSION" == "V2" ]; then
2224                 query="/v2/services/"$2
2225         else
2226                 query="/services?name="$2
2227         fi
2228     res="$(__do_curl_to_api A1PMS DELETE $query)"
2229     status=${res:${#res}-3}
2230
2231         if [ $status -ne $1 ]; then
2232                 __log_test_fail_status_code $1 $status
2233                 return 1
2234         fi
2235
2236         __collect_endpoint_stats "A1PMS" 15 "DELETE" $A1PMS_API_PREFIX"/v2/services/{serviceId}" $status
2237         __log_test_pass
2238         return 0
2239 }
2240
2241 # API test function: PUT /services/keepalive and V2 PUT /v2/services/{service_id}/keepalive
2242 # args: <response-code> <service-name>
2243 # (Function for test scripts)
2244 a1pms_api_put_services_keepalive() {
2245         __log_test_start $@
2246
2247     if [ $# -ne 2 ]; then
2248                 __print_err "<response-code> <service-name>" $@
2249                 return 1
2250         fi
2251         if [ "$A1PMS_VERSION" == "V2" ]; then
2252                 query="/v2/services/$2/keepalive"
2253         else
2254         query="/services/keepalive?name="$2
2255         fi
2256
2257     res="$(__do_curl_to_api A1PMS PUT $query)"
2258     status=${res:${#res}-3}
2259
2260         if [ $status -ne $1 ]; then
2261                 __log_test_fail_status_code $1 $status
2262                 return 1
2263         fi
2264
2265         __collect_endpoint_stats "A1PMS" 16 "PUT" $A1PMS_API_PREFIX"/v2/services/{service_id}/keepalive" $status
2266         __log_test_pass
2267         return 0
2268 }
2269
2270 ##################################################################
2271 #### API Test case functions Configuration                    ####
2272 ##################################################################
2273
2274 # API Test function: PUT /v2/configuration
2275 # args: <response-code> <config-file>
2276 # (Function for test scripts)
2277 a1pms_api_put_configuration() {
2278         __log_test_start $@
2279
2280         if [ "$A1PMS_VERSION" != "V2" ]; then
2281                 __log_test_fail_not_supported
2282                 return 1
2283         fi
2284
2285     if [ $# -ne 2 ]; then
2286         __print_err "<response-code> <config-file>" $@
2287         return 1
2288     fi
2289         if [ ! -f $2 ]; then
2290                 __log_test_fail_general "Config file "$2", does not exist"
2291                 return 1
2292         fi
2293         inputJson=$(< $2)
2294         # if [ $RUNMODE == "DOCKER" ]; then  #In kube the file already has a header
2295         #       inputJson="{\"config\":"$inputJson"}"
2296         # fi
2297         file="./tmp/.config.json"
2298         echo $inputJson > $file
2299         query="/v2/configuration"
2300         res="$(__do_curl_to_api A1PMS PUT $query $file)"
2301         status=${res:${#res}-3}
2302
2303         if [ $status -ne $1 ]; then
2304                 __log_test_fail_status_code $1 $status
2305                 return 1
2306         fi
2307
2308         __collect_endpoint_stats "A1PMS" 17 "PUT" $A1PMS_API_PREFIX"/v2/configuration" $status
2309         __log_test_pass
2310         return 0
2311 }
2312
2313 # API Test function: GET /v2/configuration
2314 # args: <response-code> [<config-file>]
2315 # (Function for test scripts)
2316 a1pms_api_get_configuration() {
2317         __log_test_start $@
2318
2319         if [ "$A1PMS_VERSION" != "V2" ]; then
2320                 __log_test_fail_not_supported
2321                 return 1
2322         fi
2323
2324     if [ $# -lt 1 ] || [ $# -gt 2 ]; then
2325         __print_err "<response-code> [<config-file>]" $@
2326         return 1
2327     fi
2328         if [ ! -f $2 ]; then
2329                 __log_test_fail_general "Config file "$2" for comparison, does not exist"
2330                 return 1
2331         fi
2332
2333         query="/v2/configuration"
2334         res="$(__do_curl_to_api A1PMS GET $query)"
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         if [ $# -eq 2 ]; then
2343
2344                 body=${res:0:${#res}-3}
2345
2346                 targetJson=$(< $2)
2347                 # if [ $RUNMODE == "DOCKER" ]; then  #In kube the file already has a header
2348                 #       inputJson="{\"config\":"$inputJson"}"
2349                 # fi
2350                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
2351                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
2352
2353                 if [ $res -ne 0 ]; then
2354                         __log_test_fail_body
2355                         return 1
2356                 fi
2357         fi
2358
2359         __collect_endpoint_stats "A1PMS" 18 "GET" $A1PMS_API_PREFIX"/v2/configuration" $status
2360         __log_test_pass
2361         return 0
2362 }
2363
2364 ##########################################
2365 ####     Reset types and instances    ####
2366 ##########################################
2367
2368 # Admin reset to remove all policies and services
2369 # All types and instances etc are removed - types and instances in a1 sims need to be removed separately
2370 # NOTE - only works in kubernetes and the pod should not be running
2371 # args: -
2372 # (Function for test scripts)
2373
2374 a1pms_kube_pvc_reset() {
2375         __log_test_start $@
2376
2377         pvc_name=$(kubectl $KUBECONF get pvc -n $KUBE_NONRTRIC_NAMESPACE  --no-headers -o custom-columns=":metadata.name" | grep policy)
2378         if [ -z "$pvc_name" ]; then
2379                 pvc_name=policymanagementservice-vardata-pvc
2380         fi
2381         echo " Trying to reset pvc: "$pvc_name
2382         __kube_clean_pvc $A1PMS_APP_NAME $KUBE_NONRTRIC_NAMESPACE $pvc_name $A1PMS_CONTAINER_MNT_DIR
2383
2384         __log_test_pass
2385         return 0
2386 }
2387
2388 # args: <realm> <client-name> <client-secret>
2389 a1pms_configure_sec() {
2390         export A1PMS_CREDS_GRANT_TYPE="client_credentials"
2391         export A1PMS_CREDS_CLIENT_SECRET=$3
2392         export A1PMS_CREDS_CLIENT_ID=$2
2393         export A1PMS_AUTH_SERVICE_URL=$KEYCLOAK_SERVICE_PATH$KEYCLOAK_TOKEN_URL_PREFIX/$1/protocol/openid-connect/token
2394         export A1PMS_SIDECAR_MOUNT="/token-cache"
2395         export A1PMS_SIDECAR_JWT_FILE=$A1PMS_SIDECAR_MOUNT"/jwt.txt"
2396
2397         export AUTHSIDECAR_APP_NAME
2398         export AUTHSIDECAR_DISPLAY_NAME
2399 }