ba22753fc470d25ecbc9a1eb5f093d5798a84eef
[appc.git] / appc-config / appc-config-adaptor / provider / src / main / java / org / onap / appc / ccadaptor / CCAActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.ccadaptor;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.InputStream;
30 import java.util.Properties;
31
32 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
33 import org.osgi.framework.BundleActivator;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.ServiceRegistration;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42 public class CCAActivator implements BundleActivator
43 {
44
45   private static final String CCA_PROP_FILE_VAR = "SDNC_CCA_PROPERTIES";
46   private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
47
48   @SuppressWarnings("rawtypes")
49   private ServiceRegistration registration = null;
50
51   //private static final Logger log = LoggerFactory.getLogger(CCAActivator.class);
52   private static final EELFLogger log = EELFManager.getInstance().getLogger(CCAActivator.class);
53
54     @Override
55     public void start(BundleContext ctx) throws Exception
56     {
57         // Read properties
58         Properties props = new Properties();
59
60         // Read properties from appc-config-adaptor.properties
61         String propFileName = System.getenv(CCA_PROP_FILE_VAR);
62         if (propFileName == null)
63         {
64             String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
65             if (propDir == null)
66                 throw new ConfigurationException(
67                     "Cannot find config file - " + CCA_PROP_FILE_VAR + " and " + SDNC_CONFIG_DIR_VAR + " unset");
68
69             propFileName = propDir + "/appc-config-adaptor.properties";
70             log.warn("Environment variable " + CCA_PROP_FILE_VAR + " unset - defaulting to " + propFileName);
71         }
72
73         File propFile = new File(propFileName);
74         if (!propFile.exists())
75             throw new ConfigurationException("Missing configuration properties file: " + propFile);
76
77         try(InputStream in = new FileInputStream(propFile)) {
78             props.load(in);
79             log.info("Loaded properties: ");
80         }
81         catch (Exception e)
82         {
83             throw new ConfigurationException("Could not load properties file " + propFileName, e);
84         }
85
86         // Advertise adaptor
87         ConfigComponentAdaptor adaptor = new ConfigComponentAdaptor(props);
88         if (registration == null)
89         {
90             log.info("Registering service " + ConfigComponentAdaptor.class.getName());
91             registration = ctx.registerService(ConfigComponentAdaptor.class.getName(), adaptor, null);
92         }
93
94     }
95
96     @Override
97     public void stop(BundleContext ctx) throws Exception
98     {
99         if (registration != null)
100         {
101             registration.unregister();
102             registration = null;
103         }
104     }
105 }