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