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