Saltstack now aligned with APPC
[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 : CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics. All rights reserved.
6  * ================================================================================
7  *
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  *
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
26
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;
36
37 import java.io.File;
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;
44
45 /**
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:
49  * <p>
50  * <ol>
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
57  * directory</li>
58  * </ol>
59  */
60 public class SaltstackAdapterPropertiesProviderImpl implements SaltstackAdapterPropertiesProvider {
61
62     private static final Logger LOG = LoggerFactory.getLogger(SaltstackAdapterPropertiesProviderImpl.class);
63
64     /**
65      * The name of the properties file for database configuration
66      */
67     private static final String SALTSTACKADAPTER_PROP_FILE_NAME = "saltstack-adapter.properties";
68
69     /**
70      * A prioritized list of strategies for resolving sql-resource properties files.
71      */
72     private Vector<PropertiesFileResolver> saltstackAdapterPropertiesFileResolvers = new Vector<>();
73
74     /**
75      * The configuration properties for the db connection.
76      */
77     private Properties properties;
78
79     /**
80      * Set up the prioritized list of strategies for resolving dblib properties
81      * files.
82      */
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"));
87
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));
91
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(),
102                                                      e));
103             }
104         } else {
105             // Try to read properties as resource
106
107             InputStream propStr = getClass().getResourceAsStream("/" + SALTSTACKADAPTER_PROP_FILE_NAME);
108             if (propStr != null) {
109                 properties = new Properties();
110                 try {
111                     properties.load(propStr);
112                     propStr.close();
113                 } catch (IOException e) {
114                     properties = null;
115                 }
116             }
117
118         }
119
120         if (properties == null) {
121             reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
122                     "Missing configuration properties resource(3): " + SALTSTACKADAPTER_PROP_FILE_NAME));
123
124             LOG.info("Defaulting org.onap.appc.adapter.saltstack.clientType to NONE");
125
126             properties = new Properties();
127             properties.setProperty("org.onap.appc.adapter.saltstack.clientType", "NONE");
128         }
129     }
130
131     /**
132      * Reports the method chosen for properties resolution to the
133      * <code>Logger</code>.
134      *
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
138      */
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());
143             return file;
144         }
145         return null;
146     }
147
148     /**
149      * Reports fatal errors. This is the case in which no properties file could be
150      * found.
151      *
152      * @param message                An appropriate fatal error message
153      * @param configurationException An exception describing what went wrong during resolution
154      */
155     private static void reportFailure(final String message, final ConfigurationException configurationException) {
156
157         LOG.error("{}", message, configurationException);
158     }
159
160     /**
161      * Extract svclogic config properties.
162      *
163      * @return the svclogic config properties
164      */
165     public Properties getProperties() {
166         return properties;
167     }
168
169     /**
170      * Determines the sql-resource properties file to use based on the following priority:
171      * <ol>
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
178      * directory</li>
179      * </ol>
180      */
181     File determinePropertiesFile(final SaltstackAdapterPropertiesProviderImpl resourceProvider) {
182
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);
187             }
188         }
189
190         return null;
191     }
192 }