[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / sql-resource / provider / src / main / java / org / openecomp / sdnc / sli / resource / sql / SqlResourceActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.resource.sql;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.util.Properties;
27
28 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceRegistration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class SqlResourceActivator implements BundleActivator {
36
37         private static final String SQLRESOURCE_PROP_PATH = "/sql-resource.properties";
38
39         private ServiceRegistration registration = null;
40
41         private static final Logger LOG = LoggerFactory
42                         .getLogger(SqlResourceActivator.class);
43
44         @Override
45         public void start(BundleContext ctx) throws Exception {
46
47                 String cfgDir = System.getenv("SDNC_CONFIG_DIR");
48
49                 if ((cfgDir == null) || (cfgDir.length() == 0)) {
50                         cfgDir = "/opt/sdnc/data/properties";
51                         LOG.warn("SDNC_CONFIG_DIR unset - defaulting to "+cfgDir);
52                 }
53
54                 String cryptKey = "";
55
56                 File sqlResourcePropFile = new File(cfgDir+SQLRESOURCE_PROP_PATH);
57                 Properties sqlResourceProps = new Properties();
58                 if (sqlResourcePropFile.exists()) {
59                         try {
60
61                                 sqlResourceProps.load(new FileInputStream(sqlResourcePropFile));
62
63                                 cryptKey = sqlResourceProps.getProperty("org.openecomp.sdnc.resource.sql.cryptkey");
64                         } catch (Exception e) {
65                                 LOG.warn(
66                                                 "Could not load properties file " + sqlResourcePropFile.getAbsolutePath(), e);
67                         }
68                 } else {
69                         LOG.warn("Cannot read "+sqlResourcePropFile.getAbsolutePath()+" to find encryption key - using default");
70                 }
71
72                 SqlResource.setCryptKey(cryptKey);
73
74                 // Advertise Sql resource adaptor
75                 SvcLogicResource impl = new SqlResource();
76                 String regName = impl.getClass().getName();
77
78                 if (registration == null)
79                 {
80                         LOG.debug("Registering SqlResource service "+regName);
81                         registration =ctx.registerService(regName, impl, null);
82                 }
83
84         }
85
86         @Override
87         public void stop(BundleContext ctx) throws Exception {
88
89                 if (registration != null)
90                 {
91                         registration.unregister();
92                         registration = null;
93                 }
94         }
95
96 }