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