aboutsummaryrefslogtreecommitdiffstats
path: root/dbc-client/misc/dbc-client
blob: 7247e01fe0f7560875e7f2edd93cec6a39c93f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/sh
#
# ============LICENSE_START===============================================
# org.onap.dmaap
# ========================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# Copyright (C) 2021 Nordix Foundation.
# ========================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=================================================
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

umask 0022
set -uex -o pipefail
export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin

# RESP_CACHE is (/opt/app/config/cache) empty-dir volume mount for K8s env
RESP_CACHE=${RESP_CACHE:-''}
RESP=${RESP:-'/dev/null'}
APP_ROOT=${APP_ROOT:-/opt/app/dbc-client}
CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config}

PORT=${PORT:-8443}
DBC=${DBC:-dmaap-bc}
PROTO=${PROTO:-https}
PARAM=${PARAM:-'useExisting=true'}
REQUESTID=${REQUESTID:-dbc-client}
URL=${URL:-"${PROTO}"://"${DBC}":"${PORT}"/webapi/}

CA_PEM=${CA_PEM:-ca.pem}
KEY_PEM=${KEY_PEM:-key.pem}
CLIENT_PEM=${CLIENT_PEM:-client.pem}
PEM_DIR=${PEM_DIR:-/opt/app/osaaf/local}
CERT_PWD=${CERT_PWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'}

BA_PWD=${BA_PWD:-'demo123456!'}
AUTH_METHOD=${AUTH_METHOD:-basicAuth}
BA_IDENTITY=${BA_IDENTITY:-dmaap-bc@dmaap-bc.onap.org}

function xcurl() {
  curl -X POST \
    -s "$CURL_CRED" \
    -w "%{http_code}" \
    -H "X-ECOMP-RequestID: $REQUESTID" \
    -H "Content-Type: application/json" "$@"
}

function init_config() {
  if [ ! -d "$APP_ROOT" -a ! -d "$CONFIGMAP_ROOT" ]; then
    echo "Expected either App root directory $APP_ROOT Or ConfigMap directory $CONFIGMAP_ROOT does not exist."
    exit 1
  fi
  cd "$PEM_DIR"
  if [ "$AUTH_METHOD" = "basicAuth" ]; then
    echo "-u ${BA_IDENTITY}:${BA_PWD}" >"$PEM_DIR"/curl.cred
    CURL_CRED="-K $PEM_DIR/curl.cred"
  elif [ -f "$CA_PEM" -a -f "$CLIENT_PEM" -a -f "$KEY_PEM" ]; then
    printf "key \"$PEM_DIR/$KEY_PEM\"\n cacert \"$PEM_DIR/$CA_PEM\"\n cert \"$PEM_DIR/${CLIENT_PEM}:${CERT_PWD}\"" >$PEM_DIR/curl.cred
    CURL_CRED="-K $PEM_DIR/curl.cred"
  else
    echo "PEM files for authorization not found..!"
  fi
}

function init_dbc_provisioning() {
  cd "$CONFIGMAP_ROOT"
  for dir in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs; do
    if [ -d ${dir} ]; then
      for file in $(ls ${dir}/*.json); do
        do_http_post "$file" "$dir"
      done
    fi
  done
}

function do_http_post() {
  RETRY_TIME=60
  if [ -n "$RESP_CACHE" ]; then
    RESP="$RESP_CACHE"/"$(echo "${1##*/}" | cut -d "." -f1)"-resp.json
  fi
  while true; do
    if [ "$2" != "feeds" -a "$2" != "topics" ]; then
      req_body=$(cat "${1}" | envsubst)
      rc=$(xcurl -o "$RESP" -d "$req_body" "${URL}${2}")
      if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then
        echo "Http Post request is successful with response code=$rc"
        break
      fi
    else
      rc=$(xcurl -o "$RESP" -d @"${1}" "${URL}${2}"/?"${PARAM}")
      if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then
        echo "Http Post request for feed creation is successful with response code=$rc"
        break
      fi
    fi
    echo "$(date): Http Response code=$rc.  Will retry after $RETRY_TIME seconds.."
    sleep "$RETRY_TIME"
  done
}

init_config
init_dbc_provisioning