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