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)
82 if (className==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('.', '_');
103 int minorIdx = version.indexOf('_');
105 throw new APIException("Invalid Interface Version " + version);
107 int minor = Integer.parseInt(version.substring(minorIdx+1));
108 String vprefix=version.substring(0, minorIdx+1);
109 while(cls==null && minor>=0) {
111 cls = Examples.class.getClassLoader().loadClass("aaf."+vprefix+minor+'.'+className);
112 } catch (ClassNotFoundException e) {
114 throw new APIException("No Example for Version " + version + " found.");
120 throw new APIException("ERROR: " + "aaf."+vprefix+"X not found.");
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");
130 RosettaDF<C> df = env.newDataFactory(cls);
131 df.option(Data.PRETTY);
133 Object data = meth.invoke(null,optional);
135 @SuppressWarnings("unchecked")
136 String rv = df.newData().load((C)data).out(type).asString();
137 // Object obj = df.newData().in(type).load(rv).asObject();
142 * Set Base Class Request (easier than coding over and over)
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");
153 @SuppressWarnings("unused")
154 private static Request newRequestv2_0(boolean optional) {
155 Request r = new Request();
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");
167 rpr.setRole("org.osaaf.myns.myrole");
168 if (optional)setOptional(rpr);
172 @SuppressWarnings("unused")
173 private static Roles newRolesv2_0(boolean optional) {
176 Roles rs = new Roles();
179 r.setName("org.osaaf.myns.myRole");
182 p.setType("org.osaaf.myns.myType");
183 p.setInstance("myInstance");
184 p.setAction("myAction");
188 p.setType("org.osaaf.myns.myType");
189 p.setInstance("myInstance");
190 p.setAction("myOtherAction");
194 r.setName("org.osaaf.myns.myOtherRole");
197 p.setType("org.osaaf.myns.myOtherType");
198 p.setInstance("myInstance");
199 p.setAction("myAction");
203 p.setType("org.osaaf.myns.myOthertype");
204 p.setInstance("myInstance");
205 p.setAction("myOtherAction");
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");
218 pr.setDescription("Short and meaningful verbiage about the Permission");
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");
235 pr.setDescription("Short and meaningful verbiage about the Permission");
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");
250 @SuppressWarnings("unused")
251 private static Perms newPermsv2_0(boolean optional) {
252 Perms perms = new Perms();
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");
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");
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);
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");
292 nr.setDescription("This is my Namespace to set up");
300 @SuppressWarnings("unused")
301 private static Nss newNssv2_0(boolean optional) {
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");
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");
323 @SuppressWarnings("unused")
324 private static RoleRequest newRoleRequestv2_0(boolean optional) {
325 RoleRequest rr = new RoleRequest();
326 rr.setName("org.osaaf.myns.myRole");
328 rr.setDescription("This is my Role");
334 @SuppressWarnings("unused")
335 private static CredRequest newCredRequestv2_0(boolean optional) {
336 CredRequest cr = new CredRequest();
337 cr.setId("myID@fully.qualified.domain");
340 cr.setEntry("0x125AB256344CE");
342 cr.setPassword("This is my provisioned password");
348 @SuppressWarnings("unused")
349 private static Users newUsersv2_0(boolean optional) {
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));
359 user = new Users.User();
360 users.getUser().add(user);
361 user.setId("zy9876@people.osaaf.org");
362 user.setExpires(Chrono.timeStamp(gc));
367 @SuppressWarnings("unused")
368 private static Role newRolev2_0(boolean optional) {
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");
380 @SuppressWarnings("unused")
381 private static RoleKey newRoleKeyv2_0(boolean optional) {
382 RoleKey r = new RoleKey();
384 r.setName("org.osaaf.myns.myRole");
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");
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);
406 ur.setUser("yx4321");
407 ur.setRole("com.test.yourapp.yourRole");
408 ur.setExpires(Chrono.timeStamp());
409 urs.getUserRole().add(ur);
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");
420 a.setMemo("My memo (and then some)");
421 a.setOperation("MyOperation");
422 a.setStatus("MyStatus");
423 a.setTicket("MyTicket");
425 a.setUpdated(Chrono.timeStamp());
427 as.getApprovals().add(a);
429 a.setApprover("MyApprover2");
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);
442 @SuppressWarnings("unused")
443 private static Approval newApprovalv2_0(boolean optional) {
444 Approval a = new Approval();
445 a.setApprover("MyApprover");
447 a.setMemo("My memo (and then some)");
448 a.setOperation("MyOperation");
449 a.setStatus("MyStatus");
450 a.setTicket("MyTicket");
452 a.setUpdated(Chrono.timeStamp());
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");