AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / mgmt / Deny.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd.mgmt;
23
24 import org.onap.aaf.auth.cmd.AAFcli;
25 import org.onap.aaf.auth.cmd.BaseCmd;
26 import org.onap.aaf.auth.cmd.Cmd;
27 import org.onap.aaf.auth.cmd.Param;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.client.Future;
31 import org.onap.aaf.cadi.client.Rcli;
32 import org.onap.aaf.cadi.client.Retryable;
33 import org.onap.aaf.cadi.config.Config;
34 import org.onap.aaf.misc.env.APIException;
35
36 public class Deny extends BaseCmd<Mgmt> {
37         private final static String[] options = {"add","del"};
38
39         public Deny(Mgmt mgmt) throws APIException {
40                 super(mgmt, "deny");
41                 cmds.add(new DenySomething(this,"ip","ipv4or6[,ipv4or6]*"));
42                 cmds.add(new DenySomething(this,"id","identity[,identity]*"));
43         }
44         
45         public class DenySomething extends Cmd {
46
47                 private boolean isID;
48
49                 public DenySomething(Deny deny, String type, String repeatable) {
50                         super(deny, type,
51                                 new Param(optionsToString(options),true),
52                                 new Param(repeatable,true));
53                         isID = "id".equals(type);
54                 }
55
56                 @Override
57                 protected int _exec(int _idx, String... args) throws CadiException, APIException, LocatorException {
58                         int idx = _idx;
59                         String action = args[idx++];
60                         final int option = whichOption(options, action);
61                         int rv=409;
62                         for(final String name : args[idx++].split(COMMA)) {
63                                 final String append;
64                                 if(isID && name.indexOf("@")<0) {
65                                         append='@'+ access.getProperty(Config.AAF_DEFAULT_REALM,null);
66                                 } else {
67                                         append = "";
68                                 }
69                                 final String path = "/mgmt/deny/"+getName() + '/'+ name + append;
70                                 rv = all(new Retryable<Integer>() {
71                                         @Override
72                                         public Integer code(Rcli<?> client) throws APIException, CadiException  {
73                                                 int rv = 409;
74                                                 Future<Void> fp;
75                                                 String resp;
76                                                 switch(option) {
77                                                         case 0: 
78                                                                 fp = client.create(path, Void.class);
79                                                                 resp = " added";
80                                                                 break;
81                                                         default: 
82                                                                 fp = client.delete(path, Void.class);
83                                                                 resp = " deleted";
84                                                 }
85                                                 if(fp.get(AAFcli.timeout())) {
86                                                         pw().println(name + append + resp + " on " + client);
87                                                         rv=fp.code();
88                                                 } else {
89                                                         if(rv==409)rv = fp.code();
90                                                         error(fp);
91                                                 }
92                                                 return rv;
93                                         }
94                                 });
95                         }
96                         return rv;
97                 }
98
99         }
100
101 }