Proposal to remove OSGi dependencies from the CCSDK project
[ccsdk/sli/adaptors.git] / ansible-adapter / ansible-adapter-lighty / src / main / java / org / onap / ccsdk / sli / adaptors / ansible / impl / AnsibleAdapterPropertiesProviderImplLighty.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.ansible.impl;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Optional;
28 import java.util.Properties;
29 import java.util.Vector;
30 import org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterPropertiesProvider;
31 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
32 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
33 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
34 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
35 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * THIS CLASS IS A COPY OF {@link AnsibleAdapterPropertiesProviderImpl} WITH REMOVED OSGi DEPENDENCIES
41  */
42 public class AnsibleAdapterPropertiesProviderImplLighty implements AnsibleAdapterPropertiesProvider {
43
44     private static final Logger LOG = LoggerFactory.getLogger(AnsibleAdapterPropertiesProviderImplLighty.class);
45
46     /**
47      * The name of the properties file for database configuration
48      */
49     private static final String ANSIBLEADAPTER_PROP_FILE_NAME = "ansible-adapter.properties";
50
51     /**
52      * A prioritized list of strategies for resolving sql-resource properties files.
53      */
54     private Vector<PropertiesFileResolver> ansibleAdapterPropertiesFileResolvers = new Vector<>();
55
56     /**
57      * The configuration properties for the db connection.
58      */
59     private Properties properties;
60
61     /**
62      * Set up the prioritized list of strategies for resolving dblib properties
63      * files.
64      */
65     public AnsibleAdapterPropertiesProviderImplLighty() {
66         ansibleAdapterPropertiesFileResolvers
67                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
68         ansibleAdapterPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
69
70         ansibleAdapterPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
71
72         // determines properties file as according to the priority described in the
73         // class header comment
74         final File propertiesFile = determinePropertiesFile(this);
75         if (propertiesFile != null) {
76             try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
77                 properties = new Properties();
78                 properties.load(fileInputStream);
79             } catch (final IOException e) {
80                 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
81                         new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
82                                 e));
83             }
84         } else {
85             // Try to read properties as resource
86
87             InputStream propStr = getClass().getResourceAsStream("/" + ANSIBLEADAPTER_PROP_FILE_NAME);
88             if (propStr != null) {
89                 properties = new Properties();
90                 try {
91                     properties.load(propStr);
92                     propStr.close();
93                 } catch (IOException e) {
94                     properties = null;
95                 }
96             }
97
98         }
99
100         if (properties == null) {
101             reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
102                     "Missing configuration properties resource(3): " + ANSIBLEADAPTER_PROP_FILE_NAME));
103
104             LOG.info("Defaulting org.onap.appc.adapter.ansible.clientType to TRUST_ALL");
105
106             properties = new Properties();
107             properties.setProperty("org.onap.appc.adapter.ansible.clientType", "TRUST_ALL");
108         }
109     }
110
111     /**
112      * Extract svclogic config properties.
113      *
114      * @return the svclogic config properties
115      */
116     public Properties getProperties() {
117         return properties;
118     }
119
120     /**
121      * Reports the method chosen for properties resolution to the
122      * <code>Logger</code>.
123      *
124      * @param message
125      *            Some user friendly message
126      * @param fileOptional
127      *            The file location of the chosen properties file
128      * @return the file location of the chosen properties file
129      */
130     private static File reportSuccess(final String message, final Optional<File> fileOptional) {
131         if (fileOptional.isPresent()) {
132             final File file = fileOptional.get();
133             LOG.info("{} {}", message, file.getPath());
134             return file;
135         }
136         return null;
137     }
138
139     /**
140      * Reports fatal errors. This is the case in which no properties file could be
141      * found.
142      *
143      * @param message
144      *            An appropriate fatal error message
145      * @param configurationException
146      *            An exception describing what went wrong during resolution
147      */
148     private static void reportFailure(final String message, final ConfigurationException configurationException) {
149
150         LOG.error("{}", message, configurationException);
151     }
152
153     /**
154      * Determines the sql-resource properties file to use based on the following priority:
155      * <ol>
156      * <li>A directory identified by the system environment variable
157      * <code>SDNC_CONFIG_DIR</code></li>
158      * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
159      * <li>A directory identified by the JRE argument
160      * <code>sql-resource.properties</code></li>
161      * <li>A <code>sql-resource.properties</code> file located in the karaf root
162      * directory</li>
163      * </ol>
164      */
165     File determinePropertiesFile(final AnsibleAdapterPropertiesProviderImplLighty resourceProvider) {
166
167         for (final PropertiesFileResolver sliPropertiesFileResolver : ansibleAdapterPropertiesFileResolvers) {
168             final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(ANSIBLEADAPTER_PROP_FILE_NAME);
169             if (fileOptional.isPresent()) {
170                 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
171             }
172         }
173
174         return null;
175     }
176 }