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)break;
82 throw new APIException(nameOrContentType + " does not contain Class Information");
85 className = nameOrContentType;
88 // No Void.class in aaf.v2_0 package causing errors when trying to use a newVoidv2_0
89 // method similar to others in this class. This makes it work, but is it right?
90 if ("Void".equals(className)) return "";
92 if("1.1".equals(version)) {
94 } else if(version!=null) {
95 version = "v" + version.replace('.', '_');
102 cls = Examples.class.getClassLoader().loadClass("aaf."+version+'.'+className);
103 } catch (ClassNotFoundException e) {
104 throw new APIException(e);
109 meth = Examples.class.getDeclaredMethod("new"+cls.getSimpleName()+version,boolean.class);
110 } catch (Exception e) {
111 throw new APIException("ERROR: " + cls.getName() + " does not have an Example in Code. Request from AAF Developers");
114 RosettaDF<C> df = env.newDataFactory(cls);
115 df.option(Data.PRETTY);
117 Object data = meth.invoke(null,optional);
119 @SuppressWarnings("unchecked")
120 String rv = df.newData().load((C)data).out(type).asString();
121 // Object obj = df.newData().in(type).load(rv).asObject();
126 * Set Base Class Request (easier than coding over and over)
128 private static void setOptional(Request req) {
129 GregorianCalendar gc = new GregorianCalendar();
130 req.setStart(Chrono.timeStamp(gc));
131 gc.add(GregorianCalendar.MONTH, 6);
132 req.setEnd(Chrono.timeStamp(gc));
133 // req.setForce("false");
137 @SuppressWarnings("unused")
138 private static Request newRequestv2_0(boolean optional) {
139 Request r = new Request();
143 @SuppressWarnings("unused")
144 private static RolePermRequest newRolePermRequestv2_0(boolean optional) {
145 RolePermRequest rpr = new RolePermRequest();
146 Pkey pkey = new Pkey();
147 pkey.setType("org.osaaf.myns.mytype");
148 pkey.setInstance("myInstance");
149 pkey.setAction("myAction");
151 rpr.setRole("org.osaaf.myns.myrole");
152 if(optional)setOptional(rpr);
156 @SuppressWarnings("unused")
157 private static Roles newRolesv2_0(boolean optional) {
160 Roles rs = new Roles();
161 rs.getRole().add(r = new Role());
162 r.setName("org.osaaf.myns.myRole");
163 r.getPerms().add(p = new Pkey());
164 p.setType("org.osaaf.myns.myType");
165 p.setInstance("myInstance");
166 p.setAction("myAction");
168 r.getPerms().add(p = new Pkey());
169 p.setType("org.osaaf.myns.myType");
170 p.setInstance("myInstance");
171 p.setAction("myOtherAction");
173 rs.getRole().add(r = new Role());
174 r.setName("org.osaaf.myns.myOtherRole");
175 r.getPerms().add(p = new Pkey());
176 p.setType("org.osaaf.myns.myOtherType");
177 p.setInstance("myInstance");
178 p.setAction("myAction");
180 r.getPerms().add(p = new Pkey());
181 p.setType("org.osaaf.myns.myOthertype");
182 p.setInstance("myInstance");
183 p.setAction("myOtherAction");
189 @SuppressWarnings("unused")
190 private static PermRequest newPermRequestv2_0(boolean optional) {
191 PermRequest pr = new PermRequest();
192 pr.setType("org.osaaf.myns.myType");
193 pr.setInstance("myInstance");
194 pr.setAction("myAction");
196 pr.setDescription("Short and meaningful verbiage about the Permission");
203 @SuppressWarnings("unused")
204 private static Perm newPermv2_0(boolean optional) {
205 Perm pr = new Perm();
206 pr.setType("org.osaaf.myns.myType");
207 pr.setInstance("myInstance");
208 pr.setAction("myAction");
209 pr.getRoles().add("org.osaaf.aaf.myRole");
210 pr.getRoles().add("org.osaaf.aaf.myRole2");
211 pr.setDescription("This is my description, and I'm sticking with it");
213 pr.setDescription("Short and meaningful verbiage about the Permission");
219 @SuppressWarnings("unused")
220 private static PermKey newPermKeyv2_0(boolean optional) {
221 PermKey pr = new PermKey();
222 pr.setType("org.osaaf.myns.myType");
223 pr.setInstance("myInstance");
224 pr.setAction("myAction");
228 @SuppressWarnings("unused")
229 private static Perms newPermsv2_0(boolean optional) {
230 Perms perms = new Perms();
232 perms.getPerm().add(p=new Perm());
233 p.setType("org.osaaf.myns.myType");
234 p.setInstance("myInstance");
235 p.setAction("myAction");
236 p.getRoles().add("org.osaaf.myns.myRole");
237 p.getRoles().add("org.osaaf.myns.myRole2");
240 perms.getPerm().add(p=new Perm());
241 p.setType("org.osaaf.myns.myOtherType");
242 p.setInstance("myInstance");
243 p.setAction("myOtherAction");
244 p.getRoles().add("org.osaaf.myns.myRole");
245 p.getRoles().add("org.osaaf.myns.myRole2");
251 @SuppressWarnings("unused")
252 private static UserRoleRequest newUserRoleRequestv2_0(boolean optional) {
253 UserRoleRequest urr = new UserRoleRequest();
254 urr.setRole("org.osaaf.myns.myRole");
255 urr.setUser("ab1234@people.osaaf.org");
256 if(optional) setOptional(urr);
260 @SuppressWarnings("unused")
261 private static NsRequest newNsRequestv2_0(boolean optional) {
262 NsRequest nr = new NsRequest();
263 nr.setName("org.osaaf.myns");
264 nr.getResponsible().add("ab1234@people.osaaf.org");
265 nr.getResponsible().add("cd5678@people.osaaf.org");
266 nr.getAdmin().add("zy9876@people.osaaf.org");
267 nr.getAdmin().add("xw5432@people.osaaf.org");
269 nr.setDescription("This is my Namespace to set up");
277 @SuppressWarnings("unused")
278 private static Nss newNssv2_0(boolean optional) {
282 nss.getNs().add(ns = new Nss.Ns());
283 ns.setName("org.osaaf.myns");
284 ns.getResponsible().add("ab1234@people.osaaf.org");
285 ns.getResponsible().add("cd5678@people.osaaf.org");
286 ns.getAdmin().add("zy9876@people.osaaf.org");
287 ns.getAdmin().add("xw5432@people.osaaf.org");
288 ns.setDescription("This is my Namespace to set up");
290 nss.getNs().add(ns = new Nss.Ns());
291 ns.setName("org.osaaf.myOtherNs");
292 ns.getResponsible().add("ab1234@people.osaaf.org");
293 ns.getResponsible().add("cd5678@people.osaaf.org");
294 ns.getAdmin().add("zy9876@people.osaaf.org");
295 ns.getAdmin().add("xw5432@people.osaaf.org");
299 @SuppressWarnings("unused")
300 private static RoleRequest newRoleRequestv2_0(boolean optional) {
301 RoleRequest rr = new RoleRequest();
302 rr.setName("org.osaaf.myns.myRole");
304 rr.setDescription("This is my Role");
310 @SuppressWarnings("unused")
311 private static CredRequest newCredRequestv2_0(boolean optional) {
312 CredRequest cr = new CredRequest();
313 cr.setId("myID@fully.qualified.domain");
316 cr.setEntry("0x125AB256344CE");
318 cr.setPassword("This is my provisioned password");
324 @SuppressWarnings("unused")
325 private static Users newUsersv2_0(boolean optional) {
328 Users users = new Users();
329 users.getUser().add(user = new Users.User());
330 user.setId("ab1234@people.osaaf.org");
331 GregorianCalendar gc = new GregorianCalendar();
332 user.setExpires(Chrono.timeStamp(gc));
334 users.getUser().add(user = new Users.User());
335 user.setId("zy9876@people.osaaf.org");
336 user.setExpires(Chrono.timeStamp(gc));
341 @SuppressWarnings("unused")
342 private static Role newRolev2_0(boolean optional) {
345 r.setName("org.osaaf.myns.myRole");
346 r.getPerms().add(p = new Pkey());
347 p.setType("org.osaaf.myns.myType");
348 p.setInstance("myInstance");
349 p.setAction("myAction");
354 @SuppressWarnings("unused")
355 private static RoleKey newRoleKeyv2_0(boolean optional) {
356 RoleKey r = new RoleKey();
358 r.setName("org.osaaf.myns.myRole");
362 @SuppressWarnings("unused")
363 private static Keys newKeysv2_0(boolean optional) {
364 Keys ks = new Keys();
365 ks.getKey().add("Reponse 1");
366 ks.getKey().add("Response 2");
370 @SuppressWarnings("unused")
371 private static UserRoles newUserRolesv2_0(boolean optional) {
372 UserRoles urs = new UserRoles();
373 UserRole ur = new UserRole();
374 ur.setUser("xy1234");
375 ur.setRole("com.test.myapp.myRole");
376 ur.setExpires(Chrono.timeStamp());
377 urs.getUserRole().add(ur);
380 ur.setUser("yx4321");
381 ur.setRole("com.test.yourapp.yourRole");
382 ur.setExpires(Chrono.timeStamp());
383 urs.getUserRole().add(ur);
388 @SuppressWarnings("unused")
389 private static Approvals newApprovalsv2_0(boolean optional) {
390 Approvals as = new Approvals();
391 Approval a = new Approval();
392 a.setApprover("MyApprover");
394 a.setMemo("My memo (and then some)");
395 a.setOperation("MyOperation");
396 a.setStatus("MyStatus");
397 a.setTicket("MyTicket");
399 a.setUpdated(Chrono.timeStamp());
401 as.getApprovals().add(a);
403 a.setApprover("MyApprover2");
405 a.setMemo("My memo (and then some)2");
406 a.setOperation("MyOperation2");
407 a.setStatus("MyStatus2");
408 a.setTicket("MyTicket2");
409 a.setType("MyType2");
410 a.setUpdated(Chrono.timeStamp());
411 a.setUser("MyUser2");
412 as.getApprovals().add(a);
416 @SuppressWarnings("unused")
417 private static Approval newApprovalv2_0(boolean optional) {
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());
433 @SuppressWarnings("unused")
434 private static aaf.v2_0.Error newErrorv2_0(boolean optional) {
435 aaf.v2_0.Error err = new aaf.v2_0.Error();
436 err.setMessageId("SVC1403");
437 err.setText("MyText %s, %s: The last three digits are usually the HTTP Code");
438 err.getVariables().add("Variable 1");
439 err.getVariables().add("Variable 2");