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;
27 import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider;
28 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
29 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
30 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
31 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
32 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
33 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 import java.io.FileInputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.util.Optional;
42 import java.util.Properties;
43 import java.util.Vector;
46 * Responsible for determining the properties file to use and instantiating the
47 * <code>SqlResource</code> Service. The priority for properties file
48 * resolution is as follows:
51 * <li>A directory identified by the system environment variable
52 * <code>SDNC_CONFIG_DIR</code></li>
53 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
54 * <li>A directory identified by the JRE argument
55 * <code>sql-resource.properties</code></li>
56 * <li>A <code>sql-resource.properties</code> file located in the karaf root
60 public class SaltstackAdapterPropertiesProviderImpl implements SaltstackAdapterPropertiesProvider {
62 private static final Logger LOG = LoggerFactory.getLogger(SaltstackAdapterPropertiesProviderImpl.class);
65 * The name of the properties file for database configuration
67 private static final String SALTSTACKADAPTER_PROP_FILE_NAME = "saltstack-adapter.properties";
70 * A prioritized list of strategies for resolving sql-resource properties files.
72 private Vector<PropertiesFileResolver> saltstackAdapterPropertiesFileResolvers = new Vector<>();
75 * The configuration properties for the db connection.
77 private Properties properties;
80 * Set up the prioritized list of strategies for resolving dblib properties
83 public SaltstackAdapterPropertiesProviderImpl() {
84 saltstackAdapterPropertiesFileResolvers
85 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
86 saltstackAdapterPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
88 saltstackAdapterPropertiesFileResolvers.add(
89 new JREFileResolver("Using property file (3) from JRE argument", SaltstackAdapterPropertiesProviderImpl.class));
90 saltstackAdapterPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
92 // determines properties file as according to the priority described in the
93 // class header comment
94 final File propertiesFile = determinePropertiesFile(this);
95 if (propertiesFile != null) {
96 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
97 properties = new Properties();
98 properties.load(fileInputStream);
99 } catch (final IOException e) {
100 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
101 new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
105 // Try to read properties as resource
107 InputStream propStr = getClass().getResourceAsStream("/" + SALTSTACKADAPTER_PROP_FILE_NAME);
108 if (propStr != null) {
109 properties = new Properties();
111 properties.load(propStr);
113 } catch (IOException e) {
120 if (properties == null) {
121 reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
122 "Missing configuration properties resource(3): " + SALTSTACKADAPTER_PROP_FILE_NAME));
124 LOG.info("Defaulting org.onap.appc.adapter.saltstack.clientType to NONE");
126 properties = new Properties();
127 properties.setProperty("org.onap.appc.adapter.saltstack.clientType", "NONE");
132 * Reports the method chosen for properties resolution to the
133 * <code>Logger</code>.
135 * @param message Some user friendly message
136 * @param fileOptional The file location of the chosen properties file
137 * @return the file location of the chosen properties file
139 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
140 if (fileOptional.isPresent()) {
141 final File file = fileOptional.get();
142 LOG.info("{} {}", message, file.getPath());
149 * Reports fatal errors. This is the case in which no properties file could be
152 * @param message An appropriate fatal error message
153 * @param configurationException An exception describing what went wrong during resolution
155 private static void reportFailure(final String message, final ConfigurationException configurationException) {
157 LOG.error("{}", message, configurationException);
161 * Extract svclogic config properties.
163 * @return the svclogic config properties
165 public Properties getProperties() {
170 * Determines the sql-resource properties file to use based on the following priority:
172 * <li>A directory identified by the system environment variable
173 * <code>SDNC_CONFIG_DIR</code></li>
174 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
175 * <li>A directory identified by the JRE argument
176 * <code>sql-resource.properties</code></li>
177 * <li>A <code>sql-resource.properties</code> file located in the karaf root
181 File determinePropertiesFile(final SaltstackAdapterPropertiesProviderImpl resourceProvider) {
183 for (final PropertiesFileResolver sliPropertiesFileResolver : saltstackAdapterPropertiesFileResolvers) {
184 final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SALTSTACKADAPTER_PROP_FILE_NAME);
185 if (fileOptional.isPresent()) {
186 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);