Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / mgmt / Log.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.Param;
27 import org.onap.aaf.auth.common.Define;
28 import org.onap.aaf.auth.rserv.HttpMethods;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.LocatorException;
31 import org.onap.aaf.cadi.client.Future;
32 import org.onap.aaf.cadi.client.Rcli;
33 import org.onap.aaf.cadi.client.Retryable;
34 import org.onap.aaf.cadi.config.Config;
35 import org.onap.aaf.misc.env.APIException;
36
37 public class Log extends BaseCmd<Mgmt> {
38     private static final String[] options = {"add","del"};
39
40     public Log(Mgmt mgmt) {
41         super(mgmt, "log",
42                 new Param(optionsToString(options),true),
43                 new Param("id[,id]*",true));
44     }
45
46     @Override
47     public int _exec(int idxValue, String ... args) throws CadiException, APIException, LocatorException {
48         int rv=409;
49         int idx = idxValue;
50         final int option = whichOption(options, args[idx++]);
51
52         for (String name : args[idx++].split(COMMA)) {
53             final String fname;
54             if (name.indexOf("@")<0) {
55                 fname=name+'@'+ access.getProperty(Config.AAF_DEFAULT_REALM,null);
56             } else {
57                 fname = name;
58             }
59
60             rv = all(new Retryable<Integer>() {
61                 @Override
62                 public Integer code(Rcli<?> client) throws APIException, CadiException {
63                     int rv = 409;
64                     Future<Void> fp;
65                     String str = "/mgmt/log/id/"+fname;
66                     String msg;
67                     switch(option) {
68                         case 0:
69                             fp = client.create(str,Void.class);
70                             msg = "Added";
71                             break;
72                         case 1:
73                             fp = client.delete(str,Void.class);
74                             msg = "Deleted";
75                             break;
76                         default:
77                             fp = null;
78                             msg = "Ignored";
79                     }
80
81                     if (fp!=null) {
82                         if (fp.get(AAFcli.timeout())) {
83                             pw().println(msg + " Special Log for " + fname + " on " + client);
84                             rv=200;
85                         } else {
86                             if (rv==409) {
87                                 rv = fp.code();
88                             };
89                             error(fp);
90                         }
91                         return rv;
92                     }
93                     return rv;
94                 }
95             });
96         }
97         return rv;
98     }
99
100     @Override
101     public void detailedHelp(int indentValue, StringBuilder sb) {
102             int indent = indentValue;
103         detailLine(sb,indent,"Clear the cache for certain tables");
104         indent+=2;
105         detailLine(sb,indent,"name        - name of table or 'all'");
106         detailLine(sb,indent+14,"Must have admin rights to '" + Define.ROOT_NS() + '\'');
107         indent-=2;
108         api(sb,indent,HttpMethods.DELETE,"mgmt/cache/:name",Void.class,true);
109     }
110 }