baa86c445f5e1dc7b40e5b888ef7a8b7ea86951a
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / AAIServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights
6  *                         reserved.
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.adaptors.aai;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.util.Optional;
28 import java.util.Properties;
29 import java.util.Vector;
30
31 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
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.BundleContextFileResolver;
35 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
36 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Responsible for determining the properties file to use and instantiating the <code>DBResourceManager</code>
42  * Service.  The priority for properties file resolution is as follows:
43  *
44  * <ol>
45  *     <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
46  *     <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
47  *     <li>A directory identified by the JRE argument <code>dblib.properties</code></li>
48  *     <li>A <code>dblib.properties</code> file located in the karaf root directory</li>
49  * </ol>
50  */
51 public class AAIServiceProvider implements UtilsProvider {
52
53     private static final Logger LOG = LoggerFactory.getLogger(AAIServiceProvider.class);
54
55     /**
56      * The name of the properties file for database configuration
57      */
58     private static final String AAISEERVICE_PROP_FILE_NAME = "aaiclient.properties";
59
60     /**
61      * A prioritized list of strategies for resolving dblib properties files.
62      */
63     private Vector<PropertiesFileResolver> dblibPropertiesFileResolvers = new Vector();
64
65     /**
66      * The configuration properties for the db connection.
67      */
68     private Properties properties;
69
70     /**
71      * Set up the prioritized list of strategies for resolving dblib properties files.
72      */
73     public AAIServiceProvider() {
74         dblibPropertiesFileResolvers.add(new SdncConfigEnvVarFileResolver(
75                 "Using property file (1) from environment variable"
76             ));
77         dblibPropertiesFileResolvers.add(new JREFileResolver(
78             "Using property file (2) from JRE argument", AAIServiceProvider.class
79         ));
80         dblibPropertiesFileResolvers.add(new BundleContextFileResolver(
81             "Using property file (3) from JRE argument", AAIServiceProvider.class
82         ));
83         dblibPropertiesFileResolvers.add(new KarafRootFileResolver(
84             "Using property file (4) from karaf root", this
85         ));
86         dblibPropertiesFileResolvers.add(new CoreDefaultFileResolver(
87             "Using property file (5) from default directory"
88         ));
89
90         // determines properties file as according to the priority described in the class header comment
91         final File propertiesFile = determinePropertiesFile();
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 AAIServiceException("Failed to load properties for file: "
99                                 + propertiesFile.toString(), e));
100             }
101         }
102     }
103
104     /**
105      * Extract db config properties.
106      *
107      * @return the db config properties
108      */
109     public Properties getProperties() {
110         return properties;
111     }
112
113     /**
114      * Reports the method chosen for properties resolution to the <code>Logger</code>.
115      *
116      * @param message Some user friendly message
117      * @param fileOptional The file location of the chosen properties file
118      * @return the file location of the chosen properties file
119      */
120     private static File reportSuccess(final String message, final Optional<File> fileOptional) {
121         if(fileOptional.isPresent()) {
122             final File file = fileOptional.get();
123             LOG.info("{} {}", message, file.getPath());
124             return file;
125         }
126         return null;
127     }
128
129     /**
130      * Reports fatal errors.  This is the case in which no properties file could be found.
131      *
132      * @param message An appropriate fatal error message
133      * @param dblibConfigurationException An exception describing what went wrong during resolution
134      */
135     private static void reportFailure(final String message,
136                                       final AAIServiceException dblibConfigurationException) {
137
138         LOG.error("{}", message, dblibConfigurationException);
139     }
140
141     /**
142      * Determines the dblib properties file to use based on the following priority:
143      * <ol>
144      *     <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
145      *     <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
146      *     <li>A directory identified by the JRE argument <code>dblib.properties</code></li>
147      *     <li>A <code>dblib.properties</code> file located in the karaf root directory</li>
148      * </ol>
149      */
150     File determinePropertiesFile() {
151
152         for (final PropertiesFileResolver dblibPropertiesFileResolver : dblibPropertiesFileResolvers) {
153             final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(AAISEERVICE_PROP_FILE_NAME);
154             if (fileOptional.isPresent()) {
155                 return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
156             }
157         }
158
159         reportFailure("Missing configuration properties resource(3)",
160                 new AAIServiceException("Missing configuration properties resource(3): "
161                         + AAISEERVICE_PROP_FILE_NAME));
162         return null;
163     }
164 }