aboutsummaryrefslogtreecommitdiffstats
path: root/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2PropertiesProvider.java
blob: 6802c9a8b9b22fe17b9af491534c419f9c5988c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.core.slipluginutils;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Properties;
import java.util.Vector;

import org.onap.ccsdk.sli.core.utils.JREFileResolver;
import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Immutable properties container for dme2 properties. Since the initial design
 * decision was made to utilize <code>Properties</code> instead of an OSGi
 * <code>ManagedService</code>, it was decided to make these properties
 * immutable.
 */
public final class Dme2PropertiesProvider {

	private static final Logger LOG = LoggerFactory.getLogger(Dme2PropertiesProvider.class);

	/**
	 * The name of the environment variable to specify the configuration directory.
	 */
	private static final String SDNC_ROOT_DIR_ENV_VAR_KEY = "SDNC_CONFIG_DIR";

	/**
	 * the dme2 properties file name.
	 */
	private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";

	/**
	 * the key for <code>proxyUrl</code>, which represents a CSV list of urls
	 */
	static final String PROXY_URL_KEY = "proxyUrl";

	/**
	 * indicates that proxy urls are separated by commas
	 */
	private static final String PROXY_URLS_VALUE_SEPARATOR = ",";

	/**
	 * the key for <code>aafUserName</code>
	 */
	static final String AAF_USERNAME_KEY = "aafUserName";

	/**
	 * the key for <code>aafPassword</code>
	 */
	static final String AAF_PASSWORD_KEY = "aafPassword";

	/**
	 * the key for <code>envContext</code>
	 */
	static final String ENV_CONTEXT_KEY = "envContext";

	/**
	 * the key for <code>routeOffer</code>
	 */
	static final String ROUTE_OFFER_KEY = "routeOffer";

	/**
	 * the key for <code>commonServiceVersion</code>
	 */
	static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion";

	/**
	 * the key for <code>partner</code>
	 */
	static final String PARTNER_KEY = "partner";

	private Optional<String[]> proxyUrls = Optional.empty();

	private Optional<String> aafUsername = Optional.empty();

	private Optional<String> aafPassword = Optional.empty();

	private Optional<String> envContext = Optional.empty();

	private Optional<String> routeOffer = Optional.empty();

	private Optional<String> commonServiceVersion = Optional.empty();

	private Optional<String> partner = Optional.empty();

	/**
	 * A prioritized list of strategies for resolving dme2 properties files.
	 */
	private Vector<PropertiesFileResolver> dme2PropertiesFileResolvers = new Vector<>();

	/**
	 * Instantiates the properties provider, which involves loading the appropriate
	 * properties for dme2.
	 */
	public Dme2PropertiesProvider() {
		this(DME2_PROPERTIES_FILE_NAME);
	}

	/**
	 * Instantiates the properties provider, which involves loading the appropriate
	 * properties for dme2.
	 *
	 * @param dme2Path
	 *            location of the dme2.properties file
	 */
	@VisibleForTesting
	Dme2PropertiesProvider(final String dme2FileName) {
		dme2PropertiesFileResolvers
				.add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
		dme2PropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));

		dme2PropertiesFileResolvers
				.add(new JREFileResolver("Using property file (3) from JRE argument", Dme2PropertiesProvider.class));
		dme2PropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));

		File dme2File = getDme2File(dme2FileName);

		init(dme2File);
	}

	private void init(final File dme2Path) {
		final Properties properties;
		try {
			properties = getProperties(dme2Path);
			this.proxyUrls = getProxyUrls(properties);
			this.aafUsername = getAafUsername(properties);
			this.aafPassword = getAafPassword(properties);
			this.envContext = getEnvContext(properties);
			this.routeOffer = getRouteOffer(properties);
			this.commonServiceVersion = getCommonServiceVersion(properties);
			this.partner = getPartner(properties);
		} catch (final FileNotFoundException e) {

			LOG.error("dme2.properties file could not be found at path: {}", dme2Path, e);
		} catch (final IOException e) {
			LOG.error("fatal error reading dme2.properties at path: {}", dme2Path, e);
		}
	}

	/**
	 * Reports the method chosen for properties resolution to the
	 * <code>Logger</code>.
	 *
	 * @param message
	 *            Some user friendly message
	 * @param fileOptional
	 *            The file location of the chosen properties file
	 * @return the file location of the chosen properties file
	 */
	private static File reportSuccess(final String message, final Optional<File> fileOptional) {
		if (fileOptional.isPresent()) {
			final File file = fileOptional.get();
			LOG.info("{} {}", message, file.getPath());
			return file;
		}
		return null;
	}

	private File getDme2File(final String dme2Filename) {

		for (final PropertiesFileResolver dblibPropertiesFileResolver : dme2PropertiesFileResolvers) {
			final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(dme2Filename);
			if (fileOptional.isPresent()) {
				return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
			}
		}
		return (new File(dme2Filename));
	}

	private static Properties getProperties(final File dme2File) throws IOException {

		final Properties properties = new Properties();
		properties.load(new FileReader(dme2File));
		return properties;
	}

	private String getProxyUrl(final Properties properties) {
		return properties.getProperty(PROXY_URL_KEY);
	}

	private Optional<String[]> getProxyUrls(final Properties properties) {
		final String proxyUrlsValue = getProxyUrl(properties);
		if (!Strings.isNullOrEmpty(proxyUrlsValue)) {
			return Optional.ofNullable(proxyUrlsValue.split(PROXY_URLS_VALUE_SEPARATOR));
		}
		return Optional.empty();
	}

	public Optional<String[]> getProxyUrls() {
		return this.proxyUrls;
	}

	private Optional<String> getAafUsername(final Properties properties) {
		final String aafUsernameValue = properties.getProperty(AAF_USERNAME_KEY);
		return Optional.ofNullable(aafUsernameValue);
	}

	Optional<String> getAafUsername() {
		return this.aafUsername;
	}

	private Optional<String> getAafPassword(final Properties properties) {
		final String aafPassword = properties.getProperty(AAF_PASSWORD_KEY);
		return Optional.ofNullable(aafPassword);
	}

	Optional<String> getAafPassword() {
		return this.aafPassword;
	}

	private Optional<String> getEnvContext(final Properties properties) {
		final String envContext = properties.getProperty(ENV_CONTEXT_KEY);
		return Optional.ofNullable(envContext);
	}

	Optional<String> getEnvContext() {
		return this.envContext;
	}

	private Optional<String> getRouteOffer(final Properties properties) {
		final String routeOffer = properties.getProperty(ROUTE_OFFER_KEY);
		return Optional.ofNullable(routeOffer);
	}

	Optional<String> getRouteOffer() {
		return this.routeOffer;
	}

	private Optional<String> getCommonServiceVersion(final Properties properties) {
		final String commonServiceVersion = properties.getProperty(COMMON_SERVICE_VERSION_KEY);
		return Optional.ofNullable(commonServiceVersion);
	}

	Optional<String> getCommonServiceVersion() {
		return this.commonServiceVersion;
	}

	private Optional<String> getPartner(final Properties properties) {
		final String partner = properties.getProperty(PARTNER_KEY);
		return Optional.ofNullable(partner);
	}

	Optional<String> getPartner() {
		return this.partner;
	}
}