Merge "BUG2011 Fix"
[aaa.git] / aaa-idmlight / src / main / java / org / opendaylight / aaa / idm / rest / RoleHandler.java
1 /*
2  * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.aaa.idm.rest;
10
11 /**
12  *
13  * @author peter.mellquist@hp.com 
14  *
15  */
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.DELETE;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.UriInfo;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import java.util.List;
32 import java.util.ArrayList;
33 import org.opendaylight.aaa.idm.model.Roles;
34 import org.opendaylight.aaa.idm.model.Role;
35 import org.opendaylight.aaa.idm.model.IDMError;
36 import org.opendaylight.aaa.idm.persistence.RoleStore;
37 import org.opendaylight.aaa.idm.persistence.StoreException;
38 import org.opendaylight.aaa.idm.IdmLightProxy;
39         
40 @Path("/v1/roles")
41 public class RoleHandler {
42    private static Logger logger = LoggerFactory.getLogger(RoleHandler.class);
43    private static RoleStore roleStore = new RoleStore();
44    
45    @GET
46    @Produces("application/json")
47    public Response getRoles() {
48       logger.info("get /roles");
49       Roles roles=null;
50       try {
51          roles = roleStore.getRoles();
52       }
53       catch (StoreException se) {
54          return new IDMError(500,"internal error getting roles",se.message).response();
55       }
56       return Response.ok(roles).build();
57    }
58
59    @GET
60    @Path("/{id}")
61    @Produces("application/json")
62    public Response getRole(@PathParam("id") String id)  {
63       logger.info("get /roles/" + id);
64       Role role=null;
65       long longId=0;
66       try {
67          longId=Long.parseLong(id);
68       }
69       catch (NumberFormatException nfe) {
70          return new IDMError(404,"invalid role id :" + id,"").response();
71       }
72
73       try {
74          role = roleStore.getRole(longId);
75       }
76       catch(StoreException se) {
77          return new IDMError(500,"internal error getting roles",se.message).response();
78       }
79
80       if (role==null) {
81          return new IDMError(404,"role not found id :" + id,"").response();
82       }
83       return Response.ok(role).build();
84    }
85
86    @POST
87    @Consumes("application/json")
88    @Produces("application/json")
89    public Response createRole(@Context UriInfo info,Role role) {
90       logger.info("Post /roles");
91       try {
92          // TODO: role names should be unique!  
93          // name
94          if (role.getName()==null)
95             return new IDMError(404,"name must be defined on role create","").response();
96          else 
97             if (role.getName().length()>RoleStore.MAX_FIELD_LEN)
98                return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
99          
100          // description   
101          if (role.getDescription()==null)
102             role.setDescription("");
103          else
104             if (role.getDescription().length()>RoleStore.MAX_FIELD_LEN)
105                return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
106
107          role = roleStore.createRole(role);
108       }
109       catch (StoreException se) {
110          return new IDMError(500,"internal error creating role",se.message).response();
111       } 
112
113       return Response.status(201).entity(role).build();
114    } 
115
116    @PUT
117    @Path("/{id}")
118    @Consumes("application/json")
119    @Produces("application/json")
120    public Response putRole(@Context UriInfo info,Role role,@PathParam("id") String id) {
121       long longId=0;
122       logger.info("put /roles/" + id);
123        try {
124          longId= Long.parseLong(id);
125       }
126       catch (NumberFormatException nfe) {
127          return new IDMError(404,"invalid role id :" + id,"").response();
128       }
129
130       try {
131          role.setRoleid((int)longId);
132
133          // name
134          // TODO: names should be unique
135          if ((role.getName()!=null) && (role.getName().length()>RoleStore.MAX_FIELD_LEN))
136             return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
137
138          // description
139          if ((role.getDescription()!=null) && (role.getDescription().length()>RoleStore.MAX_FIELD_LEN))
140             return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
141
142          role = roleStore.putRole(role);
143          if (role==null) {
144             return new IDMError(404,"role id not found :" + id,"").response();
145          }
146          IdmLightProxy.clearClaimCache();
147          return Response.status(200).entity(role).build();
148       }
149       catch (StoreException se) {
150          return new IDMError(500,"internal error putting role",se.message).response();
151       }
152    }
153
154    @DELETE
155    @Path("/{id}")
156    public Response deleteRole(@Context UriInfo info,@PathParam("id") String id) {
157       long longId=0;
158       logger.info("Delete /roles/" + id);
159        try {
160          longId= Long.parseLong(id);
161       }
162       catch (NumberFormatException nfe) {
163          return new IDMError(404,"invalid role id :" + id,"").response();
164       }
165
166       try {
167          Role role = new Role();
168          role.setRoleid((int)longId);
169          role = roleStore.deleteRole(role);
170          if (role==null) {
171             return new IDMError(404,"role id not found :" + id,"").response();
172          }
173       }
174       catch (StoreException se) {
175          return new IDMError(500,"internal error deleting role",se.message).response();
176       }
177       IdmLightProxy.clearClaimCache();
178       return Response.status(204).build();
179    }
180
181 }