[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / perm / List.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cmd.perm;\r
24 \r
25 import java.util.ArrayList;\r
26 import java.util.Collections;\r
27 import java.util.Comparator;\r
28 \r
29 import com.att.cadi.CadiException;\r
30 import com.att.cadi.client.Future;\r
31 import com.att.cadi.client.Rcli;\r
32 import com.att.cadi.client.Retryable;\r
33 import com.att.cmd.AAFcli;\r
34 import com.att.cmd.BaseCmd;\r
35 import com.att.inno.env.APIException;\r
36 \r
37 import aaf.v2_0.Nss;\r
38 import aaf.v2_0.Perms;\r
39 import aaf.v2_0.Pkey;\r
40 \r
41 \r
42 public class List extends BaseCmd<Perm> {\r
43 //      private static final String LIST_PERM_DETAILS = "list permission details";\r
44         \r
45         public List(Perm parent) {\r
46                 super(parent,"list");\r
47 \r
48                 cmds.add(new ListByUser(this));\r
49                 cmds.add(new ListByName(this));\r
50                 cmds.add(new ListByNS(this));\r
51                 cmds.add(new ListByRole(this));\r
52                 cmds.add(new ListActivity(this));\r
53         }\r
54         // Package Level on purpose\r
55         abstract class ListPerms extends Retryable<Integer> {\r
56                 protected int list(Future<Perms> fp,Rcli<?> client, String header, String parentPerm) throws CadiException, APIException  {\r
57                         if(fp.get(AAFcli.timeout())) {  \r
58                                 ArrayList<String> permNss = null;\r
59                                 if (aafcli.isDetailed()) {\r
60                                         permNss = new ArrayList<String>();\r
61                                         String permNs = null;\r
62                                         for(Pkey perm : fp.value.getPerm()) {   \r
63                                                 if (permNs != null && perm.getType().contains(permNs)) {\r
64                                                     permNss.add(permNs);\r
65                                                 } else {\r
66                                                         Future<Nss> fpn = null;\r
67                                                         String permType = perm.getType();\r
68                                                         permNs = permType;\r
69                                                         do {\r
70                                                                 permNs = permType.substring(0,permNs.lastIndexOf('.'));\r
71                                                                 fpn = client.read("/authz/nss/"+permNs,getDF(Nss.class));\r
72                                                         } while (!fpn.get(AAFcli.timeout()));\r
73                                                         permNss.add(permNs);\r
74                                                 }\r
75                                         }                                               \r
76                                 } \r
77                                 report(fp,permNss,header, parentPerm);\r
78                         } else {\r
79                                 error(fp);\r
80                         }\r
81                         return fp.code();\r
82                 }\r
83         }\r
84 \r
85         private static final Comparator<aaf.v2_0.Perm> permCompare = new Comparator<aaf.v2_0.Perm>() {\r
86                 @Override\r
87                 public int compare(aaf.v2_0.Perm a, aaf.v2_0.Perm b) {\r
88                         int rc;\r
89                         if((rc=a.getType().compareTo(b.getType()))!=0) {\r
90                             return rc;\r
91                         }\r
92                         if((rc=a.getInstance().compareTo(b.getInstance()))!=0) {\r
93                             return rc;\r
94                         }\r
95                         return a.getAction().compareTo(b.getAction());\r
96                 }\r
97         };\r
98         \r
99         void report(Future<Perms> fp, ArrayList<String> permNss, String ... str) {\r
100                 reportHead(str);\r
101                 if (this.aafcli.isDetailed()) {         \r
102                         String format = reportColHead("%-20s %-15s %-30s %-15s\n   %-75s\n","PERM NS","Type","Instance","Action", "Description");\r
103                         Collections.sort(fp.value.getPerm(),permCompare);\r
104                         for(aaf.v2_0.Perm p : fp.value.getPerm()) {\r
105                                 String permNs = permNss.remove(0);\r
106                                 pw().format(format,\r
107                                         permNs,\r
108                                         p.getType().substring(permNs.length()+1),\r
109                                         p.getInstance(),\r
110                                         p.getAction(),\r
111                                         p.getDescription()==null?"":p.getDescription());\r
112                         }\r
113                         pw().println();\r
114                 } else {\r
115                         String format = reportColHead("%-30s %-30s %-10s\n","PERM Type","Instance","Action");\r
116 \r
117                         Collections.sort(fp.value.getPerm(),permCompare);\r
118                         for(aaf.v2_0.Perm p : fp.value.getPerm()) {\r
119                                 pw().format(format,\r
120                                         p.getType(),\r
121                                         p.getInstance(),\r
122                                         p.getAction());\r
123                         }\r
124                         pw().println();\r
125                 }\r
126         }\r
127 \r
128 }\r