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