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