2c93bc38394296d22c0fd9edfd98f3a5d95144d5
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / api / API_Roles.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.service.api;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25 import static org.onap.aaf.auth.rserv.HttpMethods.DELETE;
26 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
27 import static org.onap.aaf.auth.rserv.HttpMethods.POST;
28 import static org.onap.aaf.auth.rserv.HttpMethods.PUT;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.eclipse.jetty.http.HttpStatus;
34 import org.onap.aaf.auth.dao.cass.Status;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.auth.service.AAF_Service;
38 import org.onap.aaf.auth.service.Code;
39 import org.onap.aaf.auth.service.facade.AuthzFacade;
40 import org.onap.aaf.auth.service.mapper.Mapper.API;
41
42 public class API_Roles {
43     public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
44         /**
45          * puts a new role in Authz DB
46          */
47         authzAPI.route(POST,"/authz/role",API.ROLE_REQ, new Code(facade,"Create Role",true) {
48                     @Override
49                     public void handle(
50                             AuthzTrans trans,
51                             HttpServletRequest req, 
52                             HttpServletResponse resp) throws Exception {
53                         Result<Void> r = context.createRole(trans, req, resp);
54                             
55                         switch(r.status) {
56                             case OK:
57                                 resp.setStatus(HttpStatus.CREATED_201); 
58                                 break;
59                             case Status.ACC_Future:
60                                 resp.setStatus(HttpStatus.ACCEPTED_202); 
61                                 break;
62                             default:
63                                 context.error(trans,resp,r);
64                         }
65                     }
66                 }
67             );
68
69         /** 
70          *  get Role by name
71          */
72         authzAPI.route(GET, "/authz/roles/:role", API.ROLES, new Code(facade,"GetRolesByFullName",true) {
73             public void handle(
74                     AuthzTrans trans, 
75                     HttpServletRequest req,
76                     HttpServletResponse resp) throws Exception {
77                 
78                 Result<Void> r = context.getRolesByName(trans, resp, pathParam(req, "role"));
79                 switch(r.status) {
80                     case OK: 
81                         resp.setStatus(HttpStatus.OK_200); 
82                         break;
83                     default:
84                         context.error(trans,resp,r);
85                 }
86             }
87
88         });
89
90
91         /** 
92          *  gets all Roles by user name
93          */
94         authzAPI.route(GET, "/authz/roles/user/:name", API.ROLES, new Code(facade,"GetRolesByUser",true) {
95             public void handle(
96                     AuthzTrans trans, 
97                     HttpServletRequest req,
98                     HttpServletResponse resp) throws Exception {
99                 
100                 Result<Void> r = context.getRolesByUser(trans, resp, pathParam(req, "name"));
101                 switch(r.status) {
102                     case OK: 
103                         resp.setStatus(HttpStatus.OK_200); 
104                         break;
105                     default:
106                         context.error(trans,resp,r);
107                 }
108             }
109
110         });
111
112         /** 
113          *  gets all Roles by Namespace
114          */
115         authzAPI.route(GET, "/authz/roles/ns/:ns", API.ROLES, new Code(facade,"GetRolesByNS",true) {
116             public void handle(
117                     AuthzTrans trans, 
118                     HttpServletRequest req,
119                     HttpServletResponse resp) throws Exception {
120                 
121                 Result<Void> r = context.getRolesByNS(trans, resp, pathParam(req, "ns"));
122                 switch(r.status) {
123                     case OK: 
124                         resp.setStatus(HttpStatus.OK_200); 
125                         break;
126                     default:
127                         context.error(trans,resp,r);
128                 }
129             }
130         });
131
132         /** 
133          *  gets all Roles by Name without the Namespace
134          */
135         authzAPI.route(GET, "/authz/roles/name/:name", API.ROLES, new Code(facade,"GetRolesByNameOnly",true) {
136             public void handle(
137                     AuthzTrans trans, 
138                     HttpServletRequest req,
139                     HttpServletResponse resp) throws Exception {
140                 Result<Void> r = context.getRolesByNameOnly(trans, resp, pathParam(req, ":name"));
141                 switch(r.status) {
142                     case OK: 
143                         resp.setStatus(HttpStatus.OK_200); 
144                         break;
145                     default:
146                         context.error(trans,resp,r);
147                 }
148             }
149         });
150         
151         /**
152          * Deletes a Role from Authz DB by Object
153          */
154         authzAPI.route(DELETE,"/authz/role",API.ROLE_REQ, new Code(facade,"Delete Role",true) {
155                 @Override
156                 public void handle(
157                         AuthzTrans trans,
158                         HttpServletRequest req, 
159                         HttpServletResponse resp) throws Exception {
160                     Result<Void> r = context.deleteRole(trans, req, resp);
161                     
162                     switch(r.status) {
163                         case OK:
164                             resp.setStatus(HttpStatus.OK_200); 
165                             break;
166                         default:
167                             context.error(trans,resp,r);
168                     }
169                 }
170             
171             }
172         );
173     
174
175         
176         /**
177          * Deletes a Role from Authz DB by Key
178          */
179         authzAPI.route(DELETE,"/authz/role/:role",API.ROLE, new Code(facade,"Delete Role",true) {
180                 @Override
181                 public void handle(
182                         AuthzTrans trans,
183                         HttpServletRequest req, 
184                         HttpServletResponse resp) throws Exception {
185                     Result<Void> r = context.deleteRole(trans, resp, pathParam(req,":role"));
186                         
187                     switch(r.status) {
188                         case OK:
189                             resp.setStatus(HttpStatus.OK_200); 
190                             break;
191                         default:
192                             context.error(trans,resp,r);
193                     }
194                 }
195             
196             }
197         );
198     
199
200         /**
201          * Add a Permission to a Role (Grant)
202          */
203         authzAPI.route(POST,"/authz/role/perm",API.ROLE_PERM_REQ, new Code(facade,"Add Permission to Role",true) {
204                 @Override
205                 public void handle(
206                         AuthzTrans trans,
207                         HttpServletRequest req, 
208                         HttpServletResponse resp) throws Exception {
209                     
210                     Result<Void> r = context.addPermToRole(trans, req, resp);
211                         
212                     switch(r.status) {
213                         case OK:
214                             resp.setStatus(HttpStatus.CREATED_201); 
215                             break;
216                         default:
217                             context.error(trans,resp,r);
218                     }
219                 }
220             }
221         );
222         
223         /**
224          * Get all Roles by Permission
225          */
226         authzAPI.route(GET,"/authz/roles/perm/:type/:instance/:action",API.ROLES,new Code(facade,"GetRolesByPerm",true) {
227             public void handle(
228                     AuthzTrans trans, 
229                     HttpServletRequest req,
230                     HttpServletResponse resp) throws Exception {
231                 
232                 Result<Void> r = context.getRolesByPerm(trans, resp, 
233                         pathParam(req, "type"),
234                         pathParam(req, "instance"),
235                         pathParam(req, "action"));
236                 switch(r.status) {
237                     case OK: 
238                         resp.setStatus(HttpStatus.OK_200); 
239                         break;
240                     default:
241                         context.error(trans,resp,r);
242                 }
243             }
244         });
245         
246         /**
247          * Set a role's description
248          */
249         authzAPI.route(PUT,"/authz/role",API.ROLE_REQ,new Code(facade,"Set Description for role",true) {
250             @Override
251             public void handle(
252                     AuthzTrans trans, 
253                     HttpServletRequest req,
254                     HttpServletResponse resp) throws Exception {
255                 
256                 Result<Void> r = context.updateRoleDescription(trans, req, resp);
257                 switch(r.status) {
258                     case OK: 
259                         resp.setStatus(HttpStatus.OK_200); 
260                         break;
261                     default:
262                         context.error(trans,resp,r);
263                 }
264             }
265         });    
266         
267         /**
268          * Set a permission's roles to roles given
269          */
270         authzAPI.route(PUT,"/authz/role/perm",API.ROLE_PERM_REQ,new Code(facade,"Set a Permission's Roles",true) {
271             @Override
272             public void handle(
273                     AuthzTrans trans, 
274                     HttpServletRequest req,
275                     HttpServletResponse resp) throws Exception {
276                 
277                 Result<Void> r = context.resetPermRoles(trans, req, resp);
278                 switch(r.status) {
279                     case OK: 
280                         resp.setStatus(HttpStatus.OK_200); 
281                         break;
282                     default:
283                         context.error(trans,resp,r);
284                 }
285             }
286         });    
287         
288         /**
289          * Delete a Permission from a Role
290          * With multiple perms
291          */
292         authzAPI.route(DELETE,"/authz/role/:role/perm",API.ROLE_PERM_REQ, new Code(facade,"Delete Permission from Role",true) {
293             @Override
294             public void handle(
295                     AuthzTrans trans,
296                     HttpServletRequest req, 
297                     HttpServletResponse resp) throws Exception {
298                 Result<Void> r = context.delPermFromRole(trans, req, resp);
299                     
300                 switch(r.status) {
301                     case OK:
302                         resp.setStatus(HttpStatus.OK_200); 
303                         break;
304                     default:
305                         context.error(trans,resp,r);
306                 }
307             }
308         });
309
310
311         /*
312          * Delete a Permission from a Role by key only
313          * /
314         authzAPI.route(DELETE,"/authz/role/:role/perm/:type/:instance/:action",API.ROLE_PERM_REQ, new Code(facade,"Delete Permission from Role",true) {
315             @Override
316             public void handle(
317                     AuthzTrans trans,
318                     HttpServletRequest req, 
319                     HttpServletResponse resp) throws Exception {
320                 Result<Void> r = context.delPermFromRole(trans, resp, 
321                         pathParam(req,":role"),
322                         pathParam(req,":type"),
323                         pathParam(req,":instance"),
324                         pathParam(req,":action"));
325                     
326                 switch(r.status) {
327                     case OK:
328                         resp.setStatus(HttpStatus.OK_200); 
329                         break;
330                     default:
331                         context.error(trans,resp,r);
332                 }
333             }
334         });
335         */
336     }
337 }