Adaptation of test env to helm chart
[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$2mr.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
197 #######################################################
198
199 use_mr_http() {
200         __mr_set_protocoll "http" $MR_INTERNAL_PORT $MR_EXTERNAL_PORT $MR_INTERNAL_PORT $MR_EXTERNAL_PORT $MR_INTERNAL_SECURE_PORT $MR_EXT_SECURE_PORT
201 }
202
203 use_mr_https() {
204         __mr_set_protocoll "https" $MR_INTERNAL_SECURE_PORT $MR_EXTERNAL_SECURE_PORT
205 }
206
207 # Setup paths to svc/container for internal and external access
208 # args: <protocol> <internal-port> <external-port> <mr-stub-internal-port> <mr-stub-external-port> <mr-stub-internal-secure-port> <mr-stub-external-secure-port>
209 __mr_set_protocoll() {
210         echo -e $BOLD"$MR_STUB_DISPLAY_NAME and $MR_DMAAP_DISPLAY_NAME protocol setting"$EBOLD
211         echo -e " Using $BOLD http $EBOLD towards $MR_STUB_DISPLAY_NAME and $MR_DMAAP_DISPLAY_NAME"
212
213         ## Access to Dmaap mediator
214
215         MR_HTTPX=$1
216
217         # Access via test script
218         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME":"$2  # access from script via proxy, docker
219         MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME":"$2 # access from script via proxy, docker
220
221         MR_SERVICE_PATH=$MR_STUB_PATH # access container->container, docker -  access pod->svc, kube
222         __check_included_image "DMAAPMR"
223         if [ $? -eq 0 ]; then
224                 MR_SERVICE_PATH=$MR_DMAAP_PATH # access container->container, docker -  access pod->svc, kube
225         fi
226
227         # For directing calls from script to e.g.PMS via message rounter
228         # Theses case shall always go though the  mr-stub
229         MR_ADAPTER_HTTP="http://"$MR_STUB_APP_NAME":"$4
230         MR_ADAPTER_HTTPS="https://"$MR_STUB_APP_NAME":"$6
231
232         if [ $RUNMODE == "KUBE" ]; then
233                 MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME.$KUBE_ONAP_NAMESPACE":"$3 # access from script via proxy, kube
234                 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE":"$3 # access from script via proxy, kube
235
236                 MR_SERVICE_PATH=$MR_STUB_PATH
237                 __check_included_image "DMAAPMR"
238                 if [ $? -eq 0 ]; then
239                         MR_SERVICE_PATH=$MR_DMAAP_PATH
240                 fi
241                 __check_prestarted_image "DMAAPMR"
242                 if [ $? -eq 0 ]; then
243                         MR_SERVICE_PATH=$MR_DMAAP_PATH
244                 fi
245
246                 # For directing calls from script to e.g.PMS, via message rounter
247                 # These calls shall always go though the  mr-stub
248                 MR_ADAPTER_HTTP="http://"$MR_STUB_APP_NAME":"$5
249                 MR_ADAPTER_HTTPS="https://"$MR_STUB_APP_NAME":"$7
250         fi
251
252         # For calls from script to the mr-stub
253         MR_STUB_ADAPTER=$MR_STUB_PATH
254         MR_STUB_ADAPTER_TYPE="REST"
255
256         echo ""
257 }
258
259 # Export env vars for config files, docker compose and kube resources
260 # args: -
261 __dmaapmr_export_vars() {
262         #Docker only
263         export DOCKER_SIM_NWNAME
264         export ONAP_ZOOKEEPER_IMAGE
265         export MR_ZOOKEEPER_APP_NAME
266         export ONAP_KAFKA_IMAGE
267         export MR_KAFKA_APP_NAME
268         export ONAP_DMAAPMR_IMAGE
269         export MR_DMAAP_APP_NAME
270         export MR_DMAAP_LOCALHOST_PORT
271         export MR_INTERNAL_PORT
272         export MR_DMAAP_LOCALHOST_SECURE_PORT
273         export MR_INTERNAL_SECURE_PORT
274         export MR_DMAAP_HOST_MNT_DIR
275 }
276
277 # Export env vars for config files, docker compose and kube resources
278 # args: -
279 __mr_export_vars() {
280         #Docker only
281         export DOCKER_SIM_NWNAME
282         export MR_STUB_APP_NAME
283         export MRSTUB_IMAGE
284         export MR_INTERNAL_PORT
285         export MR_INTERNAL_SECURE_PORT
286         export MR_STUB_LOCALHOST_PORT
287         export MR_STUB_LOCALHOST_SECURE_PORT
288         export MR_STUB_CERT_MOUNT_DIR
289         export MR_STUB_DISPLAY_NAME
290 }
291
292
293 # Start the Message Router stub interface in the simulator group
294 # args: -
295 # (Function for test scripts)
296 start_mr() {
297
298         echo -e $BOLD"Starting $MR_DMAAP_DISPLAY_NAME and/or $MR_STUB_DISPLAY_NAME"$EBOLD
299
300         if [ $RUNMODE == "KUBE" ]; then
301
302         # Table of possible combinations of included mr and included/prestarted dmaap-mr
303                 # mr can never be prestarted
304                 # mr can be used stand alone
305                 # if dmaapmr is included/prestarted, then mr is needed as well as frontend
306
307         # Inverted logic - 0 mean true, 1 means false
308                 # mr prestarted      0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
309                 # mr included        0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
310                 # dmaap prestarted   0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
311                 # dmaap included     0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
312                 # ==================================================
313                 # OK                 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1
314
315                 __check_prestarted_image 'MR'
316                 retcode_prestarted_mr=$?
317                 __check_included_image 'MR'
318                 retcode_included_mr=$?
319
320                 __check_prestarted_image 'DMAAPMR'
321                 retcode_prestarted_dmaapmr=$?
322                 __check_included_image 'DMAAPMR'
323                 retcode_included_dmaapmr=$?
324
325                 paramerror=1
326
327                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
328                         if [ $retcode_prestarted_dmaapmr -ne 0 ] && [ $retcode_included_dmaapmr -eq 0 ]; then
329                                 paramerror=0
330                         fi
331                 fi
332
333                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
334                         if [ $retcode_prestarted_dmaapmr -eq 0 ] && [ $retcode_included_dmaapmr -ne 0 ]; then
335                                 paramerror=0
336                         fi
337                 fi
338
339                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
340                         if [ $retcode_prestarted_dmaapmr -ne 0 ] && [ $retcode_included_dmaapmr -ne 0 ]; then
341                                 paramerror=0
342                         fi
343                 fi
344
345                 if [ $paramerror -ne 0 ]; then
346                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
347                                 echo -e $RED"The Message Router will not be started"$ERED
348                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included and/or prestarted"$ERED
349                                 exit
350                 fi
351
352                 if [ $retcode_prestarted_dmaapmr -eq 0 ]; then
353                         echo -e " Using existing $MR_DMAAP_APP_NAME deployment and service"
354                         __kube_scale deployment $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE 1
355                 fi
356
357                 if [ $retcode_included_dmaapmr -eq 0 ]; then
358
359                         __dmaapmr_export_vars
360
361                         #export MR_DMAAP_APP_NAME
362                         export MR_DMAAP_KUBE_APP_NAME=message-router
363                         MR_DMAAP_APP_NAME=$MR_DMAAP_KUBE_APP_NAME
364                         export KUBE_ONAP_NAMESPACE
365                         export MR_EXTERNAL_PORT
366                         export MR_INTERNAL_PORT
367                         export MR_EXTERNAL_SECURE_PORT
368                         export MR_INTERNAL_SECURE_PORT
369                         export ONAP_DMAAPMR_IMAGE
370
371                         export MR_KAFKA_BWDS_NAME=akfak-bwds
372                         export MR_KAFKA_BWDS_NAME=kaka
373                         export KUBE_ONAP_NAMESPACE
374
375                         export MR_ZOOKEEPER_APP_NAME
376                         export ONAP_ZOOKEEPER_IMAGE
377
378                         #Check if onap namespace exists, if not create it
379                         __kube_create_namespace $KUBE_ONAP_NAMESPACE
380
381                         # TODO - Fix domain name substitution in the prop file
382                         # Create config maps - dmaapmr app
383                         configfile=$PWD/tmp/MsgRtrApi.properties
384                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"$MR_DMAAP_HOST_MNT_DIR"/mr/KUBE-MsgRtrApi.properties $configfile
385
386                         output_yaml=$PWD/tmp/dmaapmr_msgrtrapi_cfc.yaml
387                         __kube_create_configmap dmaapmr-msgrtrapi.properties $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
388
389                         configfile=$PWD/tmp/logback.xml
390                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"$MR_DMAAP_HOST_MNT_DIR"/mr/logback.xml $configfile
391                         output_yaml=$PWD/tmp/dmaapmr_logback_cfc.yaml
392                         __kube_create_configmap dmaapmr-logback.xml $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
393
394                         configfile=$PWD/tmp/cadi.properties
395                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"$MR_DMAAP_HOST_MNT_DIR"/mr/cadi.properties $configfile
396                         output_yaml=$PWD/tmp/dmaapmr_cadi_cfc.yaml
397                         __kube_create_configmap dmaapmr-cadi.properties $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
398
399                         # Create config maps - kafka app
400                         configfile=$PWD/tmp/zk_client_jaas.conf
401                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"$MR_DMAAP_HOST_MNT_DIR"/kafka/zk_client_jaas.conf $configfile
402                         output_yaml=$PWD/tmp/dmaapmr_zk_client_cfc.yaml
403                         __kube_create_configmap dmaapmr-zk-client-jaas.conf $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
404
405                         # Create config maps - zookeeper app
406                         configfile=$PWD/tmp/zk_server_jaas.conf
407                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"$MR_DMAAP_HOST_MNT_DIR"/zk/zk_server_jaas.conf $configfile
408                         output_yaml=$PWD/tmp/dmaapmr_zk_server_cfc.yaml
409                         __kube_create_configmap dmaapmr-zk-server-jaas.conf $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
410
411                         # Create service
412                         input_yaml=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"svc.yaml
413                         output_yaml=$PWD/tmp/dmaapmr_svc.yaml
414                         __kube_create_instance service $MR_DMAAP_APP_NAME $input_yaml $output_yaml
415
416                         # Create app
417                         input_yaml=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"app.yaml
418                         output_yaml=$PWD/tmp/dmaapmr_app.yaml
419                         __kube_create_instance app $MR_DMAAP_APP_NAME $input_yaml $output_yaml
420
421
422                         echo " Retrieving host and ports for service..."
423                         MR_DMAAP_HOST_NAME=$(__kube_get_service_host $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE)
424
425                         MR_EXT_PORT=$(__kube_get_service_port $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE "http")
426                         MR_EXT_SECURE_PORT=$(__kube_get_service_port $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE "https")
427
428                         echo " Host IP, http port, https port: $MR_DMAAP_APP_NAME $MR_EXT_PORT $MR_EXT_SECURE_PORT"
429                         MR_SERVICE_PATH=""
430                         if [ $MR_HTTPX == "http" ]; then
431                                 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_EXT_PORT
432                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_PORT
433                         else
434                                 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_EXT_SECURE_PORT
435                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_SECURE_PORT
436                         fi
437
438                                 __check_service_start $MR_DMAAP_APP_NAME $MR_DMAAP_PATH$MR_DMAAP_ALIVE_URL
439
440                 fi
441
442                 if [ $retcode_included_mr -eq 0 ]; then
443                         #exporting needed var for deployment
444                         export MR_STUB_APP_NAME
445                         export KUBE_ONAP_NAMESPACE
446                         export MRSTUB_IMAGE
447                         export MR_INTERNAL_PORT
448                         export MR_INTERNAL_SECURE_PORT
449                         export MR_EXTERNAL_PORT
450                         export MR_EXTERNAL_SECURE_PORT
451
452                         if [ $retcode_prestarted_dmaapmr -eq 0 ] || [ $retcode_included_dmaapmr -eq 0 ]; then  # Set topics for dmaap
453                                 export TOPIC_READ="http://$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE:$MR_INTERNAL_PORT/events/$MR_READ_TOPIC"
454                                 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"
455                         else
456                                 export TOPIC_READ=""
457                                 export TOPIC_WRITE=""
458                         fi
459
460                         #Check if onap namespace exists, if not create it
461                         __kube_create_namespace $KUBE_ONAP_NAMESPACE
462
463                         # Create service
464                         input_yaml=$SIM_GROUP"/"$MR_STUB_COMPOSE_DIR"/"svc.yaml
465                         output_yaml=$PWD/tmp/mr_svc.yaml
466                         __kube_create_instance service $MR_STUB_APP_NAME $input_yaml $output_yaml
467
468                         # Create app
469                         input_yaml=$SIM_GROUP"/"$MR_STUB_COMPOSE_DIR"/"app.yaml
470                         output_yaml=$PWD/tmp/mr_app.yaml
471                         __kube_create_instance app $MR_STUB_APP_NAME $input_yaml $output_yaml
472
473
474                 fi
475
476
477                 echo " Retrieving host and ports for service..."
478                 MR_STUB_HOST_NAME=$(__kube_get_service_host $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE)
479
480                 MR_EXT_PORT=$(__kube_get_service_port $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE "http")
481                 MR_EXT_SECURE_PORT=$(__kube_get_service_port $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE "https")
482
483                 echo " Host IP, http port, https port: $MR_STUB_APP_NAME $MR_EXT_PORT $MR_EXT_SECURE_PORT"
484                 if [ $MR_HTTPX == "http" ]; then
485                         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_EXT_PORT
486                         if [ -z "$MR_SERVICE_PATH" ]; then
487                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_PORT
488                         fi
489                 else
490                         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_EXT_SECURE_PORT
491                         if [ -z "$MR_SERVICE_PATH" ]; then
492                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_SECURE_PORT
493                         fi
494                 fi
495                 MR_ADAPTER_HTTP="http://"$MR_STUB_HOST_NAME":"$MR_EXT_PORT
496                 MR_ADAPTER_HTTPS="https://"$MR_STUB_HOST_NAME":"$MR_EXT_SECURE_PORT
497
498                 MR_STUB_ADAPTER=$MR_STUB_PATH
499                 MR_STUB_ADAPTER_TYPE="REST"
500
501                 __check_service_start $MR_STUB_APP_NAME $MR_STUB_PATH$MR_STUB_ALIVE_URL
502
503                 echo -ne " Service $MR_STUB_APP_NAME - reset  "$SAMELINE
504                 result=$(__do_curl $MR_STUB_PATH/reset)
505                 if [ $? -ne 0 ]; then
506                         echo -e " Service $MR_STUB_APP_NAME - reset  $RED Failed $ERED - will continue"
507                 else
508                         echo -e " Service $MR_STUB_APP_NAME - reset  $GREEN OK $EGREEN"
509                 fi
510
511
512         else
513
514                 __check_included_image 'DMAAPMR'
515                 retcode_dmaapmr=$?
516                 __check_included_image 'MR'
517                 retcode_mr=$?
518
519                 if [ $retcode_dmaapmr -ne 0 ] && [ $retcode_mr -ne 0 ]; then
520                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
521                                 echo -e $RED"The Message Router will not be started"$ERED
522                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included"
523                                 exit
524                 fi
525
526                 if [ $retcode_dmaapmr -eq 0 ] && [ $retcode_mr -ne 0 ]; then
527                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
528                                 echo -e $RED"The Message Router will not be started"$ERED
529                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included"
530                                 exit
531                 fi
532
533                 export TOPIC_READ=""
534         export TOPIC_WRITE=""
535                 if [ $retcode_dmaapmr -eq 0 ]; then  # Set topics for dmaap
536                         export TOPIC_READ="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT/events/$MR_READ_TOPIC"
537                         export TOPIC_WRITE="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT/events/$MR_WRITE_TOPIC/users/mr-stub?timeout=15000&limit=100"
538                 fi
539
540                 __dmaapmr_export_vars
541
542                 if [ $retcode_dmaapmr -eq 0 ]; then
543                         __start_container $MR_DMAAP_COMPOSE_DIR "" NODOCKERARGS 1 $MR_DMAAP_APP_NAME
544
545                         __check_service_start $MR_DMAAP_APP_NAME $MR_DMAAP_PATH$MR_DMAAP_ALIVE_URL
546
547
548                         __create_topic $MR_READ_TOPIC "Topic for reading policy messages"
549
550                         __create_topic $MR_WRITE_TOPIC "Topic for writing policy messages"
551
552                         __dmaap_pipeclean $MR_READ_TOPIC "/events/$MR_READ_TOPIC" "/events/$MR_READ_TOPIC/users/policy-agent?timeout=1000&limit=100"
553
554                         __dmaap_pipeclean $MR_WRITE_TOPIC "/events/$MR_WRITE_TOPIC" "/events/$MR_WRITE_TOPIC/users/mr-stub?timeout=1000&limit=100"
555
556                         echo " Current topics:"
557                         curlString="$MR_DMAAP_PATH/topics"
558                         result=$(__do_curl "$curlString")
559                         echo $result | indent2
560                 fi
561
562                 __mr_export_vars
563
564                 if [ $retcode_mr -eq 0 ]; then
565                         __start_container $MR_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $MR_STUB_APP_NAME
566
567                         __check_service_start $MR_STUB_APP_NAME $MR_STUB_PATH$MR_STUB_ALIVE_URL
568                 fi
569
570         fi
571         echo ""
572         return 0
573 }
574
575 # Create a dmaap mr topic
576 # args: <topic name> <topic-description>
577 __create_topic() {
578         echo -ne " Creating read topic: $1"$SAMELINE
579
580         json_topic="{\"topicName\":\"$1\",\"partitionCount\":\"2\", \"replicationCount\":\"3\", \"transactionEnabled\":\"false\",\"topicDescription\":\"$2\"}"
581         echo $json_topic > ./tmp/$1.json
582
583         curlString="$MR_DMAAP_PATH/topics/create -X POST  -H Content-Type:application/json -d@./tmp/$1.json"
584         topic_retries=5
585         while [ $topic_retries -gt 0 ]; do
586                 let topic_retries=topic_retries-1
587                 result=$(__do_curl "$curlString")
588                 if [ $? -eq 0 ]; then
589                         topic_retries=0
590                         echo -e " Creating read topic: $1 $GREEN OK $EGREEN"
591                 fi
592                 if [ $? -ne 0 ]; then
593                         if [ $topic_retries -eq 0 ]; then
594                                 echo -e " Creating read topic: $1 $RED Failed $ERED"
595                                 ((RES_CONF_FAIL++))
596                                 return 1
597                         else
598                                 sleep 1
599                         fi
600                 fi
601         done
602         return 0
603 }
604
605 # Do a pipeclean of a topic - to overcome dmaap mr bug...
606 # args: <topic> <post-url> <read-url>
607 __dmaap_pipeclean() {
608         pipeclean_retries=50
609         echo -ne " Doing dmaap-mr pipe cleaning on topic: $1"$SAMELINE
610         while [ $pipeclean_retries -gt 0 ]; do
611                 echo "{\"pipeclean-$1\":$pipeclean_retries}" > ./tmp/pipeclean.json
612                 let pipeclean_retries=pipeclean_retries-1
613                 curlString="$MR_DMAAP_PATH$2 -X POST  -H Content-Type:application/json -d@./tmp/pipeclean.json"
614                 result=$(__do_curl "$curlString")
615                 if [ $? -ne 0 ]; then
616                         sleep 1
617                 else
618                         curlString="$MR_DMAAP_PATH$3"
619                         result=$(__do_curl "$curlString")
620                         if [ $? -eq 0 ]; then
621                                 if [ $result != "[]" ]; then
622                                         echo -e " Doing dmaap-mr pipe cleaning on topic: $1 $GREEN OK $EGREEN"
623                                         return 0
624
625                                 else
626                                         sleep 1
627                                 fi
628                         fi
629                 fi
630         done
631         echo -e "Doing dmaap-mr pipe cleaning on topic: $1 $RED Failed $ERED"
632         return 1
633 }
634
635
636 ### Generic test cases for varaible checking
637
638 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
639 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
640 # equal to the target or not.
641 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
642 # before setting pass or fail depending on if the variable value becomes equal to the target
643 # value or not.
644 # (Function for test scripts)
645 mr_equal() {
646         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
647                 __var_test "MR" $MR_STUB_PATH/counter/ $1 "=" $2 $3
648         else
649                 ((RES_CONF_FAIL++))
650                 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
651         fi
652 }
653
654 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
655 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
656 # greater than the target or not.
657 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
658 # before setting pass or fail depending on if the variable value becomes greater than the target
659 # value or not.
660 # (Function for test scripts)
661 mr_greater() {
662         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
663                 __var_test "MR" $MR_STUB_PATH/counter/ $1 ">" $2 $3
664         else
665                 ((RES_CONF_FAIL++))
666                 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
667         fi
668 }
669
670 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
671 mr_read() {
672         echo "$(__do_curl $MR_STUB_PATH/counter/$1)"
673 }
674
675 # Print a variable value from the MR stub.
676 # arg: <variable-name>
677 # (Function for test scripts)
678 mr_print() {
679         if [ $# != 1 ]; then
680                 ((RES_CONF_FAIL++))
681         __print_err "need one arg, <mr-param>" $@
682                 exit 1
683         fi
684         echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $MR_STUB_PATH/counter/$1)"$EBOLD
685 }
686
687 # Send json to topic in mr-stub.
688 # arg: <topic-url> <json-msg>
689 # (Function for test scripts)
690 mr_api_send_json() {
691         __log_test_start $@
692     if [ $# -ne 2 ]; then
693         __print_err "<topic-url> <json-msg>" $@
694         return 1
695     fi
696         query=$1
697         fname=$PWD/tmp/json_payload_to_mr.json
698         echo $2 > $fname
699         res="$(__do_curl_to_api MRSTUB POST $query $fname)"
700
701         status=${res:${#res}-3}
702         if [ $status -ne 200 ]; then
703                 __log_test_fail_status_code 200 $status
704                 return 1
705         fi
706
707         __log_test_pass
708         return 0
709 }