Merge "NONRTRIC - PMS Persistent storage of policies and type definitions"
[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 CR shall never be pulled from remove 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         else
71                 echo -e $RED" Build Failed"$ERED
72                 ((RES_CONF_FAIL++))
73                 cat .dockererr
74                 echo -e $RED"Exiting...."$ERED
75                 exit 1
76         fi
77 }
78
79 # Build image (only for simulator or interfaces stubs owned by the test environment)
80 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
81 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
82 __DMAAPMR_imagebuild() {
83         echo -e $RED"Image for app DMAAPMR shall never be built"$ERED
84 }
85
86 # Generate a string for each included image using the app display name and a docker images format string
87 # arg: <docker-images-format-string> <file-to-append>
88 __MR_image_data() {
89         echo -e "$MR_STUB_DISPLAY_NAME\t$(docker images --format $1 $MRSTUB_IMAGE)" >>   $2
90 }
91
92 # Generate a string for each included image using the app display name and a docker images format string
93 # arg: <docker-images-format-string> <file-to-append>
94 __DMAAPMR_image_data() {
95         echo -e "DMAAP Message Router\t$(docker images --format $1 $ONAP_DMAAPMR_IMAGE)" >>   $2
96         echo -e "ZooKeeper\t$(docker images --format $1 $ONAP_ZOOKEEPER_IMAGE)" >>   $2
97         echo -e "Kafka\t$(docker images --format $1 $ONAP_KAFKA_IMAGE)" >>   $2
98 }
99
100 # Scale kubernetes resources to zero
101 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
102 # This function is called for apps fully managed by the test script
103 __MR_kube_scale_zero() {
104         __kube_scale_all_resources $KUBE_ONAP_NAMESPACE autotest MR
105 }
106
107 # Scale kubernetes resources to zero
108 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
109 # This function is called for apps fully managed by the test script
110 __DMAAPMR_kube_scale_zero() {
111         __kube_scale_all_resources $KUBE_ONAP_NAMESPACE autotest DMAAPMR
112 }
113
114 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
115 # This function is called for prestarted apps not managed by the test script.
116 __MR_kube_scale_zero_and_wait() {
117         echo -e " MR replicas kept as is"
118 }
119
120 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
121 # This function is called for prestarted apps not managed by the test script.
122 __DMAAPMR_kube_scale_zero_and_wait() {
123         echo -e " DMAAP replicas kept as is"
124 }
125
126 # Delete all kube resouces for the app
127 # This function is called for apps managed by the test script.
128 __MR_kube_delete_all() {
129         __kube_delete_all_resources $KUBE_ONAP_NAMESPACE autotest MR
130 }
131
132 # Delete all kube resouces for the app
133 # This function is called for apps managed by the test script.
134 __DMAAPMR_kube_delete_all() {
135         __kube_delete_all_resources $KUBE_ONAP_NAMESPACE autotest DMAAPMR
136 }
137
138 # Store docker logs
139 # This function is called for apps managed by the test script.
140 # args: <log-dir> <file-prexix>
141 __MR_store_docker_logs() {
142         docker logs $MR_STUB_APP_NAME > $1$2_mr_stub.log 2>&1
143 }
144
145 # Store docker logs
146 # This function is called for apps managed by the test script.
147 # args: <log-dir> <file-prexix>
148 __DMAAPMR_store_docker_logs() {
149         docker logs $MR_DMAAP_APP_NAME > $1$2mr.log 2>&1
150         docker logs $MR_KAFKA_APP_NAME > $1$2_mr_kafka.log 2>&1
151         docker logs $MR_ZOOKEEPER_APP_NAME > $1$2_mr_zookeeper.log 2>&1
152 }
153
154 #######################################################
155
156 ## Access to Message Router
157 # Host name may be changed if app started by kube
158 # Direct access from script
159 MR_HTTPX="http"
160 MR_STUB_HOST_NAME=$LOCALHOST_NAME
161 MR_DMAAP_HOST_NAME=$LOCALHOST_NAME
162 MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_STUB_LOCALHOST_PORT
163 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_DMAAP_LOCALHOST_PORT
164 #Docker/Kube internal path
165 if [ $RUNMODE == "KUBE" ]; then
166         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_PORT
167         __check_included_image "DMAAPMR"
168         if [ $? -eq 0 ]; then
169                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_PORT
170         fi
171         __check_prestarted_image "DMAAPMR"
172         if [ $? -eq 0 ]; then
173                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_PORT
174         fi
175 else
176         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME":"$MR_INTERNAL_PORT
177         __check_included_image "DMAAPMR"
178         if [ $? -eq 0 ]; then
179                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME":"$MR_INTERNAL_PORT
180         fi
181 fi
182 MR_ADAPTER_HTTP="http://"$MR_STUB_HOST_NAME":"$MR_STUB_LOCALHOST_PORT
183 MR_ADAPTER_HTTPS="https://"$MR_STUB_HOST_NAME":"$MR_STUB_LOCALHOST_SECURE_PORT
184
185
186 #####################
187 ### MR stub functions
188 #####################
189
190 use_mr_http() {
191         echo -e $BOLD"MR protocol setting"$EBOLD
192         echo -e " Using $BOLD http $EBOLD towards MR"
193         MR_HTTPX="http"
194         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_STUB_LOCALHOST_PORT
195         MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_DMAAP_LOCALHOST_PORT
196         #Docker/Kube internal path
197         if [ $RUNMODE == "KUBE" ]; then
198                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_PORT
199                 __check_included_image "DMAAPMR"
200                 if [ $? -eq 0 ]; then
201                         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_PORT
202                 fi
203                 __check_prestarted_image "DMAAPMR"
204                 if [ $? -eq 0 ]; then
205                         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_PORT
206                 fi
207         else
208                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME":"$MR_INTERNAL_PORT
209                 __check_included_image "DMAAPMR"
210                 if [ $? -eq 0 ]; then
211                         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME":"$MR_INTERNAL_PORT
212                 fi
213         fi
214         echo ""
215 }
216
217 use_mr_https() {
218         echo -e $BOLD"MR protocol setting"$EBOLD
219         echo -e " Using $BOLD https $EBOLD towards MR"
220         MR_HTTPX="https"
221         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_STUB_LOCALHOST_SECURE_PORT
222         MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_DMAAP_LOCALHOST_SECURE_PORT
223         #Docker/Kube internal path
224         if [ $RUNMODE == "KUBE" ]; then
225                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_SECURE_PORT
226                 __check_included_image "DMAAPMR"
227                 if [ $? -eq 0 ]; then
228                         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_SECURE_PORT
229                 fi
230                 __check_prestarted_image "DMAAPMR"
231                 if [ $? -eq 0 ]; then
232                         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXTERNAL_SECURE_PORT
233                 fi
234         else
235                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME":"$MR_INTERNAL_SECURE_PORT
236                 __check_included_image "DMAAPMR"
237                 if [ $? -eq 0 ]; then
238                         MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME":"$MR_INTERNAL_SECURE_PORT
239                 fi
240         fi
241         echo ""
242 }
243
244 # Create a dmaap mr topic
245 # args: <topic name> <topic-description>
246 __create_topic() {
247         echo -ne " Creating read topic: $1"$SAMELINE
248
249         json_topic="{\"topicName\":\"$1\",\"partitionCount\":\"2\", \"replicationCount\":\"3\", \"transactionEnabled\":\"false\",\"topicDescription\":\"$2\"}"
250         echo $json_topic > ./tmp/$1.json
251
252         curlString="$MR_DMAAP_PATH/topics/create -X POST  -H Content-Type:application/json -d@./tmp/$1.json"
253         topic_retries=5
254         while [ $topic_retries -gt 0 ]; do
255                 let topic_retries=topic_retries-1
256                 result=$(__do_curl "$curlString")
257                 if [ $? -eq 0 ]; then
258                         topic_retries=0
259                         echo -e " Creating read topic: $1 $GREEN OK $EGREEN"
260                 fi
261                 if [ $? -ne 0 ]; then
262                         if [ $topic_retries -eq 0 ]; then
263                                 echo -e " Creating read topic: $1 $RED Failed $ERED"
264                                 ((RES_CONF_FAIL++))
265                                 return 1
266                         else
267                                 sleep 1
268                         fi
269                 fi
270         done
271         return 0
272 }
273
274 # Do a pipeclean of a topic - to overcome dmaap mr bug...
275 # args: <topic> <post-url> <read-url>
276 __dmaap_pipeclean() {
277         pipeclean_retries=50
278         echo -ne " Doing dmaap-mr pipe cleaning on topic: $1"$SAMELINE
279         while [ $pipeclean_retries -gt 0 ]; do
280                 echo "{\"pipeclean-$1\":$pipeclean_retries}" > ./tmp/pipeclean.json
281                 let pipeclean_retries=pipeclean_retries-1
282                 curlString="$MR_DMAAP_PATH$2 -X POST  -H Content-Type:application/json -d@./tmp/pipeclean.json"
283                 result=$(__do_curl "$curlString")
284                 if [ $? -ne 0 ]; then
285                         sleep 1
286                 else
287                         curlString="$MR_DMAAP_PATH$3"
288                         result=$(__do_curl "$curlString")
289                         if [ $? -eq 0 ]; then
290                                 if [ $result != "[]" ]; then
291                                         echo -e " Doing dmaap-mr pipe cleaning on topic: $1 $GREEN OK $EGREEN"
292                                         return 0
293
294                                 else
295                                         sleep 1
296                                 fi
297                         fi
298                 fi
299         done
300         echo -e "Doing dmaap-mr pipe cleaning on topic: $1 $RED Failed $ERED"
301         return 1
302 }
303
304 # Start the Message Router stub interface in the simulator group
305 # args: -
306 # (Function for test scripts)
307 start_mr() {
308
309         echo -e $BOLD"Starting $MR_DMAAP_DISPLAY_NAME and/or $MR_STUB_DISPLAY_NAME"$EBOLD
310
311         if [ $RUNMODE == "KUBE" ]; then
312
313         # Table of possible combinations of included mr and included/prestarted dmaap-mr
314                 # mr can never be prestarted
315                 # mr can be used stand alone
316                 # if dmaapmr is included/prestarted, then mr is needed as well as frontend
317
318         # Inverted logic - 0 mean true, 1 means false
319                 # mr prestarted      0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
320                 # mr included        0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
321                 # dmaap prestarted   0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
322                 # dmaap included     0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
323                 # ==================================================
324                 # OK                 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1
325
326                 __check_prestarted_image 'MR'
327                 retcode_prestarted_mr=$?
328                 __check_included_image 'MR'
329                 retcode_included_mr=$?
330
331                 __check_prestarted_image 'DMAAPMR'
332                 retcode_prestarted_dmaapmr=$?
333                 __check_included_image 'DMAAPMR'
334                 retcode_included_dmaapmr=$?
335
336                 paramerror=1
337
338                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
339                         if [ $retcode_prestarted_dmaapmr -ne 0 ] && [ $retcode_included_dmaapmr -eq 0 ]; then
340                                 paramerror=0
341                         fi
342                 fi
343
344                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
345                         if [ $retcode_prestarted_dmaapmr -eq 0 ] && [ $retcode_included_dmaapmr -ne 0 ]; then
346                                 paramerror=0
347                         fi
348                 fi
349
350                 if [ $retcode_prestarted_mr -ne 0 ] && [ $retcode_included_mr -eq 0 ]; then
351                         if [ $retcode_prestarted_dmaapmr -ne 0 ] && [ $retcode_included_dmaapmr -ne 0 ]; then
352                                 paramerror=0
353                         fi
354                 fi
355
356                 if [ $paramerror -ne 0 ]; then
357                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
358                                 echo -e $RED"The Message Router will not be started"$ERED
359                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included and/or prestarted"
360                                 exit
361                 fi
362
363                 if [ $retcode_prestarted_dmaapmr -eq 0 ]; then
364                         echo -e " Using existing $MR_DMAAP_APP_NAME deployment and service"
365                         __kube_scale deployment $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE 1
366                 fi
367
368                 if [ $retcode_included_dmaapmr -eq 0 ]; then
369                         #export MR_DMAAP_APP_NAME
370                         export MR_DMAAP_KUBE_APP_NAME=message-router
371                         MR_DMAAP_APP_NAME=$MR_DMAAP_KUBE_APP_NAME
372                         export KUBE_ONAP_NAMESPACE
373                         export MR_EXTERNAL_PORT
374                         export MR_INTERNAL_PORT
375                         export MR_EXTERNAL_SECURE_PORT
376                         export MR_INTERNAL_SECURE_PORT
377                         export ONAP_DMAAPMR_IMAGE
378
379                         export MR_KAFKA_BWDS_NAME=akfak-bwds
380                         export KUBE_ONAP_NAMESPACE
381
382                         export MR_ZOOKEEPER_APP_NAME
383                         export ONAP_ZOOKEEPER_IMAGE
384
385                         #Check if onap namespace exists, if not create it
386                         __kube_create_namespace $KUBE_ONAP_NAMESPACE
387
388                         # TODO - Fix domain name substitution in the prop file
389                         # Create config maps - dmaapmr app
390                         configfile=$PWD/tmp/MsgRtrApi.properties
391                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"mnt/mr/KUBE-MsgRtrApi.properties $configfile
392                         output_yaml=$PWD/tmp/dmaapmr_msgrtrapi_cfc.yaml
393                         __kube_create_configmap dmaapmr-msgrtrapi.properties $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
394
395                         configfile=$PWD/tmp/logback.xml
396                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"mnt/mr/logback.xml $configfile
397                         output_yaml=$PWD/tmp/dmaapmr_logback_cfc.yaml
398                         __kube_create_configmap dmaapmr-logback.xml $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
399
400                         configfile=$PWD/tmp/cadi.properties
401                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"mnt/mr/cadi.properties $configfile
402                         output_yaml=$PWD/tmp/dmaapmr_cadi_cfc.yaml
403                         __kube_create_configmap dmaapmr-cadi.properties $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
404
405                         # Create config maps - kafka app
406                         configfile=$PWD/tmp/zk_client_jaas.conf
407                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"mnt/kafka/zk_client_jaas.conf $configfile
408                         output_yaml=$PWD/tmp/dmaapmr_zk_client_cfc.yaml
409                         __kube_create_configmap dmaapmr-zk-client-jaas.conf $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
410
411                         # Create config maps - zookeeper app
412                         configfile=$PWD/tmp/zk_server_jaas.conf
413                         cp $SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"mnt/zk/zk_server_jaas.conf $configfile
414                         output_yaml=$PWD/tmp/dmaapmr_zk_server_cfc.yaml
415                         __kube_create_configmap dmaapmr-zk-server-jaas.conf $KUBE_ONAP_NAMESPACE autotest DMAAPMR $configfile $output_yaml
416
417                         # Create service
418                         input_yaml=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"svc.yaml
419                         output_yaml=$PWD/tmp/dmaapmr_svc.yaml
420                         __kube_create_instance service $MR_DMAAP_APP_NAME $input_yaml $output_yaml
421
422                         # Create app
423                         input_yaml=$SIM_GROUP"/"$MR_DMAAP_COMPOSE_DIR"/"app.yaml
424                         output_yaml=$PWD/tmp/dmaapmr_app.yaml
425                         __kube_create_instance app $MR_DMAAP_APP_NAME $input_yaml $output_yaml
426
427
428                         echo " Retrieving host and ports for service..."
429                         MR_DMAAP_HOST_NAME=$(__kube_get_service_host $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE)
430
431                         MR_EXT_PORT=$(__kube_get_service_port $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE "http")
432                         MR_EXT_SECURE_PORT=$(__kube_get_service_port $MR_DMAAP_APP_NAME $KUBE_ONAP_NAMESPACE "https")
433
434                         echo " Host IP, http port, https port: $MR_DMAAP_APP_NAME $MR_EXT_PORT $MR_EXT_SECURE_PORT"
435                         MR_SERVICE_PATH=""
436                         if [ $MR_HTTPX == "http" ]; then
437                                 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_EXT_PORT
438                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_PORT
439                         else
440                                 MR_DMAAP_PATH=$MR_HTTPX"://"$MR_DMAAP_HOST_NAME":"$MR_EXT_SECURE_PORT
441                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_DMAAP_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_SECURE_PORT
442                         fi
443
444                                 __check_service_start $MR_DMAAP_APP_NAME $MR_DMAAP_PATH$MR_DMAAP_ALIVE_URL
445
446                 fi
447
448                 if [ $retcode_included_mr -eq 0 ]; then
449                         #exporting needed var for deployment
450                         export MR_STUB_APP_NAME
451                         export KUBE_ONAP_NAMESPACE
452                         export MRSTUB_IMAGE
453                         export MR_INTERNAL_PORT
454                         export MR_INTERNAL_SECURE_PORT
455                         export MR_EXTERNAL_PORT
456                         export MR_EXTERNAL_SECURE_PORT
457
458                         if [ $retcode_prestarted_dmaapmr -eq 0 ] || [ $retcode_included_dmaapmr -eq 0 ]; then  # Set topics for dmaap
459                                 export TOPIC_READ="http://$MR_DMAAP_APP_NAME.$KUBE_ONAP_NAMESPACE:$MR_INTERNAL_PORT/events/$MR_READ_TOPIC"
460                                 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"
461                         else
462                                 export TOPIC_READ=""
463                                 export TOPIC_WRITE=""
464                         fi
465
466                         #Check if onap namespace exists, if not create it
467                         __kube_create_namespace $KUBE_ONAP_NAMESPACE
468
469                         # Create service
470                         input_yaml=$SIM_GROUP"/"$MR_STUB_COMPOSE_DIR"/"svc.yaml
471                         output_yaml=$PWD/tmp/mr_svc.yaml
472                         __kube_create_instance service $MR_STUB_APP_NAME $input_yaml $output_yaml
473
474                         # Create app
475                         input_yaml=$SIM_GROUP"/"$MR_STUB_COMPOSE_DIR"/"app.yaml
476                         output_yaml=$PWD/tmp/mr_app.yaml
477                         __kube_create_instance app $MR_STUB_APP_NAME $input_yaml $output_yaml
478
479
480                 fi
481
482
483                 echo " Retrieving host and ports for service..."
484                 MR_STUB_HOST_NAME=$(__kube_get_service_host $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE)
485
486                 MR_EXT_PORT=$(__kube_get_service_port $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE "http")
487                 MR_EXT_SECURE_PORT=$(__kube_get_service_port $MR_STUB_APP_NAME $KUBE_ONAP_NAMESPACE "https")
488
489                 echo " Host IP, http port, https port: $MR_STUB_APP_NAME $MR_EXT_PORT $MR_EXT_SECURE_PORT"
490                 if [ $MR_HTTPX == "http" ]; then
491                         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_EXT_PORT
492                         if [ -z "$MR_SERVICE_PATH" ]; then
493                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_PORT
494                         fi
495                 else
496                         MR_STUB_PATH=$MR_HTTPX"://"$MR_STUB_HOST_NAME":"$MR_EXT_SECURE_PORT
497                         if [ -z "$MR_SERVICE_PATH" ]; then
498                                 MR_SERVICE_PATH=$MR_HTTPX"://"$MR_STUB_APP_NAME"."$KUBE_ONAP_NAMESPACE":"$MR_EXT_SECURE_PORT
499                         fi
500                 fi
501                 MR_ADAPTER_HTTP="http://"$MR_STUB_HOST_NAME":"$MR_EXT_PORT
502                 MR_ADAPTER_HTTPS="https://"$MR_STUB_HOST_NAME":"$MR_EXT_SECURE_PORT
503
504                 __check_service_start $MR_STUB_APP_NAME $MR_STUB_PATH$MR_STUB_ALIVE_URL
505
506                 echo -ne " Service $MR_STUB_APP_NAME - reset  "$SAMELINE
507                 result=$(__do_curl $MR_STUB_APP_NAME $MR_STUB_PATH/reset)
508                 if [ $? -ne 0 ]; then
509                         echo -e " Service $MR_STUB_APP_NAME - reset  $RED Failed $ERED - will continue"
510                 else
511                         echo -e " Service $MR_STUB_APP_NAME - reset  $GREEN OK $EGREEN"
512                 fi
513
514
515         else
516
517                 __check_included_image 'DMAAPMR'
518                 retcode_dmaapmr=$?
519                 __check_included_image 'MR'
520                 retcode_mr=$?
521
522                 if [ $retcode_dmaapmr -ne 0 ] && [ $retcode_mr -ne 0 ]; then
523                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
524                                 echo -e $RED"The Message Router will not be started"$ERED
525                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included"
526                                 exit
527                 fi
528
529                 if [ $retcode_dmaapmr -eq 0 ] && [ $retcode_mr -ne 0 ]; then
530                                 echo -e $RED"The Message Router apps 'MR' and/or 'DMAAPMR' are not included in this test script"$ERED
531                                 echo -e $RED"The Message Router will not be started"$ERED
532                                 echo -e $RED"Both MR and DAAMPMR  - or - only MR - need to be included"
533                                 exit
534                 fi
535
536                 export TOPIC_READ=""
537         export TOPIC_WRITE=""
538                 if [ $retcode_dmaapmr -eq 0 ]; then  # Set topics for dmaap
539                         export TOPIC_READ="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT/events/$MR_READ_TOPIC"
540                         export TOPIC_WRITE="http://$MR_DMAAP_APP_NAME:$MR_INTERNAL_PORT/events/$MR_WRITE_TOPIC/users/mr-stub?timeout=15000&limit=100"
541                 fi
542
543                 export DOCKER_SIM_NWNAME
544                 export ONAP_ZOOKEEPER_IMAGE
545                 export MR_ZOOKEEPER_APP_NAME
546                 export ONAP_KAFKA_IMAGE
547                 export MR_KAFKA_APP_NAME
548                 export ONAP_DMAAPMR_IMAGE
549                 export MR_DMAAP_APP_NAME
550                 export MR_DMAAP_LOCALHOST_PORT
551                 export MR_INTERNAL_PORT
552                 export MR_DMAAP_LOCALHOST_SECURE_PORT
553                 export MR_INTERNAL_SECURE_PORT
554
555                 if [ $retcode_dmaapmr -eq 0 ]; then
556                         __start_container $MR_DMAAP_COMPOSE_DIR "" NODOCKERARGS 1 $MR_DMAAP_APP_NAME
557
558                         __check_service_start $MR_DMAAP_APP_NAME $MR_DMAAP_PATH$MR_DMAAP_ALIVE_URL
559
560
561                         __create_topic $MR_READ_TOPIC "Topic for reading policy messages"
562
563                         __create_topic $MR_WRITE_TOPIC "Topic for writing policy messages"
564
565                         __dmaap_pipeclean $MR_READ_TOPIC "/events/A1-POLICY-AGENT-READ" "/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=1000&limit=100"
566
567                         __dmaap_pipeclean $MR_WRITE_TOPIC "/events/A1-POLICY-AGENT-WRITE" "/events/A1-POLICY-AGENT-WRITE/users/mr-stub?timeout=1000&limit=100"
568
569                         echo " Current topics:"
570                         curlString="$MR_DMAAP_PATH/topics"
571                         result=$(__do_curl "$curlString")
572                         echo $result | indent2
573                 fi
574
575                 export DOCKER_SIM_NWNAME
576                 export MR_STUB_APP_NAME
577                 export MRSTUB_IMAGE
578                 export MR_INTERNAL_PORT
579                 export MR_INTERNAL_SECURE_PORT
580                 export MR_STUB_LOCALHOST_PORT
581                 export MR_STUB_LOCALHOST_SECURE_PORT
582                 export MR_STUB_CERT_MOUNT_DIR
583                 export MR_STUB_DISPLAY_NAME
584
585                 if [ $retcode_mr -eq 0 ]; then
586                         __start_container $MR_STUB_COMPOSE_DIR "" NODOCKERARGS 1 $MR_STUB_APP_NAME
587
588                         __check_service_start $MR_STUB_APP_NAME $MR_STUB_PATH$MR_STUB_ALIVE_URL
589                 fi
590
591         fi
592         echo ""
593         return 0
594 }
595
596 ### Generic test cases for varaible checking
597
598 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
599 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
600 # equal to the target or not.
601 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
602 # before setting pass or fail depending on if the variable value becomes equal to the target
603 # value or not.
604 # (Function for test scripts)
605 mr_equal() {
606         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
607                 __var_test "MR" $MR_STUB_PATH/counter/ $1 "=" $2 $3
608         else
609                 ((RES_CONF_FAIL++))
610                 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
611         fi
612 }
613
614 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
615 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
616 # greater than the target or not.
617 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
618 # before setting pass or fail depending on if the variable value becomes greater than the target
619 # value or not.
620 # (Function for test scripts)
621 mr_greater() {
622         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
623                 __var_test "MR" $MR_STUB_PATH/counter/ $1 ">" $2 $3
624         else
625                 ((RES_CONF_FAIL++))
626                 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
627         fi
628 }
629
630 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
631 mr_read() {
632         echo "$(__do_curl $MR_STUB_PATH/counter/$1)"
633 }
634
635 # Print a variable value from the MR stub.
636 # arg: <variable-name>
637 # (Function for test scripts)
638 mr_print() {
639         if [ $# != 1 ]; then
640                 ((RES_CONF_FAIL++))
641         __print_err "need one arg, <mr-param>" $@
642                 exit 1
643         fi
644         echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $MR_STUB_PATH/counter/$1)"$EBOLD
645 }