Examples.java
[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,  NoSuchMethodException,  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         if(cls==null) {
120             throw new APIException("ERROR: " + "aaf."+vprefix+"X not found.");
121         }
122
123         Method meth;
124         try {
125             meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+vprefix+minor,boolean.class);
126         } catch (Exception e) {
127             throw new APIException("ERROR: " + cls.getName() + " does not have an Example in Code.  Request from AAF Developers");
128         }
129
130         RosettaDF<C> df = env.newDataFactory(cls);
131         df.option(Data.PRETTY);
132
133         Object data = meth.invoke(null,optional);
134
135         @SuppressWarnings("unchecked")
136         String rv = df.newData().load((C)data).out(type).asString();
137
138         return rv;
139     }
140
141     /*
142      *  Set Base Class Request (easier than coding over and over)
143      */
144     private static void setOptional(Request req) {
145         GregorianCalendar gc = new GregorianCalendar();
146         req.setStart(Chrono.timeStamp(gc));
147         gc.add(GregorianCalendar.MONTH, 6);
148         req.setEnd(Chrono.timeStamp(gc));
149
150     }
151
152     @SuppressWarnings("unused")
153     private static Request newRequestv2_0(boolean optional) {
154         Request r = new Request();
155         setOptional(r);
156         return r;
157     }
158     @SuppressWarnings("unused")
159     private static RolePermRequest newRolePermRequestv2_0(boolean optional) {
160         RolePermRequest rpr = new RolePermRequest();
161         Pkey pkey = new Pkey();
162         pkey.setType("org.osaaf.myns.mytype");
163         pkey.setInstance("myInstance");
164         pkey.setAction("myAction");
165         rpr.setPerm(pkey);
166         rpr.setRole("org.osaaf.myns.myrole");
167         if (optional)setOptional(rpr);
168         return rpr;
169     }
170
171     @SuppressWarnings("unused")
172     private static Roles newRolesv2_0(boolean optional) {
173         Role r;
174         Pkey p;
175         Roles rs = new Roles();
176     r = new Role();
177         rs.getRole().add(r);
178         r.setName("org.osaaf.myns.myRole");
179     p = new Pkey();
180         r.getPerms().add(p);
181         p.setType("org.osaaf.myns.myType");
182         p.setInstance("myInstance");
183         p.setAction("myAction");
184
185     p = new Pkey();
186         r.getPerms().add(p);
187         p.setType("org.osaaf.myns.myType");
188         p.setInstance("myInstance");
189         p.setAction("myOtherAction");
190
191     r = new Role();
192         rs.getRole().add(r);
193         r.setName("org.osaaf.myns.myOtherRole");
194     p = new Pkey();
195         r.getPerms().add(p);
196         p.setType("org.osaaf.myns.myOtherType");
197         p.setInstance("myInstance");
198         p.setAction("myAction");
199
200     p = new Pkey();
201         r.getPerms().add(p);
202         p.setType("org.osaaf.myns.myOthertype");
203         p.setInstance("myInstance");
204         p.setAction("myOtherAction");
205
206         return rs;
207     }
208
209
210     @SuppressWarnings("unused")
211     private static PermRequest newPermRequestv2_0(boolean optional) {
212         PermRequest pr = new PermRequest();
213         pr.setType("org.osaaf.myns.myType");
214         pr.setInstance("myInstance");
215         pr.setAction("myAction");
216         if (optional) {
217             pr.setDescription("Short and meaningful verbiage about the Permission");
218
219             setOptional(pr);
220         }
221         return pr;
222     }
223
224     @SuppressWarnings("unused")
225     private static Perm newPermv2_0(boolean optional) {
226         Perm pr = new Perm();
227         pr.setType("org.osaaf.myns.myType");
228         pr.setInstance("myInstance");
229         pr.setAction("myAction");
230         pr.getRoles().add("org.osaaf.aaf.myRole");
231         pr.getRoles().add("org.osaaf.aaf.myRole2");
232         pr.setDescription("This is my description, and I'm sticking with it");
233         if (optional) {
234             pr.setDescription("Short and meaningful verbiage about the Permission");
235         }
236         return pr;
237     }
238
239
240     @SuppressWarnings("unused")
241     private static PermKey newPermKeyv2_0(boolean optional) {
242         PermKey pr = new PermKey();
243         pr.setType("org.osaaf.myns.myType");
244         pr.setInstance("myInstance");
245         pr.setAction("myAction");
246         return pr;
247     }
248
249     @SuppressWarnings("unused")
250     private static Perms newPermsv2_0(boolean optional) {
251         Perms perms = new Perms();
252         Perm p=new Perm();
253         perms.getPerm().add(p);
254         p.setType("org.osaaf.myns.myType");
255         p.setInstance("myInstance");
256         p.setAction("myAction");
257         p.getRoles().add("org.osaaf.myns.myRole");
258         p.getRoles().add("org.osaaf.myns.myRole2");
259
260
261     p=new Perm();
262         perms.getPerm().add(p);
263         p.setType("org.osaaf.myns.myOtherType");
264         p.setInstance("myInstance");
265         p.setAction("myOtherAction");
266         p.getRoles().add("org.osaaf.myns.myRole");
267         p.getRoles().add("org.osaaf.myns.myRole2");
268
269         return perms;
270
271     }
272
273     @SuppressWarnings("unused")
274     private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
275         UserRoleRequest urr = new UserRoleRequest();
276         urr.setRole("org.osaaf.myns.myRole");
277         urr.setUser("ab1234@people.osaaf.org");
278         if (optional) { 
279            setOptional(urr);
280         }
281         return urr;
282     }
283
284     @SuppressWarnings("unused")
285     private static NsRequest newNsRequestv2_0(boolean optional) {
286         NsRequest nr = new NsRequest();
287         nr.setName("org.osaaf.myns");
288         nr.getResponsible().add("ab1234@people.osaaf.org");
289         nr.getResponsible().add("cd5678@people.osaaf.org");
290         nr.getAdmin().add("zy9876@people.osaaf.org");
291         nr.getAdmin().add("xw5432@people.osaaf.org");
292         if (optional) {
293             nr.setDescription("This is my Namespace to set up");
294             nr.setType("APP");
295             setOptional(nr);
296         }
297         return nr;
298     }
299
300
301     @SuppressWarnings("unused")
302     private static Nss newNssv2_0(boolean optional) {
303         Ns ns;
304
305         Nss nss = new Nss();
306         ns = new Nss.Ns();
307         nss.getNs().add(ns);
308         ns.setName("org.osaaf.myns");
309         ns.getResponsible().add("ab1234@people.osaaf.org");
310         ns.getResponsible().add("cd5678@people.osaaf.org");
311         ns.getAdmin().add("zy9876@people.osaaf.org");
312         ns.getAdmin().add("xw5432@people.osaaf.org");
313         ns.setDescription("This is my Namespace to set up");
314         ns = new Nss.Ns();
315         nss.getNs().add(ns);
316         ns.setName("org.osaaf.myOtherNs");
317         ns.getResponsible().add("ab1234@people.osaaf.org");
318         ns.getResponsible().add("cd5678@people.osaaf.org");
319         ns.getAdmin().add("zy9876@people.osaaf.org");
320         ns.getAdmin().add("xw5432@people.osaaf.org");
321
322         return nss;
323     }
324     @SuppressWarnings("unused")
325     private static RoleRequest newRoleRequestv2_0(boolean optional) {
326         RoleRequest rr = new RoleRequest();
327         rr.setName("org.osaaf.myns.myRole");
328         if (optional) {
329             rr.setDescription("This is my Role");
330             setOptional(rr);
331         }
332         return rr;
333     }
334
335     @SuppressWarnings("unused")
336     private static CredRequest newCredRequestv2_0(boolean optional) {
337         CredRequest cr = new CredRequest();
338         cr.setId("myID@fully.qualified.domain");
339         if (optional) {
340             cr.setType(2);
341             cr.setEntry("0x125AB256344CE");
342         } else {
343             cr.setPassword("This is my provisioned password");
344         }
345
346         return cr;
347     }
348
349     @SuppressWarnings("unused")
350     private static Users newUsersv2_0(boolean optional) {
351         User user;
352
353         Users users = new Users();
354     user = new Users.User();
355         users.getUser().add(user);
356         user.setId("ab1234@people.osaaf.org");
357         GregorianCalendar gc = new GregorianCalendar();
358         user.setExpires(Chrono.timeStamp(gc));
359
360     user = new Users.User();
361         users.getUser().add(user);
362         user.setId("zy9876@people.osaaf.org");
363         user.setExpires(Chrono.timeStamp(gc));
364
365         return users;
366     }
367
368     @SuppressWarnings("unused")
369     private static Role newRolev2_0(boolean optional) {
370         Role r = new Role();
371         Pkey p;
372         r.setName("org.osaaf.myns.myRole");
373         r.getPerms().add(p = new Pkey());
374         p.setType("org.osaaf.myns.myType");
375         p.setInstance("myInstance");
376         p.setAction("myAction");
377
378         return r;
379     }
380
381     @SuppressWarnings("unused")
382     private static RoleKey newRoleKeyv2_0(boolean optional) {
383         RoleKey r = new RoleKey();
384         Pkey p;
385         r.setName("org.osaaf.myns.myRole");
386         return r;
387     }
388
389     @SuppressWarnings("unused")
390     private static Keys newKeysv2_0(boolean optional) {
391         Keys ks = new Keys();
392         ks.getKey().add("Reponse 1");
393         ks.getKey().add("Response 2");
394         return ks;
395     }
396
397     @SuppressWarnings("unused")
398     private static UserRoles newUserRolesv2_0(boolean optional) {
399         UserRoles urs = new UserRoles();
400         UserRole ur = new UserRole();
401         ur.setUser("xy1234");
402         ur.setRole("com.test.myapp.myRole");
403         ur.setExpires(Chrono.timeStamp());
404         urs.getUserRole().add(ur);
405
406         ur = new UserRole();
407         ur.setUser("yx4321");
408         ur.setRole("com.test.yourapp.yourRole");
409         ur.setExpires(Chrono.timeStamp());
410         urs.getUserRole().add(ur);
411         return urs;
412     }
413
414
415     @SuppressWarnings("unused")
416     private static Approvals newApprovalsv2_0(boolean optional) {
417         Approvals as = new Approvals();
418         Approval a = new Approval();
419         a.setApprover("MyApprover");
420         a.setId("MyID");
421         a.setMemo("My memo (and then some)");
422         a.setOperation("MyOperation");
423         a.setStatus("MyStatus");
424         a.setTicket("MyTicket");
425         a.setType("MyType");
426         a.setUpdated(Chrono.timeStamp());
427         a.setUser("MyUser");
428         as.getApprovals().add(a);
429         a = new Approval();
430         a.setApprover("MyApprover2");
431         a.setId("MyID2");
432         a.setMemo("My memo (and then some)2");
433         a.setOperation("MyOperation2");
434         a.setStatus("MyStatus2");
435         a.setTicket("MyTicket2");
436         a.setType("MyType2");
437         a.setUpdated(Chrono.timeStamp());
438         a.setUser("MyUser2");
439         as.getApprovals().add(a);
440         return as;
441     }
442
443     @SuppressWarnings("unused")
444     private static Approval newApprovalv2_0(boolean optional) {
445         Approval a = new Approval();
446         a.setApprover("MyApprover");
447         a.setId("MyID");
448         a.setMemo("My memo (and then some)");
449         a.setOperation("MyOperation");
450         a.setStatus("MyStatus");
451         a.setTicket("MyTicket");
452         a.setType("MyType");
453         a.setUpdated(Chrono.timeStamp());
454         a.setUser("MyUser");
455         return a;
456     }
457
458
459
460     @SuppressWarnings("unused")
461     private static aaf.v2_0.Error newErrorv2_0(boolean optional) {
462         aaf.v2_0.Error err = new aaf.v2_0.Error();
463         err.setMessageId("SVC1403");
464         err.setText("MyText %s, %s: The last three digits are usually the HTTP Code");
465         err.getVariables().add("Variable 1");
466         err.getVariables().add("Variable 2");
467         return err;
468     }
469
470 }