Add INFO.yaml file
[aaf/authz.git] / authz-cmd / src / main / java / org / onap / aaf / 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 org.onap.aaf.cmd.user;\r
24 \r
25 import java.text.ParseException;\r
26 import java.util.Date;\r
27 \r
28 import org.onap.aaf.cmd.AAFcli;\r
29 import org.onap.aaf.cmd.BaseCmd;\r
30 import org.onap.aaf.cmd.Param;\r
31 import org.onap.aaf.cssa.rserv.HttpMethods;\r
32 \r
33 import org.onap.aaf.cadi.CadiException;\r
34 import org.onap.aaf.cadi.LocatorException;\r
35 import org.onap.aaf.cadi.client.Future;\r
36 import org.onap.aaf.cadi.client.Rcli;\r
37 import org.onap.aaf.cadi.client.Retryable;\r
38 import org.onap.aaf.inno.env.APIException;\r
39 import org.onap.aaf.inno.env.util.Chrono;\r
40 import org.onap.aaf.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