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