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