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