6802c9a8b9b22fe17b9af491534c419f9c5988c9
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 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.core.slipluginutils;
23
24 import com.google.common.annotations.VisibleForTesting;
25 import com.google.common.base.Strings;
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Optional;
33 import java.util.Properties;
34 import java.util.Vector;
35
36 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
37 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
38 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
39 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
40 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Immutable properties container for dme2 properties. Since the initial design
46  * decision was made to utilize <code>Properties</code> instead of an OSGi
47  * <code>ManagedService</code>, it was decided to make these properties
48  * immutable.
49  */
50 public final class Dme2PropertiesProvider {
51
52         private static final Logger LOG = LoggerFactory.getLogger(Dme2PropertiesProvider.class);
53
54         /**
55          * The name of the environment variable to specify the configuration directory.
56          */
57         private static final String SDNC_ROOT_DIR_ENV_VAR_KEY = "SDNC_CONFIG_DIR";
58
59         /**
60          * the dme2 properties file name.
61          */
62         private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
63
64         /**
65          * the key for <code>proxyUrl</code>, which represents a CSV list of urls
66          */
67         static final String PROXY_URL_KEY = "proxyUrl";
68
69         /**
70          * indicates that proxy urls are separated by commas
71          */
72         private static final String PROXY_URLS_VALUE_SEPARATOR = ",";
73
74         /**
75          * the key for <code>aafUserName</code>
76          */
77         static final String AAF_USERNAME_KEY = "aafUserName";
78
79         /**
80          * the key for <code>aafPassword</code>
81          */
82         static final String AAF_PASSWORD_KEY = "aafPassword";
83
84         /**
85          * the key for <code>envContext</code>
86          */
87         static final String ENV_CONTEXT_KEY = "envContext";
88
89         /**
90          * the key for <code>routeOffer</code>
91          */
92         static final String ROUTE_OFFER_KEY = "routeOffer";
93
94         /**
95          * the key for <code>commonServiceVersion</code>
96          */
97         static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion";
98
99         /**
100          * the key for <code>partner</code>
101          */
102         static final String PARTNER_KEY = "partner";
103
104         private Optional<String[]> proxyUrls = Optional.empty();
105
106         private Optional<String> aafUsername = Optional.empty();
107
108         private Optional<String> aafPassword = Optional.empty();
109
110         private Optional<String> envContext = Optional.empty();
111
112         private Optional<String> routeOffer = Optional.empty();
113
114         private Optional<String> commonServiceVersion = Optional.empty();
115
116         private Optional<String> partner = Optional.empty();
117
118         /**
119          * A prioritized list of strategies for resolving dme2 properties files.
120          */
121         private Vector<PropertiesFileResolver> dme2PropertiesFileResolvers = new Vector<>();
122
123         /**
124          * Instantiates the properties provider, which involves loading the appropriate
125          * properties for dme2.
126          */
127         public Dme2PropertiesProvider() {
128                 this(DME2_PROPERTIES_FILE_NAME);
129         }
130
131         /**
132          * Instantiates the properties provider, which involves loading the appropriate
133          * properties for dme2.
134          *
135          * @param dme2Path
136          *            location of the dme2.properties file
137          */
138         @VisibleForTesting
139         Dme2PropertiesProvider(final String dme2FileName) {
140                 dme2PropertiesFileResolvers
141                                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
142                 dme2PropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
143
144                 dme2PropertiesFileResolvers
145                                 .add(new JREFileResolver("Using property file (3) from JRE argument", Dme2PropertiesProvider.class));
146                 dme2PropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
147
148                 File dme2File = getDme2File(dme2FileName);
149
150                 init(dme2File);
151         }
152
153         private void init(final File dme2Path) {
154                 final Properties properties;
155                 try {
156                         properties = getProperties(dme2Path);
157                         this.proxyUrls = getProxyUrls(properties);
158                         this.aafUsername = getAafUsername(properties);
159                         this.aafPassword = getAafPassword(properties);
160                         this.envContext = getEnvContext(properties);
161                         this.routeOffer = getRouteOffer(properties);
162                         this.commonServiceVersion = getCommonServiceVersion(properties);
163                         this.partner = getPartner(properties);
164                 } catch (final FileNotFoundException e) {
165
166                         LOG.error("dme2.properties file could not be found at path: {}", dme2Path, e);
167                 } catch (final IOException e) {
168                         LOG.error("fatal error reading dme2.properties at path: {}", dme2Path, e);
169                 }
170         }
171
172         /**
173          * Reports the method chosen for properties resolution to the
174          * <code>Logger</code>.
175          *
176          * @param message
177          *            Some user friendly message
178          * @param fileOptional
179          *            The file location of the chosen properties file
180          * @return the file location of the chosen properties file
181          */
182         private static File reportSuccess(final String message, final Optional<File> fileOptional) {
183                 if (fileOptional.isPresent()) {
184                         final File file = fileOptional.get();
185                         LOG.info("{} {}", message, file.getPath());
186                         return file;
187                 }
188                 return null;
189         }
190
191         private File getDme2File(final String dme2Filename) {
192
193                 for (final PropertiesFileResolver dblibPropertiesFileResolver : dme2PropertiesFileResolvers) {
194                         final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(dme2Filename);
195                         if (fileOptional.isPresent()) {
196                                 return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
197                         }
198                 }
199                 return (new File(dme2Filename));
200         }
201
202         private static Properties getProperties(final File dme2File) throws IOException {
203
204                 final Properties properties = new Properties();
205                 properties.load(new FileReader(dme2File));
206                 return properties;
207         }
208
209         private String getProxyUrl(final Properties properties) {
210                 return properties.getProperty(PROXY_URL_KEY);
211         }
212
213         private Optional<String[]> getProxyUrls(final Properties properties) {
214                 final String proxyUrlsValue = getProxyUrl(properties);
215                 if (!Strings.isNullOrEmpty(proxyUrlsValue)) {
216                         return Optional.ofNullable(proxyUrlsValue.split(PROXY_URLS_VALUE_SEPARATOR));
217                 }
218                 return Optional.empty();
219         }
220
221         public Optional<String[]> getProxyUrls() {
222                 return this.proxyUrls;
223         }
224
225         private Optional<String> getAafUsername(final Properties properties) {
226                 final String aafUsernameValue = properties.getProperty(AAF_USERNAME_KEY);
227                 return Optional.ofNullable(aafUsernameValue);
228         }
229
230         Optional<String> getAafUsername() {
231                 return this.aafUsername;
232         }
233
234         private Optional<String> getAafPassword(final Properties properties) {
235                 final String aafPassword = properties.getProperty(AAF_PASSWORD_KEY);
236                 return Optional.ofNullable(aafPassword);
237         }
238
239         Optional<String> getAafPassword() {
240                 return this.aafPassword;
241         }
242
243         private Optional<String> getEnvContext(final Properties properties) {
244                 final String envContext = properties.getProperty(ENV_CONTEXT_KEY);
245                 return Optional.ofNullable(envContext);
246         }
247
248         Optional<String> getEnvContext() {
249                 return this.envContext;
250         }
251
252         private Optional<String> getRouteOffer(final Properties properties) {
253                 final String routeOffer = properties.getProperty(ROUTE_OFFER_KEY);
254                 return Optional.ofNullable(routeOffer);
255         }
256
257         Optional<String> getRouteOffer() {
258                 return this.routeOffer;
259         }
260
261         private Optional<String> getCommonServiceVersion(final Properties properties) {
262                 final String commonServiceVersion = properties.getProperty(COMMON_SERVICE_VERSION_KEY);
263                 return Optional.ofNullable(commonServiceVersion);
264         }
265
266         Optional<String> getCommonServiceVersion() {
267                 return this.commonServiceVersion;
268         }
269
270         private Optional<String> getPartner(final Properties properties) {
271                 final String partner = properties.getProperty(PARTNER_KEY);
272                 return Optional.ofNullable(partner);
273         }
274
275         Optional<String> getPartner() {
276                 return this.partner;
277         }
278 }