a4156558ec874c32cb52f46d7ceb7904652a3b3b
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / impl / SaltstackAdapterPropertiesProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
22
23 import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider;
24 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
25 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
26 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
27 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
28 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
29 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.Optional;
38 import java.util.Properties;
39 import java.util.Vector;
40
41 /**
42  * Responsible for determining the properties file to use and instantiating the
43  * <code>SqlResource</code> Service. The priority for properties file
44  * resolution is as follows:
45  * <p>
46  * <ol>
47  * <li>A directory identified by the system environment variable
48  * <code>SDNC_CONFIG_DIR</code></li>
49  * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
50  * <li>A directory identified by the JRE argument
51  * <code>sql-resource.properties</code></li>
52  * <li>A <code>sql-resource.properties</code> file located in the karaf root
53  * directory</li>
54  * </ol>
55  */
56 public class SaltstackAdapterPropertiesProviderImpl implements SaltstackAdapterPropertiesProvider {
57
58     private static final Logger LOG = LoggerFactory.getLogger(SaltstackAdapterPropertiesProviderImpl.class);
59
60     /**
61      * The name of the properties file for database configuration
62      */
63     private static final String SALTSTACKADAPTER_PROP_FILE_NAME = "saltstack-adapter.properties";
64
65     /**
66      * A prioritized list of strategies for resolving sql-resource properties files.
67      */
68     private Vector<PropertiesFileResolver> saltstackAdapterPropertiesFileResolvers = new Vector<>();
69
70     /**
71      * The configuration properties for the db connection.
72      */
73     private Properties properties;
74
75     /**
76      * Set up the prioritized list of strategies for resolving dblib properties
77      * files.
78      */
79     public SaltstackAdapterPropertiesProviderImpl() {
80         saltstackAdapterPropertiesFileResolvers
81                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
82         saltstackAdapterPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
83
84         saltstackAdapterPropertiesFileResolvers.add(
85                 new JREFileResolver("Using property file (3) from JRE argument", SaltstackAdapterPropertiesProviderImpl.class));
86         saltstackAdapterPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
87
88         // determines properties file as according to the priority described in the
89         // class header comment
90         final File propertiesFile = determinePropertiesFile(this);
91         if (propertiesFile != null) {
92             try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
93                 properties = new Properties();
94                 properties.load(fileInputStream);
95             } catch (final IOException e) {
96                 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
97                           new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
98                                                      e));
99             }
100         } else {
101             // Try to read properties as resource
102
103             InputStream propStr = getClass().getResourceAsStream("/" + SALTSTACKADAPTER_PROP_FILE_NAME);
104             if (propStr != null) {
105                 properties = new Properties();
106                 try {
107                     properties.load(propStr);
108                     propStr.close();
109                 } catch (IOException e) {
110                     properties = null;
111                 }
112             }
113
114         }
115
116         if (properties == null) {
117             reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
118                     "Missing configuration properties resource(3): " + SALTSTACKADAPTER_PROP_FILE_NAME));
119
120             LOG.info("Defaulting org.onap.appc.adapter.saltstack.clientType to NONE");
121
122             properties = new Properties();
123             properties.setProperty("org.onap.appc.adapter.saltstack.clientType", "NONE");
124         }
125     }
126
127     /**
128      * Reports the method chosen for properties resolution to the
129      * <code>Logger</code>.
130      *
131      * @param message      Some user friendly message
132      * @param fileOptional The file location of the chosen properties file
133      * @return the file location of the chosen properties file
134      */
135     private static File reportSuccess(final String message, final Optional<File> fileOptional) {
136         if (fileOptional.isPresent()) {
137             final File file = fileOptional.get();
138             LOG.info("{} {}", message, file.getPath());
139             return file;
140         }
141         return null;
142     }
143
144     /**
145      * Reports fatal errors. This is the case in which no properties file could be
146      * found.
147      *
148      * @param message                An appropriate fatal error message
149      * @param configurationException An exception describing what went wrong during resolution
150      */
151     private static void reportFailure(final String message, final ConfigurationException configurationException) {
152
153         LOG.error("{}", message, configurationException);
154     }
155
156     /**
157      * Extract svclogic config properties.
158      *
159      * @return the svclogic config properties
160      */
161     public Properties getProperties() {
162         return properties;
163     }
164
165     /**
166      * Determines the sql-resource properties file to use based on the following priority:
167      * <ol>
168      * <li>A directory identified by the system environment variable
169      * <code>SDNC_CONFIG_DIR</code></li>
170      * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
171      * <li>A directory identified by the JRE argument
172      * <code>sql-resource.properties</code></li>
173      * <li>A <code>sql-resource.properties</code> file located in the karaf root
174      * directory</li>
175      * </ol>
176      */
177     File determinePropertiesFile(final SaltstackAdapterPropertiesProviderImpl resourceProvider) {
178
179         for (final PropertiesFileResolver sliPropertiesFileResolver : saltstackAdapterPropertiesFileResolvers) {
180             final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SALTSTACKADAPTER_PROP_FILE_NAME);
181             if (fileOptional.isPresent()) {
182                 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
183             }
184         }
185
186         return null;
187     }
188 }