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.");
121 meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+vprefix+minor,boolean.class);
122 } catch (Exception e) {
123 throw new APIException("ERROR: " + cls.getName() + " does not have an Example in Code. Request from AAF Developers");
126 RosettaDF<C> df = env.newDataFactory(cls);
127 df.option(Data.PRETTY);
129 Object data = meth.invoke(null,optional);
131 @SuppressWarnings("unchecked")
132 String rv = df.newData().load((C)data).out(type).asString();
133 // Object obj = df.newData().in(type).load(rv).asObject();
138 * Set Base Class Request (easier than coding over and over)
140 private static void setOptional(Request req) {
141 GregorianCalendar gc = new GregorianCalendar();
142 req.setStart(Chrono.timeStamp(gc));
143 gc.add(GregorianCalendar.MONTH, 6);
144 req.setEnd(Chrono.timeStamp(gc));
145 // req.setForce("false");
149 @SuppressWarnings("unused")
150 private static Request newRequestv2_0(boolean optional) {
151 Request r = new Request();
155 @SuppressWarnings("unused")
156 private static RolePermRequest newRolePermRequestv2_0(boolean optional) {
157 RolePermRequest rpr = new RolePermRequest();
158 Pkey pkey = new Pkey();
159 pkey.setType("org.osaaf.myns.mytype");
160 pkey.setInstance("myInstance");
161 pkey.setAction("myAction");
163 rpr.setRole("org.osaaf.myns.myrole");
164 if (optional)setOptional(rpr);
168 @SuppressWarnings("unused")
169 private static Roles newRolesv2_0(boolean optional) {
172 Roles rs = new Roles();
175 r.setName("org.osaaf.myns.myRole");
178 p.setType("org.osaaf.myns.myType");
179 p.setInstance("myInstance");
180 p.setAction("myAction");
184 p.setType("org.osaaf.myns.myType");
185 p.setInstance("myInstance");
186 p.setAction("myOtherAction");
190 r.setName("org.osaaf.myns.myOtherRole");
193 p.setType("org.osaaf.myns.myOtherType");
194 p.setInstance("myInstance");
195 p.setAction("myAction");
199 p.setType("org.osaaf.myns.myOthertype");
200 p.setInstance("myInstance");
201 p.setAction("myOtherAction");
207 @SuppressWarnings("unused")
208 private static PermRequest newPermRequestv2_0(boolean optional) {
209 PermRequest pr = new PermRequest();
210 pr.setType("org.osaaf.myns.myType");
211 pr.setInstance("myInstance");
212 pr.setAction("myAction");
214 pr.setDescription("Short and meaningful verbiage about the Permission");
221 @SuppressWarnings("unused")
222 private static Perm newPermv2_0(boolean optional) {
223 Perm pr = new Perm();
224 pr.setType("org.osaaf.myns.myType");
225 pr.setInstance("myInstance");
226 pr.setAction("myAction");
227 pr.getRoles().add("org.osaaf.aaf.myRole");
228 pr.getRoles().add("org.osaaf.aaf.myRole2");
229 pr.setDescription("This is my description, and I'm sticking with it");
231 pr.setDescription("Short and meaningful verbiage about the Permission");
237 @SuppressWarnings("unused")
238 private static PermKey newPermKeyv2_0(boolean optional) {
239 PermKey pr = new PermKey();
240 pr.setType("org.osaaf.myns.myType");
241 pr.setInstance("myInstance");
242 pr.setAction("myAction");
246 @SuppressWarnings("unused")
247 private static Perms newPermsv2_0(boolean optional) {
248 Perms perms = new Perms();
250 perms.getPerm().add(p);
251 p.setType("org.osaaf.myns.myType");
252 p.setInstance("myInstance");
253 p.setAction("myAction");
254 p.getRoles().add("org.osaaf.myns.myRole");
255 p.getRoles().add("org.osaaf.myns.myRole2");
259 perms.getPerm().add(p);
260 p.setType("org.osaaf.myns.myOtherType");
261 p.setInstance("myInstance");
262 p.setAction("myOtherAction");
263 p.getRoles().add("org.osaaf.myns.myRole");
264 p.getRoles().add("org.osaaf.myns.myRole2");
270 @SuppressWarnings("unused")
271 private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
272 UserRoleRequest urr = new UserRoleRequest();
273 urr.setRole("org.osaaf.myns.myRole");
274 urr.setUser("ab1234@people.osaaf.org");
275 if (optional) setOptional(urr);
279 @SuppressWarnings("unused")
280 private static NsRequest newNsRequestv2_0(boolean optional) {
281 NsRequest nr = new NsRequest();
282 nr.setName("org.osaaf.myns");
283 nr.getResponsible().add("ab1234@people.osaaf.org");
284 nr.getResponsible().add("cd5678@people.osaaf.org");
285 nr.getAdmin().add("zy9876@people.osaaf.org");
286 nr.getAdmin().add("xw5432@people.osaaf.org");
288 nr.setDescription("This is my Namespace to set up");
296 @SuppressWarnings("unused")
297 private static Nss newNssv2_0(boolean optional) {
301 nss.getNs().add(ns = new Nss.Ns());
302 ns.setName("org.osaaf.myns");
303 ns.getResponsible().add("ab1234@people.osaaf.org");
304 ns.getResponsible().add("cd5678@people.osaaf.org");
305 ns.getAdmin().add("zy9876@people.osaaf.org");
306 ns.getAdmin().add("xw5432@people.osaaf.org");
307 ns.setDescription("This is my Namespace to set up");
309 nss.getNs().add(ns = new Nss.Ns());
310 ns.setName("org.osaaf.myOtherNs");
311 ns.getResponsible().add("ab1234@people.osaaf.org");
312 ns.getResponsible().add("cd5678@people.osaaf.org");
313 ns.getAdmin().add("zy9876@people.osaaf.org");
314 ns.getAdmin().add("xw5432@people.osaaf.org");
318 @SuppressWarnings("unused")
319 private static RoleRequest newRoleRequestv2_0(boolean optional) {
320 RoleRequest rr = new RoleRequest();
321 rr.setName("org.osaaf.myns.myRole");
323 rr.setDescription("This is my Role");
329 @SuppressWarnings("unused")
330 private static CredRequest newCredRequestv2_0(boolean optional) {
331 CredRequest cr = new CredRequest();
332 cr.setId("myID@fully.qualified.domain");
335 cr.setEntry("0x125AB256344CE");
337 cr.setPassword("This is my provisioned password");
343 @SuppressWarnings("unused")
344 private static Users newUsersv2_0(boolean optional) {
347 Users users = new Users();
348 user = new Users.User();
349 users.getUser().add(user);
350 user.setId("ab1234@people.osaaf.org");
351 GregorianCalendar gc = new GregorianCalendar();
352 user.setExpires(Chrono.timeStamp(gc));
354 user = new Users.User();
355 users.getUser().add(user);
356 user.setId("zy9876@people.osaaf.org");
357 user.setExpires(Chrono.timeStamp(gc));
362 @SuppressWarnings("unused")
363 private static Role newRolev2_0(boolean optional) {
366 r.setName("org.osaaf.myns.myRole");
367 r.getPerms().add(p = new Pkey());
368 p.setType("org.osaaf.myns.myType");
369 p.setInstance("myInstance");
370 p.setAction("myAction");
375 @SuppressWarnings("unused")
376 private static RoleKey newRoleKeyv2_0(boolean optional) {
377 RoleKey r = new RoleKey();
379 r.setName("org.osaaf.myns.myRole");
383 @SuppressWarnings("unused")
384 private static Keys newKeysv2_0(boolean optional) {
385 Keys ks = new Keys();
386 ks.getKey().add("Reponse 1");
387 ks.getKey().add("Response 2");
391 @SuppressWarnings("unused")
392 private static UserRoles newUserRolesv2_0(boolean optional) {
393 UserRoles urs = new UserRoles();
394 UserRole ur = new UserRole();
395 ur.setUser("xy1234");
396 ur.setRole("com.test.myapp.myRole");
397 ur.setExpires(Chrono.timeStamp());
398 urs.getUserRole().add(ur);
401 ur.setUser("yx4321");
402 ur.setRole("com.test.yourapp.yourRole");
403 ur.setExpires(Chrono.timeStamp());
404 urs.getUserRole().add(ur);
409 @SuppressWarnings("unused")
410 private static Approvals newApprovalsv2_0(boolean optional) {
411 Approvals as = new Approvals();
412 Approval a = new Approval();
413 a.setApprover("MyApprover");
415 a.setMemo("My memo (and then some)");
416 a.setOperation("MyOperation");
417 a.setStatus("MyStatus");
418 a.setTicket("MyTicket");
420 a.setUpdated(Chrono.timeStamp());
422 as.getApprovals().add(a);
424 a.setApprover("MyApprover2");
426 a.setMemo("My memo (and then some)2");
427 a.setOperation("MyOperation2");
428 a.setStatus("MyStatus2");
429 a.setTicket("MyTicket2");
430 a.setType("MyType2");
431 a.setUpdated(Chrono.timeStamp());
432 a.setUser("MyUser2");
433 as.getApprovals().add(a);
437 @SuppressWarnings("unused")
438 private static Approval newApprovalv2_0(boolean optional) {
439 Approval a = new Approval();
440 a.setApprover("MyApprover");
442 a.setMemo("My memo (and then some)");
443 a.setOperation("MyOperation");
444 a.setStatus("MyStatus");
445 a.setTicket("MyTicket");
447 a.setUpdated(Chrono.timeStamp());
454 @SuppressWarnings("unused")
455 private static aaf.v2_0.Error newErrorv2_0(boolean optional) {
456 aaf.v2_0.Error err = new aaf.v2_0.Error();
457 err.setMessageId("SVC1403");
458 err.setText("MyText %s, %s: The last three digits are usually the HTTP Code");
459 err.getVariables().add("Variable 1");
460 err.getVariables().add("Variable 2");