1 package org.opendaylight.mwtn.config.impl;
3 import java.io.IOException;
4 import java.util.Base64;
5 import java.util.HashMap;
7 import java.util.Map.Entry;
9 import org.json.JSONArray;
10 import org.json.JSONException;
11 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile;
12 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile.ConfigurationException;
13 import org.opendaylight.mwtn.config.impl.HtDevicemanagerConfiguration.ISubConfigHandler;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public class AaiConfig extends BaseSubConfig {
19 private static final Logger LOG = LoggerFactory.getLogger(AaiConfig.class);
21 private static final String SECTION_MARKER_AAI = "aai";
23 private static final String PROPERTY_KEY_AAIPROP_FILE ="aaiPropertiesFile";
24 private static final String PROPERTY_KEY_BASEURL = "aaiUrl";
25 private static final String PROPERTY_KEY_USERCREDENTIALS = "aaiUserCredentials";
26 private static final String PROPERTY_KEY_HEADERS = "aaiHeaders";
27 private static final String PROPERTY_KEY_DELETEONMOUNTPOINTREMOVED = "aaiDeleteOnMountpointRemove";
28 private static final String PROPERTY_KEY_TRUSTALLCERTS = "aaiTrustAllCerts";
29 private static final String PROPERTY_KEY_APIVERSION = "aaiApiVersion";
30 private static final String PROPERTY_KEY_PCKS12CERTFILENAME = "aaiPcks12ClientCertFile";
31 private static final String PROPERTY_KEY_PCKS12PASSPHRASE = "aaiPcks12ClientCertPassphrase";
32 private static final String PROPERTY_KEY_CONNECTIONTIMEOUT = "aaiClientConnectionTimeout";
33 private static final String PROPERTY_KEY_APPLICATIONID = "aaiApplicationId";
35 private static final String DEFAULT_VALUE_AAIPROP_FILE ="null";
36 private static final String DEFAULT_VALUE_BASEURL = "off";
37 private static final String DEFAULT_VALUE_APPLICATION = "SDNR";
38 private static final String DEFAULT_VALUE_USERNAME = "";
39 private static final String DEFAULT_VALUE_USERPASSWORD = "";
40 private static final String DEFAULT_VALUE_USERCREDENTIALS = "";
41 private static final String DEFAULT_VALUE_HEADERS = "[\"X-TransactionId: 9999\"]";
42 private static final boolean DEFAULT_VALUE_DELETEONMOUNTPOINTREMOVED = false;
43 private static final boolean DEFAULT_VALUE_TRUSTALLCERTS = false;
44 private static final int DEFAULT_VALUE_CONNECTION_TIMEOUT = 30000; //in ms
45 private static final String DEFAULT_VALUE_APIVERSION = "aai/v13";
46 private static final String DEFAULT_VALUE_PCKS12CERTFILENAME ="";
47 private static final String DEFAULT_VALUE_PCKS12PASSPHRASE = "";
48 private static final String DEFAULT_VALUE_APPLICATIONID = "SDNR";
50 private static final String HEADER_KEY_APPLICATION = "X-FromAppId";
53 private static AaiConfig aaiConfig;
55 private String aaiPropFile;
56 private String baseUrl;
57 private String apiVersion;
58 private String applicationIdentifier;
59 private String username;
60 private String password;
61 private String pcks12CertificateFilename;
62 private String pcks12CertificatePassphrase;
63 private int connectionTimeout;
64 private boolean deleteOnMountPointRemoved;
65 private boolean trustAllCerts;
67 public boolean doDeleteOnMountPointRemoved() {
68 return this.deleteOnMountPointRemoved;
71 private Map<String, String> headers;
76 this.aaiPropFile = DEFAULT_VALUE_AAIPROP_FILE;
77 this.apiVersion=DEFAULT_VALUE_APIVERSION;
78 this.applicationIdentifier = DEFAULT_VALUE_APPLICATION;
79 this.baseUrl = DEFAULT_VALUE_BASEURL;
80 this.username = DEFAULT_VALUE_USERNAME;
81 this.password = DEFAULT_VALUE_USERPASSWORD;
82 this.deleteOnMountPointRemoved = DEFAULT_VALUE_DELETEONMOUNTPOINTREMOVED;
83 this.trustAllCerts=DEFAULT_VALUE_TRUSTALLCERTS;
84 this.applicationIdentifier=DEFAULT_VALUE_APPLICATIONID;
88 * private void change(AaiConfig cfg) { this.baseUrl=cfg.baseUrl;
89 * this.username=cfg.username; this.password=cfg.password;
90 * this.deleteOnMountPointRemoved=cfg.deleteOnMountPointRemoved; }
92 public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
93 this(config, configHandler, true);
96 public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
97 throws ConfigurationException {
98 super(config, configHandler, SECTION_MARKER_AAI);
100 this.aaiPropFile=this.getString(PROPERTY_KEY_AAIPROP_FILE, "");
101 AaiClientPropertiesFile aaiProperties = new AaiClientPropertiesFile(this.aaiPropFile);
102 String defBaseUrl=DEFAULT_VALUE_BASEURL;
103 String defPCKSCertFilename=DEFAULT_VALUE_PCKS12CERTFILENAME;
104 String defPCKSPassphrase=DEFAULT_VALUE_PCKS12PASSPHRASE;
105 String defApplicationId=DEFAULT_VALUE_APPLICATION;
106 int defconnectionTimeout=DEFAULT_VALUE_CONNECTION_TIMEOUT;
107 boolean loaded=false;
108 if(aaiProperties.exists())
110 LOG.debug("found another aaiclient.properties file");
113 aaiProperties.load();
115 LOG.debug("loaded successfully");
117 catch(IOException|NumberFormatException e)
119 LOG.warn("problem loading external properties file "+aaiProperties.getFilename()+": "+e.getMessage());
121 if(loaded) //preload new default values
124 value = aaiProperties.getRemoteUrl();
127 value = aaiProperties.getPCKS12CertFilename();
129 defPCKSCertFilename = value;
130 value = aaiProperties.getPCKS12Passphrase();
132 defPCKSPassphrase = value;
133 value = aaiProperties.getApplicationIdentifier();
135 defApplicationId = value;
139 LOG.debug("no aaiclient.properties file found");
142 this.baseUrl = this.getString(PROPERTY_KEY_BASEURL, defBaseUrl);
143 this.apiVersion=this.getString(PROPERTY_KEY_APIVERSION,DEFAULT_VALUE_APIVERSION);
144 String credentials = this.getString(PROPERTY_KEY_USERCREDENTIALS, DEFAULT_VALUE_USERCREDENTIALS);
145 if (credentials.contains(":")) {
147 this.username = credentials.split(":")[0];
148 this.password = credentials.split(":")[1];
149 } catch (Exception e) {
150 this.username = DEFAULT_VALUE_USERNAME;
151 this.password = DEFAULT_VALUE_USERPASSWORD;
154 this.username = DEFAULT_VALUE_USERNAME;
155 this.password = DEFAULT_VALUE_USERPASSWORD;
157 this.headers = _parseHeadersMap(this.getString(PROPERTY_KEY_HEADERS, DEFAULT_VALUE_HEADERS));
158 this.applicationIdentifier = this.getString(PROPERTY_KEY_APPLICATIONID, defApplicationId);
159 this.pcks12CertificateFilename=this.getString(PROPERTY_KEY_PCKS12CERTFILENAME, defPCKSCertFilename);
160 this.pcks12CertificatePassphrase=this.getString(PROPERTY_KEY_PCKS12PASSPHRASE, defPCKSPassphrase);
161 this.connectionTimeout = this.getInt(PROPERTY_KEY_CONNECTIONTIMEOUT, defconnectionTimeout);
162 this.deleteOnMountPointRemoved = this.getBoolean(PROPERTY_KEY_DELETEONMOUNTPOINTREMOVED,
163 DEFAULT_VALUE_DELETEONMOUNTPOINTREMOVED);
164 this.trustAllCerts = this.getBoolean(PROPERTY_KEY_TRUSTALLCERTS, DEFAULT_VALUE_TRUSTALLCERTS);
166 boolean missing=(!this.hasKey(PROPERTY_KEY_APPLICATIONID))|| (!this.hasKey(PROPERTY_KEY_CONNECTIONTIMEOUT))||
167 (!this.hasKey(PROPERTY_KEY_TRUSTALLCERTS)) || (!this.hasKey(PROPERTY_KEY_PCKS12CERTFILENAME)) ||
168 (!this.hasKey(PROPERTY_KEY_PCKS12PASSPHRASE));
170 LOG.debug("some params missing in config file");
171 //re-save if external aaiproperties file changed to show that params are submitted internally
172 if(missing || (aaiConfig!=null && aaiConfig!=this && (
173 !propertyEquals(aaiConfig.aaiPropFile, this.aaiPropFile) ||
174 !propertyEquals(aaiConfig.pcks12CertificateFilename, this.pcks12CertificateFilename) ||
175 !propertyEquals(aaiConfig.pcks12CertificatePassphrase, this.pcks12CertificatePassphrase) ||
176 !propertyEquals(aaiConfig.connectionTimeout, this.connectionTimeout)
180 LOG.debug("force saving because of reload changes from remote file");
184 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_BASEURL, this.baseUrl);
185 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_USERCREDENTIALS,
186 (nullorempty(this.username) && nullorempty(this.password))?"":(this.username + ":" + this.password));
187 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_HEADERS, _printHeadersMap(this.headers));
188 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_DELETEONMOUNTPOINTREMOVED,
189 this.deleteOnMountPointRemoved);
190 config.setProperty(SECTION_MARKER_AAI + "." + PROPERTY_KEY_TRUSTALLCERTS, this.trustAllCerts);
191 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_AAIPROP_FILE, this.aaiPropFile);
192 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_APIVERSION,this.apiVersion);
193 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_APPLICATIONID, this.applicationIdentifier);
194 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_CONNECTIONTIMEOUT, this.connectionTimeout);
195 /*if(this.pcks12CertificateFilename !=null && !this.pcks12CertificateFilename.isEmpty() &&
196 this.pcks12CertificatePassphrase!=null && !this.pcks12CertificatePassphrase.isEmpty())*/
198 LOG.debug("no client credentials to save");
199 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_PCKS12CERTFILENAME, this.pcks12CertificateFilename);
200 config.setProperty(SECTION_MARKER_AAI+"."+PROPERTY_KEY_PCKS12PASSPHRASE, this.pcks12CertificatePassphrase);
207 private boolean nullorempty(String s) {
208 return s==null || s.isEmpty();
211 public boolean isOff() {
212 return this.baseUrl == null || this.baseUrl.toLowerCase().equals("off");
215 private static boolean propertyEquals(final Object p1,final Object p2)
217 return (p1==null && (p2==null) || p1.equals(p2));
219 private static boolean propertyEquals(final boolean p1,final boolean p2)
223 private static boolean propertyEquals(final int p1,final int p2)
228 public boolean equals(Object obj) {
229 if (obj instanceof AaiConfig) {
230 AaiConfig cobj = (AaiConfig) obj;
231 if(!AaiConfig.propertyEquals(cobj.baseUrl, this.baseUrl))
233 if(!AaiConfig.propertyEquals(cobj.apiVersion, this.apiVersion))
235 if(!AaiConfig.propertyEquals(cobj.username, this.username))
237 if(!AaiConfig.propertyEquals(cobj.password, this.password))
239 if(!AaiConfig.propertyEquals(cobj.deleteOnMountPointRemoved, this.deleteOnMountPointRemoved))
241 if(!AaiConfig.propertyEquals(cobj.headers, this.headers))
243 if(!AaiConfig.propertyEquals(cobj.trustAllCerts, this.trustAllCerts))
245 if(!AaiConfig.propertyEquals(cobj.aaiPropFile, this.aaiPropFile))
247 if(!AaiConfig.propertyEquals(cobj.connectionTimeout, this.connectionTimeout))
249 if(!AaiConfig.propertyEquals(cobj.pcks12CertificateFilename, this.pcks12CertificateFilename))
251 if(!AaiConfig.propertyEquals(cobj.pcks12CertificatePassphrase, this.pcks12CertificatePassphrase))
253 if(!AaiConfig.propertyEquals(cobj.applicationIdentifier, this.applicationIdentifier))
257 if (!((cobj.baseUrl == null && this.baseUrl == null) || cobj.baseUrl.equals(this.baseUrl)))
259 if (!((cobj.apiVersion == null && this.apiVersion == null) || cobj.apiVersion.equals(this.apiVersion)))
261 if (!((cobj.username == null && this.username == null) || cobj.username.equals(this.username)))
263 if (!((cobj.password == null && this.password == null) || cobj.password.equals(this.password)))
265 if (!(cobj.deleteOnMountPointRemoved != this.deleteOnMountPointRemoved))
267 if (!((cobj.headers == null && this.headers == null) || cobj.headers.equals(this.headers)))
269 if (!(cobj.trustAllCerts != this.trustAllCerts))
274 return super.equals(obj);
277 public String getBaseUrl() {
278 String url=this.baseUrl;
279 if(!url.endsWith("/"))
281 if(this.apiVersion.startsWith("/"))
282 this.apiVersion=this.apiVersion.substring(1);
283 return url+this.apiVersion;
286 public Map<String, String> getHeaders() {
287 if (this.headers == null)
288 this.headers = new HashMap<String, String>();
289 this.headers.put(HEADER_KEY_APPLICATION, this.applicationIdentifier);
290 String s = this.headers.getOrDefault("Authorization", null);
291 if (nullorempty(s) && !nullorempty(this.username) && !nullorempty(this.password)) {
292 this.headers.put("Authorization", "Basic "
293 + new String(Base64.getEncoder().encode((this.username + ":" + this.password).getBytes())));
299 public String toString() {
300 return "AaiConfig [aaiPropFile=" + aaiPropFile + ", baseUrl=" + baseUrl + ", apiVersion=" + apiVersion
301 + ", applicationIdentifier=" + applicationIdentifier + ", username=" + username + ", password="
302 + password + ", pcks12CertificateFilename=" + pcks12CertificateFilename
303 + ", pcks12CertificatePassphrase=" + pcks12CertificatePassphrase + ", connectionTimeout="
304 + connectionTimeout + ", deleteOnMountPointRemoved=" + deleteOnMountPointRemoved + ", trustAllCerts="
305 + trustAllCerts + ", headers=" + this.getHeaders() + "]";
308 private static String _printHeadersMap(Map<String, String> headers) {
310 if (headers != null) {
312 for (Entry<String, String> entry : headers.entrySet()) {
315 r += "\"" + entry.getKey() + ":" + entry.getValue() + "\"";
323 private static Map<String, String> _parseHeadersMap(String s) throws JSONException {
324 Map<String, String> r = new HashMap<String, String>();
325 JSONArray a = new JSONArray(s);
326 if (a != null && a.length() > 0) {
327 for (int i = 0; i < a.length(); i++) {
328 String item = a.getString(i);
329 String[] hlp = item.split(":");
330 if (hlp.length > 1) {
331 r.put(hlp[0], hlp[1]);
338 public static boolean isInstantiated() {
339 return aaiConfig != null;
342 public static AaiConfig getDefaultConfiguration() {
343 return new AaiConfig();
346 public static AaiConfig getAai(IniConfigurationFile config, ISubConfigHandler configHandler) {
347 if (aaiConfig == null)
349 aaiConfig = new AaiConfig(config, configHandler);
350 } catch (ConfigurationException e) {
351 aaiConfig = AaiConfig.getDefaultConfiguration();
356 public static AaiConfig reload() {
357 if (aaiConfig == null)
361 tmpConfig = new AaiConfig(aaiConfig.getConfig(), aaiConfig.getConfigHandler(), false);
362 } catch (ConfigurationException e) {
363 tmpConfig = AaiConfig.getDefaultConfiguration();
364 LOG.warn("problem loading config: "+e.getMessage());
366 aaiConfig = tmpConfig;
370 public boolean getTrustAll() {
371 return this.trustAllCerts;
374 public String getPcks12CertificateFilename() {
375 return this.pcks12CertificateFilename;
378 public String getPcks12CertificatePassphrase() {
379 return this.pcks12CertificatePassphrase;
382 public int getConnectionTimeout() {
383 return this.connectionTimeout;