Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / config / impl / AaiConfig.java
1 package org.opendaylight.mwtn.config.impl;
2
3 import java.io.IOException;
4 import java.util.Base64;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Map.Entry;
8
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;
16
17 public class AaiConfig extends BaseSubConfig {
18
19         private static final Logger LOG = LoggerFactory.getLogger(AaiConfig.class);
20
21         private static final String SECTION_MARKER_AAI = "aai";
22
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";
34
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";
49
50         private static final String HEADER_KEY_APPLICATION = "X-FromAppId";
51
52
53         private static AaiConfig aaiConfig;
54
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;
66
67         public boolean doDeleteOnMountPointRemoved() {
68                 return this.deleteOnMountPointRemoved;
69         }
70
71         private Map<String, String> headers;
72
73
74         private AaiConfig() {
75                 super();
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;
85         }
86
87         /*
88          * private void change(AaiConfig cfg) { this.baseUrl=cfg.baseUrl;
89          * this.username=cfg.username; this.password=cfg.password;
90          * this.deleteOnMountPointRemoved=cfg.deleteOnMountPointRemoved; }
91          */
92         public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
93                 this(config, configHandler, true);
94         }
95
96         public AaiConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
97                         throws ConfigurationException {
98                 super(config, configHandler, SECTION_MARKER_AAI);
99                 // load
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())
109                 {
110                         LOG.debug("found another aaiclient.properties file");
111                         try
112                         {
113                                 aaiProperties.load();
114                                 loaded=true;
115                                 LOG.debug("loaded successfully");
116                         }
117                         catch(IOException|NumberFormatException e)
118                         {
119                                 LOG.warn("problem loading external properties file "+aaiProperties.getFilename()+": "+e.getMessage());
120                         }
121                         if(loaded)      //preload new default values
122                         {
123                                 String value;
124                                 value = aaiProperties.getRemoteUrl();
125                                 if (value != null)
126                                         defBaseUrl = value;
127                                 value = aaiProperties.getPCKS12CertFilename();
128                                 if (value != null)
129                                         defPCKSCertFilename = value;
130                                 value = aaiProperties.getPCKS12Passphrase();
131                                 if (value != null)
132                                         defPCKSPassphrase = value;
133                                 value = aaiProperties.getApplicationIdentifier();
134                                 if (value != null)
135                                         defApplicationId = value;
136                         }
137                 }
138                 else
139                         LOG.debug("no aaiclient.properties file found");
140
141
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(":")) {
146                         try {
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;
152                         }
153                 } else {
154                         this.username = DEFAULT_VALUE_USERNAME;
155                         this.password = DEFAULT_VALUE_USERPASSWORD;
156                 }
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);
165
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));
169                 if(missing)
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)
177
178                                 )))
179                 {
180                         LOG.debug("force saving because of reload changes from remote file");
181                         save=true;
182                 }
183                 if (save) {
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())*/
197                         {
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);
201                         }
202                         LOG.debug("save");
203                         this.save();
204                 }
205         }
206
207         private boolean nullorempty(String s) {
208                 return s==null || s.isEmpty();
209         }
210
211         public boolean isOff() {
212                 return this.baseUrl == null || this.baseUrl.toLowerCase().equals("off");
213         }
214
215         private static boolean propertyEquals(final Object p1,final Object p2)
216         {
217                 return (p1==null && (p2==null) || p1.equals(p2));
218         }
219         private static boolean propertyEquals(final boolean p1,final boolean p2)
220         {
221                 return p1==p2;
222         }
223         private static boolean propertyEquals(final int p1,final int p2)
224         {
225                 return p1==p2;
226         }
227         @Override
228         public boolean equals(Object obj) {
229                 if (obj instanceof AaiConfig) {
230                         AaiConfig cobj = (AaiConfig) obj;
231                         if(!AaiConfig.propertyEquals(cobj.baseUrl, this.baseUrl))
232                                 return false;
233                         if(!AaiConfig.propertyEquals(cobj.apiVersion, this.apiVersion))
234                                 return false;
235                         if(!AaiConfig.propertyEquals(cobj.username, this.username))
236                                 return false;
237                         if(!AaiConfig.propertyEquals(cobj.password, this.password))
238                                 return false;
239                         if(!AaiConfig.propertyEquals(cobj.deleteOnMountPointRemoved, this.deleteOnMountPointRemoved))
240                                 return false;
241                         if(!AaiConfig.propertyEquals(cobj.headers, this.headers))
242                                 return false;
243                         if(!AaiConfig.propertyEquals(cobj.trustAllCerts, this.trustAllCerts))
244                                 return false;
245                         if(!AaiConfig.propertyEquals(cobj.aaiPropFile, this.aaiPropFile))
246                                 return false;
247                         if(!AaiConfig.propertyEquals(cobj.connectionTimeout, this.connectionTimeout))
248                                 return false;
249                         if(!AaiConfig.propertyEquals(cobj.pcks12CertificateFilename, this.pcks12CertificateFilename))
250                                 return false;
251                         if(!AaiConfig.propertyEquals(cobj.pcks12CertificatePassphrase, this.pcks12CertificatePassphrase))
252                                 return false;
253                         if(!AaiConfig.propertyEquals(cobj.applicationIdentifier, this.applicationIdentifier))
254                                 return false;
255
256                         /*
257                         if (!((cobj.baseUrl == null && this.baseUrl == null) || cobj.baseUrl.equals(this.baseUrl)))
258                                 return false;
259                         if (!((cobj.apiVersion == null && this.apiVersion == null) || cobj.apiVersion.equals(this.apiVersion)))
260                                 return false;
261                         if (!((cobj.username == null && this.username == null) || cobj.username.equals(this.username)))
262                                 return false;
263                         if (!((cobj.password == null && this.password == null) || cobj.password.equals(this.password)))
264                                 return false;
265                         if (!(cobj.deleteOnMountPointRemoved != this.deleteOnMountPointRemoved))
266                                 return false;
267                         if (!((cobj.headers == null && this.headers == null) || cobj.headers.equals(this.headers)))
268                                 return false;
269                         if (!(cobj.trustAllCerts != this.trustAllCerts))
270                                 return false;
271 */
272                         return true;
273                 }
274                 return super.equals(obj);
275         }
276
277         public String getBaseUrl() {
278                 String url=this.baseUrl;
279                 if(!url.endsWith("/"))
280                         url+="/";
281                 if(this.apiVersion.startsWith("/"))
282                         this.apiVersion=this.apiVersion.substring(1);
283                 return url+this.apiVersion;
284         }
285
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())));
294                 }
295                 return this.headers;
296         }
297
298         @Override
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() + "]";
306         }
307
308         private static String _printHeadersMap(Map<String, String> headers) {
309                 String r = "[";
310                 if (headers != null) {
311                         int i = 0;
312                         for (Entry<String, String> entry : headers.entrySet()) {
313                                 if (i > 0)
314                                         r += ",";
315                                 r += "\"" + entry.getKey() + ":" + entry.getValue() + "\"";
316                                 i++;
317                         }
318                 }
319                 r += "]";
320                 return r;
321         }
322
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]);
332                                 }
333                         }
334                 }
335                 return r;
336         }
337
338         public static boolean isInstantiated() {
339                 return aaiConfig != null;
340         }
341
342         public static AaiConfig getDefaultConfiguration() {
343                 return new AaiConfig();
344         }
345
346         public static AaiConfig getAai(IniConfigurationFile config, ISubConfigHandler configHandler) {
347                 if (aaiConfig == null)
348                         try {
349                                 aaiConfig = new AaiConfig(config, configHandler);
350                         } catch (ConfigurationException e) {
351                                 aaiConfig = AaiConfig.getDefaultConfiguration();
352                         }
353                 return aaiConfig;
354         }
355
356         public static AaiConfig reload() {
357                 if (aaiConfig == null)
358                         return null;
359                 AaiConfig tmpConfig;
360                 try {
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());
365                 }
366                 aaiConfig = tmpConfig;
367                 return aaiConfig;
368         }
369
370         public boolean getTrustAll() {
371                 return this.trustAllCerts;
372         }
373
374         public String getPcks12CertificateFilename() {
375                 return this.pcks12CertificateFilename;
376         }
377
378         public String getPcks12CertificatePassphrase() {
379                 return this.pcks12CertificatePassphrase;
380         }
381
382         public int getConnectionTimeout() {
383                 return this.connectionTimeout;
384         }
385
386 }