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