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.auth.cmd;
24 import java.io.PrintWriter;
25 import java.io.StringReader;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Comparator;
31 import java.util.GregorianCalendar;
32 import java.util.List;
33 import java.util.Stack;
34 import java.util.concurrent.ConcurrentHashMap;
36 import org.onap.aaf.auth.env.AuthzEnv;
37 import org.onap.aaf.auth.org.OrganizationException;
38 import org.onap.aaf.auth.rserv.HttpMethods;
39 import org.onap.aaf.cadi.Access;
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.LocatorException;
42 import org.onap.aaf.cadi.client.Future;
43 import org.onap.aaf.cadi.client.Rcli;
44 import org.onap.aaf.cadi.client.Retryable;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.cadi.http.HMangr;
47 import org.onap.aaf.misc.env.APIException;
48 import org.onap.aaf.misc.env.Data.TYPE;
49 import org.onap.aaf.misc.env.util.Chrono;
50 import org.onap.aaf.misc.rosetta.env.RosettaDF;
52 import aaf.v2_0.Error;
53 import aaf.v2_0.History;
54 import aaf.v2_0.History.Item;
55 import aaf.v2_0.Request;
58 public abstract class Cmd {
59 private static final DateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
60 protected static final String BLANK = "";
61 protected static final String COMMA = ","; // for use in splits
63 protected static final int lineLength = 80;
65 private final static String hformat = "%-23s %-5s %-20s %-35s\n";
67 public static final String STARTDATE = "startdate";
68 public static final String ENDDATE = "enddate";
71 private final Param[] params;
73 protected final Cmd parent;
74 protected final List<Cmd> children;
75 private final static ConcurrentHashMap<Class<?>,RosettaDF<?>> dfs = new ConcurrentHashMap<Class<?>,RosettaDF<?>>();
76 public final AAFcli aafcli;
77 protected Access access;
79 private final String defaultRealm;
81 public Cmd(AAFcli aafcli, String name, Param ... params) {
82 this(aafcli,null, name,params);
85 public Cmd(Cmd parent, String name, Param ... params) {
86 this(parent.aafcli,parent, name,params);
89 Cmd(AAFcli aafcli, Cmd parent, String name, Param ... params) {
92 this.env = aafcli.env;
93 this.access = aafcli.access;
95 parent.children.add(this);
97 children = new ArrayList<Cmd>();
101 for(Param p : params) {
107 String temp = access.getProperty(Config.AAF_DEFAULT_REALM,null);
108 if(temp!=null && !temp.startsWith("@")) {
109 defaultRealm = '@' + temp;
111 defaultRealm="<Set Default Realm>";
115 public final int exec(int idx, String ... args) throws CadiException, APIException, LocatorException {
116 if(args.length-idx<required) {
117 throw new CadiException(build(new StringBuilder("Too few args: "),null).toString());
119 return _exec(idx,args);
122 protected abstract int _exec(int idx, final String ... args) throws CadiException, APIException, LocatorException;
124 public void detailedHelp(int indent,StringBuilder sb) {
127 protected void detailLine(StringBuilder sb, int length, String s) {
128 multiChar(sb,length,' ',0);
132 public void apis(int indent,StringBuilder sb) {
135 protected void api(StringBuilder sb, int indent, HttpMethods meth, String pathInfo, Class<?> cls,boolean head) {
136 final String smeth = meth.name();
139 detailLine(sb,indent,"APIs:");
142 multiChar(sb,indent,' ',0);
146 String cliString = aafcli.typeString(cls,true);
147 if(indent+smeth.length()+pathInfo.length()+cliString.length()+2>80) {
149 multiChar(sb,indent+3+smeth.length(),' ',0);
150 } else { // same line
153 sb.append(cliString);
156 protected void multiChar(StringBuilder sb, int length, char c, int indent) {
158 for(int i=0;i<indent;++i)sb.append(' ');
159 for(int i=indent;i<length;++i)sb.append(c);
162 public StringBuilder build(StringBuilder sb, StringBuilder detail) {
167 int line = sb.lastIndexOf("\n")+1;
171 int indent = sb.length()-line;
172 for(Param p : params) {
173 sb.append(p.required?'<':'[');
175 sb.append(p.required?"> ": "] ");
178 boolean first = true;
179 for(Cmd child : children) {
180 if(!(child instanceof DeprecatedCMD)) {
183 } else if(detail==null) {
184 multiChar(sb,indent,' ',0);
186 // Write parents for Detailed Report
187 Stack<String> stack = new Stack<String>();
188 for(Cmd c = child.parent;c!=null;c=c.parent) {
193 if(!stack.isEmpty()) {
195 while(!stack.isEmpty()) {
196 sb.append(stack.pop());
201 child.build(sb,detail);
203 child.detailedHelp(4, detail);
204 // If Child wrote something, then add, bracketing by lines
205 if(detail.length()>0) {
206 multiChar(sb,80,'-',2);
209 multiChar(sb,80,'-',2);
211 detail.setLength(0); // reuse
221 protected void error(Future<?> future) {
222 StringBuilder sb = new StringBuilder("Failed");
223 String desc = future.body();
224 int code = future.code();
225 if(desc==null || desc.length()==0) {
227 } else if(desc.startsWith("{")) {
228 StringReader sr = new StringReader(desc);
230 // Note: 11-18-2013, JG1555. This rather convoluted Message Structure required by TSS Restful Specs, reflecting "Northbound" practices.
231 Error err = getDF(Error.class).newData().in(TYPE.JSON).load(sr).asObject();
233 sb.append(err.getMessageId());
235 String messageBody = err.getText();
236 List<String> vars = err.getVariables();
238 for (int varCounter=0;varCounter<vars.size();) {
239 String var = vars.get(varCounter);
241 if (messageBody.indexOf("%" + varCounter) >= 0) {
242 if((pipe = var.indexOf('|'))>=0) { // In AAF, we use a PIPE for Choice
243 if (aafcli.isTest()) {
244 String expiresStr = var.substring(pipe);
245 var = var.replace(expiresStr, "[Placeholder]");
247 StringBuilder varsb = new StringBuilder(var);
248 varsb.deleteCharAt(pipe);
249 var = varsb.toString();
251 messageBody = messageBody.replace("%" + varCounter, varCounter-1 + ") " + var);
253 messageBody = messageBody.replace("%" + varCounter, var);
257 sb.append(messageBody);
258 } catch (Exception e) {
260 sb.append(" (Note: Details cannot be obtained from Error Structure)");
262 } else if(desc.startsWith("<html>")){ // Core Jetty, etc sends HTML for Browsers
265 sb.append(" with code ");
274 private void withCode(StringBuilder sb, Integer code) {
275 sb.append(" with code ");
279 sb.append(" (HTTP Not Authenticated)");
282 sb.append(" (HTTP Forbidden)");
285 sb.append(" (HTTP Not Found)");
292 * Consistently set start and end dates from Requests (all derived from Request)
295 protected void setStartEnd(Request req) {
296 // Set Start/End Dates, if exist
298 if((str = access.getProperty(Cmd.STARTDATE,null))!=null) {
299 req.setStart(Chrono.timeStamp(Date.valueOf(str)));
302 if((str = access.getProperty(Cmd.ENDDATE,null))!=null) {
303 req.setEnd(Chrono.timeStamp(Date.valueOf(str)));
308 * For Derived classes, who have ENV in this parent
312 * @throws APIException
314 protected <T> RosettaDF<T> getDF(Class<T> cls) throws APIException {
315 return getDF(env,cls);
319 * This works well, making available for GUI, etc.
323 * @throws APIException
325 @SuppressWarnings("unchecked")
326 public static <T> RosettaDF<T> getDF(AuthzEnv env, Class<T> cls) throws APIException {
327 RosettaDF<T> rdf = (RosettaDF<T>)dfs.get(cls);
329 rdf = env.newDataFactory(cls);
335 public void activity(History history, String header) {
336 if (history.getItem().isEmpty()) {
337 int start = header.indexOf('[');
339 pw().println("No Activity Found for " + header.substring(start));
342 pw().println(header);
343 for(int i=0;i<lineLength;++i)pw().print('-');
346 pw().format(hformat,"Date","Table","User","Memo");
347 for(int i=0;i<lineLength;++i)pw().print('-');
350 // Save Server time by Sorting locally
351 List<Item> items = history.getItem();
352 java.util.Collections.sort(items, new Comparator<Item>() {
354 public int compare(Item o1, Item o2) {
355 return o2.getTimestamp().compare(o1.getTimestamp());
359 for(History.Item item : items) {
360 GregorianCalendar gc = item.getTimestamp().toGregorianCalendar();
362 dateFmt.format(gc.getTime()),
371 * Turn String Array into a | delimited String
375 public static String optionsToString(String[] options) {
376 StringBuilder sb = new StringBuilder();
377 boolean first = true;
378 for(String s : options) {
386 return sb.toString();
390 * return which index number the Option matches.
392 * throws an Exception if not part of this Option Set
399 public int whichOption(String[] options, String test) throws CadiException {
400 for(int i=0;i<options.length;++i) {
401 if(options[i].equals(test)) {
405 throw new CadiException(build(new StringBuilder("Invalid Option: "),null).toString());
408 // protected RosettaEnv env() {
409 // return aafcli.env;
412 protected HMangr hman() {
416 public<RET> RET same(Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
417 // We're storing in AAFCli, because we know it's always the same, and single threaded
418 if(aafcli.prevCall!=null) {
419 retryable.item(aafcli.prevCall.item());
420 retryable.lastClient=aafcli.prevCall.lastClient;
423 RET ret = aafcli.hman.same(aafcli.ss,retryable);
425 // Store last call in AAFcli, because Cmds are all different instances.
426 aafcli.prevCall = retryable;
430 public<RET> RET all(Retryable<RET> retryable) throws APIException, CadiException, LocatorException {
431 this.setQueryParamsOn(retryable.lastClient);
432 return aafcli.hman.all(aafcli.ss,retryable);
435 public<RET> RET oneOf(Retryable<RET> retryable,String host) throws APIException, CadiException, LocatorException {
436 this.setQueryParamsOn(retryable.lastClient);
437 return aafcli.hman.oneOf(aafcli.ss,retryable,true,host);
440 protected PrintWriter pw() {
444 public String getName() {
448 public void reportHead(String ... str) {
450 boolean first = true;
452 for(String s : str) {
470 public String reportColHead(String format, String ... args) {
471 pw().format(format,(Object[])args);
476 public void reportLine() {
477 for(int i=0;i<lineLength;++i)pw().print('-');
481 protected void setQueryParamsOn(Rcli<?> rcli) {
482 StringBuilder sb=null;
484 if((force=aafcli.forceString())!=null) {
485 sb = new StringBuilder("force=");
488 if(aafcli.addRequest()) {
490 sb = new StringBuilder("future=true");
492 sb.append("&future=true");
495 if(sb!=null && rcli!=null) {
496 rcli.setQueryParams(sb.toString());
501 // * If Force is set, will return True once only, then revert to "FALSE".
505 // protected String checkForce() {
506 // if(TRUE.equalsIgnoreCase(env.getProperty(FORCE, FALSE))) {
507 // env.setProperty(FORCE, FALSE);
513 public String toString() {
514 StringBuilder sb = new StringBuilder();
515 if(parent==null) { // ultimate parent
517 return sb.toString();
519 return parent.toString();
523 // private String getOrgRealm() {
528 * Appends shortID with Realm, but only when allowed by Organization
529 * @throws OrganizationException
531 public String fullID(String id) {
533 if (id.indexOf('@') < 0) {
536 return id; // is already a full ID