Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / ns / ListUsersInRole.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.ns;
23
24 import java.util.HashSet;
25 import java.util.Set;
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.misc.env.APIException;
37
38 import aaf.v2_0.Nss;
39 import aaf.v2_0.Nss.Ns;
40 import aaf.v2_0.Role;
41 import aaf.v2_0.Roles;
42 import aaf.v2_0.Users;
43 import aaf.v2_0.Users.User;
44
45 /**
46  * p
47  * @author Jonathan
48  *
49  */
50 public class ListUsersInRole extends Cmd {
51     private static final String HEADER="List Users in Roles of Namespace ";
52
53     public ListUsersInRole(ListUsers parent) {
54         super(parent,"role",
55                 new Param("ns-name",true));
56     }
57
58     @Override
59     public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException {
60             int idx = _idx;
61         final String ns=args[idx++];
62         final boolean detail = aafcli.isDetailed();
63         return same(new Retryable<Integer>() {
64             @Override
65             public Integer code(Rcli<?> client) throws CadiException, APIException {
66                 ((ListUsers)parent).report(HEADER,ns);
67                 Future<Nss> fn = client.read("/authz/nss/"+ns,getDF(Nss.class));
68                 if (fn.get(AAFcli.timeout())) {
69                     if (fn.value!=null) {
70                         Set<String> uset = detail?null:new HashSet<>();
71                         for (Ns n : fn.value.getNs()) {
72                             Future<Roles> fr = client.read("/authz/roles/ns/"+n.getName(), getDF(Roles.class));
73                             if (fr.get(AAFcli.timeout())) {
74                                 for (Role r : fr.value.getRole()) {
75                                     if (detail) {
76                                         ((ListUsers)parent).report(r.getName());
77                                     }
78                                     Future<Users> fus = client.read(
79                                             "/authz/users/role/"+r.getName(),
80                                             getDF(Users.class)
81                                             );
82                                     if (fus.get(AAFcli.timeout())) {
83                                         for (User u : fus.value.getUser()) {
84                                             if (detail) {
85                                                 ((ListUsers)parent).report("  ",u);
86                                             } else {
87                                                 uset.add(u.getId());
88                                             }
89                                         }
90                                     } else if (fn.code()==404) {
91                                         return 200;
92                                     }
93                                 }
94                             }
95                         }
96                         if (uset!=null) {
97                             for (String u : uset) {
98                                 pw().print("  ");
99                                 pw().println(u);
100                             }
101                         }
102                     }
103                 } else if (fn.code()==404) {
104                     return 200;
105                 } else {
106                     error(fn);
107                 }
108                 return fn.code();
109             }
110         });
111     }
112
113     @Override
114     public void detailedHelp(int _indent, StringBuilder sb) {
115             int indent = _indent;
116         detailLine(sb,indent,HEADER);
117         indent+=4;
118         detailLine(sb,indent,"Report Users associated with this Namespace's Roles");
119         sb.append('\n');
120         detailLine(sb,indent,"If \"details\" is specified, then all roles are printed ");
121         detailLine(sb,indent,"with the associated users and expiration dates");
122         indent-=4;
123         api(sb,indent,HttpMethods.GET,"authz/nss/<ns>",Nss.class,true);
124         api(sb,indent,HttpMethods.GET,"authz/roles/ns/<ns>",Roles.class,false);
125         api(sb,indent,HttpMethods.GET,"authz/users/role/<ns>",Users.class,false);
126     }
127
128 }