3e00ec37ad0916dc6a56361cfc948f2e2fbe0e4a
[nonrtric.git] / test / common / mr_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 container/service management function
21 # and test functions for Message Router - mr stub
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __MR_imagesetup() {
29         __check_and_create_image_var MR "MRSTUB_IMAGE" "MRSTUB_IMAGE_BASE" "MRSTUB_IMAGE_TAG" LOCAL "$MR_STUB_DISPLAY_NAME"
30 }
31
32 # Create the image var used during the test
33 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
34 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
35 __DMAAPMR_imagesetup() {
36         __check_and_create_image_var DMAAPMR "ONAP_DMAAPMR_IMAGE"    "ONAP_DMAAPMR_IMAGE_BASE"  "ONAP_DMAAPMR_IMAGE_TAG"   REMOTE_RELEASE_ONAP "DMAAP Message Router"
37         __check_and_create_image_var DMAAPMR "ONAP_ZOOKEEPER_IMAGE" "ONAP_ZOOKEEPER_IMAGE_BASE" "ONAP_ZOOKEEPER_IMAGE_TAG" REMOTE_RELEASE_ONAP "ZooKeeper"
38         __check_and_create_image_var DMAAPMR "ONAP_KAFKA_IMAGE"     "ONAP_KAFKA_IMAGE_BASE"     "ONAP_KAFKA_IMAGE_TAG"     REMOTE_RELEASE_ONAP "Kafka"
39 }
40
41 # Pull image from remote repo or use locally built image
42 # arg: <pull-policy-override> <pull-policy-original>
43 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
44 # <pull-policy-original> Shall be used for images that does not allow overriding
45 # Both var may contain: 'remote', 'remote-remove' or 'local'
46 __MR_imagepull() {
47         echo -e $RED"Image for app MR shall never be pulled from remote repo"$ERED
48 }
49
50 # Pull image from remote repo or use locally built image
51 # arg: <pull-policy-override> <pull-policy-original>
52 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released (remote) images
53 # <pull-policy-original> Shall be used for images that does not allow overriding
54 # Both var may contain: 'remote', 'remote-remove' or 'local'
55 __DMAAPMR_imagepull() {
56         __check_and_pull_image $2 "DMAAP Message Router" $MR_DMAAP_APP_NAME ONAP_DMAAPMR_IMAGE
57         __check_and_pull_image $2 "ZooKeeper" $MR_ZOOKEEPER_APP_NAME ONAP_ZOOKEEPER_IMAGE
58         __check_and_pull_image $2 "Kafka" $MR_KAFKA_APP_NAME ONAP_KAFKA_IMAGE
59 }
60
61 # Build image (only for simulator or interfaces stubs owned by the test environment)
62 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
63 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
64 __MR_imagebuild() {
65         cd ../mrstub
66         echo " Building MR - $MR_STUB_DISPLAY_NAME - image: $MRSTUB_IMAGE"
67         docker build  --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $MRSTUB_IMAGE . &> .dockererr
68         if [ $? -eq 0 ]; then
69                 echo -e  $GREEN"  Build Ok"$EGREEN
70                 __retag_and_push_image MRSTUB_IMAGE
71                 if [ $? -ne 0 ]; then
72                         exit 1
73                 fi
74         else
75                 echo -e $RED"  Build Failed"$ERED
76                 ((RES_CONF_FAIL++))
77                 cat .dockererr
78                 echo -e $RED"Exiting...."$ERED
79                 exit 1
80         fi
81 }
82
83 # Build image (only for simulator or interfaces stubs owned by the test environment)
84 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
85 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
86 __DMAAPMR_imagebuild() {
87         echo -e $RED"Image for app DMAAPMR shall never be built"$ERED
88 }
89
90 # Generate a string for each included image using the app display name and a docker images format string
91 # If a custom image repo is used then also the source image from the local repo is listed
92 # arg: <docker-images-format-string> <file-to-append>
93 __MR_image_data() {
94         echo -e "$MR_STUB_DISPLAY_NAME\t$(docker images --format $1 $MRSTUB_IMAGE)" >>   $2
95         if [ ! -z "$MRSTUB_IMAGE_SOURCE" ]; then
96                 echo -e "-- source image --\t$(docker images --format $1 $MRSTUB_IMAGE_SOURCE)" >>   $2
97         fi
98 }
99
100 # Generate a string for each included image using the app display name and a docker images format string
101 # If a custom image repo is used then also the source image from the local repo is listed
102 # arg: <docker-images-format-string> <file-to-append>
103 __DMAAPMR_image_data() {
104         echo -e "DMAAP Message Router\t$(docker images --format $1 $ONAP_DMAAPMR_IMAGE)" >>   $2
105         if [ ! -z "$ONAP_DMAAPMR_IMAGE_SOURCE" ]; then
106                 echo -e "-- source image --\t$(docker images --format $1 $ONAP_DMAAPMR_IMAGE_SOURCE)" >>   $2
107         fi
108         echo -e "ZooKeeper\t$(docker images --format $1 $ONAP_ZOOKEEPER_IMAGE)" >>   $2
109         if [ ! -z "$ONAP_ZOOKEEPER_IMAGE_SOURCE" ]; then
110                 echo -e "-- source image --\t$(docker images --format $1 $ONAP_ZOOKEEPER_IMAGE_SOURCE)" >>   $2
111         fi
112         echo -e "Kafka\t$(docker images --format $1 $ONAP_KAFKA_IMAGE)" >>   $2
113         if [ ! -z "$ONAP_KAFKA_IMAGE_SOURCE" ]; then
114                 echo -e "-- source image --\t$(docker images --format $1 $ONAP_KAFKA_IMAGE_SOURCE)" >>   $2
115         fi
116 }
117
118 # Scale kubernetes resources to zero
119 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
120 # This function is called for apps fully managed by the test script
121 __MR_kube_scale_zero() {
122         __kube_scale_all_resources $KUBE_ONAP_NAMESPACE autotest MR
123 }
124
125 # Scale kubernetes resources to zero
126 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
127 # This function is called for apps fully managed by the test script
128 __DMAAPMR_kube_scale_zero() {
129         __kube_scale_all_resources $KUBE_ONAP_NAMESPACE autotest DMAAPMR
130 }
131
132 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
133 # This function is called for prestarted apps not managed by the test script.
134 __MR_kube_scale_zero_and_wait() {
135         echo -e " MR replicas kept as is"
136 }
137
138 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
139 # This function is called for prestarted apps not managed by the test script.
140 __DMAAPMR_kube_scale_zero_and_wait() {
141         echo -e " DMAAP replicas kept as is"
142 }
143
144 # Delete all kube resouces for the app
145 # This function is called for apps managed by the test script.
146 __MR_kube_delete_all() {
147         __kube_delete_all_resources $KUBE_ONAP_NAMESPACE autotest MR
148 }
149
150 # Delete all kube resouces for the app
151 # This function is called for apps managed by the test script.
152 __DMAAPMR_kube_delete_all() {
153         __kube_delete_all_resources $KUBE_ONAP_NAMESPACE autotest DMAAPMR
154 }
155
156 # Store docker logs
157 # This function is called for apps managed by the test script.
158 # args: <log-dir> <file-prexix>
159 __MR_store_docker_logs() {
160         if [ $RUNMODE == "KUBE" ]; then
161                 kubectl  logs -l "autotest=MR" -n $KUBE_ONAP_NAMESPACE --tail=-1 > $1$2_mr_stub.log 2>&1
162         else
163                 docker logs $MR_STUB_APP_NAME > $1$2_mr_stub.log 2>&1
164         fi
165 }
166
167 # Store docker logs
168 # This function is called for apps managed by the test script.
169 # args: <log-dir> <file-prexix>
170 __DMAAPMR_store_docker_logs() {
171         if [ $RUNMODE == "KUBE" ]; then
172                 for podname in $(kubectl get pods -n $KUBE_ONAP_NAMESPACE -l "autotest=DMAAPMR" -o custom-columns=":metadata.name"); do
173                         kubectl logs -n $KUBE_ONAP_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
174                 done
175         else
176                 docker logs $MR_DMAAP_APP_NAME > $1$2_mr.log 2>&1
177                 docker logs $MR_KAFKA_APP_NAME > $1$2_mr_kafka.log 2>&1
178                 docker logs $MR_ZOOKEEPER_APP_NAME > $1$2_mr_zookeeper.log 2>&1
179         fi
180 }
181
182 # Initial setup of protocol, host and ports
183 # This function is called for apps managed by the test script.
184 # args: -
185 __MR_initial_setup() {
186         use_mr_http
187 }
188
189 # Initial setup of protocol, host and ports
190 # This function is called for apps managed by the test script.
191 # args: -
192 __DMAAPMR_initial_setup() {
193         :  # handle by __MR_initial_setup
194 }
195
196 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
197 # For docker, the namespace shall be excluded
198 # This function is called for apps managed by the test script as well as for prestarted apps.
199 # args: -
200 __MR_statisics_setup() {
201         if [ $RUNMODE == "KUBE" ]; then
202                 echo "MR-STUB $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE"
203         else
204                 echo "MR-STUB $MR_STUB_APP_NAME"
205         fi
206 }
207
208 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
209 # For docker, the namespace shall be excluded
210 # This function is called for apps managed by the test script as well as for prestarted apps.
211 # args: -
212 __DMAAPMR_statisics_setup() {
213         if [ $RUNMODE == "KUBE" ]; then
214                 echo "KAFKA $MR_KAFKA_APP_NAME $KUBE_ONAP_NAMESPACE MESSAGE-ROUTER $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE ZOOKEEPER $MR_ZOOKEEPER_APP_NAME $KUBE_ONAP_NAMESPACE"
215         else
216                 echo "KAFKA $MR_KAFKA_APP_NAME MESSAGE-ROUTER $MR_DMAAP_APP_NAME ZOOKEEPER $MR_ZOOKEEPER_APP_NAME"
217         fi
218 }
219
220 #######################################################
221
222 # Description of port mappings when running MR-STUB only or MR-STUB + MESSAGE-ROUTER
223 #
224 # 'MR-STUB only' is started when only 'MR' is included in the test script. Both the test scripts and app will then use MR-STUB as a message-router simulator.
225 #
226 # 'MR-STUB + MESSAGE-ROUTER' is started when 'MR' and 'DMAAPMR' is included in the testscripts. DMAAPMR is the real message router including kafka and zookeeper.
227 # In this configuration, MR-STUB is used by the test-script as frontend to the message-router while app are using the real message-router.
228 #
229 # DOCKER                                                                      KUBE
230 # ---------------------------------------------------------------------------------------------------------------------------------------------------
231
232 #                             MR-STUB                                                             MR-STUB
233 #                             +++++++                                                             +++++++
234 # localhost                               container                           service                                 pod
235 # ==============================================================================================================================================
236 # 10 MR_STUB_LOCALHOST_PORT          ->   13 MR_INTERNAL_PORT                 15 MR_EXTERNAL_PORT                ->   17 MR_INTERNAL_PORT
237 # 12 MR_STUB_LOCALHOST_SECURE_PORT   ->   14 MR_INTERNAL_SECURE_PORT          16 MR_EXTERNAL_SECURE_PORT                 ->   18 MR_INTERNAL_SECURE_PORT
238
239
240
241 #                             MESSAGE-ROUTER                                                      MESSAGE-ROUTER
242 #                             ++++++++++++++                                                      ++++++++++++++
243 # localhost                               container                           service                                 pod
244 # ===================================================================================================================================================
245 # 20 MR_DMAAP_LOCALHOST_PORT         ->   23 MR_INTERNAL_PORT                 25 MR_EXTERNAL_PORT                ->   27 MR_INTERNAL_PORT
246 # 22 MR_DMAAP_LOCALHOST_SECURE_PORT  ->   24 MR_INTERNAL_SECURE_PORT          26 MR_EXTERNAL_SECURE_PORT                 ->   28 MR_INTERNAL_SECURE_PORT
247
248
249 # Running only the MR-STUB - apps using MR-STUB
250 # DOCKER                                                                      KUBE
251 # localhost:          10 and 12                                                -
252 # via proxy (script): 13 and 14                                               via proxy (script): 15 and 16
253 # apps:               13 and 14                                               apps:               15 and 16
254
255 # Running MR-STUB (as frontend for test script) and MESSAGE-ROUTER - apps using MESSAGE-ROUTER
256 # DOCKER                                                                      KUBE
257 # localhost:          10 and 12                                                -
258 # via proxy (script): 13 and 14                                               via proxy (script): 15 and 16
259 # apps:               23 and 24                                               apps:               25 and 26
260 #
261
262
263
264 use_mr_http() {
265         __mr_set_protocoll "http" $MR_INTERNAL_PORT $MR_EXTERNAL_PORT $MR_INTERNAL_SECURE_PORT $MR_EXTERNAL_SECURE_PORT
266 }
267
268 use_mr_https() {
269         __mr_set_protocoll "https" $MR_INTERNAL_PORT $MR_EXTERNAL_PORT $MR_INTERNAL_SECURE_PORT $MR_EXTERNAL_SECURE_PORT
270 }
271
272 # Setup paths to svc/container for internal and external access
273 # args: <protocol> <internal-port> <external-port> <internal-secure-port> <external-secure-port>
274 __mr_set_protocoll() {
275         echo -e $BOLD"$MR_STUB_DISPLAY_NAME and $MR_DMAAP_DISPLAY_NAME protocol setting"$EBOLD
276         echo -e " Using $BOLD $1 $EBOLD towards $MR_STUB_DISPLAY_NAME and $MR_DMAAP_DISPLAY_NAME"
277
278         ## Access to Dmaap mediator
279
280         MR_HTTPX=$1
281
282         if [ $MR_HTTPX == "http" ]; then
283                 INT_PORT=$2
284                 EXT_PORT=$3
285         else
286                 INT_PORT=$4
287                 EXT_PORT=$5
288         fi
289
290         # Access via test script
291         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME":"$INT_PORT  # access from script via proxy, docker
292         MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME":"$INT_PORT # access from script via proxy, docker
293         MR_DMAAP_ADAPTER_HTTP="" # Access to dmaap mr via proyx - set only if app is included
294
295         MR_SERVICE_PATH=$MR_STUB_PATH # access container->container, docker -  access pod->svc, kube
296         MR_KAFKA_SERVICE_PATH=""
297         MR_ZOOKEEPER_SERVICE_PATH=""
298         __check_included_image "DMAAPMR"
299         if [ $? -eq 0 ]; then
300                 MR_SERVICE_PATH=$MR_DMAAP_PATH # access container->container, docker -  access pod->svc, kube
301                 MR_DMAAP_ADAPTER_HTTP=$MR_DMAAP_PATH
302
303                 MR_KAFKA_SERVICE_PATH=$MR_KAFKA_APP_NAME":"$MR_KAFKA_PORT
304                 MR_ZOOKEEPER_SERVICE_PATH=$MR_ZOOKEEPER_APP_NAME":"$MR_ZOOKEEPER_PORT
305         fi
306
307         # For directing calls from script to e.g.PMS via message rounter
308         # These cases shall always go though the  mr-stub
309         MR_ADAPTER_HTTP="http://"$MR_STUB_APP_NAME":"$2
310         MR_ADAPTER_HTTPS="https://"$MR_STUB_APP_NAME":"$4
311
312         MR_DMAAP_ADAPTER_TYPE="REST"
313
314
315
316         if [ $RUNMODE == "KUBE" ]; then
317                 MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME.$KUBE_ONAP_NAMESPACE":"$EXT_PORT # access from script via proxy, kube
318                 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE":"$EXT_PORT # access from script via proxy, kube
319
320                 MR_SERVICE_PATH=$MR_STUB_PATH
321                 __check_included_image "DMAAPMR"
322                 if [ $? -eq 0 ]; then
323                         MR_SERVICE_PATH=$MR_DMAAP_PATH
324                         MR_DMAAP_ADAPTER_HTTP=$MR_DMAAP_PATH
325                         MR_KAFKA_SERVICE_PATH=$MR_KAFKA_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_KAFKA_PORT
326                         MR_ZOOKEEPER_SERVICE_PATH=$MR_ZOOKEEPER_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_ZOOKEEPER_PORT
327                 fi
328                 __check_prestarted_image "DMAAPMR"
329                 if [ $? -eq 0 ]; then
330                         MR_SERVICE_PATH=$MR_DMAAP_PATH
331                         MR_DMAAP_ADAPTER_HTTP=$MR_DMAAP_PATH
332                         MR_KAFKA_SERVICE_PATH=$MR_KAFKA_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_KAFKA_PORT
333                         MR_ZOOKEEPER_SERVICE_PATH=$MR_ZOOKEEPER_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_ZOOKEEPER_PORT
334                 fi
335
336                 # For directing calls from script to e.g.PMS, via message rounter
337                 # These calls shall always go though the  mr-stub
338                 MR_ADAPTER_HTTP="http://"$MR_STUB_APP_NAME.$KUBE_ONAP_NAMESPACE":"$3
339                 MR_ADAPTER_HTTPS="https://"$MR_STUB_APP_NAME.$KUBE_ONAP_NAMESPACE":"$5
340         fi
341
342         # For calls from script to the mr-stub
343         MR_STUB_ADAPTER=$MR_STUB_PATH
344         MR_STUB_ADAPTER_TYPE="REST"
345
346         echo ""
347
348 }
349
350 # Export env vars for config files, docker compose and kube resources
351 # args: -
352 __dmaapmr_export_vars() {
353         #Docker only
354         export DOCKER_SIM_NWNAME
355         export ONAP_ZOOKEEPER_IMAGE
356         export MR_ZOOKEEPER_APP_NAME
357         export ONAP_KAFKA_IMAGE
358         export MR_KAFKA_APP_NAME
359         export ONAP_DMAAPMR_IMAGE
360         export MR_DMAAP_APP_NAME
361         export MR_DMAAP_LOCALHOST_PORT
362         export MR_INTERNAL_PORT
363         export MR_DMAAP_LOCALHOST_SECURE_PORT
364         export MR_INTERNAL_SECURE_PORT
365         export MR_DMAAP_HOST_MNT_DIR
366
367         export KUBE_ONAP_NAMESPACE
368         export MR_EXTERNAL_PORT
369         export MR_EXTERNAL_SECURE_PORT
370         export MR_KAFKA_PORT
371         export MR_ZOOKEEPER_PORT
372
373         export MR_KAFKA_SERVICE_PATH
374         export MR_ZOOKEEPER_SERVICE_PATH
375
376         export MR_KAFKA_KUBE_NODE_PORT
377         export MR_KAFKA_DOCKER_LOCALHOST_PORT
378 }
379
380 # Export env vars for config files, docker compose and kube resources
381 # args: -
382 __mr_export_vars() {
383         #Docker only
384         export DOCKER_SIM_NWNAME
385         export MR_STUB_APP_NAME
386         export MRSTUB_IMAGE
387         export MR_INTERNAL_PORT
388         export MR_INTERNAL_SECURE_PORT
389         export MR_EXTERNAL_PORT
390         export MR_EXTERNAL_SECURE_PORT
391         export MR_STUB_LOCALHOST_PORT
392         export MR_STUB_LOCALHOST_SECURE_PORT
393         export MR_STUB_CERT_MOUNT_DIR
394         export MR_STUB_DISPLAY_NAME
395
396         export KUBE_ONAP_NAMESPACE
397         export MR_EXTERNAL_PORT
398
399         export MR_KAFKA_SERVICE_PATH
400         export MR_ZOOKEEPER_SERVICE_PATH
401 }
402
403
404 # Start the Message Router stub interface in the simulator group
405 # args: -
406 # (Function for test scripts)
407 start_mr() {
408
409         echo -e $BOLD"Starting $MR_DMAAP_DISPLAY_NAME and/or $MR_STUB_DISPLAY_NAME"$EBOLD
410
411         if [ $RUNMODE == "KUBE" ]; then
412
413         # Table of possible combinations of included mr and included/prestarted dmaap-mr
414                 # mr can never be prestarted
415                 # mr can be used stand alone
416                 # if dmaapmr is included/prestarted, then mr is needed as well as frontend
417
418         # Inverted logic - 0 mean true, 1 means false
419                 # mr prestarted      0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
420                 # mr included        0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
421                 # dmaap prestarted   0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
422                 # dmaap included     0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
423                 # ==================================================
424                 # OK                 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1
425
426                 __check_prestarted_image 'MR'
427                 retcode_prestarted_mr=$?
428                 __check_included_image 'MR'
429                 retcode_included_mr=$?
430
431                 __check_prestarted_image 'DMAAPMR'
432                 retcode_prestarted_dmaapmr=$?
433                 __check_included_image 'DMAAPMR'
434                 retcode_included_dmaapmr=$?
435
436                 paramerror=1
437
438                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
439                         if [ $retcode_prestarted_dmaapmr -ne 0 ] && [ $retcode_included_dmaapmr -eq 0 ]; then
440                                 paramerror=0
441                         fi
442                 fi
443
444                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
445                         if [ $retcode_prestarted_dmaapmr -eq 0 ] && [ $retcode_included_dmaapmr -ne 0 ]; then
446                                 paramerror=0
447                         fi
448                 fi
449
450                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
451                         if [ $retcode_prestarted_dmaapmr -ne 0 ] && [ $retcode_included_dmaapmr -ne 0 ]; then
452                                 paramerror=0
453                         fi
454                 fi
455
456                 if [ $paramerror -ne 0 ]; then
457                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
458                                 echo -e $RED"The Message Router will not be started"$ERED
459                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included and/or prestarted"$ERED
460                                 exit
461                 fi
462
463                 if [ $retcode_prestarted_dmaapmr -eq 0 ]; then
464                         echo -e " Using existing $MR_DMAAP_APP_NAME deployment and service"
465                         __kube_scale deployment $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE 1
466                 fi
467
468                 if [ $retcode_included_dmaapmr -eq 0 ]; then
469
470                         __dmaapmr_export_vars
471
472                         #Check if onap namespace exists, if not create it
473                         __kube_create_namespace $KUBE_ONAP_NAMESPACE
474
475                         # copy config files
476                         MR_MNT_CONFIG_BASEPATH=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR$MR_DMAAP_HOST_MNT_DIR
477                         cp -r $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR$MR_DMAAP_HOST_CONFIG_DIR/*  $MR_MNT_CONFIG_BASEPATH
478
479                         # Create config maps - dmaapmr app
480                         configfile=$MR_MNT_CONFIG_BASEPATH/mr/MsgRtrApi.properties
481                         output_yaml=$PWD/tmp/dmaapmr_msgrtrapi_cfc.yaml
482                         __kube_create_configmap dmaapmr-msgrtrapi.properties $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
483
484                         configfile=$MR_MNT_CONFIG_BASEPATH/mr/logback.xml
485                         output_yaml=$PWD/tmp/dmaapmr_logback_cfc.yaml
486                         __kube_create_configmap dmaapmr-logback.xml $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
487
488                         configfile=$MR_MNT_CONFIG_BASEPATH/mr/cadi.properties
489                         output_yaml=$PWD/tmp/dmaapmr_cadi_cfc.yaml
490                         __kube_create_configmap dmaapmr-cadi.properties $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
491
492                         # Create config maps - kafka app
493                         configfile=$MR_MNT_CONFIG_BASEPATH/kafka/zk_client_jaas.conf
494                         output_yaml=$PWD/tmp/dmaapmr_zk_client_cfc.yaml
495                         __kube_create_configmap dmaapmr-zk-client-jaas.conf $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
496
497                         # Create config maps - zookeeper app
498                         configfile=$MR_MNT_CONFIG_BASEPATH/zk/zk_server_jaas.conf
499                         output_yaml=$PWD/tmp/dmaapmr_zk_server_cfc.yaml
500                         __kube_create_configmap dmaapmr-zk-server-jaas.conf $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
501
502                         # Create service
503                         input_yaml=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"svc.yaml
504                         output_yaml=$PWD/tmp/dmaapmr_svc.yaml
505                         __kube_create_instance service $MR_DMAAP_APP_NAME $input_yaml $output_yaml
506
507                         # Create app
508                         input_yaml=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"app.yaml
509                         output_yaml=$PWD/tmp/dmaapmr_app.yaml
510                         __kube_create_instance app $MR_DMAAP_APP_NAME $input_yaml $output_yaml
511
512
513                         __check_service_start $MR_DMAAP_APP_NAME $MR_DMAAP_PATH$MR_DMAAP_ALIVE_URL
514
515                         echo " Kafka TCP node port $MR_KAFKA_KUBE_NODE_PORT"
516
517
518                         if [ $# -gt 0 ]; then
519                                 if [ $(($#%3)) -eq 0 ]; then
520                                         while [ $# -gt 0 ]; do
521                                                 __dmaap_pipeclean "$1" "$2/$1" "$2/$1/$3?timeout=1000&limit=100"
522                                                 shift; shift; shift;
523                                         done
524                                 else
525                                         echo -e $RED" args: start_mr [<topic-name> <base-url> <group-and-user-url>]*"$ERED
526                                         echo -e $RED" Got: $@"$ERED
527                                         exit 1
528                                 fi
529                         fi
530
531                         echo " Current topics:"
532                         curlString="$MR_DMAAP_PATH/topics"
533                         result=$(__do_curl "$curlString")
534                         echo $result | indent2
535
536                 fi
537
538                 if [ $retcode_included_mr -eq 0 ]; then
539
540                         __mr_export_vars
541
542                         if [ $retcode_prestarted_dmaapmr -eq 0 ] || [ $retcode_included_dmaapmr -eq 0 ]; then  # Set topics for dmaap
543                                 export TOPIC_READ="http://$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE:$MR_INTERNAL_PORT/events/$MR_READ_TOPIC"
544                                 export TOPIC_WRITE="http://$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE:$MR_INTERNAL_PORT/events/$MR_WRITE_TOPIC/users/mr-stub?timeout=15000&limit=100"
545                                 export GENERIC_TOPICS_UPLOAD_BASEURL="http://$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE:$MR_INTERNAL_PORT"
546                         else
547                                 export TOPIC_READ=""
548                                 export TOPIC_WRITE=""
549                                 export GENERIC_TOPICS_UPLOAD_BASEURL=""
550                         fi
551
552                         #Check if onap namespace exists, if not create it
553                         __kube_create_namespace $KUBE_ONAP_NAMESPACE
554
555                         # Create service
556                         input_yaml=$SIM_GROUP"/"$MR_STUB_COMPOSE_DIR"/"svc.yaml
557                         output_yaml=$PWD/tmp/mr_svc.yaml
558                         __kube_create_instance service $MR_STUB_APP_NAME $input_yaml $output_yaml
559
560                         # Create app
561                         input_yaml=$SIM_GROUP"/"$MR_STUB_COMPOSE_DIR"/"app.yaml
562                         output_yaml=$PWD/tmp/mr_app.yaml
563                         __kube_create_instance app $MR_STUB_APP_NAME $input_yaml $output_yaml
564
565
566                 fi
567
568                 __check_service_start $MR_STUB_APP_NAME $MR_STUB_PATH$MR_STUB_ALIVE_URL
569
570         else
571
572                 __check_included_image 'DMAAPMR'
573                 retcode_dmaapmr=$?
574                 __check_included_image 'MR'
575                 retcode_mr=$?
576
577                 if [ $retcode_dmaapmr -ne 0 ] && [ $retcode_mr -ne 0 ]; then
578                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
579                                 echo -e $RED"The Message Router will not be started"$ERED
580                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included"
581                                 exit
582                 fi
583
584                 if [ $retcode_dmaapmr -eq 0 ] && [ $retcode_mr -ne 0 ]; then
585                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
586                                 echo -e $RED"The Message Router will not be started"$ERED
587                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included"
588                                 exit
589                 fi
590
591                 export TOPIC_READ=""
592         export TOPIC_WRITE=""
593                 export GENERIC_TOPICS_UPLOAD_BASEURL=""
594                 if [ $retcode_dmaapmr -eq 0 ]; then  # Set topics for dmaap
595                         export TOPIC_READ="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT/events/$MR_READ_TOPIC"
596                         export TOPIC_WRITE="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT/events/$MR_WRITE_TOPIC/users/mr-stub?timeout=15000&limit=100"
597                         export GENERIC_TOPICS_UPLOAD_BASEURL="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT"
598                 fi
599
600                 __dmaapmr_export_vars
601
602                 if [ $retcode_dmaapmr -eq 0 ]; then
603
604                         # copy config files
605                         MR_MNT_CONFIG_BASEPATH=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR$MR_DMAAP_HOST_MNT_DIR
606                         cp -r $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR$MR_DMAAP_HOST_CONFIG_DIR/*  $MR_MNT_CONFIG_BASEPATH
607
608                         # substitute vars
609                         configfile=$MR_MNT_CONFIG_BASEPATH/mr/MsgRtrApi.properties
610                         cp $configfile $configfile"_tmp"
611                         envsubst < $configfile"_tmp" > $configfile
612
613                         __start_container $MR_DMAAP_COMPOSE_DIR "" NODOCKERARGS 1 $MR_DMAAP_APP_NAME
614
615                         __check_service_start $MR_DMAAP_APP_NAME $MR_DMAAP_PATH$MR_DMAAP_ALIVE_URL
616
617                         echo " Kafka TCP node port $MR_KAFKA_DOCKER_LOCALHOST_PORT"
618
619                         if [ $# -gt 0 ]; then
620                                 if [ $(($#%3)) -eq 0 ]; then
621                                         while [ $# -gt 0 ]; do
622                                                 __dmaap_pipeclean "$1" "$2/$1" "$2/$1/$3?timeout=1000&limit=100"
623                                                 shift; shift; shift;
624                                         done
625                                 else
626                                         echo -e $RED" args: start_mr [<topic-name> <base-url> <group-and-user-url>]*"$ERED
627                                         echo -e $RED" Got: $@"$ERED
628                                         exit 1
629                                 fi
630                         fi
631
632                         dmaap_api_print_topics
633                 fi
634
635                 __mr_export_vars
636
637                 if [ $retcode_mr -eq 0 ]; then
638                         __start_container $MR_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $MR_STUB_APP_NAME
639
640                         __check_service_start $MR_STUB_APP_NAME $MR_STUB_PATH$MR_STUB_ALIVE_URL
641                 fi
642
643         fi
644         echo ""
645         return 0
646 }
647
648 # Create a dmaap mr topic
649 # args: <topic name> <topic-description>
650 __create_topic() {
651         echo -ne " Creating topic: $1"$SAMELINE
652
653         json_topic="{\"topicName\":\"$1\",\"partitionCount\":\"2\", \"replicationCount\":\"3\", \"transactionEnabled\":\"false\",\"topicDescription\":\"$2\"}"
654         fname="./tmp/$1.json"
655         echo $json_topic > $fname
656
657         query="/topics/create"
658         topic_retries=10
659         while [ $topic_retries -gt 0 ]; do
660                 let topic_retries=topic_retries-1
661                 res="$(__do_curl_to_api DMAAPMR POST $query $fname)"
662                 status=${res:${#res}-3}
663
664                 if [[ $status == "2"* ]]; then
665                         topic_retries=0
666                         echo -e " Creating topic: $1 $GREEN OK $EGREEN"
667                 else
668                         if [ $topic_retries -eq 0 ]; then
669                                 echo -e " Creating topic: $1 $RED Failed $ERED"
670                                 ((RES_CONF_FAIL++))
671                                 return 1
672                         else
673                                 sleep 1
674                         fi
675                 fi
676         done
677         echo
678         return 0
679 }
680
681 # Do a pipeclean of a topic - to overcome dmaap mr bug...
682 # args: <topic> <post-url> <read-url> [<num-retries>]
683 __dmaap_pipeclean() {
684         pipeclean_retries=50
685         if [ $# -eq 4 ]; then
686                 pipeclean_retries=$4
687         fi
688         echo -ne " Doing dmaap-mr pipe cleaning on topic: $1"$SAMELINE
689         while [ $pipeclean_retries -gt 0 ]; do
690                 if [[ $1 == *".text" ]]; then
691                         echo "pipeclean-$1:$pipeclean_retries" > ./tmp/__dmaap_pipeclean.txt
692                         curlString="$MR_DMAAP_PATH$2 -X POST  -H Content-Type:text/plain -d@./tmp/__dmaap_pipeclean.txt"
693                 else
694                         echo "{\"pipeclean-$1\":$pipeclean_retries}" > ./tmp/__dmaap_pipeclean.json
695                         curlString="$MR_DMAAP_PATH$2 -X POST  -H Content-Type:application/json -d@./tmp/__dmaap_pipeclean.json"
696                 fi
697                 let pipeclean_retries=pipeclean_retries-1
698                 result=$(__do_curl "$curlString")
699                 if [ $? -ne 0 ]; then
700                         sleep 1
701                 else
702                         curlString="$MR_DMAAP_PATH$3"
703                         result=$(__do_curl "$curlString")
704                         if [ $? -eq 0 ]; then
705                                 if [ $result != "[]" ]; then
706                                         echo -e " Doing dmaap-mr pipe cleaning on topic: $1 $GREEN OK $EGREEN"
707                                         return 0
708
709                                 else
710                                         sleep 1
711                                 fi
712                         fi
713                 fi
714         done
715         echo -e "Doing dmaap-mr pipe cleaning on topic: $1 $RED Failed $ERED"
716         return 1
717 }
718
719 # Helper function to list the current topics in DMAAP MR
720 # args: -
721 dmaap_api_print_topics() {
722         echo " Current topics:"
723         curlString="$MR_DMAAP_PATH/topics"
724         result=$(__do_curl "$curlString")
725         echo $result | indent2
726 }
727
728
729 ### Generic test cases for varaible checking
730
731 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
732 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
733 # equal to the target or not.
734 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
735 # before setting pass or fail depending on if the variable value becomes equal to the target
736 # value or not.
737 # (Function for test scripts)
738 mr_equal() {
739         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
740                 __var_test "MR" $MR_STUB_PATH/counter/ $1 "=" $2 $3
741         else
742                 ((RES_CONF_FAIL++))
743                 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
744         fi
745 }
746
747 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
748 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
749 # greater than the target or not.
750 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
751 # before setting pass or fail depending on if the variable value becomes greater than the target
752 # value or not.
753 # (Function for test scripts)
754 mr_greater() {
755         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
756                 __var_test "MR" $MR_STUB_PATH/counter/ $1 ">" $2 $3
757         else
758                 ((RES_CONF_FAIL++))
759                 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
760         fi
761 }
762
763 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
764 mr_read() {
765         echo "$(__do_curl $MR_STUB_PATH/counter/$1)"
766 }
767
768 # Print a variable value from the MR stub.
769 # arg: <variable-name>
770 # (Function for test scripts)
771 mr_print() {
772         if [ $# != 1 ]; then
773                 ((RES_CONF_FAIL++))
774         __print_err "need one arg, <mr-param>" $@
775                 exit 1
776         fi
777         echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $MR_STUB_PATH/counter/$1)"$EBOLD
778 }
779
780 # Send json to topic in mr-stub.
781 # arg: <topic-url> <json-msg>
782 # (Function for test scripts)
783 mr_api_send_json() {
784         __log_conf_start $@
785     if [ $# -ne 2 ]; then
786         __print_err "<topic-url> <json-msg>" $@
787         return 1
788     fi
789         query=$1
790         fname=$PWD/tmp/json_payload_to_mr.json
791         echo $2 > $fname
792         res="$(__do_curl_to_api MRSTUB POST $query $fname)"
793
794         status=${res:${#res}-3}
795         if [ $status -ne 200 ]; then
796                 __log_conf_fail_status_code 200 $status
797                 return 1
798         fi
799
800         __log_conf_ok
801         return 0
802 }
803
804 # Send text to topic in mr-stub.
805 # arg: <topic-url> <text-msg>
806 # (Function for test scripts)
807 mr_api_send_text() {
808         __log_conf_start $@
809     if [ $# -ne 2 ]; then
810         __print_err "<topic-url> <text-msg>" $@
811         return 1
812     fi
813         query=$1
814         fname=$PWD/tmp/text_payload_to_mr.txt
815         echo $2 > $fname
816         res="$(__do_curl_to_api MRSTUB POST $query $fname text/plain)"
817
818         status=${res:${#res}-3}
819         if [ $status -ne 200 ]; then
820                 __log_conf_fail_status_code 200 $status
821                 return 1
822         fi
823
824         __log_conf_ok
825         return 0
826 }
827
828 # Send json file to topic in mr-stub.
829 # arg: <topic-url> <json-file>
830 # (Function for test scripts)
831 mr_api_send_json_file() {
832         __log_conf_start $@
833     if [ $# -ne 2 ]; then
834         __print_err "<topic-url> <json-file>" $@
835         return 1
836     fi
837         query=$1
838         if [ ! -f $2 ]; then
839                 __log_test_fail_general "File $2 does not exist"
840                 return 1
841         fi
842         #Create json array for mr
843         datafile="tmp/mr_api_send_json_file.json"
844         { echo -n "[" ; cat $2 ; echo -n "]" ;} > $datafile
845
846         res="$(__do_curl_to_api MRSTUB POST $query $datafile)"
847
848         status=${res:${#res}-3}
849         if [ $status -ne 200 ]; then
850                 __log_conf_fail_status_code 200 $status
851                 return 1
852         fi
853
854         __log_conf_ok
855         return 0
856 }
857
858 # Send text file to topic in mr-stub.
859 # arg: <topic-url> <text-file>
860 # (Function for test scripts)
861 mr_api_send_text_file() {
862         __log_conf_start $@
863     if [ $# -ne 2 ]; then
864         __print_err "<topic-url> <text-file>" $@
865         return 1
866     fi
867         query=$1
868         if [ ! -f $2 ]; then
869                 __log_test_fail_general "File $2 does not exist"
870                 return 1
871         fi
872
873         res="$(__do_curl_to_api MRSTUB POST $query $2 text/plain)"
874
875         status=${res:${#res}-3}
876         if [ $status -ne 200 ]; then
877                 __log_conf_fail_status_code 200 $status
878                 return 1
879         fi
880
881         __log_conf_ok
882         return 0
883 }
884
885 # Create json file for payload
886 # arg: <size-in-kb> <filename>
887 mr_api_generate_json_payload_file() {
888         __log_conf_start $@
889     if [ $# -ne 2 ]; then
890         __print_err "<topic-url> <json-file>" $@
891         return 1
892     fi
893         if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
894                 __log_conf_fail_general "Only size between 1k and 10000k supported"
895                 return 1
896         fi
897         echo -n "{\"abcdefghijklmno\":[" > $2
898         LEN=$(($1*100-2))
899         echo -n "\""ABCDEFG"\"" >> $2
900         for ((idx=1; idx<$LEN; idx++))
901         do
902                 echo -n ",\"ABCDEFG\"" >> $2
903         done
904         echo -n "]}" >> $2
905
906         __log_conf_ok
907         return 0
908 }
909
910 # Create text file for payload
911 # arg: <size-in-kb> <filename>
912 mr_api_generate_text_payload_file() {
913         __log_conf_start $@
914     if [ $# -ne 2 ]; then
915         __print_err "<topic-url> <text-file>" $@
916         return 1
917     fi
918         if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
919                 __log_conf_fail_general "Only size between 1k and 10000k supported"
920                 return 1
921         fi
922         echo -n "" > $2
923         LEN=$(($1*100))
924         for ((idx=0; idx<$LEN; idx++))
925         do
926                 echo -n "ABCDEFGHIJ" >> $2
927         done
928
929         __log_conf_ok
930         return 0
931 }