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