Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / ns / Attrib.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.cmd.ns;
25
26 import org.onap.aaf.auth.cmd.AAFcli;
27 import org.onap.aaf.auth.cmd.BaseCmd;
28 import org.onap.aaf.auth.cmd.Param;
29 import org.onap.aaf.auth.rserv.HttpMethods;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.cadi.client.Rcli;
34 import org.onap.aaf.cadi.client.Retryable;
35 import org.onap.aaf.misc.env.APIException;
36
37 public class Attrib extends BaseCmd<NS> {
38     private static final String[] options = {"add","upd","del"};
39     private String authzString = "/authz/ns/";
40     private String atrributeString = "/attrib/";
41
42     public Attrib(NS ns) {
43         super(ns,"attrib",
44                 new Param(optionsToString(options),true),
45                 new Param("ns-name",true),
46                 new Param("key",true),
47                 new Param("value",false)
48         );
49     }
50
51     @Override
52     public int _exec(final int idx, final String ... args) throws CadiException, APIException, LocatorException {
53         final int option = whichOption(options, args[idx]);
54         final String ns = args[idx+1];
55         final String key = args[idx+2];
56         final String value;
57         if (option!=2) {
58             if (args.length<=idx+3) {
59                 throw new CadiException("Not added: Need more Data");
60             }
61             value = args[idx+3];
62         } else {
63             value = "";
64         }
65
66         return same(new Retryable<Integer>() {
67             @Override
68             public Integer code(Rcli<?> client) throws CadiException, APIException {
69                 Future<Void> fp = null;
70                 String message;
71                 switch(option) {
72                     case 0:
73                         fp = client.create(authzString+ns+atrributeString+key+'/'+value,Void.class);
74                         message = String.format("Add Attrib %s=%s to %s",
75                                 key,value,ns);
76                         break;
77                     case 1:
78                         fp = client.update(authzString+ns+atrributeString+key+'/'+value);
79                         message = String.format("Update Attrib %s=%s for %s",
80                                 key,value,ns);
81                         break;
82                     case 2:
83                         fp = client.delete(authzString+ns+atrributeString+key,Void.class);
84                         message = String.format("Attrib %s deleted from %s",
85                                 key,ns);
86                         break;
87                     default:
88                         throw new CadiException("Bad Argument");
89                 };
90                 if (fp==null) {
91                     return 500;
92                 } else {
93                     if (fp.get(AAFcli.timeout())) {
94                         pw().println(message);
95                     } else {
96                         error(fp);
97                     }
98
99                     return fp.code();
100                 }
101             }
102         });
103     }
104
105     @Override
106     public void detailedHelp(int indentValue, StringBuilder sb) {
107             int indent = indentValue;
108         detailLine(sb,indent,"Add or Delete Administrator to/from Namespace");
109         indent+=4;
110         detailLine(sb,indent,"name - Name of Namespace");
111         detailLine(sb,indent,"id   - Credential of Person(s) to be Administrator");
112         sb.append('\n');
113         detailLine(sb,indent,"aafcli will call API on each ID presented.");
114         indent-=4;
115         api(sb,indent,HttpMethods.POST,"authz/ns/<ns>/admin/<id>",Void.class,true);
116         api(sb,indent,HttpMethods.DELETE,"authz/ns/<ns>/admin/<id>",Void.class,false);
117     }
118
119 }