AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / Delg.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.user;
23
24 import java.text.ParseException;
25 import java.util.Date;
26
27 import org.onap.aaf.auth.cmd.AAFcli;
28 import org.onap.aaf.auth.cmd.BaseCmd;
29 import org.onap.aaf.auth.cmd.Param;
30 import org.onap.aaf.auth.rserv.HttpMethods;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.client.Future;
34 import org.onap.aaf.cadi.client.Rcli;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.misc.env.APIException;
37 import org.onap.aaf.misc.env.util.Chrono;
38 import org.onap.aaf.misc.rosetta.env.RosettaDF;
39
40 import aaf.v2_0.DelgRequest;
41
42 public class Delg extends BaseCmd<User> {
43         static final String AUTHZ_DELG = "/authz/delegate";
44         private final static String[] options = {"add","upd","del"};
45
46         public Delg(User user) throws APIException {
47                 super(user,"delegate",
48                                 new Param(optionsToString(options),true),
49                                 new Param("from",true),
50                                 new Param("to REQ A&U",false),
51                                 new Param("until (YYYY-MM-DD) REQ A", false)
52                 );
53         }
54
55         @Override
56         public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
57                 return same(new Retryable<Integer>() {
58                         @Override
59                         public Integer code(Rcli<?> client) throws CadiException, APIException {
60                                 int idx = index;
61                                 DelgRequest dr = new DelgRequest();
62                                 setStartEnd(dr);
63                 
64                                 int option= whichOption(options, args[idx++]);
65                                 String user = fullID(args[idx++]);
66                                 dr.setUser(user);
67                                 if(option<2) {
68                                         String delegate = fullID(args[idx++]);
69                                         dr.setDelegate(delegate);
70                                         if(option<2 && args.length>idx) {
71                                                 Date date;
72                                                 try {
73                                                         date = Chrono.dateOnlyFmt.parse(args[idx++]);
74                                                 } catch (ParseException e) {
75                                                         throw new CadiException(e);
76                                                 }
77                                                 dr.setEnd(Chrono.timeStamp(date));
78                                         }
79                                 }
80                 
81                                 Future<DelgRequest> fp;
82                                 RosettaDF<DelgRequest> df = getDF(DelgRequest.class);
83                                 String verb;
84                                 setQueryParamsOn(client);
85
86                                 switch(option) {
87                                         case 0: 
88                                                 fp = client.create(AUTHZ_DELG, df, dr);
89                                                 verb = "Added";
90                                                 break;
91                                         case 1: 
92                                                 fp = client.update(AUTHZ_DELG, df, dr); 
93                                                 verb = "Updated";
94                                                 break;
95                                         case 2: 
96                                                 fp = client.delete(AUTHZ_DELG, df, dr); 
97                                                 verb = "Deleted";
98                                                 break;
99                                         default:
100                                                 throw new CadiException("Bad Argument");
101                                 };
102                                 
103                                 if(fp.get(AAFcli.timeout())) {
104                                         pw().append("Delegate ");
105                                         pw().println(verb);
106                                 } else {
107                                         error(fp);
108                                 }
109                                 return fp.code();
110                         }
111                 });
112         }
113
114         @Override
115         public void detailedHelp(int _indent, StringBuilder sb) {
116                 int indent = _indent;
117                 detailLine(sb,indent,"Add, Update or Delete Delegate");
118                 indent+=2;
119                 detailLine(sb,indent,"A Delegate is a person who will temporarily cover the Approval and");
120                 detailLine(sb,indent,"Ownership questions on behalf of the person Responsible.");
121                 sb.append('\n');
122                 detailLine(sb,indent,"fromID - the person who is the Responsible person of record");
123                 detailLine(sb,indent,"toID   - the person who will be delegated (required for Add/Update)");
124                 detailLine(sb,indent,"until  - the end date for this delegation");
125                 indent-=2;
126                 api(sb,indent,HttpMethods.POST,AUTHZ_DELG,DelgRequest.class,true);
127                 api(sb,indent,HttpMethods.DELETE,AUTHZ_DELG,DelgRequest.class,false);
128                 api(sb,indent,HttpMethods.PUT,AUTHZ_DELG,DelgRequest.class,false);
129         }
130
131 }