40d305610cbf38ea70414d1dc56cae4fa0e17344
[ccsdk/sli.git] /
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.EnvProperties;
39 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Responsible for determining the properties file to use and instantiating the
45  * <code>DBResourceManager</code> Service. The priority for properties file
46  * resolution is as follows:
47  *
48  * <ol>
49  * <li>A directory identified by the system environment variable
50  * <code>SDNC_CONFIG_DIR</code></li>
51  * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
52  * <li>A directory identified by the JRE argument
53  * <code>dblib.properties</code></li>
54  * <li>A <code>dblib.properties</code> file located in the karaf root
55  * directory</li>
56  * </ol>
57  */
58 public class SvcLogicPropertiesProviderImpl implements SvcLogicPropertiesProvider {
59
60         private static final Logger log = LoggerFactory.getLogger(SvcLogicPropertiesProviderImpl.class);
61
62         /**
63          * The name of the properties file for database configuration
64          */
65         private static final String SVCLOGIC_PROP_FILE_NAME = "svclogic.properties";
66
67         /**
68          * A prioritized list of strategies for resolving dblib properties files.
69          */
70         private Vector<PropertiesFileResolver> sliPropertiesFileResolvers = new Vector<>();
71
72         /**
73          * The configuration properties for the db connection.  Use EnvProperties class, which
74          * extends Properties and resolves env variable references in values
75          */
76         private EnvProperties properties;
77
78         /**
79          * Set up the prioritized list of strategies for resolving dblib properties
80          * files.
81          */
82         public SvcLogicPropertiesProviderImpl() {
83                 sliPropertiesFileResolvers
84                                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
85                 sliPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
86
87                 sliPropertiesFileResolvers.add(
88                                 new JREFileResolver("Using property file (3) from JRE argument", SvcLogicPropertiesProviderImpl.class));
89                 sliPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
90
91                 // determines properties file as according to the priority described in the
92                 // class header comment
93                 final File propertiesFile = determinePropertiesFile(this);
94                 if (propertiesFile != null) {
95                         try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
96                                 properties = new EnvProperties();
97                                 properties.load(fileInputStream);
98                         } catch (final IOException e) {
99                                 log.error("Failed to load properties for file: {}", propertiesFile.toString(),
100                                                 new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
101                                                                 e));
102                         }
103                 } else {
104                         // Try to read properties as resource
105
106                         InputStream propStr = getClass().getResourceAsStream("/" + SVCLOGIC_PROP_FILE_NAME);
107                         if (propStr != null) {
108                                 properties = new EnvProperties();
109                                 try {
110                                         properties.load(propStr);
111                                         propStr.close();
112                                 } catch (IOException e) {
113                                         log.error("IO Exception",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): " + SVCLOGIC_PROP_FILE_NAME));
123                 }
124         }
125
126         /**
127          * Extract svclogic config properties.
128          *
129          * @return the svclogic config properties
130          */
131         public Properties getProperties() {
132                 return properties;
133         }
134
135         /**
136          * Reports the method chosen for properties resolution to the
137          * <code>Logger</code>.
138          *
139          * @param message
140          *            Some user friendly message
141          * @param fileOptional
142          *            The file location of the chosen properties file
143          * @return the file location of the chosen properties file
144          */
145         private static File reportSuccess(final String message, final Optional<File> fileOptional) {
146                 if (fileOptional.isPresent()) {
147                         final File file = fileOptional.get();
148                         log.info("{} {}", message, file.getPath());
149                         return file;
150                 }
151                 return null;
152         }
153
154         /**
155          * Reports fatal errors. This is the case in which no properties file could be
156          * found.
157          *
158          * @param message
159          *            An appropriate fatal error message
160          * @param configurationException
161          *            An exception describing what went wrong during resolution
162          */
163         private static void reportFailure(final String message, final ConfigurationException configurationException) {
164
165                 log.error("{}", message, configurationException);
166         }
167
168         /**
169          * Determines the dblib properties file to use based on the following priority:
170          * <ol>
171          * <li>A directory identified by the system environment variable
172          * <code>SDNC_CONFIG_DIR</code></li>
173          * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
174          * <li>A directory identified by the JRE argument
175          * <code>dblib.properties</code></li>
176          * <li>A <code>dblib.properties</code> file located in the karaf root
177          * directory</li>
178          * </ol>
179          */
180         File determinePropertiesFile(final SvcLogicPropertiesProviderImpl resourceProvider) {
181
182                 for (final PropertiesFileResolver sliPropertiesFileResolver : sliPropertiesFileResolvers) {
183                         final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(SVCLOGIC_PROP_FILE_NAME);
184                         if (fileOptional.isPresent()) {
185                                 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
186                         }
187                 }
188
189                 return null;
190         }
191 }