summaryrefslogtreecommitdiffstats
path: root/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/CCAActivator.java
blob: 6815b4c011f61a08f06cbcbc5f3b9c2e632f112f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.ccadaptor;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.onap.ccsdk.sli.core.sli.ConfigurationException;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class CCAActivator implements BundleActivator
{

  private static final String CCA_PROP_FILE_VAR = "SDNC_CCA_PROPERTIES";
  private static final String APPC_CONFIG_DIR_VAR = "APPC_CONFIG_DIR";

  @SuppressWarnings("rawtypes")
  private ServiceRegistration registration = null;

  private static final EELFLogger log = EELFManager.getInstance().getLogger(CCAActivator.class);

  @Override
  public void start(BundleContext ctx) throws Exception
  {
    // Read properties
    Properties props = new Properties();

    // Read properties from appc-config-adaptor.properties
    String propFileName = System.getenv(CCA_PROP_FILE_VAR);
    if (propFileName == null)
    {
      String propDir = System.getenv(APPC_CONFIG_DIR_VAR);
      if (propDir == null)
        throw new ConfigurationException(
          "Cannot find config file - " + CCA_PROP_FILE_VAR + " and " + APPC_CONFIG_DIR_VAR + " unset");

      propFileName = propDir + "/appc-config-adaptor.properties";
      log.warn("Environment variable " + CCA_PROP_FILE_VAR + " unset - defaulting to " + propFileName);
    }

    File propFile = new File(propFileName);
    if (!propFile.exists())
      throw new ConfigurationException("Missing configuration properties file: " + propFile);

    try (InputStream in = new FileInputStream(propFile))
    {
      props.load(in);
    }
    catch (Exception e)
    {
      throw new ConfigurationException("Could not load properties file " + propFileName, e);
    }

    log.info("Loaded properties: ");

    // Advertise adaptor
    ConfigComponentAdaptor adaptor = new ConfigComponentAdaptor(props);
    if (registration == null)
    {
      log.info("Registering service " + ConfigComponentAdaptor.class.getName());
      registration = ctx.registerService(ConfigComponentAdaptor.class.getName(), adaptor, null);
    }

  }

  @Override
  public void stop(BundleContext ctx) throws Exception
  {
    if (registration != null)
    {
      registration.unregister();
      registration = null;
    }
  }
}