From d3cd4dd5d5f300684be256eb1bf70d750a9a957e Mon Sep 17 00:00:00 2001 From: Rich Tabedzki Date: Fri, 5 Jan 2018 20:26:58 +0000 Subject: Generalization of CCSDK core/utils framework Changes made: * Created generalized version of core/utils/dblib as core/utils/common * Deprecated core/utils/dblib package Change-Id: I0992c43910278fbe254674d1e39d7e4fcad0a592 Issue-ID: CCSDK-168 Signed-off-by: Rich Tabedzki --- .../ccsdk/sli/core/utils/EnvVarFileResolver.java | 2 +- .../onap/ccsdk/sli/core/utils/JREFileResolver.java | 10 +-- .../utils/common/BundleContextFileResolver.java | 80 ++++++++++++++++++++++ .../core/utils/common/CoreDefaultFileResolver.java | 41 +++++++++++ .../utils/common/SdncConfigEnvVarFileResolver.java | 38 ++++++++++ .../core/utils/dblib/DblibDefaultFileResolver.java | 10 ++- .../core/utils/dblib/DblibEnvVarFileResolver.java | 8 +++ .../utils/common/BundleContexFileResolverTest.java | 16 +++++ .../utils/common/CoreDefaultFileResolverTest.java | 25 +++++++ .../common/SdncConfigEnvVarFileResolverTest.java | 24 +++++++ 10 files changed, 248 insertions(+), 6 deletions(-) mode change 100644 => 100755 utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java mode change 100644 => 100755 utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java create mode 100755 utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/BundleContextFileResolver.java create mode 100755 utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolver.java create mode 100755 utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolver.java mode change 100644 => 100755 utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java create mode 100644 utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/BundleContexFileResolverTest.java create mode 100644 utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolverTest.java create mode 100644 utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolverTest.java (limited to 'utils') diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java old mode 100644 new mode 100755 index 3e438d1a..5e87412a --- a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java @@ -27,7 +27,7 @@ import java.nio.file.Paths; import java.util.Optional; /** - * Resolves dblib properties files relative to the directory identified by the SDNC_CONFIG_DIR + * Resolves properties files relative to the directory identified by the SDNC_CONFIG_DIR * environment variable. */ public abstract class EnvVarFileResolver implements PropertiesFileResolver { diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java old mode 100644 new mode 100755 index 5cd6c360..844c6949 --- a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java @@ -23,13 +23,15 @@ package org.onap.ccsdk.sli.core.utils; import java.io.File; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.FileSystemNotFoundException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; import org.osgi.framework.FrameworkUtil; +import org.slf4j.LoggerFactory; /** - * Resolves dblib properties files relative to the directory identified by the JRE property + * Resolves project properties files relative to the directory identified by the JRE property * dblib.properties. */ public class JREFileResolver implements PropertiesFileResolver { @@ -37,7 +39,6 @@ public class JREFileResolver implements PropertiesFileResolver { /** * Key for JRE argument representing the configuration directory */ - private static final String DBLIB_JRE_PROPERTY_KEY = "dblib.properties"; private final String successMessage; private final Class clazz; @@ -55,14 +56,15 @@ public class JREFileResolver implements PropertiesFileResolver { @Override public Optional resolveFile(final String filename) { final URL jreArgumentUrl = FrameworkUtil.getBundle(this.clazz) - .getResource(DBLIB_JRE_PROPERTY_KEY); + .getResource(filename); try { if (jreArgumentUrl == null) { return Optional.empty(); } final Path dblibPath = Paths.get(jreArgumentUrl.toURI()); return Optional.of(dblibPath.resolve(filename).toFile()); - } catch(final URISyntaxException e) { + } catch(final URISyntaxException | FileSystemNotFoundException e) { + LoggerFactory.getLogger(this.getClass()).error("", e); return Optional.empty(); } } diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/BundleContextFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/BundleContextFileResolver.java new file mode 100755 index 00000000..9496d90e --- /dev/null +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/BundleContextFileResolver.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * onap + * ================================================================================ + * Copyright (C) 2016 - 2017 ONAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.core.utils.common; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; + +import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver; +import org.osgi.framework.FrameworkUtil; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Strings; + +/** + * Resolves properties files from runtime property value SDNC_CONFIG_DIR defined in the osgi properties. + */ +public class BundleContextFileResolver implements PropertiesFileResolver { + + /** + * Key for osgi variable representing the configuration directory + */ + private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR"; + + private final String successMessage; + private final Class clazz; + + public BundleContextFileResolver(final String successMessage, final Class clazz) { + this.successMessage = successMessage; + this.clazz = clazz; + } + + /** + * Parse a properties file location based on JRE argument + * + * @return an Optional File containing the location if it exists, or an empty Optional + */ + @Override + public Optional resolveFile(final String filename) { + if(FrameworkUtil.getBundle(clazz) == null) { + return Optional.empty(); + } else { + try { + final String pathProperty = FrameworkUtil.getBundle(this.clazz).getBundleContext().getProperty(SDNC_CONFIG_DIR_PROP_KEY); + if(Strings.isNullOrEmpty(pathProperty)) { + return Optional.empty(); + } + final Path dblibPath = Paths.get(pathProperty); + return Optional.of(dblibPath.resolve(filename).toFile()); + } catch(final Exception e) { + LoggerFactory.getLogger(this.getClass()).error("", e); + return Optional.empty(); + } + } + } + + @Override + public String getSuccessfulResolutionMessage() { + return this.successMessage; + } +} diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolver.java new file mode 100755 index 00000000..4d7e9026 --- /dev/null +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolver.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * onap + * ================================================================================ + * Copyright (C) 2016 - 2017 ONAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.core.utils.common; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.onap.ccsdk.sli.core.utils.DefaultFileResolver; + +/** + * Resolve properties file location based on the default directory name. + */ +public class CoreDefaultFileResolver extends DefaultFileResolver { + + /** + * Default path to look for the configuration directory + */ + private static final Path DEFAULT_DBLIB_PROP_DIR = Paths.get("/opt", "sdnc", "data", "properties"); + + public CoreDefaultFileResolver(final String successMessage) { + super(successMessage, DEFAULT_DBLIB_PROP_DIR); + } +} diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolver.java new file mode 100755 index 00000000..51b6134f --- /dev/null +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolver.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * onap + * ================================================================================ + * Copyright (C) 2016 - 2017 ONAP + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.core.utils.common; + +import org.onap.ccsdk.sli.core.utils.EnvVarFileResolver; + +/** + * Resolve properties file location based on the default directory name. + */ +public class SdncConfigEnvVarFileResolver extends EnvVarFileResolver { + + /** + * Key for environment variable representing the configuration directory + */ + private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR"; + + public SdncConfigEnvVarFileResolver(final String successMessage) { + super(successMessage, SDNC_CONFIG_DIR_PROP_KEY); + } +} diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java old mode 100644 new mode 100755 index 56b4ca1b..082bdf40 --- a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java @@ -4,12 +4,20 @@ import java.nio.file.Path; import java.nio.file.Paths; import org.onap.ccsdk.sli.core.utils.DefaultFileResolver; +/** + * Resolve properties file location based on the default directory name. + * + * @deprecated + * This class has been replaced by generic version of this class + * {@link #CoreDefaultFileResolver} in common package. + */ +@Deprecated public class DblibDefaultFileResolver extends DefaultFileResolver { /** * Default path to look for the configuration directory */ - private static final Path DEFAULT_DBLIB_PROP_DIR = Paths.get("opt", "sdnc", "data", "properties"); + private static final Path DEFAULT_DBLIB_PROP_DIR = Paths.get("/opt", "sdnc", "data", "properties"); public DblibDefaultFileResolver(final String successMessage) { super(successMessage, DEFAULT_DBLIB_PROP_DIR); diff --git a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java index 9eef4cee..959271cb 100644 --- a/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java +++ b/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java @@ -2,6 +2,14 @@ package org.onap.ccsdk.sli.core.utils.dblib; import org.onap.ccsdk.sli.core.utils.EnvVarFileResolver; +/** + * Resolve properties file location based on the default directory name. + * + * @deprecated + * This class has been replaced by generic version of this class + * {@link #SdncConfigEnvVarFileResolver} in common package. + */ +@Deprecated public class DblibEnvVarFileResolver extends EnvVarFileResolver { /** diff --git a/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/BundleContexFileResolverTest.java b/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/BundleContexFileResolverTest.java new file mode 100644 index 00000000..4ec5e1b4 --- /dev/null +++ b/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/BundleContexFileResolverTest.java @@ -0,0 +1,16 @@ +package org.onap.ccsdk.sli.core.utils.common; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver; + +public class BundleContexFileResolverTest { + + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new BundleContextFileResolver("success", BundleContexFileResolverTest.class); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file diff --git a/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolverTest.java b/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolverTest.java new file mode 100644 index 00000000..a4f8817f --- /dev/null +++ b/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/CoreDefaultFileResolverTest.java @@ -0,0 +1,25 @@ +package org.onap.ccsdk.sli.core.utils.common; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.Optional; +import org.junit.Test; +import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver; + +public class CoreDefaultFileResolverTest { + + @Test + public void resolveFile() throws Exception { + final PropertiesFileResolver resolver = new CoreDefaultFileResolver("success"); + final Optional file = resolver.resolveFile("doesnotexist.cfg"); + assertFalse(file.isPresent()); + } + + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new CoreDefaultFileResolver("success"); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file diff --git a/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolverTest.java b/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolverTest.java new file mode 100644 index 00000000..0f3536b7 --- /dev/null +++ b/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/SdncConfigEnvVarFileResolverTest.java @@ -0,0 +1,24 @@ +package org.onap.ccsdk.sli.core.utils.common; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.Optional; +import org.junit.Test; +import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver; + +public class SdncConfigEnvVarFileResolverTest { + @Test + public void resolveFile() throws Exception { + final PropertiesFileResolver resolver = new SdncConfigEnvVarFileResolver("success"); + final Optional file = resolver.resolveFile("doesnotexist.cfg"); + assertFalse(file.isPresent()); + } + + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new SdncConfigEnvVarFileResolver("success"); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file -- cgit 1.2.3-korg