af4095f3b93c5a9ba89c75ae04fdeeb7641b58f5
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / user / Delg.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cmd.user;\r
25 \r
26 import java.text.ParseException;\r
27 import java.util.Date;\r
28 \r
29 import com.att.cadi.CadiException;\r
30 import com.att.cadi.LocatorException;\r
31 import com.att.cadi.client.Future;\r
32 import com.att.cadi.client.Rcli;\r
33 import com.att.cadi.client.Retryable;\r
34 import com.att.cmd.AAFcli;\r
35 import com.att.cmd.BaseCmd;\r
36 import com.att.cmd.Param;\r
37 import com.att.cssa.rserv.HttpMethods;\r
38 import com.att.inno.env.APIException;\r
39 import com.att.inno.env.util.Chrono;\r
40 import com.att.rosetta.env.RosettaDF;\r
41 \r
42 import aaf.v2_0.DelgRequest;\r
43 \r
44 public class Delg extends BaseCmd<User> {\r
45         static final String AUTHZ_DELG = "/authz/delegate";\r
46         private final static String[] options = {"add","upd","del"};\r
47 \r
48         public Delg(User user) throws APIException {\r
49                 super(user,"delegate",\r
50                                 new Param(optionsToString(options),true),\r
51                                 new Param("from",true),\r
52                                 new Param("to REQ A&U",false),\r
53                                 new Param("until (YYYY-MM-DD) REQ A", false)\r
54                 );\r
55         }\r
56 \r
57         @Override\r
58         public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {\r
59                 return same(new Retryable<Integer>() {\r
60                         @Override\r
61                         public Integer code(Rcli<?> client) throws CadiException, APIException {\r
62                                 int idx = index;\r
63                                 String realm = getOrgRealm();\r
64                                 DelgRequest dr = new DelgRequest();\r
65                                 setStartEnd(dr);\r
66                 \r
67                                 int option= whichOption(options, args[idx++]);\r
68                                 String user = args[idx++];\r
69                                 if (user.indexOf('@') < 0 && realm != null) user += '@' + realm;\r
70                                 dr.setUser(user);\r
71                                 if(option<2) {\r
72                                         String delegate = args[idx++];\r
73                                         if (delegate.indexOf('@') < 0 && realm != null) delegate += '@' + realm;\r
74                                         dr.setDelegate(delegate);\r
75                                         if(option<2 && args.length>idx) {\r
76                                                 Date date;\r
77                                                 try {\r
78                                                         date = Chrono.dateOnlyFmt.parse(args[idx++]);\r
79                                                 } catch (ParseException e) {\r
80                                                         throw new CadiException(e);\r
81                                                 }\r
82                                                 dr.setEnd(Chrono.timeStamp(date));\r
83                                         }\r
84                                 }\r
85                 \r
86                                 Future<DelgRequest> fp;\r
87                                 RosettaDF<DelgRequest> df = getDF(DelgRequest.class);\r
88                                 String verb;\r
89                                 setQueryParamsOn(client);\r
90 \r
91                                 switch(option) {\r
92                                         case 0: \r
93                                                 fp = client.create(AUTHZ_DELG, df, dr);\r
94                                                 verb = "Added";\r
95                                                 break;\r
96                                         case 1: \r
97                                                 fp = client.update(AUTHZ_DELG, df, dr); \r
98                                                 verb = "Updated";\r
99                                                 break;\r
100                                         case 2: \r
101                                                 fp = client.delete(AUTHZ_DELG, df, dr); \r
102                                                 verb = "Deleted";\r
103                                                 break;\r
104                                         default:\r
105                                                 throw new CadiException("Bad Argument");\r
106                                 };\r
107                                 \r
108                                 if(fp.get(AAFcli.timeout())) {\r
109                                         pw().append("Delegate ");\r
110                                         pw().println(verb);\r
111                                 } else {\r
112                                         error(fp);\r
113                                 }\r
114                                 return fp.code();\r
115                         }\r
116                 });\r
117         }\r
118 \r
119         @Override\r
120         public void detailedHelp(int _indent, StringBuilder sb) {\r
121                 int indent = _indent;\r
122                 detailLine(sb,indent,"Add, Update or Delete Delegate");\r
123                 indent+=2;\r
124                 detailLine(sb,indent,"A Delegate is a person who will temporarily cover the Approval and");\r
125                 detailLine(sb,indent,"Ownership questions on behalf of the person Responsible.");\r
126                 sb.append('\n');\r
127                 detailLine(sb,indent,"fromID - the person who is the Responsible person of record");\r
128                 detailLine(sb,indent,"toID   - the person who will be delegated (required for Add/Update)");\r
129                 detailLine(sb,indent,"until  - the end date for this delegation");\r
130                 indent-=2;\r
131                 api(sb,indent,HttpMethods.POST,AUTHZ_DELG,DelgRequest.class,true);\r
132                 api(sb,indent,HttpMethods.DELETE,AUTHZ_DELG,DelgRequest.class,false);\r
133                 api(sb,indent,HttpMethods.PUT,AUTHZ_DELG,DelgRequest.class,false);\r
134         }\r
135 \r
136 }\r