From 03235aa6cc35871630ee3e09fe467eca79ce315e Mon Sep 17 00:00:00 2001 From: Ryan Goulding Date: Tue, 19 Sep 2017 11:14:41 -0400 Subject: Abstract utility classes for re-use When looking at the existing uses of BundleActivator(s), it became immediately clear that other parts of the code have a strategy based properties file resolution very similar to those abstracted by dblib. This change aggregates a separate utils bundle for this functionality, which is further abstracted for potential reuse with other use cases. The next use case is in the sli bundle, which will be handled in a follow-up patch. Issue-Id: SDNC-54 Change-Id: Ie4d4bb679617474b1983e6044c08cca1742b9893 Signed-off-by: Ryan Goulding --- dblib/provider/pom.xml | 5 ++ .../sli/core/dblib/DBLIBResourceProvider.java | 20 +++--- .../DblibDefaultFileResolver.java | 63 ------------------ .../DblibEnvVarFileResolver.java | 69 -------------------- .../DblibJREFileResolver.java | 73 --------------------- .../DblibKarafRootFileResolver.java | 64 ------------------- .../DblibPropertiesFileResolver.java | 45 ------------- .../DblibDefaultFileResolverTest.java | 24 ------- .../DblibEnvVarFileResolverTest.java | 23 ------- .../DblibJREFileResolverTest.java | 15 ----- .../DblibKarafRootFileResolverTest.java | 14 ---- pom.xml | 1 + utils/pom.xml | 56 ++++++++++++++++ .../ccsdk/sli/core/utils/DefaultFileResolver.java | 61 ++++++++++++++++++ .../ccsdk/sli/core/utils/EnvVarFileResolver.java | 70 ++++++++++++++++++++ .../onap/ccsdk/sli/core/utils/JREFileResolver.java | 74 ++++++++++++++++++++++ .../sli/core/utils/KarafRootFileResolver.java | 63 ++++++++++++++++++ .../sli/core/utils/PropertiesFileResolver.java | 45 +++++++++++++ .../core/utils/dblib/DblibDefaultFileResolver.java | 17 +++++ .../core/utils/dblib/DblibEnvVarFileResolver.java | 15 +++++ .../ccsdk/sli/core/utils/JREFileResolverTest.java | 15 +++++ .../sli/core/utils/KarafRootFileResolverTest.java | 14 ++++ .../utils/dblib/DblibDefaultFileResolverTest.java | 25 ++++++++ .../utils/dblib/DblibEnvVarFileResolverTest.java | 24 +++++++ 24 files changed, 495 insertions(+), 400 deletions(-) delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolver.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolver.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolver.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolver.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibPropertiesFileResolver.java delete mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolverTest.java delete mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolverTest.java delete mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolverTest.java delete mode 100644 dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolverTest.java create mode 100644 utils/pom.xml create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/DefaultFileResolver.java create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolver.java create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/PropertiesFileResolver.java create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java create mode 100644 utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java create mode 100644 utils/src/test/java/org/onap/ccsdk/sli/core/utils/JREFileResolverTest.java create mode 100644 utils/src/test/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolverTest.java create mode 100644 utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolverTest.java create mode 100644 utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolverTest.java diff --git a/dblib/provider/pom.xml b/dblib/provider/pom.xml index 8ff10941..ad1cc9d2 100755 --- a/dblib/provider/pom.xml +++ b/dblib/provider/pom.xml @@ -56,6 +56,11 @@ com.google.guava guava + + org.onap.ccsdk.sli.core + utils + ${project.version} + diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBLIBResourceProvider.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBLIBResourceProvider.java index 062cd840..8e0b534f 100644 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBLIBResourceProvider.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBLIBResourceProvider.java @@ -27,11 +27,11 @@ import java.util.Optional; import java.util.Properties; import java.util.Vector; -import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibDefaultFileResolver; -import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibEnvVarFileResolver; -import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibJREFileResolver; -import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibKarafRootFileResolver; -import org.onap.ccsdk.sli.core.dblib.propertiesfileresolver.DblibPropertiesFileResolver; +import org.onap.ccsdk.sli.core.utils.dblib.DblibDefaultFileResolver; +import org.onap.ccsdk.sli.core.utils.dblib.DblibEnvVarFileResolver; +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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,7 +58,7 @@ public class DBLIBResourceProvider { /** * A prioritized list of strategies for resolving dblib properties files. */ - private Vector dblibPropertiesFileResolvers = new Vector(); + private Vector dblibPropertiesFileResolvers = new Vector(); /** * The configuration properties for the db connection. @@ -75,10 +75,10 @@ public class DBLIBResourceProvider { dblibPropertiesFileResolvers.add(new DblibDefaultFileResolver( "Using property file (1) from default directory" )); - dblibPropertiesFileResolvers.add(new DblibJREFileResolver( - "Using property file (2) from JRE argument" + dblibPropertiesFileResolvers.add(new JREFileResolver( + "Using property file (2) from JRE argument", DBLIBResourceProvider.class )); - dblibPropertiesFileResolvers.add(new DblibKarafRootFileResolver( + dblibPropertiesFileResolvers.add(new KarafRootFileResolver( "Using property file (4) from karaf root", this)); // determines properties file as according to the priority described in the class header comment @@ -141,7 +141,7 @@ public class DBLIBResourceProvider { */ File determinePropertiesFile(final DBLIBResourceProvider dblibResourceProvider) { - for (final DblibPropertiesFileResolver dblibPropertiesFileResolver : dblibPropertiesFileResolvers) { + for (final PropertiesFileResolver dblibPropertiesFileResolver : dblibPropertiesFileResolvers) { final Optional fileOptional = dblibPropertiesFileResolver.resolveFile(DBLIB_PROP_FILE_NAME); if (fileOptional.isPresent()) { return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional); diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolver.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolver.java deleted file mode 100644 index a7797d9f..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolver.java +++ /dev/null @@ -1,63 +0,0 @@ -/*- - * ============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.dblib.propertiesfileresolver; - -import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Optional; - -/** - * Resolves dblib properties files relative to the default file path. In Unix, this is represented by: - * /opt/sdnc/data/properties - */ -public class DblibDefaultFileResolver implements DblibPropertiesFileResolver { - - /** - * Default path to look for the configuration directory - */ - private static final Path DEFAULT_DBLIB_PROP_DIR = Paths.get("opt", "sdnc", "data", "properties"); - - private final String successMessage; - - public DblibDefaultFileResolver(final String successMessage) { - this.successMessage = successMessage; - } - - /** - * Parse a properties file location based on the default properties location - * - * @return an Optional File containing the location if it exists, or an empty Optional - */ - @Override - public Optional resolveFile(final String dblibFileName) { - final File fileFromDefaultDblibDir = DEFAULT_DBLIB_PROP_DIR.resolve(dblibFileName).toFile(); - if (fileFromDefaultDblibDir.exists()) { - Optional.of(fileFromDefaultDblibDir); - } - return Optional.empty(); - } - - @Override - public String getSuccessfulResolutionMessage() { - return this.successMessage; - } -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolver.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolver.java deleted file mode 100644 index 0b69109f..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolver.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============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.dblib.propertiesfileresolver; - -import com.google.common.base.Strings; - -import java.io.File; -import java.nio.file.Paths; -import java.util.Optional; - -/** - * Resolves dblib properties files relative to the directory identified by the SDNC_CONFIG_DIR - * environment variable. - */ -public class DblibEnvVarFileResolver implements DblibPropertiesFileResolver { - - /** - * Key for environment variable representing the configuration directory - */ - private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR"; - - private final String successMessage; - - public DblibEnvVarFileResolver(final String successMessage) { - this.successMessage = successMessage; - } - - /** - * Parse a properties file location based on System environment variable - * - * @return an Optional File containing the location if it exists, or an empty Optional - */ - @Override - public Optional resolveFile(final String dblibFileName) { - // attempt to resolve the property directory from the corresponding environment variable - final String propDirectoryFromEnvVariable = System.getenv(SDNC_CONFIG_DIR_PROP_KEY); - final File fileFromEnvVariable; - if (!Strings.isNullOrEmpty(propDirectoryFromEnvVariable)) { - fileFromEnvVariable = Paths.get(propDirectoryFromEnvVariable).resolve(dblibFileName).toFile(); - if(fileFromEnvVariable.exists()) { - return Optional.of(fileFromEnvVariable); - } - } - return Optional.empty(); - } - - @Override - public String getSuccessfulResolutionMessage() { - return this.successMessage; - } -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolver.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolver.java deleted file mode 100644 index 673ccbf0..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolver.java +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * ============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.dblib.propertiesfileresolver; - -import java.io.File; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Optional; -import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider; -import org.osgi.framework.FrameworkUtil; - -/** - * Resolves dblib properties files relative to the directory identified by the JRE property - * dblib.properties. - */ -public class DblibJREFileResolver implements DblibPropertiesFileResolver { - - /** - * Key for JRE argument representing the configuration directory - */ - private static final String DBLIB_JRE_PROPERTY_KEY = "dblib.properties"; - - private final String successMessage; - - public DblibJREFileResolver(final String successMessage) { - this.successMessage = successMessage; - } - - /** - * 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 dblibFileName) { - final URL jreArgumentUrl = FrameworkUtil.getBundle(DBLIBResourceProvider.class) - .getResource(DBLIB_JRE_PROPERTY_KEY); - try { - if (jreArgumentUrl == null) { - return Optional.empty(); - } - final Path dblibPath = Paths.get(jreArgumentUrl.toURI()); - return Optional.of(dblibPath.resolve(dblibFileName).toFile()); - } catch(final URISyntaxException e) { - return Optional.empty(); - } - } - - @Override - public String getSuccessfulResolutionMessage() { - return this.successMessage; - } -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolver.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolver.java deleted file mode 100644 index d2b164e3..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolver.java +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * ============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.dblib.propertiesfileresolver; - -import java.io.File; -import java.net.URL; -import java.util.Optional; -import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider; - -/** - * Resolves dblib properties files relative to the karaf root directory. - */ -public class DblibKarafRootFileResolver implements DblibPropertiesFileResolver { - - final DBLIBResourceProvider dblibResourceProvider; - - private final String successMessage; - - public DblibKarafRootFileResolver(final String successMessage, final DBLIBResourceProvider dblibResourceProvider) { - this.successMessage = successMessage; - this.dblibResourceProvider = dblibResourceProvider; - } - - /** - * Parse a properties file location relative to the karaf root - * - * @return an Optional File containing the location if it exists, or an empty Optional - */ - @Override - public Optional resolveFile(final String dblibFileName) { - final URL fromKarafRoot = dblibResourceProvider.getClass().getResource(dblibFileName); - if (fromKarafRoot != null) { - final File propertiesFile = new File(fromKarafRoot.getFile()); - if (propertiesFile.exists()) { - return Optional.of(propertiesFile); - } - return Optional.empty(); - } - return Optional.empty(); - } - - @Override - public String getSuccessfulResolutionMessage() { - return this.successMessage; - } -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibPropertiesFileResolver.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibPropertiesFileResolver.java deleted file mode 100644 index 97ab08ac..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibPropertiesFileResolver.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============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.dblib.propertiesfileresolver; - -import java.io.File; -import java.util.Optional; - -/** - * Strategy for resolving dblib properties. - */ -public interface DblibPropertiesFileResolver { - - /** - * Resolve dblib properties file. - * - * @param dblibFileName the name of the file to look for at the specific location. - * @return An optional File or empty. - */ - Optional resolveFile(final String dblibFileName); - - /** - * A success message, used only for logging now. - * - * @return a success message, used only for logging now. - */ - String getSuccessfulResolutionMessage(); -} diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolverTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolverTest.java deleted file mode 100644 index 41011e55..00000000 --- a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibDefaultFileResolverTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.onap.ccsdk.sli.core.dblib.propertiesfileresolver; - -import static org.junit.Assert.*; - -import java.io.File; -import java.util.Optional; -import org.junit.Test; - -public class DblibDefaultFileResolverTest { - - @Test - public void resolveFile() throws Exception { - final DblibPropertiesFileResolver resolver = new DblibDefaultFileResolver("success"); - final Optional file = resolver.resolveFile("doesnotexist.cfg"); - assertFalse(file.isPresent()); - } - - @Test - public void getSuccessfulResolutionMessage() throws Exception { - final DblibPropertiesFileResolver resolver = new DblibDefaultFileResolver("success"); - assertEquals("success", resolver.getSuccessfulResolutionMessage()); - } - -} \ No newline at end of file diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolverTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolverTest.java deleted file mode 100644 index 77589893..00000000 --- a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibEnvVarFileResolverTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.onap.ccsdk.sli.core.dblib.propertiesfileresolver; - -import static org.junit.Assert.*; - -import java.io.File; -import java.util.Optional; -import org.junit.Test; - -public class DblibEnvVarFileResolverTest { - @Test - public void resolveFile() throws Exception { - final DblibPropertiesFileResolver resolver = new DblibEnvVarFileResolver("success"); - final Optional file = resolver.resolveFile("doesnotexist.cfg"); - assertFalse(file.isPresent()); - } - - @Test - public void getSuccessfulResolutionMessage() throws Exception { - final DblibPropertiesFileResolver resolver = new DblibEnvVarFileResolver("success"); - assertEquals("success", resolver.getSuccessfulResolutionMessage()); - } - -} \ No newline at end of file diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolverTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolverTest.java deleted file mode 100644 index 65ebfbae..00000000 --- a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibJREFileResolverTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.onap.ccsdk.sli.core.dblib.propertiesfileresolver; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class DblibJREFileResolverTest { - - @Test - public void getSuccessfulResolutionMessage() throws Exception { - final DblibPropertiesFileResolver resolver = new DblibJREFileResolver("success"); - assertEquals("success", resolver.getSuccessfulResolutionMessage()); - } - -} \ No newline at end of file diff --git a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolverTest.java b/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolverTest.java deleted file mode 100644 index 0a032e8b..00000000 --- a/dblib/provider/src/test/java/org/onap/ccsdk/sli/core/dblib/propertiesfileresolver/DblibKarafRootFileResolverTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.onap.ccsdk.sli.core.dblib.propertiesfileresolver; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class DblibKarafRootFileResolverTest { - @Test - public void getSuccessfulResolutionMessage() throws Exception { - final DblibPropertiesFileResolver resolver = new DblibKarafRootFileResolver("success", null); - assertEquals("success", resolver.getSuccessfulResolutionMessage()); - } - -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1c6cd4ae..a3691810 100755 --- a/pom.xml +++ b/pom.xml @@ -114,6 +114,7 @@ filters sliPluginUtils sliapi + utils ONAP diff --git a/utils/pom.xml b/utils/pom.xml new file mode 100644 index 00000000..93326a9d --- /dev/null +++ b/utils/pom.xml @@ -0,0 +1,56 @@ + + + + + org.onap.ccsdk.sli.core + ccsdk-sli-core + 0.1.2-SNAPSHOT + + + 4.0.0 + bundle + utils + SLI Core Utilities Package + + + The SLI Core Utilities Package provides common functionality for setting up SLI connectivity. + + + + + com.google.guava + guava + + + equinoxSDK381 + org.eclipse.osgi + ${equinox.osgi.version} + + + + + org.mockito + mockito-core + test + + + junit + junit + test + + + + + + + org.apache.felix + maven-bundle-plugin + ${bundle.plugin.version} + + + + + Inocybe Technologies and Others + + diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/DefaultFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/DefaultFileResolver.java new file mode 100644 index 00000000..8938aa6e --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/DefaultFileResolver.java @@ -0,0 +1,61 @@ +/*- + * ============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; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; + +/** + * Resolves dblib properties files relative to the default file path. In Unix, this is represented by: + * /opt/sdnc/data/properties + */ +public abstract class DefaultFileResolver implements PropertiesFileResolver { + + private final String successMessage; + + private final Path propertyPath; + + public DefaultFileResolver(final String successMessage, final Path propertyPath) { + this.successMessage = successMessage; + this.propertyPath = propertyPath; + } + + /** + * Parse a properties file location based on the default properties location + * + * @return an Optional File containing the location if it exists, or an empty Optional + */ + @Override + public Optional resolveFile(final String filename) { + final File fileFromDefaultDblibDir = propertyPath.resolve(filename).toFile(); + if (fileFromDefaultDblibDir.exists()) { + return Optional.of(fileFromDefaultDblibDir); + } + return Optional.empty(); + } + + @Override + public String getSuccessfulResolutionMessage() { + return this.successMessage; + } +} diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java new file mode 100644 index 00000000..3e438d1a --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/EnvVarFileResolver.java @@ -0,0 +1,70 @@ +/*- + * ============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; + +import com.google.common.base.Strings; + +import java.io.File; +import java.nio.file.Paths; +import java.util.Optional; + +/** + * Resolves dblib properties files relative to the directory identified by the SDNC_CONFIG_DIR + * environment variable. + */ +public abstract class EnvVarFileResolver implements PropertiesFileResolver { + + /** + * Key for environment variable representing the configuration directory + */ + private final String propertyKey; + + private final String successMessage; + + public EnvVarFileResolver(final String successMessage, final String propertyKey) { + this.successMessage = successMessage; + this.propertyKey = propertyKey; + } + + /** + * Parse a properties file location based on System environment variable + * + * @return an Optional File containing the location if it exists, or an empty Optional + */ + @Override + public Optional resolveFile(final String filename) { + // attempt to resolve the property directory from the corresponding environment variable + final String propDirectoryFromEnvVariable = System.getenv(propertyKey); + final File fileFromEnvVariable; + if (!Strings.isNullOrEmpty(propDirectoryFromEnvVariable)) { + fileFromEnvVariable = Paths.get(propDirectoryFromEnvVariable).resolve(filename).toFile(); + if(fileFromEnvVariable.exists()) { + return Optional.of(fileFromEnvVariable); + } + } + return Optional.empty(); + } + + @Override + public String getSuccessfulResolutionMessage() { + return this.successMessage; + } +} diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java new file mode 100644 index 00000000..5cd6c360 --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/JREFileResolver.java @@ -0,0 +1,74 @@ +/*- + * ============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; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; +import org.osgi.framework.FrameworkUtil; + +/** + * Resolves dblib properties files relative to the directory identified by the JRE property + * dblib.properties. + */ +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; + + public JREFileResolver(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) { + final URL jreArgumentUrl = FrameworkUtil.getBundle(this.clazz) + .getResource(DBLIB_JRE_PROPERTY_KEY); + 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) { + return Optional.empty(); + } + } + + @Override + public String getSuccessfulResolutionMessage() { + return this.successMessage; + } +} diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolver.java new file mode 100644 index 00000000..0cb75450 --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolver.java @@ -0,0 +1,63 @@ +/*- + * ============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; + +import java.io.File; +import java.net.URL; +import java.util.Optional; + +/** + * Resolves dblib properties files relative to the karaf root directory. + */ +public class KarafRootFileResolver implements PropertiesFileResolver { + + final Object provider; + + private final String successMessage; + + public KarafRootFileResolver(final String successMessage, final Object provider) { + this.successMessage = successMessage; + this.provider = provider; + } + + /** + * Parse a properties file location relative to the karaf root + * + * @return an Optional File containing the location if it exists, or an empty Optional + */ + @Override + public Optional resolveFile(final String filename) { + final URL fromKarafRoot = provider.getClass().getResource(filename); + if (fromKarafRoot != null) { + final File propertiesFile = new File(fromKarafRoot.getFile()); + if (propertiesFile.exists()) { + return Optional.of(propertiesFile); + } + return Optional.empty(); + } + return Optional.empty(); + } + + @Override + public String getSuccessfulResolutionMessage() { + return this.successMessage; + } +} diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/PropertiesFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/PropertiesFileResolver.java new file mode 100644 index 00000000..bfb417dc --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/PropertiesFileResolver.java @@ -0,0 +1,45 @@ +/*- + * ============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; + +import java.io.File; +import java.util.Optional; + +/** + * Strategy for resolving dblib properties. + */ +public interface PropertiesFileResolver { + + /** + * Resolve dblib properties file. + * + * @param filename the name of the file to look for at the specific location. + * @return An optional File or empty. + */ + Optional resolveFile(final String filename); + + /** + * A success message, used only for logging now. + * + * @return a success message, used only for logging now. + */ + String getSuccessfulResolutionMessage(); +} diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java new file mode 100644 index 00000000..56b4ca1b --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolver.java @@ -0,0 +1,17 @@ +package org.onap.ccsdk.sli.core.utils.dblib; + +import java.nio.file.Path; +import java.nio.file.Paths; +import org.onap.ccsdk.sli.core.utils.DefaultFileResolver; + +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"); + + public DblibDefaultFileResolver(final String successMessage) { + super(successMessage, DEFAULT_DBLIB_PROP_DIR); + } +} diff --git a/utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java new file mode 100644 index 00000000..9eef4cee --- /dev/null +++ b/utils/src/main/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolver.java @@ -0,0 +1,15 @@ +package org.onap.ccsdk.sli.core.utils.dblib; + +import org.onap.ccsdk.sli.core.utils.EnvVarFileResolver; + +public class DblibEnvVarFileResolver extends EnvVarFileResolver { + + /** + * Key for environment variable representing the configuration directory + */ + private static final String SDNC_CONFIG_DIR_PROP_KEY = "SDNC_CONFIG_DIR"; + + public DblibEnvVarFileResolver(final String successMessage) { + super(successMessage, SDNC_CONFIG_DIR_PROP_KEY); + } +} diff --git a/utils/src/test/java/org/onap/ccsdk/sli/core/utils/JREFileResolverTest.java b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/JREFileResolverTest.java new file mode 100644 index 00000000..e5051d65 --- /dev/null +++ b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/JREFileResolverTest.java @@ -0,0 +1,15 @@ +package org.onap.ccsdk.sli.core.utils; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class JREFileResolverTest { + + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new JREFileResolver("success", JREFileResolverTest.class); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file diff --git a/utils/src/test/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolverTest.java b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolverTest.java new file mode 100644 index 00000000..5e407daf --- /dev/null +++ b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/KarafRootFileResolverTest.java @@ -0,0 +1,14 @@ +package org.onap.ccsdk.sli.core.utils; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class KarafRootFileResolverTest { + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new KarafRootFileResolver("success", null); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file diff --git a/utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolverTest.java b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolverTest.java new file mode 100644 index 00000000..4b28d449 --- /dev/null +++ b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibDefaultFileResolverTest.java @@ -0,0 +1,25 @@ +package org.onap.ccsdk.sli.core.utils.dblib; + +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 DblibDefaultFileResolverTest { + + @Test + public void resolveFile() throws Exception { + final PropertiesFileResolver resolver = new DblibDefaultFileResolver("success"); + final Optional file = resolver.resolveFile("doesnotexist.cfg"); + assertFalse(file.isPresent()); + } + + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new DblibDefaultFileResolver("success"); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file diff --git a/utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolverTest.java b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolverTest.java new file mode 100644 index 00000000..bae4168d --- /dev/null +++ b/utils/src/test/java/org/onap/ccsdk/sli/core/utils/dblib/DblibEnvVarFileResolverTest.java @@ -0,0 +1,24 @@ +package org.onap.ccsdk.sli.core.utils.dblib; + +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 DblibEnvVarFileResolverTest { + @Test + public void resolveFile() throws Exception { + final PropertiesFileResolver resolver = new DblibEnvVarFileResolver("success"); + final Optional file = resolver.resolveFile("doesnotexist.cfg"); + assertFalse(file.isPresent()); + } + + @Test + public void getSuccessfulResolutionMessage() throws Exception { + final PropertiesFileResolver resolver = new DblibEnvVarFileResolver("success"); + assertEquals("success", resolver.getSuccessfulResolutionMessage()); + } + +} \ No newline at end of file -- cgit 1.2.3-korg