1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/**
* ============LICENSE_START====================================================
* org.onap.aaf
* ===========================================================================
* Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
* ===========================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END====================================================
*
*/
package org.onap.aaf.auth.cmd;
import java.io.PrintWriter;
import java.io.StringReader;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import org.onap.aaf.auth.env.AuthzEnv;
import org.onap.aaf.auth.org.OrganizationException;
import org.onap.aaf.auth.rserv.HttpMethods;
import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.LocatorException;
import org.onap.aaf.cadi.client.Future;
import org.onap.aaf.cadi.client.Rcli;
import org.onap.aaf.cadi.client.Retryable;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.cadi.http.HMangr;
import org.onap.aaf.misc.env.APIException;
import org.onap.aaf.misc.env.Data.TYPE;
import org.onap.aaf.misc.env.util.Chrono;
import org.onap.aaf.misc.rosetta.env.RosettaDF;
import aaf.v2_0.Error;
import aaf.v2_0.History;
import aaf.v2_0.History.Item;
import aaf.v2_0.Request;
public abstract class Cmd {
// Sonar claims DateFormat is not thread safe. Leave as Instance Variable.
private final DateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
protected static final String BLANK = "";
protected static final String COMMA = ","; // for use in splits
protected static final int lineLength = 80;
private final static String hformat = "%-23s %-5s %-20s %-35s\n";
public static final String STARTDATE = "startdate";
public static final String ENDDATE = "enddate";
private String name;
private final Param[] params;
private int required;
protected final Cmd parent;
protected final List<Cmd> children;
private final static ConcurrentHashMap<Class<?>,RosettaDF<?>> dfs = new ConcurrentHashMap<Class<?>,RosettaDF<?>>();
public final AAFcli aafcli;
protected Access access;
private AuthzEnv env;
private final String defaultRealm;
public Cmd(AAFcli aafcli, String name, Param ... params) {
this(aafcli,null, name,params);
}
public Cmd(Cmd parent, String name, Param ... params) {
this(parent.aafcli,parent, name,params);
}
Cmd(AAFcli aafcli, Cmd parent, String name, Param ... params) {
this.parent = parent;
this.aafcli = aafcli;
this.env = aafcli.env;
this.access = aafcli.access;
if(parent!=null) {
parent.children.add(this);
}
children = new ArrayList<Cmd>();
this.params = params;
this.name = name;
required=0;
for(Param p : params) {
if(p.required) {
++required;
}
}
String temp = access.getProperty(Config.AAF_DEFAULT_REALM,null);
if(temp!=null && !temp.startsWith("@")) {
defaultRealm = '@' + temp;
} else {
defaultRealm="<Set Default Realm>";
}
}
public final int exec(int idx, String ... args) throws CadiException, APIException, LocatorException {
if(args.length-idx<required) {
throw new CadiException(build(new StringBuilder("Too few args: "),null).toString());
}
return _exec(idx,args);
}
protected abstract int _exec(int idx, final String ... args) throws CadiException, APIException, LocatorException;
public void detailedHelp(int indent,StringBuilder sb) {
}
protected void detailLine(StringBuilder sb, int length, String s) {
multiChar(sb,length,' ',0);
sb.append(s);
}
public void apis(int indent,StringBuilder sb) {
}
protected void api(StringBuilder sb, int indent, HttpMethods meth, String pathInfo, Class<?> cls,boolean head) {
final String smeth = meth.name();
if(head) {
sb.append('\n');
detailLine(sb,indent,"APIs:");
}
indent+=2;
multiChar(sb,indent,' ',0);
sb.append(smeth);
sb.append(' ');
sb.append(pathInfo);
String cliString = aafcli.typeString(cls,true);
if(indent+smeth.length()+pathInfo.length()+cliString.length()+2>80) {
sb.append(" ...");
multiChar(sb,indent+3+smeth.length(),' ',0);
} else { // same line
sb.append(' ');
}
sb.append(cliString);
}
protected void multiChar(StringBuilder sb, int length, char c, int indent) {
sb.append('\n');
for(int i=0;i<indent;++i)sb.append(' ');
for(int i=indent;i<length;++i)sb.append(c);
}
public StringBuilder build(StringBuilder sb, StringBuilder detail) {
if(name!=null) {
sb.append(name);
sb.append(' ');
}
int line = sb.lastIndexOf("\n")+1;
if(line<0) {
line=0;
}
int indent = sb.length()-line;
for(Param p : params) {
sb.append(p.required?'<':'[');
sb.append(p.tag);
sb.append(p.required?"> ": "] ");
}
boolean first = true;
for(Cmd child : children) {
if(!(child instanceof DeprecatedCMD)) {
if(first) {
first = false;
} else if(detail==null) {
multiChar(sb,indent,' ',0);
} else {
// Write parents for Detailed Report
Stack<String> stack = new Stack<String>();
for(Cmd c = child.parent;c!=null;c=c.parent) {
if(c.name!=null) {
stack.push(c.name);
}
}
if(!stack.isEmpty()) {
sb.append(" ");
while(!stack.isEmpty()) {
sb.append(stack.pop());
sb.append(' ');
}
}
}
child.build(sb,detail);
if(detail!=null) {
child.detailedHelp(4, detail);
// If Child wrote something, then add, bracketing by lines
if(detail.length()>0) {
multiChar(sb,80,'-',2);
sb.append(detail);
sb.append('\n');
multiChar(sb,80,'-',2);
sb.append('\n');
detail.setLength(0); // reuse
} else {
sb.append('\n');
}
}
}
}
return sb;
}
protected void error(Future<?> future) {
StringBuilder sb = new StringBuilder("Failed");
String desc = future.body();
int code = future.code();
if(desc==null || desc.length()==0) {
withCode(sb,code);
} else if(desc.startsWith("{")) {
StringReader sr = new StringReader(desc);
try {
// Note: 11-18-2013, JonathanGathman. This rather convoluted Message Structure required by TSS Restful Specs, reflecting "Northbound" practices.
Error err = getDF(Error.class).newData().in(TYPE.JSON).load(sr).asObject();
sb.append(" [");
sb.append(err.getMessageId());
sb.append("]: ");
String messageBody = err.getText();
List<String> vars = err.getVariables();
int pipe;
for (int varCounter=0;varCounter<vars.size();) {
String var = vars.get(varCounter);
++varCounter;
if (messageBody.indexOf("%" + varCounter) >= 0) {
if((pipe = var.indexOf('|'))>=0) { // In AAF, we use a PIPE for Choice
if (aafcli.isTest()) {
String expiresStr = var.substring(pipe);
var = var.replace(expiresStr, "[Placeholder]");
} else {
StringBuilder varsb = new StringBuilder(var);
varsb.deleteCharAt(pipe);
var = varsb.toString();
}
messageBody = messageBody.replace("%" + varCounter, varCounter-1 + ") " + var);
} else {
messageBody = messageBody.replace("%" + varCounter, var);
}
}
}
sb.append(messageBody);
} catch (Exception e) {
withCode(sb,code);
sb.append(" (Note: Details cannot be obtained from Error Structure)");
}
} else if(desc.startsWith("<html>")){ // Core Jetty, etc sends HTML for Browsers
withCode(sb,code);
} else {
sb.append(" with code ");
sb.append(code);
sb.append(", ");
sb.append(desc);
}
pw().println(sb);
}
private void withCode(StringBuilder sb, Integer code) {
sb.append(" with code ");
sb.append(code);
switch(code) {
case 401:
sb.append(" (HTTP Not Authenticated)");
break;
case 403:
sb.append(" (HTTP Forbidden)");
break;
case 404:
sb.append(" (HTTP Not Found)");
break;
default:
}
}
/**
* Consistently set start and end dates from Requests (all derived from Request)
* @param req
*/
protected void setStartEnd(Request req) {
// Set Start/End Dates, if exist
String str;
if((str = access.getProperty(Cmd.STARTDATE,null))!=null) {
req.setStart(Chrono.timeStamp(Date.valueOf(str)));
}
if((str = access.getProperty(Cmd.ENDDATE,null))!=null) {
req.setEnd(Chrono.timeStamp(Date.valueOf(str)));
}
}
/**
* For Derived classes, who have ENV in this parent
*
* @param cls
* @return
* @throws APIException
*/
protected <T> RosettaDF<T> getDF(Class<T> cls) throws APIException {
return getDF(env,cls);
}
/**
* This works well, making available for GUI, etc.
* @param env
* @param cls
* @return
* @throws APIException
*/
@SuppressWarnings("unchecked")
public static <T> RosettaDF<T> getDF(AuthzEnv env, Class<T> cls) throws APIException {
RosettaDF<T> rdf = (RosettaDF<T>)dfs.get(cls);
if(rdf == null) {
rdf = env.newDataFactory(cls);
dfs.put(cls, rdf);
}
return rdf;
}
public void activity(History history, String header) {
if (history.getItem().isEmpty()) {
int start = header.indexOf('[');
if (start >= 0) {
pw().println("No Activity Found for " + header.substring(start));
}
} else {
pw().println(header);
for(int i=0;i<lineLength;++i)pw().print('-');
pw().println();
pw().format(hformat,"Date","Table","User","Memo");
for(int i=0;i<lineLength;++i)pw().print('-');
pw().println();
// Save Server time by Sorting locally
List<Item> items = history.getItem();
java.util.Collections.sort(items, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o2.getTimestamp().compare(o1.getTimestamp());
}
});
for(History.Item item : items) {
GregorianCalendar gc = item.getTimestamp().toGregorianCalendar();
pw().format(hformat,
dateFmt.format(gc.getTime()),
item.getTarget(),
item.getUser(),
item.getMemo());
}
}
}
/**
* Turn String Array into a | delimited String
* @param options
* @return
*/
public static String optionsToString(String[] options) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for(String s : options) {
if(first) {
first = false;
} else {
sb.append('|');
}
sb.append(s);
}
return sb.toString();
}
/**
* return which index number the Option matches.
*
* throws an Exception if not part of this Option Set
*
* @param options
* @param test
* @return
* @throws Exception
*/
public int whichOption(String[] options, String test) throws CadiException {
for(int i=0;i<options.length;++i) {
if(options[i].equals(test)) {
return i;
}
}
throw new CadiException(build(new StringBuilder("Invalid Option: "),null).toString());
}
// protected RosettaEnv env() {
// return aafcli.env;
// }
protected HMangr hman() {
return aafcli.hman;
}
public<RET> RET same(Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
// We're storing in AAFCli, because we know it's always the same, and single threaded
if(aafcli.prevCall!=null) {
retryable.item(aafcli.prevCall.item());
retryable.lastClient=aafcli.prevCall.lastClient;
}
RET ret = aafcli.hman.same(aafcli.ss,retryable);
// Store last call in AAFcli, because Cmds are all different instances.
aafcli.prevCall = retryable;
return ret;
}
public<RET> RET all(Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
this.setQueryParamsOn(retryable.lastClient);
return aafcli.hman.all(aafcli.ss,retryable);
}
public<RET> RET oneOf(Retryable<RET> retryable,String host) throws APIException, CadiException, LocatorException {
this.setQueryParamsOn(retryable.lastClient);
return aafcli.hman.oneOf(aafcli.ss,retryable,true,host);
}
protected PrintWriter pw() {
return AAFcli.pw;
}
public String getName() {
return name;
}
public void reportHead(String ... str) {
pw().println();
boolean first = true;
int i=0;
for(String s : str) {
if(first) {
if(++i>1) {
first = false;
pw().print("[");
}
} else {
pw().print("] [");
}
pw().print(s);
}
if(!first) {
pw().print(']');
}
pw().println();
reportLine();
}
public String reportColHead(String format, String ... args) {
pw().format(format,(Object[])args);
reportLine();
return format;
}
public void reportLine() {
for(int i=0;i<lineLength;++i)pw().print('-');
pw().println();
}
protected void setQueryParamsOn(Rcli<?> rcli) {
StringBuilder sb=null;
String force;
if((force=aafcli.forceString())!=null) {
sb = new StringBuilder("force=");
sb.append(force);
}
if(aafcli.addRequest()) {
if(sb==null) {
sb = new StringBuilder("future=true");
} else {
sb.append("&future=true");
}
}
if(sb!=null && rcli!=null) {
rcli.setQueryParams(sb.toString());
}
}
//
// /**
// * If Force is set, will return True once only, then revert to "FALSE".
// *
// * @return
// */
// protected String checkForce() {
// if(TRUE.equalsIgnoreCase(env.getProperty(FORCE, FALSE))) {
// env.setProperty(FORCE, FALSE);
// return "true";
// }
// return FALSE;
// }
public String toString() {
StringBuilder sb = new StringBuilder();
if(parent==null) { // ultimate parent
build(sb,null);
return sb.toString();
} else {
return parent.toString();
}
}
// private String getOrgRealm() {
// return ;
// }
//
/**
* Appends shortID with Realm, but only when allowed by Organization
* @throws OrganizationException
*/
public String fullID(String id) {
if(id != null) {
if (id.indexOf('@') < 0) {
id+=defaultRealm;
} else {
return id; // is already a full ID
}
}
return id;
}
}
|