7247e01fe0f7560875e7f2edd93cec6a39c93f77
[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 # Copyright (C) 2021 Nordix Foundation.
8 # ========================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #        http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END=================================================
21 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22
23 umask 0022
24 set -uex -o pipefail
25 export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
26
27 # RESP_CACHE is (/opt/app/config/cache) empty-dir volume mount for K8s env
28 RESP_CACHE=${RESP_CACHE:-''}
29 RESP=${RESP:-'/dev/null'}
30 APP_ROOT=${APP_ROOT:-/opt/app/dbc-client}
31 CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config}
32
33 PORT=${PORT:-8443}
34 DBC=${DBC:-dmaap-bc}
35 PROTO=${PROTO:-https}
36 PARAM=${PARAM:-'useExisting=true'}
37 REQUESTID=${REQUESTID:-dbc-client}
38 URL=${URL:-"${PROTO}"://"${DBC}":"${PORT}"/webapi/}
39
40 CA_PEM=${CA_PEM:-ca.pem}
41 KEY_PEM=${KEY_PEM:-key.pem}
42 CLIENT_PEM=${CLIENT_PEM:-client.pem}
43 PEM_DIR=${PEM_DIR:-/opt/app/osaaf/local}
44 CERT_PWD=${CERT_PWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'}
45
46 BA_PWD=${BA_PWD:-'demo123456!'}
47 AUTH_METHOD=${AUTH_METHOD:-basicAuth}
48 BA_IDENTITY=${BA_IDENTITY:-dmaap-bc@dmaap-bc.onap.org}
49
50 function xcurl() {
51   curl -X POST \
52     -s "$CURL_CRED" \
53     -w "%{http_code}" \
54     -H "X-ECOMP-RequestID: $REQUESTID" \
55     -H "Content-Type: application/json" "$@"
56 }
57
58 function init_config() {
59   if [ ! -d "$APP_ROOT" -a ! -d "$CONFIGMAP_ROOT" ]; then
60     echo "Expected either App root directory $APP_ROOT Or ConfigMap directory $CONFIGMAP_ROOT does not exist."
61     exit 1
62   fi
63   cd "$PEM_DIR"
64   if [ "$AUTH_METHOD" = "basicAuth" ]; then
65     echo "-u ${BA_IDENTITY}:${BA_PWD}" >"$PEM_DIR"/curl.cred
66     CURL_CRED="-K $PEM_DIR/curl.cred"
67   elif [ -f "$CA_PEM" -a -f "$CLIENT_PEM" -a -f "$KEY_PEM" ]; then
68     printf "key \"$PEM_DIR/$KEY_PEM\"\n cacert \"$PEM_DIR/$CA_PEM\"\n cert \"$PEM_DIR/${CLIENT_PEM}:${CERT_PWD}\"" >$PEM_DIR/curl.cred
69     CURL_CRED="-K $PEM_DIR/curl.cred"
70   else
71     echo "PEM files for authorization not found..!"
72   fi
73 }
74
75 function init_dbc_provisioning() {
76   cd "$CONFIGMAP_ROOT"
77   for dir in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs; do
78     if [ -d ${dir} ]; then
79       for file in $(ls ${dir}/*.json); do
80         do_http_post "$file" "$dir"
81       done
82     fi
83   done
84 }
85
86 function do_http_post() {
87   RETRY_TIME=60
88   if [ -n "$RESP_CACHE" ]; then
89     RESP="$RESP_CACHE"/"$(echo "${1##*/}" | cut -d "." -f1)"-resp.json
90   fi
91   while true; do
92     if [ "$2" != "feeds" -a "$2" != "topics" ]; then
93       req_body=$(cat "${1}" | envsubst)
94       rc=$(xcurl -o "$RESP" -d "$req_body" "${URL}${2}")
95       if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then
96         echo "Http Post request is successful with response code=$rc"
97         break
98       fi
99     else
100       rc=$(xcurl -o "$RESP" -d @"${1}" "${URL}${2}"/?"${PARAM}")
101       if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then
102         echo "Http Post request for feed creation is successful with response code=$rc"
103         break
104       fi
105     fi
106     echo "$(date): Http Response code=$rc.  Will retry after $RETRY_TIME seconds.."
107     sleep "$RETRY_TIME"
108   done
109 }
110
111 init_config
112 init_dbc_provisioning