Changes to Config Component Adaptor
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.ccadaptor;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import java.util.Properties;
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
37 public class CCAActivator implements BundleActivator
38 {
39
40   private static final String CCA_PROP_FILE_VAR = "SDNC_CCA_PROPERTIES";
41   private static final String APPC_CONFIG_DIR_VAR = "APPC_CONFIG_DIR";
42
43   @SuppressWarnings("rawtypes")
44   private ServiceRegistration registration = null;
45
46   private static final EELFLogger log = EELFManager.getInstance().getLogger(CCAActivator.class);
47
48   @Override
49   public void start(BundleContext ctx) throws Exception
50   {
51     // Read properties
52     Properties props = new Properties();
53
54     // Read properties from appc-config-adaptor.properties
55     String propFileName = System.getenv(CCA_PROP_FILE_VAR);
56     if (propFileName == null)
57     {
58       String propDir = System.getenv(APPC_CONFIG_DIR_VAR);
59       if (propDir == null)
60         throw new ConfigurationException(
61           "Cannot find config file - " + CCA_PROP_FILE_VAR + " and " + APPC_CONFIG_DIR_VAR + " unset");
62
63       propFileName = propDir + "/appc-config-adaptor.properties";
64       log.warn("Environment variable " + CCA_PROP_FILE_VAR + " unset - defaulting to " + propFileName);
65     }
66
67     File propFile = new File(propFileName);
68     if (!propFile.exists())
69       throw new ConfigurationException("Missing configuration properties file: " + propFile);
70
71     try (InputStream in = new FileInputStream(propFile))
72     {
73       props.load(in);
74     }
75     catch (Exception e)
76     {
77       throw new ConfigurationException("Could not load properties file " + propFileName, e);
78     }
79
80     log.info("Loaded properties: ");
81
82     // Advertise adaptor
83     ConfigComponentAdaptor adaptor = new ConfigComponentAdaptor(props);
84     if (registration == null)
85     {
86       log.info("Registering service " + ConfigComponentAdaptor.class.getName());
87       registration = ctx.registerService(ConfigComponentAdaptor.class.getName(), adaptor, null);
88     }
89
90   }
91
92   @Override
93   public void stop(BundleContext ctx) throws Exception
94   {
95     if (registration != null)
96     {
97       registration.unregister();
98       registration = null;
99     }
100   }
101 }