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