Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / ns / ListUsersWithPerm.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.Perm;
41 import aaf.v2_0.Perms;
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 ListUsersWithPerm extends Cmd {
51     private static final String HEADER="List Users of Permissions of Namespace ";
52
53     public ListUsersWithPerm(ListUsers parent) {
54         super(parent,"perm",
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
72                         for (Ns n : fn.value.getNs()) {
73                             Future<Perms> fp = client.read("/authz/perms/ns/"+n.getName()+(aafcli.isDetailed()?"?ns":"")
74                                     , getDF(Perms.class));
75                             if (fp.get(AAFcli.timeout())) {
76                                 for (Perm p : fp.value.getPerm()) {
77                                     String perm = p.getType()+'/'+p.getInstance()+'/'+p.getAction();
78                                     if (detail)((ListUsers)parent).report(perm);
79                                     Future<Users> fus = client.read(
80                                             "/authz/users/perm/"+perm,
81                                             getDF(Users.class)
82                                             );
83                                     if (fus.get(AAFcli.timeout())) {
84                                         for (User u : fus.value.getUser()) {
85                                             if (detail)
86                                                 ((ListUsers)parent).report("  ",u);
87                                             else
88                                                 uset.add(u.getId());
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 Permissions");
119         sb.append('\n');
120         detailLine(sb,indent,"If \"set detail=true\" is specified, then Permissions are printed with the associated");
121         detailLine(sb,indent,"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/perms/ns/<ns>",Perms.class,false);
125         api(sb,indent,HttpMethods.GET,"authz/users/perm/<type>/<instance>/<action>",Users.class,false);
126     }
127
128 }