2 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
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
9 package org.opendaylight.aaa.idm.rest;
13 * @author peter.mellquist@hp.com
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;
41 public class RoleHandler {
42 private static Logger logger = LoggerFactory.getLogger(RoleHandler.class);
43 private static RoleStore roleStore = new RoleStore();
46 @Produces("application/json")
47 public Response getRoles() {
48 logger.info("get /roles");
51 roles = roleStore.getRoles();
53 catch (StoreException se) {
54 return new IDMError(500,"internal error getting roles",se.message).response();
56 return Response.ok(roles).build();
61 @Produces("application/json")
62 public Response getRole(@PathParam("id") String id) {
63 logger.info("get /roles/" + id);
67 longId=Long.parseLong(id);
69 catch (NumberFormatException nfe) {
70 return new IDMError(404,"invalid role id :" + id,"").response();
74 role = roleStore.getRole(longId);
76 catch(StoreException se) {
77 return new IDMError(500,"internal error getting roles",se.message).response();
81 return new IDMError(404,"role not found id :" + id,"").response();
83 return Response.ok(role).build();
87 @Consumes("application/json")
88 @Produces("application/json")
89 public Response createRole(@Context UriInfo info,Role role) {
90 logger.info("Post /roles");
92 // TODO: role names should be unique!
94 if (role.getName()==null)
95 return new IDMError(404,"name must be defined on role create","").response();
97 if (role.getName().length()>RoleStore.MAX_FIELD_LEN)
98 return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
101 if (role.getDescription()==null)
102 role.setDescription("");
104 if (role.getDescription().length()>RoleStore.MAX_FIELD_LEN)
105 return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
107 role = roleStore.createRole(role);
109 catch (StoreException se) {
110 return new IDMError(500,"internal error creating role",se.message).response();
113 return Response.status(201).entity(role).build();
118 @Consumes("application/json")
119 @Produces("application/json")
120 public Response putRole(@Context UriInfo info,Role role,@PathParam("id") String id) {
122 logger.info("put /roles/" + id);
124 longId= Long.parseLong(id);
126 catch (NumberFormatException nfe) {
127 return new IDMError(404,"invalid role id :" + id,"").response();
131 role.setRoleid((int)longId);
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();
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();
142 role = roleStore.putRole(role);
144 return new IDMError(404,"role id not found :" + id,"").response();
146 IdmLightProxy.clearClaimCache();
147 return Response.status(200).entity(role).build();
149 catch (StoreException se) {
150 return new IDMError(500,"internal error putting role",se.message).response();
156 public Response deleteRole(@Context UriInfo info,@PathParam("id") String id) {
158 logger.info("Delete /roles/" + id);
160 longId= Long.parseLong(id);
162 catch (NumberFormatException nfe) {
163 return new IDMError(404,"invalid role id :" + id,"").response();
167 Role role = new Role();
168 role.setRoleid((int)longId);
169 role = roleStore.deleteRole(role);
171 return new IDMError(404,"role id not found :" + id,"").response();
174 catch (StoreException se) {
175 return new IDMError(500,"internal error deleting role",se.message).response();
177 IdmLightProxy.clearClaimCache();
178 return Response.status(204).build();