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 else if (role.getName().length()>RoleStore.MAX_FIELD_LEN) {
98 return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
102 if (role.getDescription()==null) {
103 role.setDescription("");
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();
109 role = roleStore.createRole(role);
111 catch (StoreException se) {
112 return new IDMError(500,"internal error creating role",se.message).response();
115 return Response.status(201).entity(role).build();
120 @Consumes("application/json")
121 @Produces("application/json")
122 public Response putRole(@Context UriInfo info,Role role,@PathParam("id") String id) {
124 logger.info("put /roles/" + id);
126 longId= Long.parseLong(id);
128 catch (NumberFormatException nfe) {
129 return new IDMError(404,"invalid role id :" + id,"").response();
133 role.setRoleid((int)longId);
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();
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();
146 role = roleStore.putRole(role);
148 return new IDMError(404,"role id not found :" + id,"").response();
150 IdmLightProxy.clearClaimCache();
151 return Response.status(200).entity(role).build();
153 catch (StoreException se) {
154 return new IDMError(500,"internal error putting role",se.message).response();
160 public Response deleteRole(@Context UriInfo info,@PathParam("id") String id) {
162 logger.info("Delete /roles/" + id);
164 longId= Long.parseLong(id);
166 catch (NumberFormatException nfe) {
167 return new IDMError(404,"invalid role id :" + id,"").response();
171 Role role = new Role();
172 role.setRoleid((int)longId);
173 role = roleStore.deleteRole(role);
175 return new IDMError(404,"role id not found :" + id,"").response();
178 catch (StoreException se) {
179 return new IDMError(500,"internal error deleting role",se.message).response();
181 IdmLightProxy.clearClaimCache();
182 return Response.status(204).build();