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