[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dbc-client / misc / dbc-client
1 #!/bin/sh
2 #
3 # ============LICENSE_START==========================================
4 # org.onap.dmaap
5 # ===================================================================
6 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
7 # ===================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #        http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END============================================
20 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 #
22 #
23
24 umask 0022
25 TZ=GMT0
26 COMPONENT=dbc-client
27 APP_ROOT=${APP_ROOT:-/opt/app/$COMPONENT}
28 export TZ
29 PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
30 export PATH
31 CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config}
32 CONTAINER_CONFIG=$CONFIGMAP_ROOT/conf/dbc-client.env
33 REQUESTID=${REQUESTID:-dbc-client}
34 DBC=${DBC:-dmaap-bc}
35 PROTO=${PROTO:-https}
36 PORT=${PORT:-8443}
37 WAIT_TO_EXIT=${WAIT_TO_EXIT:-N}
38 PEMDIR=${PEMDIR:-/opt/app/osaaf/local}
39 CAPEM=${CAPEM:-ca.pem}
40 CLIENTPEM=${CLIENTPEM:-client.pem}
41 KEYPEM=${KEYPEM:-key.pem}
42 CERTPWD=${CERTPWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'}
43 AUTH_METHOD=${AUTH_METHOD:-basicAuth}
44 BA_IDENTITY=${BA_IDENTITY:-dmaap-bc@dmaap-bc.onap.org}
45 BA_PWD=${BA_PWD:-'demo123456!'}
46
47 config() {
48         echo "ENTER config"
49         set -x
50         if [ ! -d $APP_ROOT ]
51         then
52                 echo "Expected app root directory $APP_ROOT does not exist"
53                 exit 1
54         fi
55         cd $PEMDIR
56         pwd
57         ls -l
58         echo "AUTH_METHOD=$AUTH_METHOD"
59         if [ "$AUTH_METHOD"  =  "basicAuth" ]
60         then
61                 echo "-u ${BA_IDENTITY}:${BA_PWD}" > $PEMDIR/curl.cred
62                 CURLCRED="-K $PEMDIR/curl.cred"
63         elif [ -f $CAPEM  -a -f $CLIENTPEM -a -f $KEYPEM ]
64         then
65                 printf "key \"$PEMDIR/$KEYPEM\"\ncacert \"$PEMDIR/$CAPEM\"\ncert \"$PEMDIR/${CLIENTPEM}:${CERTPWD}\"" > $PEMDIR/curl.cred
66                 CURLCRED="-K $PEMDIR/curl.cred"
67         else
68                 echo "Warning: PEM files for authorization not found!"
69                 CURLCRED=""
70         fi
71         echo "CURLCRED=$CURLCRED"
72         cd $APP_ROOT
73         find . -type f -exec ls -l {} \;
74         find $CONFIGMAP_ROOT -type f -exec ls -l {} \;
75         if [ !  -f $CONTAINER_CONFIG ]
76         then
77                 echo "WARNING: Expected env file $CONTAINER_CONFIG not found. Default behaviors in effect"
78         else
79                 source $CONTAINER_CONFIG
80         fi
81
82         if [ "$WAIT_TO_EXIT" != "Y" ]
83         then
84                 echo "Creating $APP_ROOT/ok_to_exit so no waiting..."
85                 > $APP_ROOT/ok_to_exit
86         else
87                 echo "Not creating $APP_ROOT/ok_to_exit"
88         fi      
89         
90         set +x
91 }
92
93
94 init() {
95         set -x
96         echo "ENTER init"
97         if [ ! -d $CONFIGMAP_ROOT ]
98         then
99                 echo $CONFIGMAP_ROOT does not exist
100                 return
101         fi
102
103         #loop on get /dmaap until we get a good response to indicate other provisioning can continue
104         rc=${RESP:-999}
105         while [ $rc != "200" ]
106         do
107                 sleep 10
108                 rc=`curl $CURLCRED -s -o /dev/null -I -w "%{http_code}" -X GET -H "X-ECOMP-RequestID: $REQUESTID" -H "Content-Type: application/json" ${PROTO}://${DBC}:${PORT}/webapi/dmaap`
109                 echo "get dmaap response=${rc}"
110         done
111         set +x
112 }
113
114 dopost() {
115         set -x
116         RETRY_TIME=60
117
118         loop=true
119
120         while [ $loop = true ]
121         do
122                 rc=`curl $CURLCRED -s -X POST -w "%{http_code}" -H "X-ECOMP-RequestID: $REQUESTID" -H "Content-Type: application/json" -d @${1} ${PROTO}://${DBC}:${PORT}/webapi/${2} -o /dev/null`
123                 case $rc in
124                 200 | 201 | 409 )
125                         echo "response=$rc"
126                         loop=false
127                         ;;
128                 * )
129                         echo "`date`: http response=$rc.  Will retry after $RETRY_TIME seconds"
130                         sleep $RETRY_TIME
131                         ;;
132                 esac
133         done
134         set +x
135
136 }
137
138 doprov() {
139
140         set -x
141         cd $CONFIGMAP_ROOT
142         pwd
143         # order is important in this next list
144         for uri in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs
145         do
146                 if [ -d ${uri} ]
147                 then
148                         for j in `ls ${uri}/*.json`
149                         do
150                                 echo "POST $j to $uri"
151                                 dopost $j $uri
152                         done
153                 fi
154         done
155         set +x
156 }
157
158 delay() {
159         echo "DELAY=$DELAY"
160         if [ ! -z "$DELAY" ]
161         then
162                 sleep $DELAY
163         fi
164 }
165
166 delay
167 config
168 init
169 doprov
170
171 echo "Check $APP_ROOT/ok_to_exit"
172 while [ ! -f $APP_ROOT/ok_to_exit ]
173 do
174         echo "$APP_ROOT/ok_to_exit does not exist.  Sticking around for debugging..."
175         sleep 10
176 done
177 exit 0