Add INFO.yaml file
[aaf/authz.git] / authz-cmd / src / main / java / org / onap / aaf / cmd / perm / Create.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.perm;\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.PermRequest;\r
39 import aaf.v2_0.RoleRequest;\r
40 \r
41 /**\r
42  * \r
43  *\r
44  */\r
45 public class Create extends Cmd {\r
46         public Create(Perm parent) {\r
47                 super(parent,"create", \r
48                                 new Param("type",true), \r
49                                 new Param("instance",true),\r
50                                 new Param("action", true),\r
51                                 new Param("role[,role]* (to Grant to)", false)\r
52                                 );\r
53         }\r
54 \r
55         @Override\r
56         public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {\r
57                 return same(new Retryable<Integer>() {\r
58                         @Override\r
59                         public Integer code(Rcli<?> client) throws CadiException, APIException {\r
60                                 int idx = index;\r
61                                 final PermRequest pr = new PermRequest();  \r
62                                 pr.setType(args[idx++]);\r
63                                 pr.setInstance(args[idx++]);\r
64                                 pr.setAction(args[idx++]);\r
65                                 String roleCommas = (args.length>idx)?args[idx++]:null;\r
66                                 String[] roles = roleCommas==null?null:roleCommas.split("\\s*,\\s*");\r
67                                 boolean force = aafcli.forceString()!=null;\r
68                                 int rv;\r
69                                 \r
70                                 if(roles!=null && force) { // Make sure Roles are Created\r
71                                         RoleRequest rr = new RoleRequest();\r
72                                         for(String role : roles) {\r
73                                                 rr.setName(role);;\r
74                                                 Future<RoleRequest> fr = client.create(\r
75                                                         "/authz/role",\r
76                                                         getDF(RoleRequest.class),\r
77                                                         rr\r
78                                                         );\r
79                                                 fr.get(AAFcli.timeout());\r
80                                                 switch(fr.code()){\r
81                                                         case 201:\r
82                                                                 pw().println("Created Role [" + role + ']');\r
83                                                                 break;\r
84                                                         case 409:\r
85                                                                 break;\r
86                                                         default: \r
87                                                                 pw().println("Role [" + role + "] does not exist, and cannot be created.");\r
88                                                                 return HttpStatus.PARTIAL_CONTENT_206;\r
89                                                 }\r
90                                         }\r
91                                 }\r
92 \r
93                                 // Set Start/End commands\r
94                                 setStartEnd(pr);\r
95                                 setQueryParamsOn(client);\r
96                                 Future<PermRequest> fp = client.create(\r
97                                                 "/authz/perm",\r
98                                                 getDF(PermRequest.class),\r
99                                                 pr\r
100                                                 );\r
101                                 if(fp.get(AAFcli.timeout())) {\r
102                                         rv = fp.code();\r
103                                         pw().println("Created Permission");\r
104                                         if(roles!=null) {\r
105                                                 if(aafcli.forceString()!=null) { // Make sure Roles are Created\r
106                                                         RoleRequest rr = new RoleRequest();\r
107                                                         for(String role : roles) {\r
108                                                                 rr.setName(role);;\r
109                                                                 Future<RoleRequest> fr = client.create(\r
110                                                                         "/authz/role",\r
111                                                                         getDF(RoleRequest.class),\r
112                                                                         rr\r
113                                                                         );\r
114                                                                 fr.get(AAFcli.timeout());\r
115                                                                 switch(fr.code()){\r
116                                                                         case 201:\r
117                                                                         case 409:break;\r
118                                                                         default: \r
119                                                                                 \r
120                                                                 }\r
121                                                         }\r
122                                                 }\r
123                                                 \r
124                                                 try {\r
125                                                         if(201!=(rv=((Perm)parent)._exec(0, \r
126                                                                         new String[] {"grant",pr.getType(),pr.getInstance(),pr.getAction(),roleCommas}))) {\r
127                                                                 rv = HttpStatus.PARTIAL_CONTENT_206;\r
128                                                         }\r
129                                                 } catch (LocatorException e) {\r
130                                                         throw new CadiException(e);\r
131                                                 }\r
132                                         }\r
133                                 } else {\r
134                                         rv = fp.code();\r
135                                         if(rv==409 && force) {\r
136                                                 rv = 201;\r
137                                         } else if(rv==202) {\r
138                                                 pw().println("Permission Creation Accepted, but requires Approvals before actualizing");\r
139                                                 if (roles!=null)\r
140                                                         pw().println("You need to grant the roles after approval.");\r
141                                         } else {\r
142                                                 error(fp);\r
143                                         }\r
144                                 }\r
145                                 return rv;\r
146                         }\r
147                 });\r
148         }\r
149         \r
150         @Override\r
151         public void detailedHelp(int _indent, StringBuilder sb) {\r
152                 int indent = _indent;\r
153                 detailLine(sb,indent,"Create a Permission with:");\r
154                 detailLine(sb,indent+=2,"type     - A Namespace qualified identifier identifying the kind of");\r
155                 detailLine(sb,indent+11,"resource to be protected");\r
156                 detailLine(sb,indent,"instance - A name that distinguishes a particular instance of resource");\r
157                 detailLine(sb,indent,"action   - What kind of action is allowed");\r
158                 detailLine(sb,indent,"role(s)  - Perms granted to these Comma separated Role(s)");\r
159                 detailLine(sb,indent+11,"Nonexistent role(s) will be created, if in same namespace");\r
160                 sb.append('\n');\r
161                 detailLine(sb,indent+2,"Note: Instance and Action can be a an '*' (enter \\\\* on Unix Shell)");\r
162                 api(sb,indent,HttpMethods.POST,"authz/perm",PermRequest.class,true);\r
163         }\r
164 \r
165 }\r