3a5f73f2fb1a23eff80335892ad7f1bfdd50eb31
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / ns / ListUsersInRole.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cmd.ns;\r
24 \r
25 import java.util.HashSet;\r
26 import java.util.Set;\r
27 \r
28 import com.att.cadi.CadiException;\r
29 import com.att.cadi.LocatorException;\r
30 import com.att.cadi.client.Future;\r
31 import com.att.cadi.client.Rcli;\r
32 import com.att.cadi.client.Retryable;\r
33 import com.att.cmd.AAFcli;\r
34 import com.att.cmd.Cmd;\r
35 import com.att.cmd.Param;\r
36 import com.att.cssa.rserv.HttpMethods;\r
37 import com.att.inno.env.APIException;\r
38 \r
39 import aaf.v2_0.Nss;\r
40 import aaf.v2_0.Nss.Ns;\r
41 import aaf.v2_0.Role;\r
42 import aaf.v2_0.Roles;\r
43 import aaf.v2_0.Users;\r
44 import aaf.v2_0.Users.User;\r
45 \r
46 /**\r
47  * p\r
48  *\r
49  */\r
50 public class ListUsersInRole extends Cmd {\r
51         private static final String HEADER="List Users in Roles of Namespace ";\r
52         \r
53         public ListUsersInRole(ListUsers parent) {\r
54                 super(parent,"role", \r
55                                 new Param("ns",true)); \r
56         }\r
57 \r
58         @Override\r
59         public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException {\r
60                 int idx = _idx;\r
61                 final String ns=args[idx++];\r
62                 final boolean detail = aafcli.isDetailed();\r
63                 return same(new Retryable<Integer>() {\r
64                         @Override\r
65                         public Integer code(Rcli<?> client) throws CadiException, APIException {\r
66                                 ((ListUsers)parent).report(HEADER,ns);\r
67                                 Future<Nss> fn = client.read("/authz/nss/"+ns,getDF(Nss.class));\r
68                                 if(fn.get(AAFcli.timeout())) {\r
69                                         if(fn.value!=null) {\r
70                                                 Set<String> uset = detail?null:new HashSet<String>();\r
71                                                 for(Ns n : fn.value.getNs()) {\r
72                                                         Future<Roles> fr = client.read("/authz/roles/ns/"+n.getName(), getDF(Roles.class));\r
73                                                         if(fr.get(AAFcli.timeout())) {\r
74                                                                 for(Role r : fr.value.getRole()) {\r
75                                                                         if(detail) {\r
76                                                                                 ((ListUsers)parent).report(r.getName());\r
77                                                                         }\r
78                                                                         Future<Users> fus = client.read(\r
79                                                                                         "/authz/users/role/"+r.getName(), \r
80                                                                                         getDF(Users.class)\r
81                                                                                         );\r
82                                                                         if(fus.get(AAFcli.timeout())) {\r
83                                                                                 for(User u : fus.value.getUser()) {\r
84                                                                                         if(detail) {\r
85                                                                                                 ((ListUsers)parent).report("  ",u);\r
86                                                                                         } else {\r
87                                                                                             uset.add(u.getId());\r
88                                                                                         }\r
89                                                                                 }\r
90                                                                         } else if(fn.code()==404) {\r
91                                                                                 return 200;\r
92                                                                         }\r
93                                                                 }\r
94                                                         }\r
95                                                 }\r
96                                                 if(uset!=null) {\r
97                                                         for(String u : uset) {\r
98                                                                 pw().print("  ");\r
99                                                                 pw().println(u);\r
100                                                         }\r
101                                                 }\r
102                                         }\r
103                                 } else if(fn.code()==404) {\r
104                                         return 200;\r
105                                 } else {        \r
106                                         error(fn);\r
107                                 }\r
108                                 return fn.code();\r
109                         }\r
110                 });\r
111         }\r
112 \r
113         @Override\r
114         public void detailedHelp(int _indent, StringBuilder sb) {\r
115                 int indent = _indent;\r
116                 detailLine(sb,indent,HEADER);\r
117                 indent+=4;\r
118                 detailLine(sb,indent,"Report Users associated with this Namespace's Roles");\r
119                 sb.append('\n');\r
120                 detailLine(sb,indent,"If \"set details=true\" is specified, then all roles are printed ");\r
121                 detailLine(sb,indent,"with the associated users and expiration dates");\r
122                 indent-=4;\r
123                 api(sb,indent,HttpMethods.GET,"authz/nss/<ns>",Nss.class,true);\r
124                 api(sb,indent,HttpMethods.GET,"authz/roles/ns/<ns>",Roles.class,false);\r
125                 api(sb,indent,HttpMethods.GET,"authz/users/role/<ns>",Users.class,false);\r
126         }\r
127 \r
128 }\r