[AAF-21] Initial code import
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / user / ListForPermission.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cmd.user;\r
25 \r
26 import java.util.Collections;\r
27 import java.util.Comparator;\r
28 \r
29 import com.att.cadi.CadiException;\r
30 import com.att.cadi.LocatorException;\r
31 import com.att.cadi.client.Future;\r
32 import com.att.cadi.client.Rcli;\r
33 import com.att.cadi.client.Retryable;\r
34 import com.att.cmd.AAFcli;\r
35 import com.att.cmd.Cmd;\r
36 import com.att.cmd.Param;\r
37 import com.att.cssa.rserv.HttpMethods;\r
38 import com.att.inno.env.APIException;\r
39 \r
40 import aaf.v2_0.Users;\r
41 import aaf.v2_0.Users.User;\r
42 \r
43 /**\r
44  * p\r
45  *\r
46  */\r
47 public class ListForPermission extends Cmd {\r
48         private static final String HEADER = "List Users for Permission";\r
49         public ListForPermission(List parent) {\r
50                 super(parent,"perm", \r
51                                 new Param("type",true),\r
52                                 new Param("instance",true),\r
53                                 new Param("action",true)); \r
54         }\r
55 \r
56         @Override\r
57         public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {\r
58                 return same(new Retryable<Integer>() {\r
59                         @Override\r
60                         public Integer code(Rcli<?> client) throws CadiException, APIException {\r
61                                 int idx = index;\r
62                                 String type = args[idx++];\r
63                                 String instance = args[idx++];\r
64                                 if("\\*".equals(instance))instance="*";\r
65                                 String action = args[idx++];\r
66                                 if("\\*".equals(action))action="*";\r
67                                 Future<Users> fp = client.read(\r
68                                                 "/authz/users/perm/"+type+'/'+instance+'/'+action, \r
69                                                 getDF(Users.class)\r
70                                                 );\r
71                                 if(fp.get(AAFcli.timeout())) {\r
72                                         if (aafcli.isTest())\r
73                                                 Collections.sort(fp.value.getUser(), new Comparator<User>() {\r
74                                                         @Override\r
75                                                         public int compare(User u1, User u2) {\r
76                                                                 return u1.getId().compareTo(u2.getId());\r
77                                                         }                       \r
78                                                 });\r
79                                         ((com.att.cmd.user.List)parent).report(fp.value,false,HEADER,type+"|"+instance+"|"+action);\r
80                                         if(fp.code()==404)return 200;\r
81                                 } else {\r
82                                         error(fp);\r
83                                 }\r
84                                 return fp.code();\r
85                         }\r
86                 });\r
87         }\r
88         \r
89         @Override\r
90         public void detailedHelp(int _indent, StringBuilder sb) {\r
91                 int indent = _indent;\r
92                 detailLine(sb,indent,HEADER);\r
93                 indent+=2;\r
94                 detailLine(sb,indent,"This report lists the users associated to Permissions.  Since Users");\r
95                 detailLine(sb,indent,"are associated to Roles, and Roles have Permissions, this report");\r
96                 detailLine(sb,indent,"accomodates all these linkages.");\r
97                 sb.append('\n');\r
98                 detailLine(sb,indent,"The URL must contain the Permission's type,instance and action, and ");\r
99                 detailLine(sb,indent,"may include \"*\"s (type in as \\\\*).");\r
100                 detailLine(sb,indent,"See Perm Create Documentation for definitions.");\r
101                 indent-=2;\r
102                 api(sb,indent,HttpMethods.GET,"authz/users/perm/<type>/<instance>/<action>",Users.class,true);\r
103         }\r
104 }\r