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;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.util.HashSet;
22 import java.util.Optional;
23 import java.util.Properties;
26 import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException;
27 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
28 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
29 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
30 import org.onap.ccsdk.sli.core.utils.common.BundleContextFileResolver;
31 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Responsible for determining the properties file to use.
39 * <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
40 * <li>A directory identified by the JRE argument <code>netbox.properties</code></li>
41 * <li>A <code>netbox.properties</code> file located in the karaf root directory</li>
44 * Partial copy and adaptation of org.onap.ccsdk.sli.adaptors.aai.AAIServiceProvider
46 public class NetboxProperties {
48 private static final Logger LOG = LoggerFactory.getLogger(NetboxProperties.class);
50 private static final String NETBOX_PROPERTY_FILE_NAME = "netbox.properties";
51 private static final String MISSING_PROPERTY_FILE =
52 "Missing configuration properties resource for Netbox: " + NETBOX_PROPERTY_FILE_NAME;
53 private static final String NETBOX_URL_PROP = "org.onap.ccsdk.sli.adaptors.netbox.url";
54 private static final String NETBOX_API_KEY_PROP = "org.onap.ccsdk.sli.adaptors.netbox.apikey";
56 private Set<PropertiesFileResolver> fileResolvers = new HashSet<>();
57 private Properties properties;
59 public NetboxProperties() {
60 fileResolvers.add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
61 fileResolvers.add(new BundleContextFileResolver("Using property file (2) from BundleContext property",
62 NetboxProperties.class));
63 fileResolvers.add(new JREFileResolver("Using property file (3) from JRE argument", NetboxProperties.class));
64 fileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
69 public String getHost() {
70 checkArgument(properties != null);
71 return properties.getProperty(NETBOX_URL_PROP);
74 public String getApiKey() {
75 checkArgument(properties != null);
76 return properties.getProperty(NETBOX_API_KEY_PROP);
79 private void checkArgument(final boolean argument) {
81 LOG.info("Propety file {} was missing, trying to reload it", NETBOX_PROPERTY_FILE_NAME);
83 if (properties == null) {
84 throw new IllegalArgumentException(MISSING_PROPERTY_FILE);
89 private void loadProps() {
90 // determines properties file as according to the priority described in the class header comment
91 final File propertiesFile = determinePropertiesFile();
92 if (propertiesFile != null) {
93 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
94 properties = new Properties();
95 properties.load(fileInputStream);
96 } catch (final IOException e) {
97 String errorMsg = "Failed to load properties for file: " + propertiesFile.toString();
98 LOG.error(errorMsg, new IpamException(errorMsg));
103 private File determinePropertiesFile() {
104 for (final PropertiesFileResolver resolver : fileResolvers) {
105 final Optional<File> fileOptional = resolver.resolveFile(NETBOX_PROPERTY_FILE_NAME);
106 if (fileOptional.isPresent()) {
107 final File file = fileOptional.get();
108 LOG.info("{} {}", resolver.getSuccessfulResolutionMessage(), file.getPath());
113 LOG.error(MISSING_PROPERTY_FILE, new IpamException(MISSING_PROPERTY_FILE));