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