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