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