AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / role / 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.role;
23
24 import java.util.Collections;
25 import java.util.Comparator;
26
27 import javax.xml.datatype.XMLGregorianCalendar;
28
29 import org.onap.aaf.auth.cmd.AAFcli;
30 import org.onap.aaf.auth.cmd.BaseCmd;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.cadi.client.Rcli;
34 import org.onap.aaf.cadi.client.Retryable;
35 import org.onap.aaf.misc.env.APIException;
36 import org.onap.aaf.misc.env.util.Chrono;
37
38 import aaf.v2_0.Perm;
39 import aaf.v2_0.Perms;
40 import aaf.v2_0.Pkey;
41 import aaf.v2_0.Roles;
42 import aaf.v2_0.UserRole;
43 import aaf.v2_0.UserRoles;
44
45
46
47 public class List extends BaseCmd<Role> {
48         private static final String XXXX_XX_XX = "XXXX-XX-XX";
49         private static final String LIST_ROLES_BY_NAME = "list roles for role";
50
51         public List(Role parent) {
52                 super(parent,"list");
53                 cmds.add(new ListByUser(this));
54                 cmds.add(new ListByRole(this));
55                 cmds.add(new ListByNS(this));
56                 cmds.add(new ListByNameOnly(this));
57                 cmds.add(new ListByPerm(this));
58                 cmds.add(new ListActivity(this));
59         }
60         
61         // Package Level on purpose
62         abstract class ListRoles extends Retryable<Integer> {
63                 protected int list(Future<Roles> fr,Rcli<?> client, String header) throws APIException, CadiException {
64                         if(fr.get(AAFcli.timeout())) {
65                                 Perms perms=null;
66                                 if (aafcli.isDetailed()) {
67                                         for(aaf.v2_0.Role r : fr.value.getRole()) {
68                                                 Future<Perms> fp = client.read(
69                                                                 "/authz/perms/role/"+r.getName()+(aafcli.isDetailed()?"?ns":""), 
70                                                                 getDF(Perms.class)
71                                                         );
72                                                 if(fp.get(AAFcli.timeout())) {
73                                                         if(perms==null) {
74                                                                 perms = fp.value;
75                                                         } else {
76                                                                 perms.getPerm().addAll(fp.value.getPerm());
77                                                         }
78                                                 }
79                                         }
80                                 }
81                                 report(fr.value,perms,null,header);
82                         } else {
83                                 error(fr);
84                         }
85                         return fr.code();
86                 }
87         }
88
89         private final static String roleFormat = "%-56s Expires %s\n";
90         private final static String roleFormatNoDate = "%-61s\n";
91         private final static String roleExpiredFormat = "%-53s !!! EXPIRED !!! %s\n";
92         private final static String permFormat = "   %-30s %-30s %-15s\n";
93
94         
95         private static final Comparator<aaf.v2_0.Role> roleCompare = new Comparator<aaf.v2_0.Role>() {
96                 @Override
97                 public int compare(aaf.v2_0.Role a, aaf.v2_0.Role b) {
98                         return a.getName().compareTo(b.getName());
99                 }
100         };
101         public void report(Roles roles, Perms perms, UserRoles urs, String ... str) {
102                 reportHead(str);
103                 XMLGregorianCalendar now = Chrono.timeStamp().normalize();
104                 if(roles==null || roles.getRole().isEmpty()) {
105                         pw().println("<No Roles Found>");
106                 } else if (aafcli.isDetailed()){
107                         if (aafcli.isDetailed() && str[0].toLowerCase().contains(LIST_ROLES_BY_NAME)) {
108                                 String description = roles.getRole().get(0).getDescription();
109                                 if (description == null) description = "";
110                                 reportColHead("%-80s\n","Description: " + description);
111                         }                       
112
113                         String fullFormat = roleFormat+permFormat;
114                         reportColHead(fullFormat,"[ROLE NS].Name","","[PERM NS].Type","Instance","Action");
115                         Collections.sort(roles.getRole(),roleCompare);
116                         for(aaf.v2_0.Role r : roles.getRole()) {
117                                 String roleName = r.getName();
118                                 String ns = r.getNs();
119                                 if(aafcli.isTest()) {
120                                         if(ns==null) {
121                                                 pw().format(roleFormat, roleName,XXXX_XX_XX);
122                                         } else {
123                                                 pw().format(roleFormat, "["+ns+"]"+roleName.substring(ns.length()),XXXX_XX_XX);
124                                         }
125                                 } else {
126                                         UserRole ur = get(roleName,urs);
127                                         if(ur!=null && now.compare(ur.getExpires().normalize())>0) {
128                                                 if(ns==null) {
129                                                         pw().format(roleExpiredFormat, roleName,Chrono.dateOnlyStamp(ur.getExpires()));
130                                                 } else {
131                                                         pw().format(roleExpiredFormat, "["+ns+"]"+roleName.substring(ns.length()),Chrono.dateOnlyStamp(ur.getExpires()));
132                                                 }
133                                         } else {
134                                                 if(ns==null) {
135                                                         pw().format(roleFormat, roleName,ur!=null?Chrono.dateOnlyStamp(ur.getExpires()):"");
136                                                 } else {
137                                                         pw().format(roleFormat, "["+ns+"]"+roleName.substring(ns.length()),ur!=null?Chrono.dateOnlyStamp(ur.getExpires()):"");
138                                                 }
139                                         }
140                                 }
141
142                                 for(Pkey pkey : r.getPerms()) {
143                                         Perm perm = get(pkey,perms);
144                                         if(perm==null || perm.getNs()==null) {
145                                                 pw().format(permFormat, 
146                                                                 pkey.getType(),
147                                                                 pkey.getInstance(),
148                                                                 pkey.getAction());
149                                         } else {
150                                                 String ns1 = perm.getNs();
151                                                 pw().format(permFormat, 
152                                                                 '['+ns1+"]"+perm.getType().substring(ns1.length()),
153                                                                 perm.getInstance(),
154                                                                 perm.getAction());
155                                         }
156                                 }
157                         }
158                 } else {
159                         String fullFormat = roleFormat;
160                         reportColHead(fullFormat,"ROLE Name","","PERM Type","Instance","Action");
161                         Collections.sort(roles.getRole(),roleCompare);
162                         for(aaf.v2_0.Role r : roles.getRole()) {
163                                 if (urs != null) {
164                                         String roleName = r.getName();
165                                         if(!aafcli.isTest()) {
166                                                 UserRole ur = get(roleName,urs);
167                                                 if(ur!=null && now.compare(ur.getExpires().normalize())>0) {
168                                                         pw().format(roleExpiredFormat, roleName+"*",Chrono.dateOnlyStamp(ur.getExpires()));
169                                                 } else {
170                                                         pw().format(roleFormat, roleName,ur!=null?Chrono.dateOnlyStamp(ur.getExpires()):"");
171                                                 }
172                                         } else {
173                                                 pw().format(roleFormat, roleName,XXXX_XX_XX);
174                                         }
175                                 } else {
176                                         pw().format(roleFormatNoDate, r.getName());
177                                         for(Pkey perm : r.getPerms()) {
178                                                 pw().format(permFormat, 
179                                                                 perm.getType(),
180                                                                 perm.getInstance(),
181                                                                 perm.getAction());
182                                         }
183                                 }
184                         }
185                 }
186         }
187         private Perm get(Pkey pkey, Perms perms) {
188                 if(perms!=null) {
189                         for(Perm p : perms.getPerm()) {
190                                 if(pkey.getAction().equals(p.getAction()) &&
191                                    pkey.getInstance().equals(p.getInstance()) &&
192                                    pkey.getType().equals(p.getType())) {
193                                         return p;
194                                 }
195                         }
196                 }
197                 return null;
198         }
199         // The assumption is that these UserRoles are already pulled in by User... no need to check
200         private UserRole get(String roleName, UserRoles urs) {
201                 if(urs!=null) {
202                         for(UserRole ur : urs.getUserRole()) {
203                                 if(roleName.equals(ur.getRole())) {
204                                         return ur;
205                                 }
206                         }
207                 }
208                 return null;
209         }
210
211 }