2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Samsung Electronics. All rights reserved.
6 * ================================================================================
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 * ============LICENSE_END=========================================================
25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Optional;
32 import java.util.Properties;
33 import java.util.Vector;
34 import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider;
35 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
36 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
37 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
38 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
39 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * THIS CLASS IS A COPY OF {@link SaltstackAdapterPropertiesProviderImpl} WITH REMOVED OSGi DEPENDENCIES
46 public class SaltstackAdapterPropertiesProviderImplLighty implements SaltstackAdapterPropertiesProvider {
48 private static final Logger LOG = LoggerFactory.getLogger(SaltstackAdapterPropertiesProviderImplLighty.class);
51 * The name of the properties file for database configuration
53 private static final String SALTSTACKADAPTER_PROP_FILE_NAME = "saltstack-adapter.properties";
56 * A prioritized list of strategies for resolving sql-resource properties files.
58 private Vector<PropertiesFileResolver> saltstackAdapterPropertiesFileResolvers = new Vector<>();
61 * The configuration properties for the db connection.
63 private Properties properties;
66 * Set up the prioritized list of strategies for resolving dblib properties
69 public SaltstackAdapterPropertiesProviderImplLighty() {
70 saltstackAdapterPropertiesFileResolvers
71 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
72 saltstackAdapterPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
73 saltstackAdapterPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
75 // determines properties file as according to the priority described in the
76 // class header comment
77 final File propertiesFile = determinePropertiesFile(this);
78 if (propertiesFile != null) {
79 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
80 properties = new Properties();
81 properties.load(fileInputStream);
82 } catch (final IOException e) {
83 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
84 new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
88 // Try to read properties as resource
90 InputStream propStr = getClass().getResourceAsStream("/" + SALTSTACKADAPTER_PROP_FILE_NAME);
91 if (propStr != null) {
92 properties = new Properties();
94 properties.load(propStr);
96 } catch (IOException e) {
103 if (properties == null) {
104 reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
105 "Missing configuration properties resource(3): " + SALTSTACKADAPTER_PROP_FILE_NAME));
107 LOG.info("Defaulting org.onap.appc.adapter.saltstack.clientType to NONE");
109 properties = new Properties();
110 properties.setProperty("org.onap.appc.adapter.saltstack.clientType", "NONE");
115 * Reports the method chosen for properties resolution to the
116 * <code>Logger</code>.
118 * @param message Some user friendly message
119 * @param fileOptional The file location of the chosen properties file
120 * @return the file location of the chosen properties file
122 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
123 if (fileOptional.isPresent()) {
124 final File file = fileOptional.get();
125 LOG.info("{} {}", message, file.getPath());
132 * Reports fatal errors. This is the case in which no properties file could be
135 * @param message An appropriate fatal error message
136 * @param configurationException An exception describing what went wrong during resolution
138 private static void reportFailure(final String message, final ConfigurationException configurationException) {
140 LOG.error("{}", message, configurationException);
144 * Extract svclogic config properties.
146 * @return the svclogic config properties
148 public Properties getProperties() {
153 * Determines the sql-resource properties file to use based on the following priority:
155 * <li>A directory identified by the system environment variable
156 * <code>SDNC_CONFIG_DIR</code></li>
157 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
158 * <li>A directory identified by the JRE argument
159 * <code>sql-resource.properties</code></li>
160 * <li>A <code>sql-resource.properties</code> file located in the karaf root
164 File determinePropertiesFile(final SaltstackAdapterPropertiesProviderImplLighty resourceProvider) {
166 for (final PropertiesFileResolver sliPropertiesFileResolver : saltstackAdapterPropertiesFileResolvers) {
167 final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SALTSTACKADAPTER_PROP_FILE_NAME);
168 if (fileOptional.isPresent()) {
169 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);