Merge "AAFcli.java -Declare "value" on a separate line"
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / role / ListByUser.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.Map;
25 import java.util.TreeMap;
26
27 import org.onap.aaf.auth.cmd.AAFcli;
28 import org.onap.aaf.auth.cmd.Cmd;
29 import org.onap.aaf.auth.cmd.Param;
30 import org.onap.aaf.auth.rserv.HttpMethods;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.client.Future;
34 import org.onap.aaf.cadi.client.Rcli;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.cadi.util.Split;
37 import org.onap.aaf.misc.env.APIException;
38
39 import aaf.v2_0.Perm;
40 import aaf.v2_0.Perms;
41 import aaf.v2_0.Role;
42 import aaf.v2_0.Roles;
43 import aaf.v2_0.UserRole;
44 import aaf.v2_0.UserRoles;
45
46 /**
47  * p
48  * @author Jonathan
49  *
50  */
51 public class ListByUser extends Cmd {
52     private static final String HEADER = "List Roles for User ";
53
54     public ListByUser(List parent) {
55         super(parent,"user",
56                 new Param("id",true),
57                 new Param("detail", false));
58     }
59
60     @Override
61     public int _exec( int idx, final String ... args) throws CadiException, APIException, LocatorException {
62         final String user=fullID(args[idx]);
63
64
65         return same(new Retryable<Integer>() {
66             @Override
67             public Integer code(Rcli<?> client) throws CadiException, APIException {
68                 Perms perms=null;
69                 UserRoles urs=null;
70                 Roles roles = null;
71                 int code;
72                 Future<UserRoles> fur = client.read(
73                         "/authz/userRoles/user/"+user,
74                         getDF(UserRoles.class)
75                     );
76                 if (fur.get(AAFcli.timeout())) {
77                     urs = fur.value;
78                     code = fur.code();
79                 } else {
80                     error(fur);
81                     return fur.code();
82                 }
83
84                 if (aafcli.isDetailed()) {
85                     roles = new Roles();
86                     Future<Perms> fp = client.read(
87                             "/authz/perms/user/"+user+"?ns&force",
88                             getDF(Perms.class)
89                         );
90                     if (fp.get(AAFcli.timeout())) {
91                         Map<String, Role> rs = new TreeMap<>();
92                         perms = fp.value;
93                         for( Perm p : perms.getPerm()) {
94                             for(String sr : p.getRoles()) {
95                                 Role r = rs.get(sr);
96                                 if(r==null) {
97                                     r = new Role();
98                                     String[] split = Split.split('|', sr);
99                                     if(split.length>1) {
100                                         r.setNs(split[0]);
101                                         r.setName(split[1]);
102                                     } else {
103                                         r.setName(sr);
104                                     }
105                                     rs.put(sr, r);
106                                     roles.getRole().add(r);
107                                 }
108                                 r.getPerms().add(p);
109                             }
110                         }
111                     }
112                     code = fp.code();
113                 } else {
114                     roles = new Roles();
115                     java.util.List<Role> lr = roles.getRole();
116                     Role r;
117                     for(UserRole ur : urs.getUserRole()) {
118                         r = new Role();
119                         r.setName(ur.getRole());
120                         lr.add(r);
121                     }
122                 }
123
124
125                 ((List)parent).report(roles,perms,urs,HEADER,user);
126                 return code;
127             }
128         });
129     }
130
131     @Override
132     public void detailedHelp(int indent, StringBuilder sb) {
133         detailLine(sb,indent,HEADER);
134         api(sb,indent,HttpMethods.GET,"authz/roles/user/<user>",Roles.class,true);
135     }
136 }