2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.core.slipluginutils;
24 import com.google.common.annotations.VisibleForTesting;
25 import com.google.common.base.Strings;
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;
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;
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
50 public final class Dme2PropertiesProvider {
52 private static final Logger LOG = LoggerFactory.getLogger(Dme2PropertiesProvider.class);
55 * The name of the environment variable to specify the configuration directory.
57 private static final String SDNC_ROOT_DIR_ENV_VAR_KEY = "SDNC_CONFIG_DIR";
60 * the dme2 properties file name.
62 private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
65 * the key for <code>proxyUrl</code>, which represents a CSV list of urls
67 static final String PROXY_URL_KEY = "proxyUrl";
70 * indicates that proxy urls are separated by commas
72 private static final String PROXY_URLS_VALUE_SEPARATOR = ",";
75 * the key for <code>aafUserName</code>
77 static final String AAF_USERNAME_KEY = "aafUserName";
80 * the key for <code>aafPassword</code>
82 static final String AAF_PASSWORD_KEY = "aafPassword";
85 * the key for <code>envContext</code>
87 static final String ENV_CONTEXT_KEY = "envContext";
90 * the key for <code>routeOffer</code>
92 static final String ROUTE_OFFER_KEY = "routeOffer";
95 * the key for <code>commonServiceVersion</code>
97 static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion";
100 * the key for <code>partner</code>
102 static final String PARTNER_KEY = "partner";
104 private Optional<String[]> proxyUrls = Optional.empty();
106 private Optional<String> aafUsername = Optional.empty();
108 private Optional<String> aafPassword = Optional.empty();
110 private Optional<String> envContext = Optional.empty();
112 private Optional<String> routeOffer = Optional.empty();
114 private Optional<String> commonServiceVersion = Optional.empty();
116 private Optional<String> partner = Optional.empty();
119 * A prioritized list of strategies for resolving dme2 properties files.
121 private Vector<PropertiesFileResolver> dme2PropertiesFileResolvers = new Vector<>();
124 * Instantiates the properties provider, which involves loading the appropriate
125 * properties for dme2.
127 public Dme2PropertiesProvider() {
128 this(DME2_PROPERTIES_FILE_NAME);
132 * Instantiates the properties provider, which involves loading the appropriate
133 * properties for dme2.
136 * location of the dme2.properties file
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"));
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));
148 File dme2File = getDme2File(dme2FileName);
153 private void init(final File dme2Path) {
154 final Properties properties;
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) {
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);
173 * Reports the method chosen for properties resolution to the
174 * <code>Logger</code>.
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
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());
191 private File getDme2File(final String dme2Filename) {
193 for (final PropertiesFileResolver dblibPropertiesFileResolver : dme2PropertiesFileResolvers) {
194 final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(dme2Filename);
195 if (fileOptional.isPresent()) {
196 return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
199 return (new File(dme2Filename));
202 private static Properties getProperties(final File dme2File) throws IOException {
204 final Properties properties = new Properties();
205 properties.load(new FileReader(dme2File));
209 private String getProxyUrl(final Properties properties) {
210 return properties.getProperty(PROXY_URL_KEY);
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));
218 return Optional.empty();
221 public Optional<String[]> getProxyUrls() {
222 return this.proxyUrls;
225 private Optional<String> getAafUsername(final Properties properties) {
226 final String aafUsernameValue = properties.getProperty(AAF_USERNAME_KEY);
227 return Optional.ofNullable(aafUsernameValue);
230 Optional<String> getAafUsername() {
231 return this.aafUsername;
234 private Optional<String> getAafPassword(final Properties properties) {
235 final String aafPassword = properties.getProperty(AAF_PASSWORD_KEY);
236 return Optional.ofNullable(aafPassword);
239 Optional<String> getAafPassword() {
240 return this.aafPassword;
243 private Optional<String> getEnvContext(final Properties properties) {
244 final String envContext = properties.getProperty(ENV_CONTEXT_KEY);
245 return Optional.ofNullable(envContext);
248 Optional<String> getEnvContext() {
249 return this.envContext;
252 private Optional<String> getRouteOffer(final Properties properties) {
253 final String routeOffer = properties.getProperty(ROUTE_OFFER_KEY);
254 return Optional.ofNullable(routeOffer);
257 Optional<String> getRouteOffer() {
258 return this.routeOffer;
261 private Optional<String> getCommonServiceVersion(final Properties properties) {
262 final String commonServiceVersion = properties.getProperty(COMMON_SERVICE_VERSION_KEY);
263 return Optional.ofNullable(commonServiceVersion);
266 Optional<String> getCommonServiceVersion() {
267 return this.commonServiceVersion;
270 private Optional<String> getPartner(final Properties properties) {
271 final String partner = properties.getProperty(PARTNER_KEY);
272 return Optional.ofNullable(partner);
275 Optional<String> getPartner() {