b30b3271d26b70f140545b7e172d689c2c67de55
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl;
19
20 import java.io.IOException;
21 import java.util.Base64;
22 import java.util.HashMap;
23 import java.util.Map;
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;
34
35 public class AaiConfig extends BaseSubConfig {
36
37     private static final Logger LOG = LoggerFactory.getLogger(AaiConfig.class);
38
39     private static final String SECTION_MARKER_AAI = "aai";
40
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";
52
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";
67
68     private static final String HEADER_KEY_APPLICATION = "X-FromAppId";
69
70
71     private static AaiConfig aaiConfig;
72
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;
84
85     public boolean doDeleteOnMountPointRemoved() {
86         return this.deleteOnMountPointRemoved;
87     }
88
89     private Map<String, String> headers;
90
91
92     private AaiConfig() {
93         super();
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;
103     }
104
105     public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
106         this(config, configHandler, true);
107     }
108
109     public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
110             throws ConfigurationException {
111         super(config, configHandler, SECTION_MARKER_AAI);
112         // load
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())
122         {
123             LOG.debug("found another aaiclient.properties file");
124             try
125             {
126                 aaiProperties.load();
127                 loaded=true;
128                 LOG.debug("loaded successfully");
129             }
130             catch(IOException|NumberFormatException e)
131             {
132                 LOG.warn("problem loading external properties file "+aaiProperties.getFilename()+": "+e.getMessage());
133             }
134             if(loaded)    //preload new default values
135             {
136                 String value;
137                 value = aaiProperties.getRemoteUrl();
138                 if (value != null) {
139                     defBaseUrl = value;
140                 }
141                 value = aaiProperties.getPCKS12CertFilename();
142                 if (value != null) {
143                     defPCKSCertFilename = value;
144                 }
145                 value = aaiProperties.getPCKS12Passphrase();
146                 if (value != null) {
147                     defPCKSPassphrase = value;
148                 }
149                 value = aaiProperties.getApplicationIdentifier();
150                 if (value != null) {
151                     defApplicationId = value;
152                 }
153             }
154         } else {
155             LOG.debug("no aaiclient.properties file found");
156         }
157
158
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(":")) {
163             try {
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;
169             }
170         } else {
171             this.username = DEFAULT_VALUE_USERNAME;
172             this.password = DEFAULT_VALUE_USERPASSWORD;
173         }
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);
182
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);
186         if(missing) {
187             LOG.debug("some params missing in config file");
188         }
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)
195
196                 ))
197         {
198             LOG.debug("force saving because of reload changes from remote file");
199             save=true;
200         }
201         if (save) {
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())*/
215             {
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);
219             }
220             LOG.debug("save");
221             this.save();
222         }
223     }
224
225     private boolean nullorempty(String s) {
226         return s==null || s.isEmpty();
227     }
228
229     public boolean isOff() {
230         return this.baseUrl == null || this.baseUrl.toLowerCase().equals("off");
231     }
232
233     private static boolean propertyEquals(final Object p1,final Object p2)
234     {
235         return p1==null && p2==null || p1 != null && p1.equals(p2);
236     }
237     private static boolean propertyEquals(final int p1,final int p2)
238     {
239         return p1==p2;
240     }
241
242     @Override
243     public int hashCode() {
244         final int prime = 31;
245         int result = 1;
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());
258         return result;
259     }
260
261     @Override
262     public boolean equals(Object obj) {
263         if (this == obj) {
264             return true;
265         }
266         if (obj == null) {
267             return false;
268         }
269         if (getClass() != obj.getClass()) {
270             return false;
271         }
272         AaiConfig other = (AaiConfig) obj;
273         if (aaiPropFile == null) {
274             if (other.aaiPropFile != null) {
275                 return false;
276             }
277         } else if (!aaiPropFile.equals(other.aaiPropFile)) {
278             return false;
279         }
280         if (apiVersion == null) {
281             if (other.apiVersion != null) {
282                 return false;
283             }
284         } else if (!apiVersion.equals(other.apiVersion)) {
285             return false;
286         }
287         if (applicationIdentifier == null) {
288             if (other.applicationIdentifier != null) {
289                 return false;
290             }
291         } else if (!applicationIdentifier.equals(other.applicationIdentifier)) {
292             return false;
293         }
294         if (baseUrl == null) {
295             if (other.baseUrl != null) {
296                 return false;
297             }
298         } else if (!baseUrl.equals(other.baseUrl)) {
299             return false;
300         }
301         if (connectionTimeout != other.connectionTimeout) {
302             return false;
303         }
304         if (deleteOnMountPointRemoved != other.deleteOnMountPointRemoved) {
305             return false;
306         }
307         if (headers == null) {
308             if (other.headers != null) {
309                 return false;
310             }
311         } else if (!headers.equals(other.headers)) {
312             return false;
313         }
314         if (password == null) {
315             if (other.password != null) {
316                 return false;
317             }
318         } else if (!password.equals(other.password)) {
319             return false;
320         }
321         if (pcks12CertificateFilename == null) {
322             if (other.pcks12CertificateFilename != null) {
323                 return false;
324             }
325         } else if (!pcks12CertificateFilename.equals(other.pcks12CertificateFilename)) {
326             return false;
327         }
328         if (pcks12CertificatePassphrase == null) {
329             if (other.pcks12CertificatePassphrase != null) {
330                 return false;
331             }
332         } else if (!pcks12CertificatePassphrase.equals(other.pcks12CertificatePassphrase)) {
333             return false;
334         }
335         if (trustAllCerts != other.trustAllCerts) {
336             return false;
337         }
338         if (username == null) {
339             if (other.username != null) {
340                 return false;
341             }
342         } else if (!username.equals(other.username)) {
343             return false;
344         }
345         return true;
346     }
347
348     public String getBaseUrl() {
349         String url=this.baseUrl;
350         if(!url.endsWith("/")) {
351             url+="/";
352         }
353         if(this.apiVersion.startsWith("/")) {
354             this.apiVersion=this.apiVersion.substring(1);
355         }
356         return url+this.apiVersion;
357     }
358
359     public Map<String, String> getHeaders() {
360         if (this.headers == null) {
361             this.headers = new HashMap<>();
362         }
363         this.headers.put(HEADER_KEY_APPLICATION, this.applicationIdentifier);
364         String s = this.headers.getOrDefault("Authorization", null);
365         if (nullorempty(s) && !nullorempty(this.username) && !nullorempty(this.password)) {
366             this.headers.put("Authorization", "Basic "
367                     + new String(Base64.getEncoder().encode((this.username + ":" + this.password).getBytes())));
368         }
369         return this.headers;
370     }
371
372     @Override
373     public String toString() {
374         return "AaiConfig [aaiPropFile=" + aaiPropFile + ", baseUrl=" + baseUrl + ", apiVersion=" + apiVersion
375                 + ", applicationIdentifier=" + applicationIdentifier + ", username=" + username + ", password="
376                 + password + ", pcks12CertificateFilename=" + pcks12CertificateFilename
377                 + ", pcks12CertificatePassphrase=" + pcks12CertificatePassphrase + ", connectionTimeout="
378                 + connectionTimeout + ", deleteOnMountPointRemoved=" + deleteOnMountPointRemoved + ", trustAllCerts="
379                 + trustAllCerts + ", headers=" + this.getHeaders() + "]";
380     }
381
382     private static String _printHeadersMap(Map<String, String> headers) {
383         String r = "[";
384         if (headers != null) {
385             int i = 0;
386             for (Entry<String, String> entry : headers.entrySet()) {
387                 if (i > 0) {
388                     r += ",";
389                 }
390                 r += "\"" + entry.getKey() + ":" + entry.getValue() + "\"";
391                 i++;
392             }
393         }
394         r += "]";
395         return r;
396     }
397
398     private static Map<String, String> _parseHeadersMap(String s) throws JSONException {
399         Map<String, String> r = new HashMap<>();
400         JSONArray a = new JSONArray(s);
401         if (a != null && a.length() > 0) {
402             for (int i = 0; i < a.length(); i++) {
403                 String item = a.getString(i);
404                 String[] hlp = item.split(":");
405                 if (hlp.length > 1) {
406                     r.put(hlp[0], hlp[1]);
407                 }
408             }
409         }
410         return r;
411     }
412
413     public static boolean isInstantiated() {
414         return aaiConfig != null;
415     }
416
417     public static AaiConfig getDefaultConfiguration() {
418         return new AaiConfig();
419     }
420
421     public static AaiConfig getAai(IniConfigurationFile config, ISubConfigHandler configHandler) {
422         if (aaiConfig == null) {
423             try {
424                 aaiConfig = new AaiConfig(config, configHandler);
425             } catch (ConfigurationException e) {
426                 aaiConfig = AaiConfig.getDefaultConfiguration();
427             }
428         }
429         return aaiConfig;
430     }
431
432     public static @Nullable AaiConfig reload() {
433         if (aaiConfig == null) {
434             return null;
435         }
436         AaiConfig tmpConfig;
437         try {
438             tmpConfig = new AaiConfig(aaiConfig.getConfig(), aaiConfig.getConfigHandler(), false);
439         } catch (ConfigurationException e) {
440             tmpConfig = AaiConfig.getDefaultConfiguration();
441             LOG.warn("problem loading config: "+e.getMessage());
442         }
443         aaiConfig = tmpConfig;
444         return aaiConfig;
445     }
446
447     public boolean getTrustAll() {
448         return this.trustAllCerts;
449     }
450
451     public String getPcks12CertificateFilename() {
452         return this.pcks12CertificateFilename;
453     }
454
455     public String getPcks12CertificatePassphrase() {
456         return this.pcks12CertificatePassphrase;
457     }
458
459     public int getConnectionTimeout() {
460         return this.connectionTimeout;
461     }
462
463     public static void clear() {
464         aaiConfig=null;
465     }
466
467 }