Fix handling of non-OSGi
[ccsdk/sli/adaptors.git] / netbox-client / provider / src / main / java / org / onap / ccsdk / sli / adaptors / netbox / property / NetboxProperties.java
1 /*
2  * Copyright (C) 2018 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.sli.adaptors.netbox.property;
17
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
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;
28
29 public class NetboxProperties {
30
31     private static final Logger LOG = LoggerFactory.getLogger(NetboxProperties.class);
32
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";
36
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";
39
40     private Properties properties;
41
42     public NetboxProperties() {
43         loadProps();
44     }
45
46     public String getHost() {
47         return properties.getProperty(NETBOX_URL_PROP);
48     }
49
50     public String getApiKey() {
51         return properties.getProperty(NETBOX_API_KEY_PROP);
52     }
53
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)) {
60             properties.load(in);
61             LOG.info("Loaded {} properties from file {}", properties.size(), ccsdkConfigDir);
62         } catch (Exception e) {
63             try {
64                 // Try to load config from jar via OSGi
65                 final Bundle bundle = FrameworkUtil.getBundle(NetboxProperties.class);
66                 final BundleContext ctx = bundle.getBundleContext();
67                 final URL url = ctx.getBundle().getResource(NETBOX_PROPERTY_FILE_NAME);
68                 InputStream inputStream = url.openStream();
69                 properties.load(inputStream);
70                 LOG.info("Loaded {} properties from file {}", properties.size(), NETBOX_PROPERTY_FILE_NAME);
71             } catch (NoClassDefFoundError e1) { 
72                 // Try to load config from jar via class loader
73                 try (InputStream inputStream = NetboxProperties.class.getResourceAsStream("/"+NETBOX_PROPERTY_FILE_NAME)) {
74                     properties.load(inputStream);
75                 } catch (Exception e2) {
76                     LOG.error("Failed to load properties for file: {} " + NETBOX_PROPERTY_FILE_NAME, e1);
77                 }
78                 ;
79                 LOG.info("Loaded {} properties from file {}", properties.size(), NETBOX_PROPERTY_FILE_NAME);
80
81             } catch (IOException e1) {
82                 LOG.error("Failed to load properties for file: {} " + NETBOX_PROPERTY_FILE_NAME, e1);
83             }
84         }
85     }
86 }