Proposal to remove OSGi dependencies from the CCSDK project
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-lighty / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / impl / SaltstackAdapterPropertiesProviderImplLighty.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 java.io.File;
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;
42
43 /**
44  * THIS CLASS IS A COPY OF {@link SaltstackAdapterPropertiesProviderImpl} WITH REMOVED OSGi DEPENDENCIES
45  */
46 public class SaltstackAdapterPropertiesProviderImplLighty implements SaltstackAdapterPropertiesProvider {
47
48     private static final Logger LOG = LoggerFactory.getLogger(SaltstackAdapterPropertiesProviderImplLighty.class);
49
50     /**
51      * The name of the properties file for database configuration
52      */
53     private static final String SALTSTACKADAPTER_PROP_FILE_NAME = "saltstack-adapter.properties";
54
55     /**
56      * A prioritized list of strategies for resolving sql-resource properties files.
57      */
58     private Vector<PropertiesFileResolver> saltstackAdapterPropertiesFileResolvers = new Vector<>();
59
60     /**
61      * The configuration properties for the db connection.
62      */
63     private Properties properties;
64
65     /**
66      * Set up the prioritized list of strategies for resolving dblib properties
67      * files.
68      */
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));
74
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(),
85                                                      e));
86             }
87         } else {
88             // Try to read properties as resource
89
90             InputStream propStr = getClass().getResourceAsStream("/" + SALTSTACKADAPTER_PROP_FILE_NAME);
91             if (propStr != null) {
92                 properties = new Properties();
93                 try {
94                     properties.load(propStr);
95                     propStr.close();
96                 } catch (IOException e) {
97                     properties = null;
98                 }
99             }
100
101         }
102
103         if (properties == null) {
104             reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
105                     "Missing configuration properties resource(3): " + SALTSTACKADAPTER_PROP_FILE_NAME));
106
107             LOG.info("Defaulting org.onap.appc.adapter.saltstack.clientType to NONE");
108
109             properties = new Properties();
110             properties.setProperty("org.onap.appc.adapter.saltstack.clientType", "NONE");
111         }
112     }
113
114     /**
115      * Reports the method chosen for properties resolution to the
116      * <code>Logger</code>.
117      *
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
121      */
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());
126             return file;
127         }
128         return null;
129     }
130
131     /**
132      * Reports fatal errors. This is the case in which no properties file could be
133      * found.
134      *
135      * @param message                An appropriate fatal error message
136      * @param configurationException An exception describing what went wrong during resolution
137      */
138     private static void reportFailure(final String message, final ConfigurationException configurationException) {
139
140         LOG.error("{}", message, configurationException);
141     }
142
143     /**
144      * Extract svclogic config properties.
145      *
146      * @return the svclogic config properties
147      */
148     public Properties getProperties() {
149         return properties;
150     }
151
152     /**
153      * Determines the sql-resource properties file to use based on the following priority:
154      * <ol>
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
161      * directory</li>
162      * </ol>
163      */
164     File determinePropertiesFile(final SaltstackAdapterPropertiesProviderImplLighty resourceProvider) {
165
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);
170             }
171         }
172
173         return null;
174     }
175 }