From ed7162521270d33da1bdc57286b23c64e23e5b0f Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Sat, 9 Feb 2019 15:01:10 -0500 Subject: Rework property file load logic for grpc & netbox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We first try to load from the filesystem properties folder, if not found, then we load from the bundle itself. Change-Id: Ib4b422b1fe18f80b2f2258f96f3739d25bd756c9 Issue-ID: CCSDK-1038 Signed-off-by: Alexis de Talhouët --- netbox-client/provider/pom.xml | 10 +-- .../adaptors/netbox/property/NetboxProperties.java | 90 ++++++---------------- .../netbox/property/NetboxPropertiesTest.java | 66 ---------------- 3 files changed, 30 insertions(+), 136 deletions(-) delete mode 100644 netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java (limited to 'netbox-client/provider') diff --git a/netbox-client/provider/pom.xml b/netbox-client/provider/pom.xml index 3af22edb2..f7e8ccf92 100644 --- a/netbox-client/provider/pom.xml +++ b/netbox-client/provider/pom.xml @@ -79,6 +79,11 @@ ${project.version} compile + + org.osgi + org.osgi.core + provided + @@ -111,11 +116,6 @@ 1.2.3 test - - org.osgi - org.osgi.core - test - diff --git a/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxProperties.java b/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxProperties.java index ee493ec18..2eecf6e3a 100644 --- a/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxProperties.java +++ b/netbox-client/provider/src/main/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxProperties.java @@ -15,102 +15,62 @@ */ package org.onap.ccsdk.sli.adaptors.netbox.property; -import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.util.HashSet; -import java.util.Optional; +import java.io.InputStream; +import java.net.URL; import java.util.Properties; -import java.util.Set; - -import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException; -import org.onap.ccsdk.sli.core.utils.JREFileResolver; -import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver; -import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver; -import org.onap.ccsdk.sli.core.utils.common.BundleContextFileResolver; -import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Responsible for determining the properties file to use. - * - *
    - *
  1. A directory identified by the system environment variable SDNC_CONFIG_DIR
  2. - *
  3. A directory identified by the JRE argument netbox.properties
  4. - *
  5. A netbox.properties file located in the karaf root directory
  6. - *
- * - * Partial copy and adaptation of org.onap.ccsdk.sli.adaptors.aai.AAIServiceProvider - */ public class NetboxProperties { private static final Logger LOG = LoggerFactory.getLogger(NetboxProperties.class); private static final String NETBOX_PROPERTY_FILE_NAME = "netbox.properties"; - private static final String MISSING_PROPERTY_FILE = - "Missing configuration properties resource for Netbox: " + NETBOX_PROPERTY_FILE_NAME; + private static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties"; + private static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR"; + private static final String NETBOX_URL_PROP = "org.onap.ccsdk.sli.adaptors.netbox.url"; private static final String NETBOX_API_KEY_PROP = "org.onap.ccsdk.sli.adaptors.netbox.apikey"; - private Set fileResolvers = new HashSet<>(); private Properties properties; public NetboxProperties() { - fileResolvers.add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable")); - fileResolvers.add(new BundleContextFileResolver("Using property file (2) from BundleContext property", - NetboxProperties.class)); - fileResolvers.add(new JREFileResolver("Using property file (3) from JRE argument", NetboxProperties.class)); - fileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this)); - loadProps(); } public String getHost() { - checkArgument(properties != null); return properties.getProperty(NETBOX_URL_PROP); } public String getApiKey() { - checkArgument(properties != null); return properties.getProperty(NETBOX_API_KEY_PROP); } - private void checkArgument(final boolean argument) { - if (!argument) { - LOG.info("Propety file {} was missing, trying to reload it", NETBOX_PROPERTY_FILE_NAME); - loadProps(); - if (properties == null) { - throw new IllegalArgumentException(MISSING_PROPERTY_FILE); - } - } - } - private void loadProps() { - // determines properties file as according to the priority described in the class header comment - final File propertiesFile = determinePropertiesFile(); - if (propertiesFile != null) { - try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) { - properties = new Properties(); - properties.load(fileInputStream); - } catch (final IOException e) { - String errorMsg = "Failed to load properties for file: " + propertiesFile.toString(); - LOG.error(errorMsg, new IpamException(errorMsg)); - } - } - } + properties = new Properties(); + // Try to load config from dir + final String ccsdkConfigDir = + System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR) + "/" + NETBOX_PROPERTY_FILE_NAME; + try (FileInputStream in = new FileInputStream(ccsdkConfigDir)) { + properties.load(in); + LOG.info("Loaded {} properties from file {}", properties.size(), ccsdkConfigDir); + } catch (Exception e) { + // Try to load config from jar + final Bundle bundle = FrameworkUtil.getBundle(NetboxProperties.class); + final BundleContext ctx = bundle.getBundleContext(); + final URL url = ctx.getBundle().getResource(NETBOX_PROPERTY_FILE_NAME); - private File determinePropertiesFile() { - for (final PropertiesFileResolver resolver : fileResolvers) { - final Optional fileOptional = resolver.resolveFile(NETBOX_PROPERTY_FILE_NAME); - if (fileOptional.isPresent()) { - final File file = fileOptional.get(); - LOG.info("{} {}", resolver.getSuccessfulResolutionMessage(), file.getPath()); - return file; + try (InputStream inputStream = url.openStream()) { + properties.load(inputStream); + LOG.info("Loaded {} properties from file {}", properties.size(), NETBOX_PROPERTY_FILE_NAME); + } catch (IOException e1) { + LOG.error("Failed to load properties for file: {} " + NETBOX_PROPERTY_FILE_NAME, e1); } } - - LOG.error(MISSING_PROPERTY_FILE, new IpamException(MISSING_PROPERTY_FILE)); - return null; } } diff --git a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java deleted file mode 100644 index be5aabfc2..000000000 --- a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2018 Bell Canada. - * - * 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. - */ -package org.onap.ccsdk.sli.adaptors.netbox.property; - -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.Appender; -import java.util.List; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; -import org.slf4j.LoggerFactory; - -@RunWith(MockitoJUnitRunner.class) -public class NetboxPropertiesTest { - - private NetboxProperties props; - - @Mock - private Appender appender; - @Captor - private ArgumentCaptor captor; - - @Before - public void setup() { - ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory - .getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); - logger.addAppender(appender); - } - - @Test - public void testMissingFile() { - props = new NetboxProperties(); - - verifyLogEntry( - "Missing configuration properties resource for Netbox: netbox.properties"); - } - - private void verifyLogEntry(String message) { - verify(appender, times(1)).doAppend(captor.capture()); - List allValues = captor.getAllValues(); - for (ILoggingEvent loggingEvent : allValues) { - Assert.assertTrue(loggingEvent.getFormattedMessage().contains(message)); - } - } -} \ No newline at end of file -- cgit 1.2.3-korg