[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / ns / ListUsersWithPerm.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.Perm;\r
42 import aaf.v2_0.Perms;\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 ListUsersWithPerm extends Cmd {\r
51         private static final String HEADER="List Users of Permissions of Namespace ";\r
52         \r
53         public ListUsersWithPerm(ListUsers parent) {\r
54                 super(parent,"perm", \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                                                 \r
72                                                 for(Ns n : fn.value.getNs()) {\r
73                                                         Future<Perms> fp = client.read("/authz/perms/ns/"+n.getName(), getDF(Perms.class));\r
74                                                         if(fp.get(AAFcli.timeout())) {\r
75                                                                 for(Perm p : fp.value.getPerm()) {\r
76                                                                         String perm = p.getType()+'/'+p.getInstance()+'/'+p.getAction();\r
77                                                                         if(detail)((ListUsers)parent).report(perm);\r
78                                                                         Future<Users> fus = client.read(\r
79                                                                                         "/authz/users/perm/"+perm, \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                                                                         } else if(fn.code()==404) {\r
90                                                                                 return 200;\r
91                                                                         }\r
92                                                                 }\r
93                                                         }\r
94                                                 }\r
95                                                 if(uset!=null) {\r
96                                                         for(String u : uset) {\r
97                                                                 pw().print("  ");\r
98                                                                 pw().println(u);\r
99                                                         }\r
100                                                 }\r
101                                         }\r
102                                 } else if(fn.code()==404) {\r
103                                         return 200;\r
104                                 } else {        \r
105                                         error(fn);\r
106                                 }\r
107                                 return fn.code();\r
108                         }\r
109                 });\r
110         }\r
111 \r
112         @Override\r
113         public void detailedHelp(int _indent, StringBuilder sb) {\r
114                 int indent = _indent;\r
115                 detailLine(sb,indent,HEADER);\r
116                 indent+=4;\r
117                 detailLine(sb,indent,"Report Users associated with this Namespace's Permissions");\r
118                 sb.append('\n');\r
119                 detailLine(sb,indent,"If \"set detail=true\" is specified, then Permissions are printed with the associated");\r
120                 detailLine(sb,indent,"users and expiration dates");\r
121                 indent-=4;\r
122                 api(sb,indent,HttpMethods.GET,"authz/nss/<ns>",Nss.class,true);\r
123                 api(sb,indent,HttpMethods.GET,"authz/perms/ns/<ns>",Perms.class,false);\r
124                 api(sb,indent,HttpMethods.GET,"authz/users/perm/<type>/<instance>/<action>",Users.class,false);\r
125         }\r
126 \r
127 }\r