AT&T 2.0.19 Code drop, stage 3
[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 LIST_PERM_DETAILS = "list permission details";
38         
39         public List(Perm parent) {
40                 super(parent,"list");
41
42                 cmds.add(new ListByUser(this));
43                 cmds.add(new ListByName(this));
44                 cmds.add(new ListByNS(this));
45                 cmds.add(new ListByRole(this));
46                 cmds.add(new ListActivity(this));
47         }
48         // Package Level on purpose
49         abstract class ListPerms extends Retryable<Integer> {
50                 protected int list(Future<Perms> fp,String header, String parentPerm) throws CadiException, APIException  {
51                         if(fp.get(AAFcli.timeout())) {  
52                                 report(fp,header, parentPerm);
53                         } else {
54                                 error(fp);
55                         }
56                         return fp.code();
57                 }
58         }
59
60         private static final Comparator<aaf.v2_0.Perm> permCompare = new Comparator<aaf.v2_0.Perm>() {
61                 @Override
62                 public int compare(aaf.v2_0.Perm a, aaf.v2_0.Perm b) {
63                         int rc;
64                         if((rc=a.getType().compareTo(b.getType()))!=0) {
65                             return rc;
66                         }
67                         if((rc=a.getInstance().compareTo(b.getInstance()))!=0) {
68                             return rc;
69                         }
70                         return a.getAction().compareTo(b.getAction());
71                 }
72         };
73         
74         private static final String permFormat = "%-30s %-30s %-10s\n";
75         
76         void report(Future<Perms> fp, String ... str) {
77                 reportHead(str);
78                 if (this.aafcli.isDetailed()) {         
79                         String format = "%-36s %-30s %-15s\n";
80                         String descFmt = "   %-75s\n";
81                         reportColHead(format + descFmt,"[PERM NS].Type","Instance","Action", "Description");
82                         Collections.sort(fp.value.getPerm(),permCompare);
83                         for(aaf.v2_0.Perm p : fp.value.getPerm()) {
84                                 String pns = p.getNs();
85                                 if(pns==null) {
86                                         pw().format(format,
87                                                         p.getType(),
88                                                         p.getInstance(),
89                                                         p.getAction());
90                                 } else {
91                                         pw().format(format,
92                                                         '['+pns + "]." + p.getType().substring(pns.length()+1),
93                                                         p.getInstance(),
94                                                         p.getAction());
95                                 }
96                                 String desc = p.getDescription();
97                                 if(desc!=null && desc.length()>0) {
98                                         pw().format(descFmt,p.getDescription());
99                                 }
100                         }
101                         pw().println();
102                 } else {
103                         String format = reportColHead(permFormat,"PERM Type","Instance","Action");
104
105                         Collections.sort(fp.value.getPerm(),permCompare);
106                         for(aaf.v2_0.Perm p : fp.value.getPerm()) {
107                                 pw().format(format,
108                                         p.getType(),
109                                         p.getInstance(),
110                                         p.getAction());
111                         }
112                         pw().println();
113                 }
114         }
115
116 }