GUI fixes
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / client / Examples.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.aaf.client;
23
24
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.GregorianCalendar;
28
29 import org.onap.aaf.misc.env.APIException;
30 import org.onap.aaf.misc.env.Data;
31 import org.onap.aaf.misc.env.Data.TYPE;
32 import org.onap.aaf.misc.env.util.Chrono;
33 import org.onap.aaf.misc.rosetta.env.RosettaDF;
34 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
35
36 import aaf.v2_0.Approval;
37 import aaf.v2_0.Approvals;
38 import aaf.v2_0.CredRequest;
39 import aaf.v2_0.Keys;
40 import aaf.v2_0.NsRequest;
41 import aaf.v2_0.Nss;
42 import aaf.v2_0.Nss.Ns;
43 import aaf.v2_0.Perm;
44 import aaf.v2_0.PermKey;
45 import aaf.v2_0.PermRequest;
46 import aaf.v2_0.Perms;
47 import aaf.v2_0.Pkey;
48 import aaf.v2_0.Request;
49 import aaf.v2_0.Role;
50 import aaf.v2_0.RoleKey;
51 import aaf.v2_0.RolePermRequest;
52 import aaf.v2_0.RoleRequest;
53 import aaf.v2_0.Roles;
54 import aaf.v2_0.UserRole;
55 import aaf.v2_0.UserRoleRequest;
56 import aaf.v2_0.UserRoles;
57 import aaf.v2_0.Users;
58 import aaf.v2_0.Users.User;
59
60 public class Examples {
61     public static <C> String print(RosettaEnv env, String nameOrContentType, boolean optional) throws APIException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
62         // Discover ClassName
63         String className = null;
64         String version = null;
65         TYPE type = TYPE.JSON; // default
66         if (nameOrContentType.startsWith("application/")) {
67             for (String ct : nameOrContentType.split("\\s*,\\s*")) {
68                 for (String elem : ct.split("\\s*;\\s*")) {
69                     if (elem.endsWith("+json")) {
70                         type = TYPE.JSON;
71                         className = elem.substring(elem.indexOf('/')+1, elem.length()-5);
72                     } else if (elem.endsWith("+xml")) {
73                         type = TYPE.XML;
74                         className = elem.substring(elem.indexOf('/')+1, elem.length()-4);
75                     } else if (elem.startsWith("version=")) {
76                         version = elem.substring(8);
77                     }
78                 }
79                 if (className!=null && version!=null)
80                     break;
81             }
82             if (className==null) {
83                 throw new APIException(nameOrContentType + " does not contain Class Information");
84             }
85         } else {
86             className = nameOrContentType;
87         }
88         
89         // No Void.class in aaf.v2_0 package causing errors when trying to use a newVoidv2_0
90         // method similar to others in this class. This makes it work, but is it right?
91         if ("Void".equals(className))
92             return "";
93                 
94         if ("1.1".equals(version)) {
95             version = "v1_0";
96         } else if (version!=null) {
97             version = "v" + version.replace('.', '_');
98         } else {
99             version = "v2_0";
100         }
101         
102         Class<?> cls=null;
103         int minorIdx = version.indexOf('_');
104         if(minorIdx<0) {
105                 throw new APIException("Invalid Interface Version " + version);
106         }
107         int minor = Integer.parseInt(version.substring(minorIdx+1));
108         String vprefix=version.substring(0, minorIdx+1);
109         while(cls==null && minor>=0) {
110                 try {
111                         cls = Examples.class.getClassLoader().loadClass("aaf."+vprefix+minor+'.'+className);
112             } catch (ClassNotFoundException e) {
113                 if(--minor<0) {
114                         throw new APIException("No Example for Version " + version + " found.");
115                 }
116             }
117         }
118         
119         Method meth;
120         try {
121             meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+vprefix+minor,boolean.class);
122         } catch (Exception e) {
123             throw new APIException("ERROR: " + cls.getName() + " does not have an Example in Code.  Request from AAF Developers");
124         }
125         
126         RosettaDF<C> df = env.newDataFactory(cls);
127         df.option(Data.PRETTY);
128         
129         Object data = meth.invoke(null,optional);
130         
131         @SuppressWarnings("unchecked")
132         String rv = df.newData().load((C)data).out(type).asString();
133 //        Object obj = df.newData().in(type).load(rv).asObject();
134         return rv;
135     }
136     
137     /*
138      *  Set Base Class Request (easier than coding over and over)
139      */
140     private static void setOptional(Request req) {
141         GregorianCalendar gc = new GregorianCalendar();
142         req.setStart(Chrono.timeStamp(gc));
143         gc.add(GregorianCalendar.MONTH, 6);
144         req.setEnd(Chrono.timeStamp(gc));
145 //        req.setForce("false");
146         
147     }
148     
149     @SuppressWarnings("unused")
150     private static Request newRequestv2_0(boolean optional) {
151         Request r = new Request();
152         setOptional(r);
153         return r;
154     }
155     @SuppressWarnings("unused")
156     private static RolePermRequest newRolePermRequestv2_0(boolean optional) {
157         RolePermRequest rpr = new RolePermRequest();
158         Pkey pkey = new Pkey();
159         pkey.setType("org.osaaf.myns.mytype");
160         pkey.setInstance("myInstance");
161         pkey.setAction("myAction");
162         rpr.setPerm(pkey);
163         rpr.setRole("org.osaaf.myns.myrole");
164         if (optional)setOptional(rpr);
165         return rpr;
166     }
167     
168     @SuppressWarnings("unused")
169     private static Roles newRolesv2_0(boolean optional) {
170         Role r;
171         Pkey p;
172         Roles rs = new Roles();
173     r = new Role();
174         rs.getRole().add(r);
175         r.setName("org.osaaf.myns.myRole");
176     p = new Pkey();
177         r.getPerms().add(p);
178         p.setType("org.osaaf.myns.myType");
179         p.setInstance("myInstance");
180         p.setAction("myAction");
181
182     p = new Pkey();
183         r.getPerms().add(p);
184         p.setType("org.osaaf.myns.myType");
185         p.setInstance("myInstance");
186         p.setAction("myOtherAction");
187
188     r = new Role();
189         rs.getRole().add(r);
190         r.setName("org.osaaf.myns.myOtherRole");
191     p = new Pkey();
192         r.getPerms().add(p);
193         p.setType("org.osaaf.myns.myOtherType");
194         p.setInstance("myInstance");
195         p.setAction("myAction");
196
197     p = new Pkey();
198         r.getPerms().add(p);
199         p.setType("org.osaaf.myns.myOthertype");
200         p.setInstance("myInstance");
201         p.setAction("myOtherAction");
202
203         return rs;
204     }
205     
206     
207     @SuppressWarnings("unused")
208     private static PermRequest newPermRequestv2_0(boolean optional) {
209         PermRequest pr = new PermRequest();
210         pr.setType("org.osaaf.myns.myType");
211         pr.setInstance("myInstance");
212         pr.setAction("myAction");
213         if (optional) {
214             pr.setDescription("Short and meaningful verbiage about the Permission");
215             
216             setOptional(pr);
217         }
218         return pr;
219     }
220     
221     @SuppressWarnings("unused")
222     private static Perm newPermv2_0(boolean optional) {
223         Perm pr = new Perm();
224         pr.setType("org.osaaf.myns.myType");
225         pr.setInstance("myInstance");
226         pr.setAction("myAction");
227         pr.getRoles().add("org.osaaf.aaf.myRole");
228         pr.getRoles().add("org.osaaf.aaf.myRole2");
229         pr.setDescription("This is my description, and I'm sticking with it");
230         if (optional) {
231             pr.setDescription("Short and meaningful verbiage about the Permission");
232         }
233         return pr;
234     }
235
236
237     @SuppressWarnings("unused")
238     private static PermKey newPermKeyv2_0(boolean optional) {
239         PermKey pr = new PermKey();
240         pr.setType("org.osaaf.myns.myType");
241         pr.setInstance("myInstance");
242         pr.setAction("myAction");
243         return pr;
244     }
245     
246     @SuppressWarnings("unused")
247     private static Perms newPermsv2_0(boolean optional) {
248         Perms perms = new Perms();
249         Perm p=new Perm();
250         perms.getPerm().add(p);
251         p.setType("org.osaaf.myns.myType");
252         p.setInstance("myInstance");
253         p.setAction("myAction");
254         p.getRoles().add("org.osaaf.myns.myRole");
255         p.getRoles().add("org.osaaf.myns.myRole2");
256
257
258     p=new Perm();
259         perms.getPerm().add(p);
260         p.setType("org.osaaf.myns.myOtherType");
261         p.setInstance("myInstance");
262         p.setAction("myOtherAction");
263         p.getRoles().add("org.osaaf.myns.myRole");
264         p.getRoles().add("org.osaaf.myns.myRole2");
265
266         return perms;
267         
268     }
269     
270     @SuppressWarnings("unused")
271     private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
272         UserRoleRequest urr = new UserRoleRequest();
273         urr.setRole("org.osaaf.myns.myRole");
274         urr.setUser("ab1234@people.osaaf.org");
275         if (optional) setOptional(urr);
276         return urr;
277     }
278     
279     @SuppressWarnings("unused")
280     private static NsRequest newNsRequestv2_0(boolean optional) {
281         NsRequest nr = new NsRequest();
282         nr.setName("org.osaaf.myns");
283         nr.getResponsible().add("ab1234@people.osaaf.org");
284         nr.getResponsible().add("cd5678@people.osaaf.org");
285         nr.getAdmin().add("zy9876@people.osaaf.org");
286         nr.getAdmin().add("xw5432@people.osaaf.org");        
287         if (optional) {
288             nr.setDescription("This is my Namespace to set up");
289             nr.setType("APP");
290             setOptional(nr);
291         }
292         return nr;
293     }
294     
295     
296     @SuppressWarnings("unused")
297     private static Nss newNssv2_0(boolean optional) {
298         Ns ns;
299         
300         Nss nss = new Nss();
301         nss.getNs().add(ns = new Nss.Ns());
302         ns.setName("org.osaaf.myns");
303         ns.getResponsible().add("ab1234@people.osaaf.org");
304         ns.getResponsible().add("cd5678@people.osaaf.org");
305         ns.getAdmin().add("zy9876@people.osaaf.org");
306         ns.getAdmin().add("xw5432@people.osaaf.org");
307         ns.setDescription("This is my Namespace to set up");
308         
309         nss.getNs().add(ns = new Nss.Ns());
310         ns.setName("org.osaaf.myOtherNs");
311         ns.getResponsible().add("ab1234@people.osaaf.org");
312         ns.getResponsible().add("cd5678@people.osaaf.org");
313         ns.getAdmin().add("zy9876@people.osaaf.org");
314         ns.getAdmin().add("xw5432@people.osaaf.org");        
315             
316         return nss;
317     }
318     @SuppressWarnings("unused")
319     private static RoleRequest newRoleRequestv2_0(boolean optional) {
320         RoleRequest rr = new RoleRequest();
321         rr.setName("org.osaaf.myns.myRole");
322         if (optional) {
323             rr.setDescription("This is my Role");
324             setOptional(rr);
325         }
326         return rr;
327     }
328
329     @SuppressWarnings("unused")
330     private static CredRequest newCredRequestv2_0(boolean optional) {
331         CredRequest cr = new CredRequest();
332         cr.setId("myID@fully.qualified.domain");
333         if (optional) {
334             cr.setType(2);
335             cr.setEntry("0x125AB256344CE");
336         } else {
337             cr.setPassword("This is my provisioned password");
338         }
339
340         return cr;
341     }
342     
343     @SuppressWarnings("unused")
344     private static Users newUsersv2_0(boolean optional) {
345         User user;
346     
347         Users users = new Users();
348     user = new Users.User();
349         users.getUser().add(user);
350         user.setId("ab1234@people.osaaf.org");    
351         GregorianCalendar gc = new GregorianCalendar();
352         user.setExpires(Chrono.timeStamp(gc));
353
354     user = new Users.User();
355         users.getUser().add(user);
356         user.setId("zy9876@people.osaaf.org");    
357         user.setExpires(Chrono.timeStamp(gc));    
358             
359         return users;
360     }
361
362     @SuppressWarnings("unused")
363     private static Role newRolev2_0(boolean optional) {
364         Role r = new Role();
365         Pkey p;
366         r.setName("org.osaaf.myns.myRole");
367         r.getPerms().add(p = new Pkey());
368         p.setType("org.osaaf.myns.myType");
369         p.setInstance("myInstance");
370         p.setAction("myAction");
371
372         return r;
373     }
374
375     @SuppressWarnings("unused")
376     private static RoleKey newRoleKeyv2_0(boolean optional) {
377         RoleKey r = new RoleKey();
378         Pkey p;
379         r.setName("org.osaaf.myns.myRole");
380         return r;
381     }
382
383     @SuppressWarnings("unused")
384     private static Keys newKeysv2_0(boolean optional) {
385         Keys ks = new Keys();
386         ks.getKey().add("Reponse 1");
387         ks.getKey().add("Response 2");
388         return ks;
389     }
390
391     @SuppressWarnings("unused")
392     private static UserRoles newUserRolesv2_0(boolean optional) {
393         UserRoles urs = new UserRoles();
394         UserRole ur = new UserRole();
395         ur.setUser("xy1234");
396         ur.setRole("com.test.myapp.myRole");
397         ur.setExpires(Chrono.timeStamp());
398         urs.getUserRole().add(ur);
399         
400         ur = new UserRole();
401         ur.setUser("yx4321");
402         ur.setRole("com.test.yourapp.yourRole");
403         ur.setExpires(Chrono.timeStamp());
404         urs.getUserRole().add(ur);
405         return urs;
406     }
407
408
409     @SuppressWarnings("unused")
410     private static Approvals newApprovalsv2_0(boolean optional) {
411         Approvals as = new Approvals();
412         Approval a = new Approval();
413         a.setApprover("MyApprover");
414         a.setId("MyID");
415         a.setMemo("My memo (and then some)");
416         a.setOperation("MyOperation");
417         a.setStatus("MyStatus");
418         a.setTicket("MyTicket");
419         a.setType("MyType");
420         a.setUpdated(Chrono.timeStamp());
421         a.setUser("MyUser");
422         as.getApprovals().add(a);
423         a = new Approval();
424         a.setApprover("MyApprover2");
425         a.setId("MyID2");
426         a.setMemo("My memo (and then some)2");
427         a.setOperation("MyOperation2");
428         a.setStatus("MyStatus2");
429         a.setTicket("MyTicket2");
430         a.setType("MyType2");
431         a.setUpdated(Chrono.timeStamp());
432         a.setUser("MyUser2");
433         as.getApprovals().add(a);
434         return as;
435     }
436
437     @SuppressWarnings("unused")
438     private static Approval newApprovalv2_0(boolean optional) {
439         Approval a = new Approval();
440         a.setApprover("MyApprover");
441         a.setId("MyID");
442         a.setMemo("My memo (and then some)");
443         a.setOperation("MyOperation");
444         a.setStatus("MyStatus");
445         a.setTicket("MyTicket");
446         a.setType("MyType");
447         a.setUpdated(Chrono.timeStamp());
448         a.setUser("MyUser");
449         return a;
450     }
451
452     
453
454     @SuppressWarnings("unused")
455     private static aaf.v2_0.Error newErrorv2_0(boolean optional) {
456         aaf.v2_0.Error err = new aaf.v2_0.Error();
457         err.setMessageId("SVC1403");
458         err.setText("MyText %s, %s: The last three digits are usually the HTTP Code");
459         err.getVariables().add("Variable 1");
460         err.getVariables().add("Variable 2");
461         return err;
462     }
463
464 }