f345cfa7e7b783306f9d36f851db701d62e20e52
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / mgmt / Deny.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.mgmt;\r
25 \r
26 import com.att.cadi.CadiException;\r
27 import com.att.cadi.LocatorException;\r
28 import com.att.cadi.client.Future;\r
29 import com.att.cadi.client.Rcli;\r
30 import com.att.cadi.client.Retryable;\r
31 import com.att.cmd.AAFcli;\r
32 import com.att.cmd.BaseCmd;\r
33 import com.att.cmd.Cmd;\r
34 import com.att.cmd.Param;\r
35 import com.att.inno.env.APIException;\r
36 \r
37 public class Deny extends BaseCmd<Mgmt> {\r
38         private final static String[] options = {"add","del"};\r
39 \r
40         public Deny(Mgmt mgmt) throws APIException {\r
41                 super(mgmt, "deny");\r
42                 cmds.add(new DenySomething(this,"ip","ipv4or6[,ipv4or6]*"));\r
43                 cmds.add(new DenySomething(this,"id","identity[,identity]*"));\r
44         }\r
45         \r
46         public class DenySomething extends Cmd {\r
47 \r
48                 private boolean isID;\r
49 \r
50                 public DenySomething(Deny deny, String type, String repeatable) {\r
51                         super(deny, type,\r
52                                 new Param(optionsToString(options),true),\r
53                                 new Param(repeatable,true));\r
54                         isID = "id".equals(type);\r
55                 }\r
56 \r
57                 @Override\r
58                 protected int _exec(int _idx, String... args) throws CadiException, APIException, LocatorException {\r
59                         int idx = _idx;\r
60                         String action = args[idx++];\r
61                         final int option = whichOption(options, action);\r
62                         int rv=409;\r
63                         for(final String name : args[idx++].split(COMMA)) {\r
64                                 final String append;\r
65                                 if(isID && name.indexOf("@")<0) {\r
66                                         append='@'+ env.getProperty(AAFcli.AAF_DEFAULT_REALM);\r
67                                 } else {\r
68                                         append = "";\r
69                                 }\r
70                                 final String path = "/mgmt/deny/"+getName() + '/'+ name + append;\r
71                                 rv = all(new Retryable<Integer>() {\r
72                                         @Override\r
73                                         public Integer code(Rcli<?> client) throws APIException, CadiException  {\r
74                                                 int rv = 409;\r
75                                                 Future<Void> fp;\r
76                                                 String resp;\r
77                                                 switch(option) {\r
78                                                         case 0: \r
79                                                                 fp = client.create(path, Void.class);\r
80                                                                 resp = " added";\r
81                                                                 break;\r
82                                                         default: \r
83                                                                 fp = client.delete(path, Void.class);\r
84                                                                 resp = " deleted";\r
85                                                 }\r
86                                                 if(fp.get(AAFcli.timeout())) {\r
87                                                         pw().println(name + append + resp + " on " + client);\r
88                                                         rv=fp.code();\r
89                                                 } else {\r
90                                                         if(rv==409)rv = fp.code();\r
91                                                         error(fp);\r
92                                                 }\r
93                                                 return rv;\r
94                                         }\r
95                                 });\r
96                         }\r
97                         return rv;\r
98                 }\r
99 \r
100         }\r
101 \r
102 }\r