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