78ab1811b34b3df3a4a2f868c4080ac364cb6e89
[aaf/authz.git] / authz-cmd / src / main / java / org / onap / aaf / cmd / role / CreateDelete.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.role;\r
24 \r
25 import org.onap.aaf.cmd.AAFcli;\r
26 import org.onap.aaf.cmd.Cmd;\r
27 import org.onap.aaf.cmd.Param;\r
28 import org.onap.aaf.cssa.rserv.HttpMethods;\r
29 \r
30 import com.att.aft.dme2.internal.jetty.http.HttpStatus;\r
31 import org.onap.aaf.cadi.CadiException;\r
32 import org.onap.aaf.cadi.LocatorException;\r
33 import org.onap.aaf.cadi.client.Future;\r
34 import org.onap.aaf.cadi.client.Rcli;\r
35 import org.onap.aaf.cadi.client.Retryable;\r
36 import org.onap.aaf.inno.env.APIException;\r
37 \r
38 import aaf.v2_0.RoleRequest;\r
39 \r
40 /**\r
41  * \r
42  *\r
43  */\r
44 public class CreateDelete extends Cmd {\r
45         private static final String ROLE_PATH = "/authz/role";\r
46         private final static String[] options = {"create","delete"};\r
47         public CreateDelete(Role parent) {\r
48                 super(parent,null, \r
49                                 new Param(optionsToString(options),true),\r
50                                 new Param("name",true)); \r
51         }\r
52 \r
53         @Override\r
54         public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {\r
55                 return same(new Retryable<Integer>() {\r
56                         @Override\r
57                         public Integer code(Rcli<?> client) throws CadiException, APIException {\r
58                                 int idx = index;\r
59                                 String action = args[idx++];\r
60                                 int option = whichOption(options, action);\r
61                 \r
62                                 RoleRequest rr = new RoleRequest();\r
63                                 rr.setName(args[idx++]);\r
64                 \r
65                                 // Set Start/End commands\r
66                                 setStartEnd(rr);\r
67                                 \r
68                                 Future<RoleRequest> fp = null;\r
69                                 String verb = null;\r
70                                 int rv;\r
71                                 switch(option) {\r
72                                         case 0:\r
73                                                 fp = client.create(\r
74                                                         ROLE_PATH,\r
75                                                         getDF(RoleRequest.class),\r
76                                                         rr\r
77                                                         );\r
78                                                 verb = "Create";\r
79                                                 break;\r
80                                         case 1:\r
81                                                 // Send "Force" if set\r
82                                                 setQueryParamsOn(client);\r
83                                                 fp = client.delete(\r
84                                                                 ROLE_PATH, // +args[idx++], \r
85                                                                 getDF(RoleRequest.class),\r
86                                                                 rr\r
87                                                                 );\r
88                                                 verb = "Delete";\r
89                                                 break;\r
90                                         default: // note, if not an option, whichOption throws Exception\r
91                                                 break;\r
92                                                 \r
93                                 }\r
94                                 boolean rolesSupplied = (args.length>idx);\r
95                                 if(fp.get(AAFcli.timeout())) {\r
96                                         rv=fp.code();\r
97                                         pw().print(verb);\r
98                                         pw().println("d Role");\r
99                                         if(rolesSupplied) {\r
100                                                 for(;args.length>idx;++idx ) {\r
101                                                         try {\r
102                                                                 if(201!=(rv=((Role)parent)._exec(0,new String[] {"user","add",rr.getName(),args[idx]}))) {\r
103                                                                         rv = HttpStatus.PARTIAL_CONTENT_206;\r
104                                                                 }\r
105                                                         } catch (LocatorException e) {\r
106                                                                 throw new CadiException(e);\r
107                                                         }\r
108                                                 }\r
109                                         }\r
110                                 } else {\r
111                                         if((rv=fp.code())==202) {\r
112                                                 pw().print("Role ");\r
113                                                 pw().print(verb);\r
114                                                 pw().println(" Accepted, but requires Approvals before actualizing");\r
115                                         } else {\r
116                                                 error(fp);\r
117                                         }\r
118                                 }\r
119                                 return rv;\r
120                         }\r
121                 });\r
122         }\r
123 \r
124         @Override\r
125         public void detailedHelp(int indent, StringBuilder sb) {\r
126                 detailLine(sb,indent,"Create OR Delete a Role");\r
127                 detailLine(sb,indent+2,"name - Name of Role to create");\r
128                 api(sb,indent,HttpMethods.POST,"authz/role",RoleRequest.class,true);\r
129                 api(sb,indent,HttpMethods.DELETE,"authz/role",RoleRequest.class,false);\r
130         }\r
131 \r
132 }\r