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, NoSuchMethodException, 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();
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));
152 @SuppressWarnings("unused")
153 private static Request newRequestv2_0(boolean optional) {
154 Request r = new Request();
158 @SuppressWarnings("unused")
159 private static RolePermRequest newRolePermRequestv2_0(boolean optional) {
160 RolePermRequest rpr = new RolePermRequest();
161 Pkey pkey = new Pkey();
162 pkey.setType("org.osaaf.myns.mytype");
163 pkey.setInstance("myInstance");
164 pkey.setAction("myAction");
166 rpr.setRole("org.osaaf.myns.myrole");
167 if (optional)setOptional(rpr);
171 @SuppressWarnings("unused")
172 private static Roles newRolesv2_0(boolean optional) {
175 Roles rs = new Roles();
178 r.setName("org.osaaf.myns.myRole");
181 p.setType("org.osaaf.myns.myType");
182 p.setInstance("myInstance");
183 p.setAction("myAction");
187 p.setType("org.osaaf.myns.myType");
188 p.setInstance("myInstance");
189 p.setAction("myOtherAction");
193 r.setName("org.osaaf.myns.myOtherRole");
196 p.setType("org.osaaf.myns.myOtherType");
197 p.setInstance("myInstance");
198 p.setAction("myAction");
202 p.setType("org.osaaf.myns.myOthertype");
203 p.setInstance("myInstance");
204 p.setAction("myOtherAction");
210 @SuppressWarnings("unused")
211 private static PermRequest newPermRequestv2_0(boolean optional) {
212 PermRequest pr = new PermRequest();
213 pr.setType("org.osaaf.myns.myType");
214 pr.setInstance("myInstance");
215 pr.setAction("myAction");
217 pr.setDescription("Short and meaningful verbiage about the Permission");
224 @SuppressWarnings("unused")
225 private static Perm newPermv2_0(boolean optional) {
226 Perm pr = new Perm();
227 pr.setType("org.osaaf.myns.myType");
228 pr.setInstance("myInstance");
229 pr.setAction("myAction");
230 pr.getRoles().add("org.osaaf.aaf.myRole");
231 pr.getRoles().add("org.osaaf.aaf.myRole2");
232 pr.setDescription("This is my description, and I'm sticking with it");
234 pr.setDescription("Short and meaningful verbiage about the Permission");
240 @SuppressWarnings("unused")
241 private static PermKey newPermKeyv2_0(boolean optional) {
242 PermKey pr = new PermKey();
243 pr.setType("org.osaaf.myns.myType");
244 pr.setInstance("myInstance");
245 pr.setAction("myAction");
249 @SuppressWarnings("unused")
250 private static Perms newPermsv2_0(boolean optional) {
251 Perms perms = new Perms();
253 perms.getPerm().add(p);
254 p.setType("org.osaaf.myns.myType");
255 p.setInstance("myInstance");
256 p.setAction("myAction");
257 p.getRoles().add("org.osaaf.myns.myRole");
258 p.getRoles().add("org.osaaf.myns.myRole2");
262 perms.getPerm().add(p);
263 p.setType("org.osaaf.myns.myOtherType");
264 p.setInstance("myInstance");
265 p.setAction("myOtherAction");
266 p.getRoles().add("org.osaaf.myns.myRole");
267 p.getRoles().add("org.osaaf.myns.myRole2");
273 @SuppressWarnings("unused")
274 private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
275 UserRoleRequest urr = new UserRoleRequest();
276 urr.setRole("org.osaaf.myns.myRole");
277 urr.setUser("ab1234@people.osaaf.org");
284 @SuppressWarnings("unused")
285 private static NsRequest newNsRequestv2_0(boolean optional) {
286 NsRequest nr = new NsRequest();
287 nr.setName("org.osaaf.myns");
288 nr.getResponsible().add("ab1234@people.osaaf.org");
289 nr.getResponsible().add("cd5678@people.osaaf.org");
290 nr.getAdmin().add("zy9876@people.osaaf.org");
291 nr.getAdmin().add("xw5432@people.osaaf.org");
293 nr.setDescription("This is my Namespace to set up");
301 @SuppressWarnings("unused")
302 private static Nss newNssv2_0(boolean optional) {
308 ns.setName("org.osaaf.myns");
309 ns.getResponsible().add("ab1234@people.osaaf.org");
310 ns.getResponsible().add("cd5678@people.osaaf.org");
311 ns.getAdmin().add("zy9876@people.osaaf.org");
312 ns.getAdmin().add("xw5432@people.osaaf.org");
313 ns.setDescription("This is my Namespace to set up");
316 ns.setName("org.osaaf.myOtherNs");
317 ns.getResponsible().add("ab1234@people.osaaf.org");
318 ns.getResponsible().add("cd5678@people.osaaf.org");
319 ns.getAdmin().add("zy9876@people.osaaf.org");
320 ns.getAdmin().add("xw5432@people.osaaf.org");
324 @SuppressWarnings("unused")
325 private static RoleRequest newRoleRequestv2_0(boolean optional) {
326 RoleRequest rr = new RoleRequest();
327 rr.setName("org.osaaf.myns.myRole");
329 rr.setDescription("This is my Role");
335 @SuppressWarnings("unused")
336 private static CredRequest newCredRequestv2_0(boolean optional) {
337 CredRequest cr = new CredRequest();
338 cr.setId("myID@fully.qualified.domain");
341 cr.setEntry("0x125AB256344CE");
343 cr.setPassword("This is my provisioned password");
349 @SuppressWarnings("unused")
350 private static Users newUsersv2_0(boolean optional) {
353 Users users = new Users();
354 user = new Users.User();
355 users.getUser().add(user);
356 user.setId("ab1234@people.osaaf.org");
357 GregorianCalendar gc = new GregorianCalendar();
358 user.setExpires(Chrono.timeStamp(gc));
360 user = new Users.User();
361 users.getUser().add(user);
362 user.setId("zy9876@people.osaaf.org");
363 user.setExpires(Chrono.timeStamp(gc));
368 @SuppressWarnings("unused")
369 private static Role newRolev2_0(boolean optional) {
372 r.setName("org.osaaf.myns.myRole");
373 r.getPerms().add(p = new Pkey());
374 p.setType("org.osaaf.myns.myType");
375 p.setInstance("myInstance");
376 p.setAction("myAction");
381 @SuppressWarnings("unused")
382 private static RoleKey newRoleKeyv2_0(boolean optional) {
383 RoleKey r = new RoleKey();
385 r.setName("org.osaaf.myns.myRole");
389 @SuppressWarnings("unused")
390 private static Keys newKeysv2_0(boolean optional) {
391 Keys ks = new Keys();
392 ks.getKey().add("Reponse 1");
393 ks.getKey().add("Response 2");
397 @SuppressWarnings("unused")
398 private static UserRoles newUserRolesv2_0(boolean optional) {
399 UserRoles urs = new UserRoles();
400 UserRole ur = new UserRole();
401 ur.setUser("xy1234");
402 ur.setRole("com.test.myapp.myRole");
403 ur.setExpires(Chrono.timeStamp());
404 urs.getUserRole().add(ur);
407 ur.setUser("yx4321");
408 ur.setRole("com.test.yourapp.yourRole");
409 ur.setExpires(Chrono.timeStamp());
410 urs.getUserRole().add(ur);
415 @SuppressWarnings("unused")
416 private static Approvals newApprovalsv2_0(boolean optional) {
417 Approvals as = new Approvals();
418 Approval a = new Approval();
419 a.setApprover("MyApprover");
421 a.setMemo("My memo (and then some)");
422 a.setOperation("MyOperation");
423 a.setStatus("MyStatus");
424 a.setTicket("MyTicket");
426 a.setUpdated(Chrono.timeStamp());
428 as.getApprovals().add(a);
430 a.setApprover("MyApprover2");
432 a.setMemo("My memo (and then some)2");
433 a.setOperation("MyOperation2");
434 a.setStatus("MyStatus2");
435 a.setTicket("MyTicket2");
436 a.setType("MyType2");
437 a.setUpdated(Chrono.timeStamp());
438 a.setUser("MyUser2");
439 as.getApprovals().add(a);
443 @SuppressWarnings("unused")
444 private static Approval newApprovalv2_0(boolean optional) {
445 Approval a = new Approval();
446 a.setApprover("MyApprover");
448 a.setMemo("My memo (and then some)");
449 a.setOperation("MyOperation");
450 a.setStatus("MyStatus");
451 a.setTicket("MyTicket");
453 a.setUpdated(Chrono.timeStamp());
460 @SuppressWarnings("unused")
461 private static aaf.v2_0.Error newErrorv2_0(boolean optional) {
462 aaf.v2_0.Error err = new aaf.v2_0.Error();
463 err.setMessageId("SVC1403");
464 err.setText("MyText %s, %s: The last three digits are usually the HTTP Code");
465 err.getVariables().add("Variable 1");
466 err.getVariables().add("Variable 2");