Update AAF Version 1.0.0
[aaf/cadi.git] / aaf / src / main / java / org / onap / aaf / cadi / aaf / ConnectivityTest.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.aaf;\r
24 \r
25 import java.io.IOException;\r
26 import java.io.PrintStream;\r
27 import java.lang.reflect.Field;\r
28 import java.net.HttpURLConnection;\r
29 import java.net.InetAddress;\r
30 import java.net.InetSocketAddress;\r
31 import java.net.Socket;\r
32 import java.net.URI;\r
33 import java.net.UnknownHostException;\r
34 import java.util.Date;\r
35 \r
36 import org.onap.aaf.cadi.CadiException;\r
37 import org.onap.aaf.cadi.Locator;\r
38 import org.onap.aaf.cadi.LocatorException;\r
39 import org.onap.aaf.cadi.Lur;\r
40 import org.onap.aaf.cadi.PropAccess;\r
41 import org.onap.aaf.cadi.SecuritySetter;\r
42 import org.onap.aaf.cadi.TrustChecker;\r
43 import org.onap.aaf.cadi.Locator.Item;\r
44 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;\r
45 import org.onap.aaf.cadi.aaf.v2_0.AAFConDME2;\r
46 import org.onap.aaf.cadi.client.Future;\r
47 import org.onap.aaf.cadi.config.Config;\r
48 import org.onap.aaf.cadi.config.SecurityInfoC;\r
49 import org.onap.aaf.cadi.http.HBasicAuthSS;\r
50 import org.onap.aaf.cadi.http.HClient;\r
51 import org.onap.aaf.cadi.http.HX509SS;\r
52 import org.onap.aaf.cadi.locator.DME2Locator;\r
53 import org.onap.aaf.cadi.locator.PropertyLocator;\r
54 \r
55 import com.att.aft.dme2.api.DME2Client;\r
56 import com.att.aft.dme2.api.DME2Manager;\r
57 import org.onap.aaf.inno.env.APIException;\r
58 import org.onap.aaf.rosetta.env.RosettaDF;\r
59 import org.onap.aaf.rosetta.env.RosettaEnv;\r
60 \r
61 import aaf.v2_0.Perms;\r
62 \r
63 public class ConnectivityTest {\r
64         private static final String PROD = "PROD";\r
65         private static final String SRV_RESOLVE = "https://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=2.0/envContext=%s/routeOffer=%s";\r
66         private static final String GW_RESOLVE = "https://DME2RESOLVE/service=com.att.authz.authz-gw/version=2.0/envContext=%s/routeOffer=%s";\r
67         \r
68         public static void main(String[] args) {\r
69                 if(args.length<2) {\r
70                         System.out.println("Usage: ConnectivityTester <TEST|IST|PROD> <cadi_prop_files>");\r
71                 } else {\r
72                         print(true,"START OF CONNECTIVITY TESTS",new Date().toString(),System.getProperty("user.name"),\r
73                                         "Note: All API Calls are /authz/perms/user/<MechID/Alias of the caller>");\r
74 \r
75                         final String aaf_env = args[0];\r
76                         args[1]=Config.CADI_PROP_FILES+'='+args[1];\r
77                         \r
78                         PropAccess pa = new PropAccess(args);\r
79                         String user = pa.getProperty(Config.AAF_MECHID);\r
80                         String pass = pa.getProperty(Config.AAF_MECHPASS);\r
81                         String alias = pa.getProperty(Config.CADI_ALIAS);\r
82                         if(user==null) {\r
83                                 user=alias;\r
84                         }\r
85                         RosettaEnv env = new RosettaEnv(pa.getProperties());\r
86                         \r
87                         try {\r
88                                 RosettaDF<Perms> permsDF = env.newDataFactory(Perms.class);\r
89                                 SecurityInfoC<HttpURLConnection> si = new SecurityInfoC<HttpURLConnection>(pa);\r
90                                 HBasicAuthSS hbass = new HBasicAuthSS(pa,si);\r
91                                 if(hbass.getID()==null) {\r
92                                         hbass=null; // not configured with ID.\r
93                                 }\r
94                                 HX509SS hxss=null;\r
95                                 AAFCon<?> aafcon;\r
96                                 \r
97                                 try {\r
98                                         hxss = new HX509SS(user,si);\r
99                                 } catch(Exception e) {\r
100                                         e.printStackTrace();\r
101                                         print(false,"Continuing");\r
102                                 }\r
103                                 String aafurl;\r
104                                 if(user==null || (pass==null && alias==null)) {\r
105                                         System.out.printf("ERROR: DME2 Client cannot be tested with out %s and %s properties"\r
106                                                         , Config.AAF_MECHID, Config.AAF_MECHPASS );\r
107                                 } else {\r
108                                         if("TEST".equals(aaf_env) || "IST".equals(aaf_env) || "PROD".equals(aaf_env)) {\r
109                                                 DME2Manager dm = null;\r
110                                                 print(false,"Attempt DME2Manager Load");\r
111                                                 if(Class.forName("com.att.aft.dme2.api.DME2Manager")==null) {\r
112                                                         print(true,"DME2 jar is not available:  Skipping DME2 Tests");\r
113                                                 } else { // DME2 Client Tests\r
114                                                         pass=pa.decrypt(pass,false);\r
115                                                         // Out of the box DME2\r
116                                                         aafurl = String.format(SRV_RESOLVE, aaf_env, PROD.equals(aaf_env)?"DEFAULT":"BAU_SE");\r
117                                                         print(true,"TEST CADI Config",aafurl);\r
118                                                         aafcon = testConfig(pa,aafurl);\r
119                                                         test(aafcon,permsDF,user);\r
120                                                         \r
121                                                         print(true,"Find and TEST Connections with DME2Locator",aafurl);\r
122                                                         DME2Locator dl = new DME2Locator(pa,dm,aafurl);\r
123                                                         connectTest(dl);\r
124         \r
125                                                         dm =  new DME2Manager("DME2Manager",pa.getProperties());\r
126         \r
127                                                         dme2RawTest(dm, aafurl,user,pass);\r
128                                                         \r
129                                                         // URL specific Variant\r
130                                                         if((aafurl = specificDME2URL(dl, aafurl))!=null) {\r
131                                                                 print(true,"TEST Specific DME2 CADI Config",aafurl);\r
132                                                                 aafcon = testConfig(pa,aafurl);\r
133                                                                 test(aafcon,permsDF,user);\r
134         \r
135                                                                 dme2RawTest(dm,aafurl,user,pass);\r
136                                                         }\r
137                                                         \r
138                                                         print(true,"CADI Direct AAFConDME2 Object Usage",aafurl);\r
139                                                         try {\r
140                                                                 pa.setProperty(Config.AAF_URL,aafurl);\r
141                                                                 aafcon = new AAFConDME2(pa);\r
142                                                                 test(aafcon,permsDF,user);\r
143                                                         } catch(Throwable t) {\r
144                                                                 t.printStackTrace();\r
145                                                         }\r
146                                                         \r
147                                                         // find a direct client to code a Direct HTTP with\r
148         //                                              \r
149                                                         if(hbass!=null) {\r
150                                                                 print(true,"CADI Http DME2Locator Client Coding Methodology BasicAuth",aafurl);\r
151                                                                 hClientTest(dl,hbass,user);\r
152                                                         }\r
153                                                         if(hxss!=null) {\r
154                                                                 print(true,"CADI Http DME2Locator Client Coding Methodology X509",aafurl);\r
155                                                                 hClientTest(dl,hxss,user);\r
156                                                         }\r
157                                                         \r
158                                                         // ##### PROXY CHECKS\r
159                                                         aafurl = String.format(GW_RESOLVE, aaf_env, PROD.equals(aaf_env)?"DEFAULT":"BAU_SE");\r
160                                                         print(true,"TEST PROXY DME2 CADI Config",aafurl);\r
161                                                         aafcon = testConfig(pa,aafurl);\r
162                                                         test(aafcon,permsDF,user);\r
163         \r
164         \r
165                                                         dme2RawTest(dm, aafurl,user,pass);\r
166                                                         \r
167                                                         // URL specific Variant\r
168                                                         dl = new DME2Locator(pa,dm,aafurl);\r
169                                                         if((aafurl = specificDME2URL(dl, aafurl))!=null) {\r
170                                                                 print(true,"TEST PROXY Specific DME2 CADI Config",aafurl);\r
171                                                                 aafcon = testConfig(pa,aafurl);\r
172                                                                 test(aafcon,permsDF,user);\r
173         \r
174                                                                 dme2RawTest(dm,aafurl,user,pass);\r
175                                                         }\r
176                                                 }\r
177                                         }\r
178 \r
179                                         // Prop Locator\r
180                                         PropertyLocator pl = servicePropLocator(aaf_env);\r
181                                         connectTest(pl);\r
182                                         URI uri = pl.get(pl.best());\r
183                                         if(uri!=null) {\r
184                                                 aafurl = uri.toString();\r
185                                                 print(true,"TEST Service PropertyLocator based Config",aafurl);\r
186                                                 aafcon = testConfig(pa,aafurl);\r
187                                                 test(aafcon,permsDF,user);\r
188         \r
189                                                 if(hbass!=null) {\r
190                                                         print(true,"CADI Service Http PropLocator Client Coding Methodology Basic Auth",aafurl);\r
191                                                         hClientTest(pl,hbass, user);\r
192                                                         print(true,"CADI Service Http PropLocator Client Coding Methodology /authn/basicAuth",aafurl);\r
193                                                         basicAuthTest(pl,hbass);\r
194                                                 }\r
195                                                 if(hxss!=null) {\r
196                                                         print(true,"CADI Service Http PropLocator Client Coding Methodology X509",aafurl);\r
197                                                         hClientTest(pl,hxss, user);\r
198                                                 }\r
199                                         }\r
200                                         pl = proxyPropLocator(aaf_env);\r
201                                         connectTest(pl);\r
202                                         uri = pl.get(pl.best());\r
203                                         if(uri!=null) {\r
204                                                 aafurl = uri.toString();\r
205                                                 print(true,"TEST PROXY PropertyLocator based Config",aafurl);\r
206                                                 aafcon = testConfig(pa,aafurl);\r
207                                                 test(aafcon,permsDF,user);\r
208         \r
209                                                 if(hbass!=null) {\r
210                                                         print(true,"CADI PROXY Http PropLocator Client Coding Methodology Basic Auth",aafurl);\r
211                                                         hClientTest(pl,hbass, user);\r
212                                                         print(true,"CADI PROXY Http PropLocator Client Coding Methodology /proxy/authn/basicAuth",aafurl);\r
213                                                         basicAuthTest(pl,hbass);\r
214                                                 }\r
215                                                 if(hxss!=null) {\r
216                                                         print(true,"CADI PROXY Http PropLocator Client Coding Methodology X509",aafurl);\r
217                                                         hClientTest(pl,hxss, user);\r
218                                                 }\r
219                                         }\r
220                                 }\r
221                                 \r
222                         } catch(Exception e) {\r
223                                 e.printStackTrace(System.err);\r
224                         } finally {\r
225                                 print(true,"END OF TESTS");\r
226                         }\r
227                 }\r
228         }\r
229         \r
230         private static void print(Boolean strong, String ... args) {\r
231                 PrintStream out = System.out;\r
232                 out.println();\r
233                 if(strong) {\r
234                         for(int i=0;i<70;++i) {\r
235                                 out.print('=');\r
236                         }\r
237                         out.println();\r
238                 }\r
239                 for(String s : args) {\r
240                         out.print(strong?"==  ":"------ ");\r
241                         out.print(s);\r
242                         if(!strong) {\r
243                                 out.print("  ------");\r
244                         }\r
245                         out.println();\r
246                 }\r
247                 if(strong) {\r
248                         for(int i=0;i<70;++i) {\r
249                                 out.print('=');\r
250                         }\r
251                 }\r
252                 out.println();\r
253         }\r
254 \r
255         private static void test(AAFCon<?> aafcon,RosettaDF<Perms> permsDF,String user) {\r
256                 if(aafcon==null) {\r
257                         print(false,"AAFCon is null");\r
258                 } else {\r
259                         try {\r
260                                 print(false,"Calling with AAFCon");\r
261                                 Future<Perms> fp = aafcon.client("2.0").read("/authz/perms/user/"+user, Perms.class, permsDF);\r
262                                 if(fp.get(4000)) {\r
263                                         System.out.printf("Found %d Permission(s)\n",fp.value.getPerm().size());\r
264                                 } else {\r
265                                         System.out.printf("Error: %d %s\n",fp.code(),fp.body());\r
266                                 }\r
267                         } catch (Throwable t) {\r
268                                 t.printStackTrace();\r
269                         }\r
270                 }\r
271         }\r
272         \r
273         private static AAFCon<?> testConfig(PropAccess pa, String aafurl) {\r
274                 try {\r
275                         pa.setProperty(Config.AAF_URL, aafurl);\r
276                         Lur lur = Config.configLur(pa);\r
277                         Config.configHttpTaf(pa, TrustChecker.NOTRUST, null, lur);\r
278                         if(lur != null) {\r
279                                 Field f = null;\r
280                                 try {\r
281                                         f = lur.getClass().getField("aaf");\r
282                                         return (AAFCon<?>)f.get(lur);\r
283                                 } catch (Exception nsfe) {\r
284                                 }\r
285                         }\r
286 \r
287                 } catch(Throwable t) {\r
288                         t.printStackTrace();\r
289                 }\r
290                 return null;\r
291         }\r
292         \r
293         private static String specificDME2URL(Locator<URI> loc, String aafurl) throws LocatorException {\r
294                 Item item = loc.best();\r
295                 if(item!=null) {\r
296                         URI uri = loc.get(item);\r
297                         return aafurl.replace("DME2RESOLVE", String.format("%s:%d",uri.getHost(),uri.getPort()));\r
298                 }\r
299                 return null;\r
300         }\r
301 \r
302         private static void connectTest(Locator<URI> dl) throws LocatorException {\r
303                 URI uri;\r
304                 Socket socket;\r
305                 print(false,"TCP/IP Connect test to all Located Services");\r
306                 for(Item li = dl.first();li!=null;li=dl.next(li)) {\r
307                         if((uri = dl.get(li)) == null) {\r
308                                 System.out.println("Locator Item empty");\r
309                         } else {\r
310                                 try {\r
311                                         socket = new Socket();\r
312                                         socket.connect(new InetSocketAddress(uri.getHost(),  uri.getPort()),3000);\r
313                                         System.out.printf("Can Connect a Socket to %s %d\n",uri.getHost(),uri.getPort());\r
314                                         try {\r
315                                                 socket.close();\r
316                                         } catch (IOException e1) {\r
317                                                 System.out.printf("Could not close Socket Connection: %s\n",e1.getMessage());\r
318                                         }\r
319                                 } catch (IOException e) {\r
320                                         System.out.printf("Cannot Connect a Socket to  %s %d: %s\n",uri.getHost(),uri.getPort(),e.getMessage());\r
321                                 }\r
322                         }\r
323                 }\r
324         }\r
325 \r
326         private static PropertyLocator servicePropLocator(String env) throws LocatorException {\r
327                 String purls;\r
328                 switch(env) {\r
329                         case "LOCAL":\r
330                                 try {\r
331                                         purls="https://"+InetAddress.getLocalHost().getHostName()+":8100";\r
332                                 } catch (UnknownHostException e) {\r
333                                         throw new LocatorException(e);\r
334                                 }\r
335                                 break;\r
336                         case "DEV":\r
337                                 purls="https://aaf.dev.att.com:8100,https://aaf.dev.att.com:8101";\r
338                                 break;\r
339                         case "TEST":\r
340                                 purls="https://aaftest.test.att.com:8100,https://aaftest.test.att.com:8101";\r
341                                 break;\r
342                         case "IST":\r
343                                 purls="https://aafist.test.att.com:8100,https://aafist.test.att.com:8101";\r
344                                 break;\r
345                         case PROD:\r
346                                 purls="https://aaf.it.att.com:8100,https://aaf.it.att.com:8101";\r
347                                 break;\r
348                         default:\r
349                                 if(env.contains(".")) {\r
350                                         purls="https://"+env+":8100";\r
351                                 } else {\r
352                                         throw new LocatorException(ConnectivityTest.class.getSimpleName() + ": unknown Env");\r
353                                 }\r
354                 }\r
355                 System.out.printf("Creating a PropertyLocator for %s\n",purls);\r
356                 return new PropertyLocator(purls);\r
357         }\r
358         \r
359         private static PropertyLocator proxyPropLocator(String env) throws LocatorException {\r
360                 String purls;\r
361                 switch(env) {\r
362                         case "LOCAL":\r
363                                 try {\r
364                                         purls="https://"+InetAddress.getLocalHost().getHostAddress()+":8100";\r
365                                 } catch (UnknownHostException e) {\r
366                                         throw new LocatorException(e);\r
367                                 }\r
368                                 break;\r
369                         case "DEV":\r
370                                 purls="https://aaf.dev.att.com:8095/proxy";\r
371                                 break;\r
372                         case "TEST":\r
373                                 purls="https://aaftest.test.att.com:8095/proxy";\r
374                                 break;\r
375                         case "IST":\r
376                                 purls="https://aafist.test.att.com:8095/proxy";\r
377                                 break;\r
378                         case PROD:\r
379                                 purls="https://aaf.it.att.com:8095/proxy";\r
380                                 break;\r
381                         default:\r
382                                 if(env.contains(".")) {\r
383                                         purls="https://"+env+":8095/proxy";\r
384                                 } else {\r
385                                         throw new LocatorException(ConnectivityTest.class.getSimpleName() + ": unknown Env");\r
386                                 }\r
387 \r
388                 }\r
389                 System.out.printf("Creating a PropertyLocator for %s\n",purls);\r
390                 return new PropertyLocator(purls);\r
391         }\r
392                 \r
393         \r
394 \r
395 \r
396         private static void hClientTest(Locator<URI> dl, SecuritySetter<HttpURLConnection> ss, String user)  {\r
397                 try {\r
398                         URI uri = dl.get(dl.best());\r
399                         System.out.println("Resolved to: " + uri);\r
400                         HClient client = new HClient(ss, uri, 3000);\r
401                         client.setMethod("GET");\r
402                         client.setPathInfo("/authz/perms/user/"+user);\r
403                         client.send();\r
404                         Future<String> future = client.futureReadString();\r
405                         if(future.get(7000)) {\r
406                                 System.out.println(future.body());      \r
407                         } else {\r
408                                 System.out.println(future.code() + ":" + future.body());\r
409                         }\r
410                 } catch (CadiException | LocatorException | APIException e) {\r
411                         e.printStackTrace();\r
412                 }\r
413         }\r
414 \r
415 \r
416         private static void basicAuthTest(PropertyLocator dl, SecuritySetter<HttpURLConnection> ss) {\r
417                 try {\r
418                         URI uri = dl.get(dl.best());\r
419                         System.out.println("Resolved to: " + uri);\r
420                         HClient client = new HClient(ss, uri, 3000);\r
421                         client.setMethod("GET");\r
422                         client.setPathInfo("/authn/basicAuth");\r
423                         client.addHeader("Accept", "text/plain");\r
424                         client.send();\r
425         \r
426                 \r
427                         Future<String> future = client.futureReadString();\r
428                         if(future.get(7000)) {\r
429                                 System.out.println("BasicAuth Validated");      \r
430                         } else {\r
431                                 System.out.println("Failure " + future.code() + ":" + future.body());\r
432                         }\r
433                 } catch (CadiException | LocatorException | APIException e) {\r
434                         e.printStackTrace();\r
435                 }\r
436         }\r
437 \r
438         // Regular DME2Client Coding Style\r
439         private static void dme2RawTest(DME2Manager dm, String aafurl, String user, String pass) {\r
440                 try { \r
441                         if(dm==null) {\r
442                                 return;\r
443                         }\r
444                         URI uri = new URI(aafurl);\r
445                         print(true,"DME2 Direct Client Coding Methodology",uri.toString());\r
446                         DME2Client client = dm.newClient( uri, 3000);\r
447                         client.setMethod("GET"); // FYI, DME2 defaults to "POST"\r
448                         client.setContext("/authz/perms/user/"+user); // DME2 direct requires separate setting of Context from URI\r
449                         if(pass!=null) { // rely on Cert if no pass\r
450                                 client.setCredentials(user, pass);\r
451                         }\r
452                         client.setPayload(""); // DME2  will not send without something\r
453                         String resp = client.sendAndWait(7000);\r
454                         System.out.println(resp);\r
455                 } catch(Throwable e) {\r
456                         e.printStackTrace();\r
457                 }\r
458         }\r
459 }\r