Fix license header violations in aaa-idmlight
[aaa.git] / aaa-idmlight / src / main / java / org / opendaylight / aaa / idm / rest / RoleHandler.java
1 /*
2  * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
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          }
97          else 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
101          // description
102          if (role.getDescription()==null) {
103             role.setDescription("");
104          }
105          else if (role.getDescription().length()>RoleStore.MAX_FIELD_LEN) {
106             return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
107          }
108
109          role = roleStore.createRole(role);
110       }
111       catch (StoreException se) {
112          return new IDMError(500,"internal error creating role",se.message).response();
113       }
114
115       return Response.status(201).entity(role).build();
116    }
117
118    @PUT
119    @Path("/{id}")
120    @Consumes("application/json")
121    @Produces("application/json")
122    public Response putRole(@Context UriInfo info,Role role,@PathParam("id") String id) {
123       long longId=0;
124       logger.info("put /roles/" + id);
125        try {
126          longId= Long.parseLong(id);
127       }
128       catch (NumberFormatException nfe) {
129          return new IDMError(404,"invalid role id :" + id,"").response();
130       }
131
132       try {
133          role.setRoleid((int)longId);
134
135          // name
136          // TODO: names should be unique
137          if ((role.getName()!=null) && (role.getName().length()>RoleStore.MAX_FIELD_LEN)) {
138             return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
139          }
140
141          // description
142          if ((role.getDescription()!=null) && (role.getDescription().length()>RoleStore.MAX_FIELD_LEN)) {
143             return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
144          }
145
146          role = roleStore.putRole(role);
147          if (role==null) {
148             return new IDMError(404,"role id not found :" + id,"").response();
149          }
150          IdmLightProxy.clearClaimCache();
151          return Response.status(200).entity(role).build();
152       }
153       catch (StoreException se) {
154          return new IDMError(500,"internal error putting role",se.message).response();
155       }
156    }
157
158    @DELETE
159    @Path("/{id}")
160    public Response deleteRole(@Context UriInfo info,@PathParam("id") String id) {
161       long longId=0;
162       logger.info("Delete /roles/" + id);
163        try {
164          longId= Long.parseLong(id);
165       }
166       catch (NumberFormatException nfe) {
167          return new IDMError(404,"invalid role id :" + id,"").response();
168       }
169
170       try {
171          Role role = new Role();
172          role.setRoleid((int)longId);
173          role = roleStore.deleteRole(role);
174          if (role==null) {
175             return new IDMError(404,"role id not found :" + id,"").response();
176          }
177       }
178       catch (StoreException se) {
179          return new IDMError(500,"internal error deleting role",se.message).response();
180       }
181       IdmLightProxy.clearClaimCache();
182       return Response.status(204).build();
183    }
184
185 }