Update project structure to org.onap.aaf
[aaf/authz.git] / authz-cmd / src / main / java / org / onap / aaf / cmd / role / 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 org.onap.aaf.cmd.role;\r
24 \r
25 import java.util.ArrayList;\r
26 import java.util.Collections;\r
27 import java.util.Comparator;\r
28 import java.util.HashMap;\r
29 \r
30 import org.onap.aaf.cmd.AAFcli;\r
31 import org.onap.aaf.cmd.BaseCmd;\r
32 \r
33 import org.onap.aaf.cadi.CadiException;\r
34 import org.onap.aaf.cadi.client.Future;\r
35 import org.onap.aaf.cadi.client.Rcli;\r
36 import org.onap.aaf.cadi.client.Retryable;\r
37 import org.onap.aaf.inno.env.APIException;\r
38 \r
39 import aaf.v2_0.Nss;\r
40 import aaf.v2_0.Pkey;\r
41 import aaf.v2_0.Roles;\r
42 \r
43 \r
44 \r
45 public class List extends BaseCmd<Role> {\r
46         private static final String LIST_ROLES_BY_NAME = "list roles for role";\r
47 \r
48         public List(Role parent) {\r
49                 super(parent,"list");\r
50                 cmds.add(new ListByUser(this));\r
51                 cmds.add(new ListByRole(this));\r
52                 cmds.add(new ListByNS(this));\r
53                 cmds.add(new ListByNameOnly(this));\r
54                 cmds.add(new ListByPerm(this));\r
55                 cmds.add(new ListActivity(this));\r
56         }\r
57         \r
58         // Package Level on purpose\r
59         abstract class ListRoles extends Retryable<Integer> {\r
60                 protected int list(Future<Roles> fp,Rcli<?> client, String header) throws APIException, CadiException {\r
61                         if(fp.get(AAFcli.timeout())) {\r
62                                 Future<Nss> fn = null;\r
63                                 ArrayList<String> roleNss = null;\r
64                                 ArrayList<String> permNss = null;\r
65                                 if (aafcli.isDetailed()) {\r
66                                         roleNss = new ArrayList<String>();\r
67                                         permNss = new ArrayList<String>();\r
68                                         for(aaf.v2_0.Role p : fp.value.getRole()) {\r
69                                                 String roleNs = p.getName();\r
70                                                 do {\r
71                                                         roleNs = p.getName().substring(0,roleNs.lastIndexOf('.'));\r
72                                                         fn = client.read("/authz/nss/"+roleNs,getDF(Nss.class));\r
73                                                 } while (!fn.get(AAFcli.timeout()));\r
74                                                 roleNss.add(roleNs);\r
75                 \r
76                                                 for(Pkey perm : p.getPerms()) {\r
77                                                         if (perm.getType().contains(roleNs))\r
78                                                                 permNss.add(roleNs);\r
79                                                         else {\r
80                                                                 Future<Nss> fpn = null;\r
81                                                                 String permType = perm.getType();\r
82                                                                 String permNs = permType;\r
83                                                                 do {\r
84                                                                         permNs = permType.substring(0,permNs.lastIndexOf('.'));\r
85                                                                         fpn = client.read("/authz/nss/"+permNs,getDF(Nss.class));\r
86                                                                 } while (!fpn.get(AAFcli.timeout()));\r
87                                                                 permNss.add(permNs);\r
88                                                         }\r
89                                                 }\r
90                                         }\r
91                                 }\r
92                                 report(fp,roleNss,permNss,null,header);\r
93                         } else {\r
94                                 error(fp);\r
95                         }\r
96                         return fp.code();\r
97                 }\r
98         }\r
99 \r
100         private final static String roleFormat = "%-50s\n";\r
101         \r
102         private static final Comparator<aaf.v2_0.Role> roleCompare = new Comparator<aaf.v2_0.Role>() {\r
103                 @Override\r
104                 public int compare(aaf.v2_0.Role a, aaf.v2_0.Role b) {\r
105                         return a.getName().compareTo(b.getName());\r
106                 }\r
107         };\r
108         public void report(Future<Roles> fp, ArrayList<String> roleNss, ArrayList<String> permNss,\r
109                         HashMap<String,Boolean> expiredMap, String ... str) {\r
110                 reportHead(str);\r
111                 if (fp != null && aafcli.isDetailed() && str[0].toLowerCase().contains(LIST_ROLES_BY_NAME)) {\r
112                         String description = fp.value.getRole().get(0).getDescription();\r
113                         if (description == null) description = "";\r
114                         reportColHead("%-80s\n","Description: " + description);\r
115                 }                       \r
116 \r
117                 if(fp==null) {\r
118                         pw().println("<No Roles Found>");\r
119                 } else if (aafcli.isDetailed()){\r
120                         String permFormat = "   %-20s %-15s %-30s %-15s\n";\r
121                         String fullFormat = roleFormat+permFormat;\r
122                         reportColHead(fullFormat,"[ROLE NS].Name","PERM NS","Type","Instance","Action");\r
123                         Collections.sort(fp.value.getRole(),roleCompare);\r
124                         for(aaf.v2_0.Role p : fp.value.getRole()) {\r
125                                 String roleNs = roleNss.remove(0);\r
126                                 pw().format(roleFormat, "["+roleNs+"]"+p.getName().substring(roleNs.length()));\r
127                                 for(Pkey perm : p.getPerms()) {\r
128                                         String permNs = permNss.remove(0);\r
129                                         pw().format(permFormat, \r
130                                                         permNs,\r
131                                                         perm.getType().substring(permNs.length()+1),\r
132                                                         perm.getInstance(),\r
133                                                         perm.getAction());\r
134                                 }\r
135                         }\r
136                 } else {\r
137                         String permFormat = "   %-30s %-30s %-15s\n";\r
138                         String fullFormat = roleFormat+permFormat;\r
139                         reportColHead(fullFormat,"ROLE Name","PERM Type","Instance","Action");\r
140                         Collections.sort(fp.value.getRole(),roleCompare);\r
141                         for(aaf.v2_0.Role p : fp.value.getRole()) {\r
142                                 if (expiredMap != null) {\r
143                                         String roleName = p.getName();\r
144                                         Boolean b = expiredMap.get(roleName);\r
145                                         if (b != null && b.booleanValue())\r
146                                                 pw().format(roleFormat, roleName+"*");\r
147                                         else {\r
148                                                 pw().format(roleFormat, roleName);\r
149                                                 for(Pkey perm : p.getPerms()) {\r
150                                                         pw().format(permFormat, \r
151                                                                         perm.getType(),\r
152                                                                         perm.getInstance(),\r
153                                                                         perm.getAction());\r
154                                                 }\r
155                                         }\r
156                                 } else {\r
157                                         pw().format(roleFormat, p.getName());\r
158                                         for(Pkey perm : p.getPerms()) {\r
159                                                 pw().format(permFormat, \r
160                                                                 perm.getType(),\r
161                                                                 perm.getInstance(),\r
162                                                                 perm.getAction());\r
163                                         }\r
164                                 }\r
165                         }\r
166                 }\r
167         }\r
168 \r
169 }\r