Improve Batches
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / api / API_NS.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.NsType;
35 import org.onap.aaf.auth.dao.cass.Status;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.layer.Result;
38 import org.onap.aaf.auth.service.AAF_Service;
39 import org.onap.aaf.auth.service.Code;
40 import org.onap.aaf.auth.service.facade.AuthzFacade;
41 import org.onap.aaf.auth.service.mapper.Mapper.API;
42
43 public class API_NS {
44     private static final String FULL = "full";
45     private static final String TRUE = "true";
46
47     public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
48         /**
49          * puts a new Namespace in Authz DB
50          * 
51          * TESTCASES: TC_NS1, TC_NSdelete1
52          */
53         authzAPI.route(POST,"/authz/ns",API.NS_REQ, new Code(facade,"Create a Namespace",true) {
54                     @Override
55                     public void handle(
56                             AuthzTrans trans,
57                             HttpServletRequest req, 
58                             HttpServletResponse resp) throws Exception {
59                         NsType nst = NsType.fromString(req.getParameter("type"));
60                         Result<Void> r = context.requestNS(trans, req, resp,nst);
61                             
62                         switch(r.status) {
63                             case OK:
64                                 resp.setStatus(HttpStatus.CREATED_201); 
65                                 break;
66                             case Status.ACC_Future:
67                                 resp.setStatus(HttpStatus.ACCEPTED_202); 
68                                 break;
69                             default:
70                                 context.error(trans,resp,r);
71                         }
72                     }
73                 }
74         );
75         
76         /**
77          * removes a Namespace from Authz DB
78          * 
79          * TESTCASES: TC_NS1, TC_NSdelete1
80          */
81         authzAPI.route(DELETE,"/authz/ns/:ns",API.VOID, new Code(facade,"Delete a Namespace",true) {
82                 @Override
83                 public void handle(
84                         AuthzTrans trans,
85                         HttpServletRequest req, 
86                         HttpServletResponse resp) throws Exception {
87                     Result<Void> r = context.deleteNS(trans, req, resp, pathParam(req,":ns"));
88                     switch(r.status) {
89                         case OK:
90                             resp.setStatus(HttpStatus.OK_200); 
91                             break;
92                         default:
93                             context.error(trans,resp,r);
94                     }
95                 }
96             }
97         );
98
99         /**
100          * Add an Admin in NS in Authz DB
101          * 
102          * TESTCASES: TC_NS1
103          */
104         authzAPI.route(POST,"/authz/ns/:ns/admin/:id",API.VOID, new Code(facade,"Add an Admin to a Namespace",true) {
105             @Override
106             public void handle(
107                 AuthzTrans trans,
108                 HttpServletRequest req, 
109                 HttpServletResponse resp) throws Exception {
110                 Result<Void> r = context.addAdminToNS(trans, resp, pathParam(req,":ns"), pathParam(req,":id"));
111                     switch(r.status) {
112                         case OK:
113                             resp.setStatus(HttpStatus.CREATED_201); 
114                             break;
115                         case Status.ACC_Future:
116                             resp.setStatus(HttpStatus.ACCEPTED_202); 
117                             break;
118                         default:
119                             context.error(trans,resp,r);
120                     }
121                 }
122             }
123         );
124     
125         /**
126          * Removes an Admin from Namespace in Authz DB
127          * 
128          * TESTCASES: TC_NS1
129          */
130         authzAPI.route(DELETE,"/authz/ns/:ns/admin/:id",API.VOID, new Code(facade,"Remove an Admin from a Namespace",true) {
131             @Override
132             public void handle(
133                 AuthzTrans trans,
134                 HttpServletRequest req, 
135                 HttpServletResponse resp) throws Exception {
136                     Result<Void> r = context.delAdminFromNS(trans, resp, pathParam(req,":ns"), pathParam(req,":id"));
137                     switch(r.status) {
138                         case OK:
139                             resp.setStatus(HttpStatus.OK_200); 
140                             break;
141                         default:
142                             context.error(trans,resp,r);
143                     }
144                 }
145             }
146         );
147
148     /**
149      * Add an Admin in NS in Authz DB
150      * 
151      * TESTCASES: TC_NS1
152      */
153         authzAPI.route(POST,"/authz/ns/:ns/responsible/:id",API.VOID, new Code(facade,"Add a Responsible Identity to a Namespace",true) {
154             @Override
155             public void handle(
156                 AuthzTrans trans,
157                 HttpServletRequest req, 
158                 HttpServletResponse resp) throws Exception {
159                 Result<Void> r = context.addResponsibilityForNS(trans, resp, pathParam(req,":ns"), pathParam(req,":id"));
160                     switch(r.status) {
161                         case OK:
162                             resp.setStatus(HttpStatus.CREATED_201); 
163                             break;
164                         case Status.ACC_Future:
165                             resp.setStatus(HttpStatus.ACCEPTED_202); 
166                             break;
167                         default:
168                             context.error(trans,resp,r);
169                     }
170                 }
171             }
172         );
173
174
175         /**
176          * 
177          */
178         authzAPI.route(GET,"/authz/nss/:id",API.NSS, new Code(facade,"Return Information about Namespaces", true) {
179             @Override
180             public void handle(
181                 AuthzTrans trans, 
182                 HttpServletRequest req, 
183                 HttpServletResponse resp) throws Exception {
184                     Result<Void> r = context.getNSsByName(trans, resp, pathParam(req,":id"),TRUE.equals(req.getParameter(FULL)));
185                     switch(r.status) {
186                         case OK:
187                             resp.setStatus(HttpStatus.OK_200); 
188                             break;
189                         default:
190                             context.error(trans,resp,r);
191                     }
192                 }
193             }
194         );    
195         
196         /**
197          * Get all Namespaces where user is an admin
198          */
199         authzAPI.route(GET,"/authz/nss/admin/:user",API.NSS, new Code(facade,"Return Namespaces where User is an Admin", true) {
200             @Override
201             public void handle(
202                 AuthzTrans trans, 
203                 HttpServletRequest req, 
204                 HttpServletResponse resp) throws Exception {
205                     Result<Void> r = context.getNSsByAdmin(trans, resp, pathParam(req,":user"),TRUE.equals(req.getParameter(FULL)));
206                     switch(r.status) {
207                         case OK:
208                             resp.setStatus(HttpStatus.OK_200); 
209                             break;
210                         default:
211                             context.error(trans,resp,r);
212                     }
213                 }
214             }
215         );
216         
217         /**
218          * Get all Namespaces where user is a responsible party
219          */
220         authzAPI.route(GET,"/authz/nss/responsible/:user",API.NSS, new Code(facade,"Return Namespaces where User is Responsible", true) {
221             @Override
222             public void handle(
223                 AuthzTrans trans, 
224                 HttpServletRequest req, 
225                 HttpServletResponse resp) throws Exception {
226                     Result<Void> r = context.getNSsByResponsible(trans, resp, pathParam(req,":user"),TRUE.equals(req.getParameter(FULL)));
227                     switch(r.status) {
228                         case OK:
229                             resp.setStatus(HttpStatus.OK_200); 
230                             break;
231                         default:
232                             context.error(trans,resp,r);
233                     }
234                 }
235             }
236         );
237
238         /**
239          * Get all Namespaces where user is an admin or owner
240          */
241         authzAPI.route(GET,"/authz/nss/either/:user",API.NSS, new Code(facade,"Return Namespaces where User Admin or Owner", true) {
242             @Override
243             public void handle(
244                 AuthzTrans trans, 
245                 HttpServletRequest req, 
246                 HttpServletResponse resp) throws Exception {
247                     Result<Void> r = context.getNSsByEither(trans, resp, pathParam(req,":user"),TRUE.equals(req.getParameter(FULL)));
248                     switch(r.status) {
249                         case OK:
250                             resp.setStatus(HttpStatus.OK_200); 
251                             break;
252                         default:
253                             context.error(trans,resp,r);
254                     }
255                 }
256             }
257         );
258
259         /**
260          * Get all children Namespaces
261          */
262         authzAPI.route(GET,"/authz/nss/children/:id",API.NSS, new Code(facade,"Return Child Namespaces", true) {
263             @Override
264             public void handle(
265                 AuthzTrans trans, 
266                 HttpServletRequest req, 
267                 HttpServletResponse resp) throws Exception {
268                     Result<Void> r = context.getNSsChildren(trans, resp, pathParam(req,":id"));
269                     switch(r.status) {
270                         case OK:
271                             resp.setStatus(HttpStatus.OK_200); 
272                             break;
273                         default:
274                             context.error(trans,resp,r);
275                     }
276                 }
277             }
278         );
279
280         /**
281          * Set a description of a Namespace
282          */
283         authzAPI.route(PUT,"/authz/ns",API.NS_REQ,new Code(facade,"Set a Description for a Namespace",true) {
284             @Override
285             public void handle(
286                     AuthzTrans trans, 
287                     HttpServletRequest req,
288                     HttpServletResponse resp) throws Exception {
289                 
290                 Result<Void> r = context.updateNsDescription(trans, req, resp);
291                 switch(r.status) {
292                     case OK: 
293                         resp.setStatus(HttpStatus.OK_200); 
294                         break;
295                     default:
296                         context.error(trans,resp,r);
297                 }
298             }
299         });    
300     
301         /**
302          * Removes an Owner from Namespace in Authz DB
303          * 
304          * TESTCASES: TC_NS1
305          */
306         authzAPI.route(DELETE,"/authz/ns/:ns/responsible/:id",API.VOID, new Code(facade,"Remove a Responsible Identity from Namespace",true) {
307             @Override
308             public void handle(
309                 AuthzTrans trans,
310                 HttpServletRequest req, 
311                 HttpServletResponse resp) throws Exception {
312                     Result<Void> r = context.delResponsibilityForNS(trans, resp, pathParam(req,":ns"), pathParam(req,":id"));
313                     switch(r.status) {
314                         case OK:
315                             resp.setStatus(HttpStatus.OK_200); 
316                             break;
317                         default:
318                             context.error(trans,resp,r);
319                     }
320                 }
321             }
322         );
323
324         authzAPI.route(POST,"/authz/ns/:ns/attrib/:key/:value",API.VOID, new Code(facade,"Add an Attribute from a Namespace",true) {
325             @Override
326             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
327                     Result<Void> r = context.createAttribForNS(trans, resp, 
328                         pathParam(req,":ns"), 
329                         pathParam(req,":key"),
330                         pathParam(req,":value"));
331                     switch(r.status) {
332                         case OK:
333                             resp.setStatus(HttpStatus.CREATED_201); 
334                             break;
335                         default:
336                             context.error(trans,resp,r);
337                     }
338                 }
339             }
340         );
341
342         authzAPI.route(GET,"/authz/ns/attrib/:key",API.KEYS, new Code(facade,"get Ns Key List From Attribute",true) {
343             @Override
344             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
345                     Result<Void> r = context.readNsByAttrib(trans, resp, pathParam(req,":key"));
346                     switch(r.status) {
347                         case OK:
348                             resp.setStatus(HttpStatus.OK_200); 
349                             break;
350                         default:
351                             context.error(trans,resp,r);
352                     }
353                 }
354             }
355         );
356
357         authzAPI.route(PUT,"/authz/ns/:ns/attrib/:key/:value",API.VOID, new Code(facade,"update an Attribute from a Namespace",true) {
358             @Override
359             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
360                     Result<Void> r = context.updAttribForNS(trans, resp, 
361                         pathParam(req,":ns"), 
362                         pathParam(req,":key"),
363                         pathParam(req,":value"));
364                     switch(r.status) {
365                         case OK:
366                             resp.setStatus(HttpStatus.OK_200); 
367                             break;
368                         default:
369                             context.error(trans,resp,r);
370                     }
371                 }
372             }
373         );
374         
375         authzAPI.route(DELETE,"/authz/ns/:ns/attrib/:key",API.VOID, new Code(facade,"delete an Attribute from a Namespace",true) {
376             @Override
377             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
378                     Result<Void> r = context.delAttribForNS(trans, resp, 
379                         pathParam(req,":ns"), 
380                         pathParam(req,":key"));
381                     switch(r.status) {
382                         case OK:
383                             resp.setStatus(HttpStatus.OK_200); 
384                             break;
385                         default:
386                             context.error(trans,resp,r);
387                     }
388                 }
389             }
390         );
391
392     }
393     
394     
395 }