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