Effort to reduce image layer and size
[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_sdnrdb() {
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/${ccsdk.features.version}/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     if $SDNRONLY; then
104       cleanupFeatureBoot
105     fi
106     addToFeatureBoot "$SDNRDM_BOOTFEATURES"
107     if ! $SDNRDM; then
108       addToFeatureBoot "$SDNRODLUX_BOOTFEATURES"
109     fi
110   fi
111 }
112 install_sdnr_oauth_features() {
113   addToFeatureBoot "$SDNROAUTH_BOOTFEATURES"
114 }
115 install_sdnr_northbound_features() {
116   addToFeatureBoot "$SDNR_NORTHBOUND_BOOTFEATURES"
117 }
118 install_a1_northbound_features() {
119   addToFeatureBoot "$A1_ADAPTER_NORTHBOUND_BOOTFEATURES"
120 }
121 # Reconfigure ODL from default single node configuration to cluster
122
123 enable_odl_cluster() {
124   if [ -z "$SDNC_REPLICAS" ]; then
125      printf "SDNC_REPLICAS is not configured in Env field"
126      exit
127   fi
128
129   # ODL NETCONF setup
130   printf "Installing Opendaylight cluster features for mdsal and netconf\n"
131
132   #Be sure to remove feature odl-netconf-connector-all from list
133   replaceFeatureBoot "odl-netconf-connector-all,"
134
135   printf "Installing Opendaylight cluster features\n"
136   replaceFeatureBoot odl-netconf-topology odl-netconf-clustered-topology
137   replaceFeatureBoot odl-mdsal-all odl-mdsal-all,odl-mdsal-clustering
138   addToFeatureBoot odl-jolokia
139   #${ODL_HOME}/bin/client feature:install odl-mdsal-clustering
140   #${ODL_HOME}/bin/client feature:install odl-jolokia
141
142   # ODL Cluster or Geo cluster configuration
143
144   printf "Update cluster information statically\n"
145   fqdn=$(hostname -f)
146   printf "%s\n" "Get current fqdn ${fqdn}"
147
148   # Extract node index using first digit after "-"
149   # Example 2 from "sdnr-2.logo.ost.das.r32.com"
150   node_index=$(echo "${fqdn}" | sed -r 's/.*-([0-9]).*/\1/g')
151   member_offset=1
152
153   if $GEO_ENABLED; then
154     printf "This is a Geo cluster\n"
155
156     if [ -z "$IS_PRIMARY_CLUSTER" ] || [ -z "$MY_ODL_CLUSTER" ] || [ -z "$PEER_ODL_CLUSTER" ]; then
157      printf "IS_PRIMARY_CLUSTER, MY_ODL_CLUSTER and PEER_ODL_CLUSTER must all be configured in Env field\n"
158      return
159     fi
160
161     if $IS_PRIMARY_CLUSTER; then
162        PRIMARY_NODE=${MY_ODL_CLUSTER}
163        SECONDARY_NODE=${PEER_ODL_CLUSTER}
164     else
165        PRIMARY_NODE=${PEER_ODL_CLUSTER}
166        SECONDARY_NODE=${MY_ODL_CLUSTER}
167        member_offset=4
168     fi
169
170     node_list="${PRIMARY_NODE} ${SECONDARY_NODE}"
171
172     "${SDNC_BIN}"/configure_geo_cluster.sh $((node_index+member_offset)) "${node_list}"
173   else
174     printf "This is a local cluster\n"
175     i=0
176     node_list=""
177     # SERVICE_NAME and NAMESPACE are used to create cluster node names and are provided via Helm charts in OOM environment
178     if [ ! -z "$SERVICE_NAME" ] && [ ! -z "$NAMESPACE" ]; then
179        # Extract node name minus the index
180        # Example sdnr from "sdnr-2.logo.ost.das.r32.com"
181        node_name=$(echo "${fqdn}" | sed 's/-[0-9].*$//g')
182        while [ $i -lt "$SDNC_REPLICAS" ]; do
183          node_list="${node_list} ${node_name}-$i.${SERVICE_NAME}-cluster.${NAMESPACE}"
184          i=$(($i + 1))
185        done
186        "${ODL_HOME}"/bin/configure_cluster.sh $((node_index+1)) "${node_list}"
187     elif [ -z "$SERVICE_NAME" ] && [ -z "$NAMESPACE" ]; then
188       # Hostname is used in Standalone environment to create cluster node names
189        while [ $i -lt "$SDNC_REPLICAS" ]; do
190          #assemble node list by replacing node-index in hostname with "i"
191          node_name=$(echo "${fqdn}" | sed -r "s/-[0-9]/-$i/g")
192          node_list="${node_list} ${node_name}"
193          i=$(($i + 1))
194        done
195        "${ODL_HOME}"/bin/configure_cluster.sh $((node_index+1)) "${node_list}"
196     else
197        printf "Unhandled cluster scenario. Terminating the container\n"
198        printf "Any one of the below 2 conditions should be satisfied for successfully enabling cluster mode : \n"
199        printf "1. OOM Environment - Both SERVICE_NAME and NAMESPACE environment variables have to be set.\n"
200        printf "2. Docker (standalone) Environment - Neither of SERVICE_NAME and NAMESPACE have to be set.\n"
201        printf "Current configuration - SERVICE_NAME = $SERVICE_NAME  NAMESPACE = $NAMESPACE\n"
202        exit $NOTOK
203     fi
204   fi
205 }
206
207
208 # Install SDN-C platform components if not already installed and start container
209
210 # -----------------------
211 # Main script starts here
212 printf "Installing SDNC/R from startODL.sh script\n"
213 ODL_HOME=${ODL_HOME:-/opt/opendaylight/current}
214 ODL_FEATURES_BOOT_FILE=$ODL_HOME/etc/org.apache.karaf.features.cfg
215
216 ODL_ADMIN_USERNAME=${ODL_ADMIN_USERNAME:-admin}
217 ODL_ADMIN_PASSWORD=${ODL_ADMIN_PASSWORD:-admin}
218 ODL_REMOVEIDMDB=${ODL_REMOVEIDMDB:-true}
219
220 if $ODL_REMOVEIDMDB ; then
221   if [ -f $ODL_HOME/data/idmlight.db.mv.db ]; then
222     rm $ODL_HOME/data/idmlight.db.mv.db
223   fi
224 fi
225
226 CCSDK_HOME=${CCSDK_HOME:-/opt/onap/ccsdk}
227 SDNC_HOME=${SDNC_HOME:-/opt/onap/sdnc}
228 SDNC_BIN=${SDNC_BIN:-/opt/onap/sdnc/bin}
229 JDEBUG=${JDEBUG:-false}
230 SDNC_AAF_ENABLED=${SDNC_AAF_ENABLED:-false}
231 INSTALLED_DIR=${INSTALLED_FILE:-/opt/opendaylight/current/daexim}
232
233 # Whether to intialize MYSql DB or not. Default is to initialize
234 SDNC_DB_INIT=${SDNC_DB_INIT:-false}
235 MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-openECOMP1.0}
236
237 IS_PRIMARY_CLUSTER=${IS_PRIMARY_CLUSTER:-false}
238 MY_ODL_CLUSTER=${MY_ODL_CLUSTER:-127.0.0.1}
239 ENABLE_ODL_CLUSTER=${ENABLE_ODL_CLUSTER:-false}
240 ENABLE_OAUTH=${ENABLE_OAUTH:-false}
241 ENABLE_ODLUX_RBAC=${ENABLE_ODLUX_RBAC:-false}
242 GEO_ENABLED=${GEO_ENABLED:-false}
243
244 SDNRWT=${SDNRWT:-false}
245 SDNRDM=${SDNRDM:-false}
246 SDNRODLUX_BOOTFEATURES=${SDNRODLUX_BOOTFEATURES:-sdnr-wt-helpserver-feature,sdnr-wt-odlux-core-feature,sdnr-wt-odlux-apps-feature}
247 SDNROAUTH_BOOTFEATURES=${SDNROAUTH_BOOTFEATURES:-sdnr-wt-feature-aggregator-oauth}
248
249 # Add devicemanager features
250 SDNRDM_SDM_LIST=${SDNRDM_SDM_LIST:-sdnr-wt-feature-aggregator-devicemanager}
251 SDNRDM_BOOTFEATURES=${SDNRDM_BOOTFEATURES:-sdnr-wt-feature-aggregator-devicemanager-base,${SDNRDM_SDM_LIST}}
252
253 # Whether to Initialize the ElasticSearch DB.
254 SDNRINIT=${SDNRINIT:-false}
255 SDNRONLY=${SDNRONLY:-false}
256 SDNRDBURL=${SDNRDBURL:-http://sdnrdb:9200}
257 SDNRDBCOMMAND=${SDNRDBCOMMAND:--c init -db $SDNRDBURL -dbu $SDNRDBUSERNAME -dbp $SDNRDBPASSWORD $SDNRDBPARAMETER}
258
259 SDNR_NORTHBOUND=${SDNR_NORTHBOUND:-false}
260 SDNR_NORTHBOUND_BOOTFEATURES=${SDNR_NORTHBOUND_BOOTFEATURES:-sdnr-northbound-all}
261 # if only SDNR features then do not start A1 adapter
262 if $SDNRONLY ; then
263   A1_ADAPTER_NORTHBOUND=false
264 else
265   A1_ADAPTER_NORTHBOUND=${A1_ADAPTER_NORTHBOUND:-true}
266 fi
267 A1_ADAPTER_NORTHBOUND_BOOTFEATURES=${A1_ADAPTER_NORTHBOUND_BOOTFEATURES:-a1-adapter-northbound}
268
269 NOTOK=1
270 #export for installCerts.py
271 export ODL_ADMIN_PASSWORD ODL_ADMIN_USERNAME
272
273 if $JDEBUG ; then
274     printf "Activate remote debugging\n"
275     #JSTADTPOLICYFILE="$ODL_HOME/etc/tools.policy"
276     #echo -e "grant codebase \"file:${JAVA_HOME}/lib/tools.jar\" {\n  permission java.security.AllPermission;\n };" > $JSTADTPOLICYFILE
277     #sleep 1
278     #$JAVA_HOME/bin/jstatd -p 1089 -J-Djava.security.policy=$JSTADTPOLICYFILE &
279     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.port=1090"
280     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.rmi.port=1090"
281     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Djava.rmi.server.hostname=$(hostname)  "
282     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.local.only=false"
283     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
284     EXTRA_JAVA_OPTS="${EXTRA_JAVA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"
285     export EXTRA_JAVA_OPTS
286 fi
287
288
289 printf "Settings:\n"
290 printf "%s\n" "  SDNC_BIN=$SDNC_BIN"
291 printf "%s\n" "  SDNC_HOME=$SDNC_HOME"
292 printf "%s\n" "  SDNC_DB_INIT=$SDNC_DB_INIT"
293 printf "%s\n" "  ODL_CERT_DIR=$ODL_CERT_DIR"
294 printf "%s\n" "  ENABLE_ODL_CLUSTER=$ENABLE_ODL_CLUSTER"
295 printf "%s\n" "  ODL_REMOVEIDMDB=$ODL_REMOVEIDMDB"
296 printf "%s\n" "  SDNC_REPLICAS=$SDNC_REPLICAS"
297 printf "%s\n" "  ENABLE_OAUTH=$ENABLE_OAUTH"
298 printf "%s\n" "  ENABLE_ODLUX_RBAC=$ENABLE_ODLUX_RBAC"
299 printf "%s\n" "  SDNRWT=$SDNRWT"
300 printf "%s\n" "  SDNRDM=$SDNRDM"
301 printf "%s\n" "  SDNRONLY=$SDNRONLY"
302 printf "%s\n" "  SDNRINIT=$SDNRINIT"
303 printf "%s\n" "  SDNRDBURL=$SDNRDBURL"
304 printf "%s\n" "  SDNRDBUSERNAME=$SDNRDBUSERNAME"
305 printf "%s\n" "  GEO_ENABLED=$GEO_ENABLED"
306 printf "%s\n" "  IS_PRIMARY_CLUSTER=$IS_PRIMARY_CLUSTER"
307 printf "%s\n" "  MY_ODL_CLUSTER=$MY_ODL_CLUSTER"
308 printf "%s\n" "  PEER_ODL_CLUSTER=$PEER_ODL_CLUSTER"
309 printf "%s\n" "  SDNR_NORTHBOUND=$SDNR_NORTHBOUND"
310 printf "%s\n" "  AAF_ENABLED=$SDNC_AAF_ENABLED"
311 printf "%s\n" "  SERVICE_NAME=$SERVICE_NAME"
312 printf "%s\n" "  NAMESPACE=$NAMESPACE"
313
314 if "$SDNC_AAF_ENABLED"; then
315         export SDNC_AAF_STORE_DIR=/opt/app/osaaf/local
316         export SDNC_AAF_CONFIG_DIR=/opt/app/osaaf/local
317         export SDNC_KEYPASS=$(cat /opt/app/osaaf/local/.pass)
318         export SDNC_KEYSTORE=org.onap.sdnc.p12
319         sed -i '/cadi_prop_files/d' "$ODL_HOME"/etc/system.properties
320         echo "cadi_prop_files=$SDNC_AAF_CONFIG_DIR/org.onap.sdnc.props" >> "$ODL_HOME"/etc/system.properties
321
322         sed -i '/org.ops4j.pax.web.ssl.keystore/d' "$ODL_HOME"/etc/custom.properties
323         sed -i '/org.ops4j.pax.web.ssl.password/d' "$ODL_HOME"/etc/custom.properties
324         sed -i '/org.ops4j.pax.web.ssl.keypassword/d' "$ODL_HOME"/etc/custom.properties
325         echo "org.ops4j.pax.web.ssl.keystore=$SDNC_AAF_STORE_DIR/$SDNC_KEYSTORE" >> "$ODL_HOME"/etc/custom.properties
326         echo "org.ops4j.pax.web.ssl.password=$SDNC_KEYPASS" >> "$ODL_HOME"/etc/custom.properties
327         echo "org.ops4j.pax.web.ssl.keypassword=$SDNC_KEYPASS" >> "$ODL_HOME"/etc/custom.properties
328 fi
329
330 if $SDNRINIT ; then
331   #One time intialization action
332   initialize_sdnrdb
333   init_result=$?
334   printf "%s\n" "Result of init script: $init_result"
335   if $SDNRWT ; then
336     printf "Proceed to initialize sdnr\n"
337   else
338     exit $init_result
339   fi
340 fi
341
342 # do not start container if ADMIN_PASSWORD is not set
343 if [ -z "$ODL_ADMIN_PASSWORD" ]; then
344   echo "ODL_ADMIN_PASSWORD is not set"
345   exit 1
346 fi
347
348 # Check for MySQL DB connectivity only if SDNC_DB_INIT is set to "true"
349 if $SDNC_DB_INIT; then
350 #
351 # Wait for database
352 #
353   printf "Waiting for mysql"
354   until mysql -h dbhost -u root -p"${MYSQL_ROOT_PASSWORD}" -e "select 1" > /dev/null 2>&1
355   do
356     printf "."
357     sleep 1
358   done
359   printf "\nmysql ready"
360 fi
361
362
363 if [ ! -d "${INSTALLED_DIR}" ]
364 then
365     mkdir -p "${INSTALLED_DIR}"
366 fi
367
368 if [ ! -f "${SDNC_HOME}"/.installed ]
369 then
370     # for integration testing. In OOM, a separate job takes care of installing it.
371     if $SDNC_DB_INIT; then
372       printf "Installing SDN-C database\n"
373       "${SDNC_HOME}"/bin/installSdncDb.sh
374     fi
375     printf "Installing SDN-C keyStore\n"
376     "${SDNC_HOME}"/bin/addSdncKeyStore.sh
377     printf "Installing A1-adapter trustStore\n"
378     "${SDNC_HOME}"/bin/addA1TrustStore.sh
379
380     if [ -x "${SDNC_HOME}"/svclogic/bin/install.sh ]
381     then
382       printf "Installing directed graphs\n"
383       "${SDNC_HOME}"/svclogic/bin/install.sh
384     fi
385
386   if $SDNRWT ; then install_sdnrwt_features ; fi
387   if $ENABLE_OAUTH ; then
388     cp $SDNC_HOME/data/oauth-aaa-app-config.xml $ODL_HOME/system/org/opendaylight/aaa/aaa-shiro/0.12.1/aaa-shiro-0.12.1-aaa-app-config.xml
389     install_sdnr_oauth_features
390   fi
391
392   # The enable_odl_cluster call should not be moved above this line as the cleanFeatureBoot will overwrite entries. Ex: odl-jolokia
393   if $ENABLE_ODL_CLUSTER ; then enable_odl_cluster ; fi
394
395   if $SDNR_NORTHBOUND ; then install_sdnr_northbound_features ; fi
396   if $A1_ADAPTER_NORTHBOUND ; then install_a1_northbound_features ; fi
397
398   printf "%s" "Installed at $(date)" > "${SDNC_HOME}"/.installed
399 fi
400
401 #cp /opt/opendaylight/current/certs/* /tmp
402 #cp /var/custom-certs/* /tmp
403
404 if [ -n "$OVERRIDE_FEATURES_BOOT" ] ; then
405   printf "%s\n" "Override features boot: $OVERRIDE_FEATURES_BOOT"
406   sed -i "/$FEATURESBOOTMARKER/c\featuresBoot = $OVERRIDE_FEATURES_BOOT" "$ODL_FEATURES_BOOT_FILE"
407 fi
408
409 # Odl configuration done
410 ODL_REPOSITORIES_BOOT=$(sed -n "/$REPOSITORIESBOOTMARKER/p" "$ODL_FEATURES_BOOT_FILE")
411 ODL_FEATURES_BOOT=$(sed -n "/$FEATURESBOOTMARKER/p" "$ODL_FEATURES_BOOT_FILE")
412 export ODL_FEATURES_BOOT
413
414 # Create ODL data log directory (it nornally is created after karaf
415 # is started, but needs to exist before installCerts.py runs)
416 if [ -z "$ODL_CERT_DIR" ] ; then
417   printf "No certs provided. Skip installation.\n"
418 else
419   printf "Start background cert installer\n"
420   mkdir -p /opt/opendaylight/data/log
421   nohup python3 "${SDNC_BIN}"/installCerts.py &
422   printf "Start monitoring certificate installation. \n"
423   nohup sh "${SDNC_BIN}"/monitorCertsInstall.sh &
424 fi
425
426 printf "Startup opendaylight\n"
427 printf "%s\n" "$ODL_REPOSITORIES_BOOT"
428 printf "%s\n" "$ODL_FEATURES_BOOT"
429
430 exec "${ODL_HOME}"/bin/karaf server