Stop SDNC on problem in certificate provisioning
[sdnc/oam.git] / installation / sdnc / src / main / scripts / startODL.sh
1 #!/bin/sh
2 ###
3 # ============LICENSE_START=======================================================
4 # SDN-C
5 # ================================================================================
6 # Copyright (C) 2020 Samsung Electronics
7 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
8 # Copyright (C) 2020 Highstreet Technologies
9 # ================================================================================
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at
13 #
14 #      http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21 # ============LICENSE_END=========================================================
22 ###
23 # A single entry point script that can be used in Kubernetes based deployments (via OOM) and standalone docker deployments.
24 # Please see https://wiki.onap.org/display/DW/startODL.sh+-+Important+Environment+variables+and+their+description for more details
25
26 # Functions
27
28 # Test if repository exists, like this mvn:org.onap.ccsdk.features.sdnr.wt/sdnr-wt-devicemanager-oran-feature/0.7.2/xml/features
29 # $1 repository
30 isRepoExisting() {
31   REPO=$(echo "$1" | sed -E "s#mvn:(.*)/xml/features\$#\1#")
32   OIFS="$IFS"
33   IFS='/' 
34   set parts $REPO
35   IFS="$OIFS"
36   path="$ODL_HOME/system/$(echo "$2" | tr '.' '/')/$3/$4"
37   [ -d "$path" ]
38 }
39
40 # Add features repository to karaf featuresRepositories configuration
41 # $1 repositories to be added
42 addRepository() {
43   CFG=$ODL_FEATURES_BOOT_FILE
44   ORIG=$CFG.orig
45   if isRepoExisting "$1" ; then
46     printf "%s\n" "Add repository: $1"
47     sed -i "\|featuresRepositories|s|$|, $1|" "$CFG"
48   else
49     printf "%s\n" "Repo does not exist: $1"
50   fi
51 }
52
53 # Append features to karaf boot feature configuration
54 # $1 additional feature to be added
55 # $2 repositories to be added (optional)
56 addToFeatureBoot() {
57   CFG=$ODL_FEATURES_BOOT_FILE
58   ORIG=$CFG.orig
59   if [ -n "$2" ] ; then
60     printf "%s\n" "Add repository: $2"
61     mv "$CFG" "$ORIG"
62     sed -e "\|featuresRepositories|s|$|,$2|" "$ORIG" > "$CFG"
63   fi
64   printf "%s\n" "Add boot feature: $1"
65   mv "$CFG" "$ORIG"
66   sed -e "\|featuresBoot *=|s|$|,$1|" "$ORIG" > "$CFG"
67 }
68
69 # Append features to karaf boot feature configuration
70 # $1 search pattern
71 # $2 replacement
72 replaceFeatureBoot() {
73   CFG="$ODL_HOME"/etc/org.apache.karaf.features.cfg
74   ORIG=$CFG.orig
75   printf "%s %s\n" "Replace boot feature $1 with: $2"
76   sed -i "/featuresBoot/ s/$1/$2/g" "$CFG"
77 }
78
79 # Remove all sdnc specific features
80 cleanupFeatureBoot() {
81   printf "Remove northbound bootfeatures \n"
82   sed -i "/featuresBoot/ s/,ccsdk-sli-core-all.*$//g" "$ODL_FEATURES_BOOT_FILE"
83 }
84
85 initialize_sdnr() {
86   printf "SDN-R Database Initialization"
87   INITCMD="$JAVA_HOME/bin/java -jar "
88   INITCMD="${INITCMD} $ODL_HOME/system/org/onap/ccsdk/features/sdnr/wt/sdnr-wt-data-provider-setup/$CCSDKFEATUREVERSION/sdnr-dmt.jar "
89   INITCMD="${INITCMD} $SDNRDBCOMMAND"
90   printf "%s\n" "Execute: $INITCMD"
91   n=0
92   until [ $n -ge 5 ] ; do
93     $INITCMD && break
94     n=$((n+1))
95     sleep 15
96   done
97   return $?
98 }
99
100 install_sdnrwt_features() {
101   # Repository setup provided via sdnc dockerfile
102   if $SDNRWT; then
103     addRepository "$SDNRDM_BASE_REPO"
104
105     if $SDNRONLY; then
106       cleanupFeatureBoot
107     fi
108     if $SDNRDM; then
109       addToFeatureBoot "$SDNRDM_BOOTFEATURES"
110     else
111       addToFeatureBoot "$SDNRWT_BOOTFEATURES"
112     fi
113   fi
114 }
115
116 install_sdnr_northbound_features() {
117   addToFeatureBoot "$SDNR_NORTHBOUND_BOOTFEATURES" 
118 }
119
120 # Reconfigure ODL from default single node configuration to cluster
121
122 enable_odl_cluster() {
123   if [ -z "$SDNC_REPLICAS" ]; then
124      printf "SDNC_REPLICAS is not configured in Env field"
125      exit
126   fi
127
128   # ODL NETCONF setup
129   printf "Installing Opendaylight cluster features for mdsal and netconf\n"
130   
131   #Be sure to remove feature odl-netconf-connector-all from list
132   replaceFeatureBoot "odl-netconf-connector-all,"
133
134   printf "Installing Opendaylight cluster features\n"
135   replaceFeatureBoot odl-netconf-topology odl-netconf-clustered-topology
136   replaceFeatureBoot odl-mdsal-all odl-mdsal-all,odl-mdsal-clustering
137   addToFeatureBoot odl-jolokia
138   #${ODL_HOME}/bin/client feature:install odl-mdsal-clustering
139   #${ODL_HOME}/bin/client feature:install odl-jolokia
140
141   # ODL Cluster or Geo cluster configuration
142   
143   printf "Update cluster information statically\n"
144   fqdn=$(hostname -f)
145   printf "%s\n" "Get current fqdn ${fqdn}"
146
147   # Extract node index using first digit after "-"
148   # Example 2 from "sdnr-2.logo.ost.das.r32.com"
149   node_index=$(echo "${fqdn}" | sed -r 's/.*-([0-9]).*/\1/g')
150   member_offset=1
151
152   if $GEO_ENABLED; then
153     printf "This is a Geo cluster\n"
154
155     if [ -z "$IS_PRIMARY_CLUSTER" ] || [ -z "$MY_ODL_CLUSTER" ] || [ -z "$PEER_ODL_CLUSTER" ]; then
156      printf "IS_PRIMARY_CLUSTER, MY_ODL_CLUSTER and PEER_ODL_CLUSTER must all be configured in Env field\n"
157      return
158     fi
159
160     if $IS_PRIMARY_CLUSTER; then
161        PRIMARY_NODE=${MY_ODL_CLUSTER}
162        SECONDARY_NODE=${PEER_ODL_CLUSTER}
163     else
164        PRIMARY_NODE=${PEER_ODL_CLUSTER}
165        SECONDARY_NODE=${MY_ODL_CLUSTER}
166        member_offset=4
167     fi
168
169     node_list="${PRIMARY_NODE} ${SECONDARY_NODE}"
170
171     "${SDNC_BIN}"/configure_geo_cluster.sh $((node_index+member_offset)) "${node_list}"
172   else
173     printf "This is a local cluster\n"
174     i=0
175     node_list=""
176     # SERVICE_NAME and NAMESPACE are used to create cluster node names and are provided via Helm charts in OOM environment
177     if [ ! -z "$SERVICE_NAME" ] && [ ! -z "$NAMESPACE" ]; then
178        # Extract node name minus the index
179        # Example sdnr from "sdnr-2.logo.ost.das.r32.com"
180        node_name=$(echo "${fqdn}" | sed 's/-[0-9].*$//g')
181        while [ $i -lt "$SDNC_REPLICAS" ]; do
182          node_list="${node_list} ${node_name}-$i.${SERVICE_NAME}-cluster.${NAMESPACE}"
183          i=$(($i + 1))
184        done
185        "${ODL_HOME}"/bin/configure_cluster.sh $((node_index+1)) "${node_list}"
186     elif [ -z "$SERVICE_NAME" ] && [ -z "$NAMESPACE" ]; then
187       # Hostname is used in Standalone environment to create cluster node names
188        while [ $i -lt "$SDNC_REPLICAS" ]; do
189          #assemble node list by replacing node-index in hostname with "i"
190          node_name=$(echo "${fqdn}" | sed -r "s/-[0-9]/-$i/g")
191          node_list="${node_list} ${node_name}"
192          i=$(($i + 1))
193        done
194        "${ODL_HOME}"/bin/configure_cluster.sh $((node_index+1)) "${node_list}"
195     else
196        printf "Unhandled cluster scenario. Terminating the container\n" 
197        printf "Any one of the below 2 conditions should be satisfied for successfully enabling cluster mode : \n"
198        printf "1. OOM Environment - Both SERVICE_NAME and NAMESPACE environment variables have to be set.\n"
199        printf "2. Docker (standalone) Environment - Neither of SERVICE_NAME and NAMESPACE have to be set.\n"
200        printf "Current configuration - SERVICE_NAME = $SERVICE_NAME  NAMESPACE = $NAMESPACE\n"
201        exit $NOTOK
202     fi
203   fi
204 }
205
206
207 # Install SDN-C platform components if not already installed and start container
208
209 # -----------------------
210 # Main script starts here
211 printf "Installing SDNC/R from startODL.sh script\n"
212 ODL_HOME=${ODL_HOME:-/opt/opendaylight/current}
213 ODL_FEATURES_BOOT_FILE=$ODL_HOME/etc/org.apache.karaf.features.cfg
214 #
215 ODL_REMOVEIDMDB=${ODL_REMOVEIDMDB:-false}
216
217 ODL_ADMIN_USERNAME=${ODL_ADMIN_USERNAME:-admin}
218 if $ODL_REMOVEIDMDB ; then
219    printf "Remove odl idmdb"
220    rm "$ODL_HOME"/data/idmlight.db.mv.db
221    ODL_ADMIN_PASSWORD=${ODL_ADMIN_PASSWORD:-admin}
222 else
223    ODL_ADMIN_PASSWORD=${ODL_ADMIN_PASSWORD:-Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U}
224 fi
225 ODL_ADMIN_PASSWORD=${ODL_ADMIN_PASSWORD:-Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U}
226 SDNC_HOME=${SDNC_HOME:-/opt/onap/sdnc}
227 SDNC_BIN=${SDNC_BIN:-/opt/onap/sdnc/bin}
228 # Whether to intialize MYSql DB or not. Default is to initialize
229 SDNC_DB_INIT=${SDNC_DB_INIT:-false}
230 CCSDK_HOME=${CCSDK_HOME:-/opt/onap/ccsdk}
231 JDEBUG=${JDEBUG:-false}
232 MYSQL_PASSWD=${MYSQL_PASSWD:-openECOMP1.0}
233 ENABLE_ODL_CLUSTER=${ENABLE_ODL_CLUSTER:-false}
234 GEO_ENABLED=${GEO_ENABLED:-false}
235 SDNC_AAF_ENABLED=${SDNC_AAF_ENABLED:-false}
236 IS_PRIMARY_CLUSTER=${IS_PRIMARY_CLUSTER:-false}
237 MY_ODL_CLUSTER=${MY_ODL_CLUSTER:-127.0.0.1}
238 INSTALLED_DIR=${INSTALLED_FILE:-/opt/opendaylight/current/daexim}
239 SDNRWT=${SDNRWT:-false}
240 SDNRWT_BOOTFEATURES=${SDNRWT_BOOTFEATURES:-sdnr-wt-feature-aggregator}
241 SDNRDM=${SDNRDM:-false}
242 # Add devicemanager base and specific repositories
243 SDNRDM_BASE_REPO=${SDNRDM_BASE_REPO:-mvn:org.onap.ccsdk.features.sdnr.wt/sdnr-wt-feature-aggregator-devicemanager-base/$CCSDKFEATUREVERSION/xml/features}
244 # Add devicemanager features
245 SDNRDM_SDM_LIST=${SDNRDM_SDM_LIST:-sdnr-wt-feature-aggregator-devicemanager}
246 SDNRDM_BOOTFEATURES=${SDNRDM_BOOTFEATURES:-sdnr-wt-feature-aggregator-devicemanager-base, ${SDNRDM_SDM_LIST}}
247 # Whether to Initialize the ElasticSearch DB.
248 SDNRINIT=${SDNRINIT:-false}
249 SDNRONLY=${SDNRONLY:-false}
250 SDNRDBURL=${SDNRDBURL:-http://sdnrdb:9200}
251 SDNRDBCOMMAND=${SDNRDBCOMMAND:--c init -db $SDNRDBURL -dbu $SDNRDBUSERNAME -dbp $SDNRDBPASSWORD $SDNRDBPARAMETER}
252
253 SDNR_NORTHBOUND=${SDNR_NORTHBOUND:-false}
254 SDNR_NORTHBOUND_BOOTFEATURES=${SDNR_NORTHBOUND_BOOTFEATURES:-sdnr-northbound-all}
255 NOTOK=1
256 export ODL_ADMIN_PASSWORD ODL_ADMIN_USERNAME
257
258 if $JDEBUG ; then
259     printf "Activate remote debugging\n"
260     #JSTADTPOLICYFILE="$ODL_HOME/etc/tools.policy"
261     #echo -e "grant codebase \"file:${JAVA_HOME}/lib/tools.jar\" {\n  permission java.security.AllPermission;\n };" > $JSTADTPOLICYFILE
262     #sleep 1
263     #$JAVA_HOME/bin/jstatd -p 1089 -J-Djava.security.policy=$JSTADTPOLICYFILE &
264     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.port=1090"
265     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.rmi.port=1090"
266     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Djava.rmi.server.hostname=$(hostname)  "
267     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.local.only=false"
268     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
269     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"
270     export EXTRA_JAVA_OPTS
271 fi
272
273
274 printf "Settings:\n"
275 printf "%s\n" "  SDNC_BIN=$SDNC_BIN"
276 printf "%s\n" "  SDNC_HOME=$SDNC_HOME"
277 printf "%s\n" "  SDNC_DB_INIT=$SDNC_DB_INIT"
278 printf "%s\n" "  ODL_CERT_DIR=$ODL_CERT_DIR"
279 printf "%s\n" "  CCSDKFEATUREVERSION=$CCSDKFEATUREVERSION"
280 printf "%s\n" "  ENABLE_ODL_CLUSTER=$ENABLE_ODL_CLUSTER"
281 printf "%s\n" "  ODL_REMOVEIDMDB=$ODL_REMOVEIDMDB"
282 printf "%s\n" "  SDNC_REPLICAS=$SDNC_REPLICAS"
283 printf "%s\n" "  SDNRWT=$SDNRWT"
284 printf "%s\n" "  SDNRDM=$SDNRDM"
285 printf "%s\n" "  SDNRONLY=$SDNRONLY"
286 printf "%s\n" "  SDNRINIT=$SDNRINIT"
287 printf "%s\n" "  SDNRDBURL=$SDNRDBURL"
288 printf "%s\n" "  SDNRDBUSERNAME=$SDNRDBUSERNAME"
289 printf "%s\n" "  GEO_ENABLED=$GEO_ENABLED"
290 printf "%s\n" "  IS_PRIMARY_CLUSTER=$IS_PRIMARY_CLUSTER"
291 printf "%s\n" "  MY_ODL_CLUSTER=$MY_ODL_CLUSTER"
292 printf "%s\n" "  PEER_ODL_CLUSTER=$PEER_ODL_CLUSTER"
293 printf "%s\n" "  SDNR_NORTHBOUND=$SDNR_NORTHBOUND"
294 printf "%s\n" "  AAF_ENABLED=$SDNC_AAF_ENABLED"
295 printf "%s\n" "  SERVICE_NAME=$SERVICE_NAME"
296 printf "%s\n" "  NAMESPACE=$NAMESPACE"
297
298 if "$SDNC_AAF_ENABLED"; then
299         export SDNC_AAF_STORE_DIR=/opt/app/osaaf/local
300         export SDNC_AAF_CONFIG_DIR=/opt/app/osaaf/local
301         export SDNC_KEYPASS=$(cat /opt/app/osaaf/local/.pass)
302         export SDNC_KEYSTORE=org.onap.sdnc.p12
303         sed -i '/cadi_prop_files/d' "$ODL_HOME"/etc/system.properties
304         echo "cadi_prop_files=$SDNC_AAF_CONFIG_DIR/org.onap.sdnc.props" >> "$ODL_HOME"/etc/system.properties
305
306         sed -i '/org.ops4j.pax.web.ssl.keystore/d' "$ODL_HOME"/etc/custom.properties
307         sed -i '/org.ops4j.pax.web.ssl.password/d' "$ODL_HOME"/etc/custom.properties
308         sed -i '/org.ops4j.pax.web.ssl.keypassword/d' "$ODL_HOME"/etc/custom.properties
309         echo "org.ops4j.pax.web.ssl.keystore=$SDNC_AAF_STORE_DIR/$SDNC_KEYSTORE" >> "$ODL_HOME"/etc/custom.properties
310         echo "org.ops4j.pax.web.ssl.password=$SDNC_KEYPASS" >> "$ODL_HOME"/etc/custom.properties
311         echo "org.ops4j.pax.web.ssl.keypassword=$SDNC_KEYPASS" >> "$ODL_HOME"/etc/custom.properties
312 fi
313
314 if $SDNRINIT ; then
315   #One time intialization action
316   initialize_sdnr
317   init_result=$?
318   printf "%s\n" "Result of init script: $init_result"
319   if $SDNRWT ; then
320     printf "Proceed to initialize sdnr\n"
321   else
322     exit $init_result
323   fi
324 fi
325
326 # Check for MySQL DB connectivity only if SDNC_DB_INIT is set to "true" 
327 if $SDNC_DB_INIT; then
328 #
329 # Wait for database
330 #
331   printf "Waiting for mysql"
332   until mysql -h dbhost -u root -p"${MYSQL_PASSWD}" mysql > /dev/null 2>&1 
333   do
334     printf "."
335     sleep 1
336   done
337   printf "\nmysql ready"
338 fi
339
340 if [ ! -d "${INSTALLED_DIR}" ]
341 then
342     mkdir -p "${INSTALLED_DIR}"
343 fi
344
345 if [ ! -f "${SDNC_HOME}"/.installed ]
346 then
347     # for integration testing. In OOM, a separate job takes care of installing it.
348     if $SDNC_DB_INIT; then
349       printf "Installing SDN-C database\n"
350       "${SDNC_HOME}"/bin/installSdncDb.sh
351     fi
352     printf "Installing SDN-C keyStore\n"
353     "${SDNC_HOME}"/bin/addSdncKeyStore.sh
354     printf "Installing A1-adapter trustStore\n"
355     "${SDNC_HOME}"/bin/addA1TrustStore.sh
356
357     if [ -x "${SDNC_HOME}"/svclogic/bin/install.sh ]
358     then
359       printf "Installing directed graphs\n"
360       "${SDNC_HOME}"/svclogic/bin/install.sh
361     fi
362
363   if $SDNRWT ; then install_sdnrwt_features ; fi
364   # The enable_odl_cluster call should not be moved above this line as the cleanFeatureBoot will overwrite entries. Ex: odl-jolokia
365   if $ENABLE_ODL_CLUSTER ; then enable_odl_cluster ; fi
366
367   if $SDNR_NORTHBOUND ; then install_sdnr_northbound_features ; fi
368   printf "%s" "Installed at $(date)" > "${SDNC_HOME}"/.installed
369 fi
370
371 #cp /opt/opendaylight/current/certs/* /tmp
372 #cp /var/custom-certs/* /tmp
373
374 if [ -n "$OVERRIDE_FEATURES_BOOT" ] ; then
375   printf "%s\n" "Override features boot: $OVERRIDE_FEATURES_BOOT"
376   sed -i "/$FEATURESBOOTMARKER/c\featuresBoot = $OVERRIDE_FEATURES_BOOT" "$ODL_FEATURES_BOOT_FILE"
377 fi
378
379 # Odl configuration done
380 ODL_REPOSITORIES_BOOT=$(sed -n "/$REPOSITORIESBOOTMARKER/p" "$ODL_FEATURES_BOOT_FILE")
381 ODL_FEATURES_BOOT=$(sed -n "/$FEATURESBOOTMARKER/p" "$ODL_FEATURES_BOOT_FILE")
382 export ODL_FEATURES_BOOT
383
384 # Create ODL data log directory (it nornally is created after karaf
385 # is started, but needs to exist before installCerts.py runs)
386 if [ -z "$ODL_CERT_DIR" ] ; then
387   printf "No certs provided. Skip installation.\n"
388 else
389   printf "Start background cert installer\n"
390   mkdir -p /opt/opendaylight/data/log
391   nohup python3 "${SDNC_BIN}"/installCerts.py &
392   printf "Start monitoring certificate installation. \n"
393   nohup sh "${SDNC_BIN}"/monitorCertsInstall.sh &
394 fi
395
396 printf "Startup opendaylight\n"
397 printf "%s\n" "$ODL_REPOSITORIES_BOOT"
398 printf "%s\n" "$ODL_FEATURES_BOOT"
399 exec "${ODL_HOME}"/bin/karaf server