2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.aaf.client;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.GregorianCalendar;
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;
36 import aaf.v2_0.Approval;
37 import aaf.v2_0.Approvals;
38 import aaf.v2_0.CredRequest;
40 import aaf.v2_0.NsRequest;
42 import aaf.v2_0.Nss.Ns;
44 import aaf.v2_0.PermKey;
45 import aaf.v2_0.PermRequest;
46 import aaf.v2_0.Perms;
48 import aaf.v2_0.Request;
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;
60 public class Examples {
61 public static <C> String print(RosettaEnv env, String nameOrContentType, boolean optional) throws APIException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
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")) {
71 className = elem.substring(elem.indexOf('/')+1, elem.length()-5);
72 } else if(elem.endsWith("+xml")) {
74 className = elem.substring(elem.indexOf('/')+1, elem.length()-4);
75 } else if(elem.startsWith("version=")) {
76 version = elem.substring(8);
79 if(className!=null && version!=null)
83 throw new APIException(nameOrContentType + " does not contain Class Information");
86 className = nameOrContentType;
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))
94 if("1.1".equals(version)) {
96 } else if(version!=null) {
97 version = "v" + version.replace('.', '_');
104 cls = Examples.class.getClassLoader().loadClass("aaf."+version+'.'+className);
105 } catch (ClassNotFoundException e) {
106 throw new APIException(e);
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");
116 RosettaDF<C> df = env.newDataFactory(cls);
117 df.option(Data.PRETTY);
119 Object data = meth.invoke(null,optional);
121 @SuppressWarnings("unchecked")
122 String rv = df.newData().load((C)data).out(type).asString();
123 // Object obj = df.newData().in(type).load(rv).asObject();
128 * Set Base Class Request (easier than coding over and over)
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");
139 @SuppressWarnings("unused")
140 private static Request newRequestv2_0(boolean optional) {
141 Request r = new Request();
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");
153 rpr.setRole("org.osaaf.myns.myrole");
154 if(optional)setOptional(rpr);
158 @SuppressWarnings("unused")
159 private static Roles newRolesv2_0(boolean optional) {
162 Roles rs = new Roles();
165 r.setName("org.osaaf.myns.myRole");
168 p.setType("org.osaaf.myns.myType");
169 p.setInstance("myInstance");
170 p.setAction("myAction");
174 p.setType("org.osaaf.myns.myType");
175 p.setInstance("myInstance");
176 p.setAction("myOtherAction");
180 r.setName("org.osaaf.myns.myOtherRole");
183 p.setType("org.osaaf.myns.myOtherType");
184 p.setInstance("myInstance");
185 p.setAction("myAction");
189 p.setType("org.osaaf.myns.myOthertype");
190 p.setInstance("myInstance");
191 p.setAction("myOtherAction");
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");
204 pr.setDescription("Short and meaningful verbiage about the Permission");
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");
221 pr.setDescription("Short and meaningful verbiage about the Permission");
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");
236 @SuppressWarnings("unused")
237 private static Perms newPermsv2_0(boolean optional) {
238 Perms perms = new Perms();
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");
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");
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);
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");
278 nr.setDescription("This is my Namespace to set up");
286 @SuppressWarnings("unused")
287 private static Nss newNssv2_0(boolean optional) {
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");
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");
308 @SuppressWarnings("unused")
309 private static RoleRequest newRoleRequestv2_0(boolean optional) {
310 RoleRequest rr = new RoleRequest();
311 rr.setName("org.osaaf.myns.myRole");
313 rr.setDescription("This is my Role");
319 @SuppressWarnings("unused")
320 private static CredRequest newCredRequestv2_0(boolean optional) {
321 CredRequest cr = new CredRequest();
322 cr.setId("myID@fully.qualified.domain");
325 cr.setEntry("0x125AB256344CE");
327 cr.setPassword("This is my provisioned password");
333 @SuppressWarnings("unused")
334 private static Users newUsersv2_0(boolean optional) {
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));
344 user = new Users.User();
345 users.getUser().add(user);
346 user.setId("zy9876@people.osaaf.org");
347 user.setExpires(Chrono.timeStamp(gc));
352 @SuppressWarnings("unused")
353 private static Role newRolev2_0(boolean optional) {
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");
365 @SuppressWarnings("unused")
366 private static RoleKey newRoleKeyv2_0(boolean optional) {
367 RoleKey r = new RoleKey();
369 r.setName("org.osaaf.myns.myRole");
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");
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);
391 ur.setUser("yx4321");
392 ur.setRole("com.test.yourapp.yourRole");
393 ur.setExpires(Chrono.timeStamp());
394 urs.getUserRole().add(ur);
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");
405 a.setMemo("My memo (and then some)");
406 a.setOperation("MyOperation");
407 a.setStatus("MyStatus");
408 a.setTicket("MyTicket");
410 a.setUpdated(Chrono.timeStamp());
412 as.getApprovals().add(a);
414 a.setApprover("MyApprover2");
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);
427 @SuppressWarnings("unused")
428 private static Approval newApprovalv2_0(boolean optional) {
429 Approval a = new Approval();
430 a.setApprover("MyApprover");
432 a.setMemo("My memo (and then some)");
433 a.setOperation("MyOperation");
434 a.setStatus("MyStatus");
435 a.setTicket("MyTicket");
437 a.setUpdated(Chrono.timeStamp());
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");