8ff2763abb06d41a36947b80b13c81effb1d4059
[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  * Modifications Copyright (C) 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.cmd.perm;
25
26 import java.util.Collections;
27 import java.util.Comparator;
28
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.cmd.BaseCmd;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.cadi.client.Retryable;
34
35 import aaf.v2_0.Perms;
36
37 public class List extends BaseCmd<Perm> {
38     private static final String permFormat = "%-30s %-30s %-10s\n";
39     private static final Comparator<aaf.v2_0.Perm> permCompare = new Comparator<aaf.v2_0.Perm>() {
40         @Override
41         public int compare(aaf.v2_0.Perm a, aaf.v2_0.Perm b) {
42             int rc;
43             if ((rc=a.getType().compareTo(b.getType()))!=0) {
44                 return rc;
45             }
46             if ((rc=a.getInstance().compareTo(b.getInstance()))!=0) {
47                 return rc;
48             }
49             return a.getAction().compareTo(b.getAction());
50         }
51     };
52     public List(Perm parent) {
53         super(parent,"list");
54
55         cmds.add(new ListByUser(this));
56         cmds.add(new ListByName(this));
57         cmds.add(new ListByNS(this));
58         cmds.add(new ListByRole(this));
59         cmds.add(new ListActivity(this));
60     }
61     // Package Level on purpose
62     abstract class ListPerms extends Retryable<Integer> {
63         protected int list(Future<Perms> fp,String header, String parentPerm) throws CadiException  {
64             if (fp.get(AAFcli.timeout())) {    
65                 report(fp,header, parentPerm);
66             } else {
67                 error(fp);
68             }
69             return fp.code();
70         }
71     }
72     
73     void report(Future<Perms> fp, String ... str) {
74         reportHead(str);
75         if (this.aafcli.isDetailed()) {        
76             String format = "%-36s %-30s %-15s\n";
77             String descFmt = "   %-75s\n";
78             reportColHead(format + descFmt,"[PERM NS].Type","Instance","Action", "Description");
79             Collections.sort(fp.value.getPerm(),permCompare);
80             for (aaf.v2_0.Perm p : fp.value.getPerm()) {
81                 String pns = p.getNs();
82                 if (pns==null) {
83                     pw().format(format,
84                             p.getType(),
85                             p.getInstance(),
86                             p.getAction());
87                 } else {
88                     pw().format(format,
89                             '['+pns + "]." + p.getType().substring(pns.length()+1),
90                             p.getInstance(),
91                             p.getAction());
92                 }
93                 String desc = p.getDescription();
94                 if (desc!=null && desc.length()>0) {
95                     pw().format(descFmt,p.getDescription());
96                 }
97             }
98             pw().println();
99         } else {
100             String format = reportColHead(permFormat,"PERM Type","Instance","Action");
101
102             Collections.sort(fp.value.getPerm(),permCompare);
103             for (aaf.v2_0.Perm p : fp.value.getPerm()) {
104                 pw().format(format,
105                     p.getType(),
106                     p.getInstance(),
107                     p.getAction());
108             }
109             pw().println();
110         }
111     }
112
113 }