2  * Copyright (C) 2018 Bell Canada.
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *      http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  16 package org.onap.ccsdk.sli.adaptors.netbox.property;
 
  18 import java.io.FileInputStream;
 
  19 import java.io.IOException;
 
  20 import java.io.InputStream;
 
  22 import java.util.Properties;
 
  23 import org.osgi.framework.Bundle;
 
  24 import org.osgi.framework.BundleContext;
 
  25 import org.osgi.framework.FrameworkUtil;
 
  26 import org.slf4j.Logger;
 
  27 import org.slf4j.LoggerFactory;
 
  29 public class NetboxProperties {
 
  31     private static final Logger LOG = LoggerFactory.getLogger(NetboxProperties.class);
 
  33     private static final String NETBOX_PROPERTY_FILE_NAME = "netbox.properties";
 
  34     private static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
 
  35     private static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
 
  37     private static final String NETBOX_URL_PROP = "org.onap.ccsdk.sli.adaptors.netbox.url";
 
  38     private static final String NETBOX_API_KEY_PROP = "org.onap.ccsdk.sli.adaptors.netbox.apikey";
 
  40     private Properties properties;
 
  42     public NetboxProperties() {
 
  46     public String getHost() {
 
  47         return properties.getProperty(NETBOX_URL_PROP);
 
  50     public String getApiKey() {
 
  51         return properties.getProperty(NETBOX_API_KEY_PROP);
 
  54     private void loadProps() {
 
  55         properties = new Properties();
 
  56         // Try to load config from dir
 
  57         final String ccsdkConfigDir =
 
  58             System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR) + "/" + NETBOX_PROPERTY_FILE_NAME;
 
  59         try (FileInputStream in = new FileInputStream(ccsdkConfigDir)) {
 
  61             LOG.info("Loaded {} properties from file {}", properties.size(), ccsdkConfigDir);
 
  62         } catch (Exception e) {
 
  63             // Try to load config from jar
 
  64             final Bundle bundle = FrameworkUtil.getBundle(NetboxProperties.class);
 
  65             final BundleContext ctx = bundle.getBundleContext();
 
  66             final URL url = ctx.getBundle().getResource(NETBOX_PROPERTY_FILE_NAME);
 
  68             try (InputStream inputStream = url.openStream()) {
 
  69                 properties.load(inputStream);
 
  70                 LOG.info("Loaded {} properties from file {}", properties.size(), NETBOX_PROPERTY_FILE_NAME);
 
  71             } catch (IOException e1) {
 
  72                 LOG.error("Failed to load properties for file: {} " + NETBOX_PROPERTY_FILE_NAME, e1);