1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl;
20 import java.io.IOException;
21 import java.util.Base64;
22 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import javax.annotation.Nullable;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile.ConfigurationException;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.BaseSubConfig;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.ISubConfigHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class AaiConfig extends BaseSubConfig {
37 private static final Logger LOG = LoggerFactory.getLogger(AaiConfig.class);
39 private static final String SECTION_MARKER_AAI = "aai";
41 private static final String PROPERTY_KEY_AAIPROP_FILE ="aaiPropertiesFile";
42 private static final String PROPERTY_KEY_BASEURL = "aaiUrl";
43 private static final String PROPERTY_KEY_USERCREDENTIALS = "aaiUserCredentials";
44 private static final String PROPERTY_KEY_HEADERS = "aaiHeaders";
45 private static final String PROPERTY_KEY_DELETEONMOUNTPOINTREMOVED = "aaiDeleteOnMountpointRemove";
46 private static final String PROPERTY_KEY_TRUSTALLCERTS = "aaiTrustAllCerts";
47 private static final String PROPERTY_KEY_APIVERSION = "aaiApiVersion";
48 private static final String PROPERTY_KEY_PCKS12CERTFILENAME = "aaiPcks12ClientCertFile";
49 private static final String PROPERTY_KEY_PCKS12PASSPHRASE = "aaiPcks12ClientCertPassphrase";
50 private static final String PROPERTY_KEY_CONNECTIONTIMEOUT = "aaiClientConnectionTimeout";
51 private static final String PROPERTY_KEY_APPLICATIONID = "aaiApplicationId";
53 private static final String DEFAULT_VALUE_AAIPROP_FILE ="null";
54 private static final String DEFAULT_VALUE_BASEURL = "off";
55 private static final String DEFAULT_VALUE_APPLICATION = "SDNR";
56 private static final String DEFAULT_VALUE_USERNAME = "";
57 private static final String DEFAULT_VALUE_USERPASSWORD = "";
58 private static final String DEFAULT_VALUE_USERCREDENTIALS = "";
59 private static final String DEFAULT_VALUE_HEADERS = "[\"X-TransactionId: 9999\"]";
60 private static final boolean DEFAULT_VALUE_DELETEONMOUNTPOINTREMOVED = false;
61 private static final boolean DEFAULT_VALUE_TRUSTALLCERTS = false;
62 private static final int DEFAULT_VALUE_CONNECTION_TIMEOUT = 30000; //in ms
63 private static final String DEFAULT_VALUE_APIVERSION = "aai/v13";
64 private static final String DEFAULT_VALUE_PCKS12CERTFILENAME ="";
65 private static final String DEFAULT_VALUE_PCKS12PASSPHRASE = "";
66 private static final String DEFAULT_VALUE_APPLICATIONID = "SDNR";
68 private static final String HEADER_KEY_APPLICATION = "X-FromAppId";
71 private static AaiConfig aaiConfig;
73 private final String aaiPropFile;
74 private final String baseUrl;
75 private String apiVersion;
76 private String applicationIdentifier;
77 private String username;
78 private String password;
79 private String pcks12CertificateFilename;
80 private String pcks12CertificatePassphrase;
81 private int connectionTimeout;
82 private final boolean deleteOnMountPointRemoved;
83 private final boolean trustAllCerts;
85 public boolean doDeleteOnMountPointRemoved() {
86 return this.deleteOnMountPointRemoved;
89 private Map<String, String> headers;
94 this.aaiPropFile = DEFAULT_VALUE_AAIPROP_FILE;
95 this.apiVersion=DEFAULT_VALUE_APIVERSION;
96 this.applicationIdentifier = DEFAULT_VALUE_APPLICATION;
97 this.baseUrl = DEFAULT_VALUE_BASEURL;
98 this.username = DEFAULT_VALUE_USERNAME;
99 this.password = DEFAULT_VALUE_USERPASSWORD;
100 this.deleteOnMountPointRemoved = DEFAULT_VALUE_DELETEONMOUNTPOINTREMOVED;
101 this.trustAllCerts=DEFAULT_VALUE_TRUSTALLCERTS;
102 this.applicationIdentifier=DEFAULT_VALUE_APPLICATIONID;
105 public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
106 this(config, configHandler, true);
109 public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
110 throws ConfigurationException {
111 super(config, configHandler, SECTION_MARKER_AAI);
113 this.aaiPropFile=this.getString(PROPERTY_KEY_AAIPROP_FILE, "");
114 AaiClientPropertiesFile aaiProperties = new AaiClientPropertiesFile(this.aaiPropFile);
115 String defBaseUrl=DEFAULT_VALUE_BASEURL;
116 String defPCKSCertFilename=DEFAULT_VALUE_PCKS12CERTFILENAME;
117 String defPCKSPassphrase=DEFAULT_VALUE_PCKS12PASSPHRASE;
118 String defApplicationId=DEFAULT_VALUE_APPLICATION;
119 int defconnectionTimeout=DEFAULT_VALUE_CONNECTION_TIMEOUT;
120 boolean loaded=false;
121 if(aaiProperties.exists())
123 LOG.debug("found another aaiclient.properties file");
126 aaiProperties.load();
128 LOG.debug("loaded successfully");
130 catch(IOException|NumberFormatException e)
132 LOG.warn("problem loading external properties file "+aaiProperties.getFilename()+": "+e.getMessage());
134 if(loaded) //preload new default values
137 value = aaiProperties.getRemoteUrl();
141 value = aaiProperties.getPCKS12CertFilename();
143 defPCKSCertFilename = value;
145 value = aaiProperties.getPCKS12Passphrase();
147 defPCKSPassphrase = value;
149 value = aaiProperties.getApplicationIdentifier();
151 defApplicationId = value;
155 LOG.debug("no aaiclient.properties file found");
159 this.baseUrl = this.getString(PROPERTY_KEY_BASEURL, defBaseUrl);
160 this.apiVersion=this.getString(PROPERTY_KEY_APIVERSION,DEFAULT_VALUE_APIVERSION);
161 String credentials = this.getString(PROPERTY_KEY_USERCREDENTIALS, DEFAULT_VALUE_USERCREDENTIALS);
162 if (credentials.contains(":")) {
164 this.username = credentials.split(":")[0];
165 this.password = credentials.split(":")[1];
166 } catch (Exception e) {
167 this.username = DEFAULT_VALUE_USERNAME;
168 this.password = DEFAULT_VALUE_USERPASSWORD;
171 this.username = DEFAULT_VALUE_USERNAME;
172 this.password = DEFAULT_VALUE_USERPASSWORD;
174 this.headers = _parseHeadersMap(this.getString(PROPERTY_KEY_HEADERS, DEFAULT_VALUE_HEADERS));
175 this.applicationIdentifier = this.getString(PROPERTY_KEY_APPLICATIONID, defApplicationId);
176 this.pcks12CertificateFilename=this.getString(PROPERTY_KEY_PCKS12CERTFILENAME, defPCKSCertFilename);
177 this.pcks12CertificatePassphrase=this.getString(PROPERTY_KEY_PCKS12PASSPHRASE, defPCKSPassphrase);
178 this.connectionTimeout = this.getInt(PROPERTY_KEY_CONNECTIONTIMEOUT, defconnectionTimeout);
179 this.deleteOnMountPointRemoved = this.getBoolean(PROPERTY_KEY_DELETEONMOUNTPOINTREMOVED,
180 DEFAULT_VALUE_DELETEONMOUNTPOINTREMOVED);
181 this.trustAllCerts = this.getBoolean(PROPERTY_KEY_TRUSTALLCERTS, DEFAULT_VALUE_TRUSTALLCERTS);
183 boolean missing=!this.hasKey(PROPERTY_KEY_APPLICATIONID)|| !this.hasKey(PROPERTY_KEY_CONNECTIONTIMEOUT)||
184 !this.hasKey(PROPERTY_KEY_TRUSTALLCERTS) || !this.hasKey(PROPERTY_KEY_PCKS12CERTFILENAME) ||
185 !this.hasKey(PROPERTY_KEY_PCKS12PASSPHRASE);
187 LOG.debug("some params missing in config file");
189 //re-save if external aaiproperties file changed to show that params are submitted internally
190 if(missing || aaiConfig!=null && aaiConfig!=this && (
191 !propertyEquals(aaiConfig.aaiPropFile, this.aaiPropFile) ||
192 !propertyEquals(aaiConfig.pcks12CertificateFilename, this.pcks12CertificateFilename) ||
193 !propertyEquals(aaiConfig.pcks12CertificatePassphrase, this.pcks12CertificatePassphrase) ||
194 !propertyEquals(aaiConfig.connectionTimeout, this.connectionTimeout)
198 LOG.debug("force saving because of reload changes from remote file");
202 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_BASEURL, this.baseUrl);
203 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_USERCREDENTIALS,
204 nullorempty(this.username) && nullorempty(this.password)?"":this.username + ":" + this.password);
205 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_HEADERS, _printHeadersMap(this.headers));
206 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_DELETEONMOUNTPOINTREMOVED,
207 this.deleteOnMountPointRemoved);
208 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_TRUSTALLCERTS, this.trustAllCerts);
209 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_AAIPROP_FILE, this.aaiPropFile);
210 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_APIVERSION,this.apiVersion);
211 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_APPLICATIONID, this.applicationIdentifier);
212 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_CONNECTIONTIMEOUT, this.connectionTimeout);
213 /*if(this.pcks12CertificateFilename !=null && !this.pcks12CertificateFilename.isEmpty() &&
214 this.pcks12CertificatePassphrase!=null && !this.pcks12CertificatePassphrase.isEmpty())*/
216 LOG.debug("no client credentials to save");
217 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_PCKS12CERTFILENAME, this.pcks12CertificateFilename);
218 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_PCKS12PASSPHRASE, this.pcks12CertificatePassphrase);
225 private boolean nullorempty(String s) {
226 return s==null || s.isEmpty();
229 public boolean isOff() {
230 return this.baseUrl == null || this.baseUrl.toLowerCase().equals("off");
233 private static boolean propertyEquals(final Object p1,final Object p2)
235 return p1==null && p2==null || p1 != null && p1.equals(p2);
237 private static boolean propertyEquals(final int p1,final int p2)
243 public int hashCode() {
244 final int prime = 31;
246 result = prime * result + (aaiPropFile == null ? 0 : aaiPropFile.hashCode());
247 result = prime * result + (apiVersion == null ? 0 : apiVersion.hashCode());
248 result = prime * result + (applicationIdentifier == null ? 0 : applicationIdentifier.hashCode());
249 result = prime * result + (baseUrl == null ? 0 : baseUrl.hashCode());
250 result = prime * result + connectionTimeout;
251 result = prime * result + (deleteOnMountPointRemoved ? 1231 : 1237);
252 result = prime * result + (headers == null ? 0 : headers.hashCode());
253 result = prime * result + (password == null ? 0 : password.hashCode());
254 result = prime * result + (pcks12CertificateFilename == null ? 0 : pcks12CertificateFilename.hashCode());
255 result = prime * result + (pcks12CertificatePassphrase == null ? 0 : pcks12CertificatePassphrase.hashCode());
256 result = prime * result + (trustAllCerts ? 1231 : 1237);
257 result = prime * result + (username == null ? 0 : username.hashCode());
262 public boolean equals(Object obj) {
269 if (getClass() != obj.getClass()) {
272 AaiConfig other = (AaiConfig) obj;
273 if (aaiPropFile == null) {
274 if (other.aaiPropFile != null) {
277 } else if (!aaiPropFile.equals(other.aaiPropFile)) {
280 if (apiVersion == null) {
281 if (other.apiVersion != null) {
284 } else if (!apiVersion.equals(other.apiVersion)) {
287 if (applicationIdentifier == null) {
288 if (other.applicationIdentifier != null) {
291 } else if (!applicationIdentifier.equals(other.applicationIdentifier)) {
294 if (baseUrl == null) {
295 if (other.baseUrl != null) {
298 } else if (!baseUrl.equals(other.baseUrl)) {
301 if (connectionTimeout != other.connectionTimeout) {
304 if (deleteOnMountPointRemoved != other.deleteOnMountPointRemoved) {
307 if (headers == null) {
308 if (other.headers != null) {
311 } else if (!headers.equals(other.headers)) {
314 if (password == null) {
315 if (other.password != null) {
318 } else if (!password.equals(other.password)) {
321 if (pcks12CertificateFilename == null) {
322 if (other.pcks12CertificateFilename != null) {
325 } else if (!pcks12CertificateFilename.equals(other.pcks12CertificateFilename)) {
328 if (pcks12CertificatePassphrase == null) {
329 if (other.pcks12CertificatePassphrase != null) {
332 } else if (!pcks12CertificatePassphrase.equals(other.pcks12CertificatePassphrase)) {
335 if (trustAllCerts != other.trustAllCerts) {
338 if (username == null) {
339 if (other.username != null) {
342 } else if (!username.equals(other.username)) {
348 public String getBaseUri() {
350 if(!this.apiVersion.startsWith("/")) {
351 s="/"+this.apiVersion;
357 public String getBaseUrl() {
358 String url=this.baseUrl;
359 if(!url.endsWith("/")) {
362 if(this.apiVersion.startsWith("/")) {
363 this.apiVersion=this.apiVersion.substring(1);
365 return url+this.apiVersion;
368 public Map<String, String> getHeaders() {
369 if (this.headers == null) {
370 this.headers = new HashMap<>();
372 this.headers.put(HEADER_KEY_APPLICATION, this.applicationIdentifier);
373 String s = this.headers.getOrDefault("Authorization", null);
374 if (nullorempty(s) && !nullorempty(this.username) && !nullorempty(this.password)) {
375 this.headers.put("Authorization", "Basic "
376 + new String(Base64.getEncoder().encode((this.username + ":" + this.password).getBytes())));
382 public String toString() {
383 return "AaiConfig [aaiPropFile=" + aaiPropFile + ", baseUrl=" + baseUrl + ", apiVersion=" + apiVersion
384 + ", applicationIdentifier=" + applicationIdentifier + ", username=" + username + ", password="
385 + password + ", pcks12CertificateFilename=" + pcks12CertificateFilename
386 + ", pcks12CertificatePassphrase=" + pcks12CertificatePassphrase + ", connectionTimeout="
387 + connectionTimeout + ", deleteOnMountPointRemoved=" + deleteOnMountPointRemoved + ", trustAllCerts="
388 + trustAllCerts + ", headers=" + this.getHeaders() + "]";
391 private static String _printHeadersMap(Map<String, String> headers) {
393 if (headers != null) {
395 for (Entry<String, String> entry : headers.entrySet()) {
399 r += "\"" + entry.getKey() + ":" + entry.getValue() + "\"";
407 private static Map<String, String> _parseHeadersMap(String s) throws JSONException {
408 Map<String, String> r = new HashMap<>();
409 JSONArray a = new JSONArray(s);
410 if (a != null && a.length() > 0) {
411 for (int i = 0; i < a.length(); i++) {
412 String item = a.getString(i);
413 String[] hlp = item.split(":");
414 if (hlp.length > 1) {
415 r.put(hlp[0], hlp[1]);
422 public static boolean isInstantiated() {
423 return aaiConfig != null;
426 public static AaiConfig getDefaultConfiguration() {
427 return new AaiConfig();
430 public static AaiConfig getAai(IniConfigurationFile config, ISubConfigHandler configHandler) {
431 if (aaiConfig == null) {
433 aaiConfig = new AaiConfig(config, configHandler);
434 } catch (ConfigurationException e) {
435 aaiConfig = AaiConfig.getDefaultConfiguration();
441 public static @Nullable AaiConfig reload() {
442 if (aaiConfig == null) {
447 tmpConfig = new AaiConfig(aaiConfig.getConfig(), aaiConfig.getConfigHandler(), false);
448 } catch (ConfigurationException e) {
449 tmpConfig = AaiConfig.getDefaultConfiguration();
450 LOG.warn("problem loading config: "+e.getMessage());
452 aaiConfig = tmpConfig;
456 public boolean getTrustAll() {
457 return this.trustAllCerts;
460 public String getPcks12CertificateFilename() {
461 return this.pcks12CertificateFilename;
464 public String getPcks12CertificatePassphrase() {
465 return this.pcks12CertificatePassphrase;
468 public int getConnectionTimeout() {
469 return this.connectionTimeout;
472 public static void clear() {