Merge "Sonar fix: NS.java"
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / perm / List.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd.perm;
23
24 import java.util.Collections;
25 import java.util.Comparator;
26
27 import org.onap.aaf.auth.cmd.AAFcli;
28 import org.onap.aaf.auth.cmd.BaseCmd;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.client.Future;
31 import org.onap.aaf.cadi.client.Retryable;
32 import org.onap.aaf.misc.env.APIException;
33
34 import aaf.v2_0.Perms;
35
36 public class List extends BaseCmd<Perm> {
37     private static final String permFormat = "%-30s %-30s %-10s\n";
38     private static final Comparator<aaf.v2_0.Perm> permCompare = new Comparator<aaf.v2_0.Perm>() {
39         @Override
40         public int compare(aaf.v2_0.Perm a, aaf.v2_0.Perm b) {
41             int rc;
42             if ((rc=a.getType().compareTo(b.getType()))!=0) {
43                 return rc;
44             }
45             if ((rc=a.getInstance().compareTo(b.getInstance()))!=0) {
46                 return rc;
47             }
48             return a.getAction().compareTo(b.getAction());
49         }
50     };
51     public List(Perm parent) {
52         super(parent,"list");
53
54         cmds.add(new ListByUser(this));
55         cmds.add(new ListByName(this));
56         cmds.add(new ListByNS(this));
57         cmds.add(new ListByRole(this));
58         cmds.add(new ListActivity(this));
59     }
60     // Package Level on purpose
61     abstract class ListPerms extends Retryable<Integer> {
62         protected int list(Future<Perms> fp,String header, String parentPerm) throws CadiException  {
63             if (fp.get(AAFcli.timeout())) {    
64                 report(fp,header, parentPerm);
65             } else {
66                 error(fp);
67             }
68             return fp.code();
69         }
70     }
71     
72     void report(Future<Perms> fp, String ... str) {
73         reportHead(str);
74         if (this.aafcli.isDetailed()) {        
75             String format = "%-36s %-30s %-15s\n";
76             String descFmt = "   %-75s\n";
77             reportColHead(format + descFmt,"[PERM NS].Type","Instance","Action", "Description");
78             Collections.sort(fp.value.getPerm(),permCompare);
79             for (aaf.v2_0.Perm p : fp.value.getPerm()) {
80                 String pns = p.getNs();
81                 if (pns==null) {
82                     pw().format(format,
83                             p.getType(),
84                             p.getInstance(),
85                             p.getAction());
86                 } else {
87                     pw().format(format,
88                             '['+pns + "]." + p.getType().substring(pns.length()+1),
89                             p.getInstance(),
90                             p.getAction());
91                 }
92                 String desc = p.getDescription();
93                 if (desc!=null && desc.length()>0) {
94                     pw().format(descFmt,p.getDescription());
95                 }
96             }
97             pw().println();
98         } else {
99             String format = reportColHead(permFormat,"PERM Type","Instance","Action");
100
101             Collections.sort(fp.value.getPerm(),permCompare);
102             for (aaf.v2_0.Perm p : fp.value.getPerm()) {
103                 pw().format(format,
104                     p.getType(),
105                     p.getInstance(),
106                     p.getAction());
107             }
108             pw().println();
109         }
110     }
111
112 }