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