summaryrefslogtreecommitdiffstats
path: root/grpc-resource/provider
diff options
context:
space:
mode:
authorAlexis de Talhouët <adetalhouet89@gmail.com>2019-02-09 15:01:10 -0500
committerAlexis de Talhouët <adetalhouet89@gmail.com>2019-02-09 20:31:02 +0000
commited7162521270d33da1bdc57286b23c64e23e5b0f (patch)
tree3edf0984b5478a491c57b5d3583d0717db89a629 /grpc-resource/provider
parent9fc8417d0f785a3fa38ed7c34d7435125f920cda (diff)
Rework property file load logic for grpc & netbox
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 <adetalhouet89@gmail.com>
Diffstat (limited to 'grpc-resource/provider')
-rw-r--r--grpc-resource/provider/pom.xml6
-rw-r--r--grpc-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/grpc/GrpcProperties.java88
-rw-r--r--[-rwxr-xr-x]grpc-resource/provider/src/main/resources/grpc.properties6
3 files changed, 35 insertions, 65 deletions
diff --git a/grpc-resource/provider/pom.xml b/grpc-resource/provider/pom.xml
index 9c0a4a0d9..c567bba13 100644
--- a/grpc-resource/provider/pom.xml
+++ b/grpc-resource/provider/pom.xml
@@ -106,6 +106,12 @@
<version>${grpc.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
diff --git a/grpc-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/grpc/GrpcProperties.java b/grpc-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/grpc/GrpcProperties.java
index fef1a5963..96775f379 100644
--- a/grpc-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/grpc/GrpcProperties.java
+++ b/grpc-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/grpc/GrpcProperties.java
@@ -15,100 +15,62 @@
*/
package org.onap.ccsdk.sli.adaptors.grpc;
-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.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.
- *
- * <ol>
- * <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
- * <li>A directory identified by the JRE argument <code>grpc.properties</code></li>
- * <li>A <code>grpc.properties</code> file located in the karaf root directory</li>
- * </ol>
- *
- * Partial copy and adaptation of org.onap.ccsdk.sli.adaptors.aai.AAIServiceProvider
- */
public class GrpcProperties {
private static final Logger LOG = LoggerFactory.getLogger(GrpcProperties.class);
private static final String GRPC_PROPERTY_FILE_NAME = "grpc.properties";
- private static final String MISSING_PROPERTY_FILE =
- "Missing configuration properties resource for GRPC: " + GRPC_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 BLUEPRINT_PROCESSOR_URL_PROP = "org.onap.ccsdk.sli.adaptors.grpc.blueprint.processor.url";
private static final String BLUEPRINT_PROCESSOR_PORT_PROP = "org.onap.ccsdk.sli.adaptors.grpc.blueprint.processor.port";
- private Set<PropertiesFileResolver> fileResolvers = new HashSet<>();
private Properties properties;
public GrpcProperties() {
- fileResolvers.add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
- fileResolvers.add(new BundleContextFileResolver("Using property file (2) from BundleContext property",
- GrpcProperties.class));
- fileResolvers.add(new JREFileResolver("Using property file (3) from JRE argument", GrpcProperties.class));
- fileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
-
loadProps();
}
public String getUrl() {
- checkArgument(properties != null);
return properties.getProperty(BLUEPRINT_PROCESSOR_URL_PROP);
}
public int getPort() {
- checkArgument(properties != null);
return Integer.parseInt(properties.getProperty(BLUEPRINT_PROCESSOR_PORT_PROP));
}
- private void checkArgument(final boolean argument) {
- if (!argument) {
- LOG.info("Propety file {} was missing, trying to reload it", GRPC_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, errorMsg);
- }
- }
- }
+ properties = new Properties();
+ // Try to load config from dir
+ final String ccsdkConfigDir =
+ System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR) + "/" + GRPC_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(GrpcProperties.class);
+ final BundleContext ctx = bundle.getBundleContext();
+ final URL url = ctx.getBundle().getResource(GRPC_PROPERTY_FILE_NAME);
- private File determinePropertiesFile() {
- for (final PropertiesFileResolver resolver : fileResolvers) {
- final Optional<File> fileOptional = resolver.resolveFile(GRPC_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(), GRPC_PROPERTY_FILE_NAME);
+ } catch (IOException e1) {
+ LOG.error("Failed to load properties for file: {} " + GRPC_PROPERTY_FILE_NAME, e1);
}
}
-
- LOG.error(MISSING_PROPERTY_FILE);
- return null;
}
}
diff --git a/grpc-resource/provider/src/main/resources/grpc.properties b/grpc-resource/provider/src/main/resources/grpc.properties
index 2d4e795e6..650910eb9 100755..100644
--- a/grpc-resource/provider/src/main/resources/grpc.properties
+++ b/grpc-resource/provider/src/main/resources/grpc.properties
@@ -1,4 +1,5 @@
-# Copyright (C) 2019 Bell Canada
+#
+# Copyright (C) 2019 Bell Canada.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -11,5 +12,6 @@
# 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.
+#
org.onap.ccsdk.sli.adaptors.grpc.blueprint.processor.url=blueprint-processor
-org.onap.ccsdk.sli.adaptors.grpc.blueprint.processor.port=9111 \ No newline at end of file
+org.onap.ccsdk.sli.adaptors.grpc.blueprint.processor.port=9111