Apply comments for MDSAL Store
[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.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.core.Context;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.UriInfo;
28
29 import org.opendaylight.aaa.api.IDMStoreException;
30 import org.opendaylight.aaa.api.IIDMStore;
31 import org.opendaylight.aaa.api.model.IDMError;
32 import org.opendaylight.aaa.api.model.Role;
33 import org.opendaylight.aaa.api.model.Roles;
34 import org.opendaylight.aaa.idm.IdmLightApplication;
35 import org.opendaylight.aaa.idm.IdmLightProxy;
36 import org.opendaylight.aaa.idm.ServiceLocator;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Path("/v1/roles")
41 public class RoleHandler {
42    private static Logger logger = LoggerFactory.getLogger(RoleHandler.class);
43
44    @GET
45    @Produces("application/json")
46    public Response getRoles() {
47       logger.info("get /roles");
48       Roles roles=null;
49       try {
50          roles = ServiceLocator.INSTANCE.getStore().getRoles();
51       }
52       catch (IDMStoreException se) {
53          return new IDMError(500,"internal error getting roles",se.getMessage()).response();
54       }
55       return Response.ok(roles).build();
56    }
57
58    @GET
59    @Path("/{id}")
60    @Produces("application/json")
61    public Response getRole(@PathParam("id") String id)  {
62       logger.info("get /roles/" + id);
63       Role role=null;
64
65       try {
66          role = ServiceLocator.INSTANCE.getStore().readRole(id);
67       }
68       catch(IDMStoreException se) {
69          return new IDMError(500,"internal error getting roles",se.getMessage()).response();
70       }
71
72       if (role==null) {
73          return new IDMError(404,"role not found id :" + id,"").response();
74       }
75       return Response.ok(role).build();
76    }
77
78    @POST
79    @Consumes("application/json")
80    @Produces("application/json")
81    public Response createRole(@Context UriInfo info,Role role) {
82       logger.info("Post /roles");
83       try {
84          // TODO: role names should be unique!
85          // name
86          if (role.getName()==null) {
87             return new IDMError(404,"name must be defined on role create","").response();
88          }
89          else if (role.getName().length()> IdmLightApplication.MAX_FIELD_LEN) {
90             return new IDMError(400,"role name max length is :" + IdmLightApplication.MAX_FIELD_LEN,"").response();
91          }
92
93          // domain
94          if (role.getDomainid()==null) {
95             return new IDMError(404,"The role's domain must be defined on role when creating a role.","").response();
96          }
97          else if (role.getDomainid().length()>IdmLightApplication.MAX_FIELD_LEN) {
98             return new IDMError(400,"role domain max length is :" + IdmLightApplication.MAX_FIELD_LEN,"").response();
99          }
100
101          // description
102          if (role.getDescription()==null) {
103             role.setDescription("");
104          }
105          else if (role.getDescription().length()>IdmLightApplication.MAX_FIELD_LEN) {
106             return new IDMError(400,"role description max length is :" + IdmLightApplication.MAX_FIELD_LEN,"").response();
107          }
108
109          role = ServiceLocator.INSTANCE.getStore().writeRole(role);
110       }
111       catch (IDMStoreException se) {
112          return new IDMError(500,"internal error creating role",se.getMessage()).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       logger.info("put /roles/" + id);
124
125       try {
126          role.setRoleid(id);
127
128          // name
129          // TODO: names should be unique
130          if ((role.getName()!=null) && (role.getName().length()>IdmLightApplication.MAX_FIELD_LEN)) {
131             return new IDMError(400,"role name max length is :" + IdmLightApplication.MAX_FIELD_LEN,"").response();
132          }
133
134          // description
135          if ((role.getDescription()!=null) && (role.getDescription().length()>IdmLightApplication.MAX_FIELD_LEN)) {
136             return new IDMError(400,"role description max length is :" + IdmLightApplication.MAX_FIELD_LEN,"").response();
137          }
138
139          role = ServiceLocator.INSTANCE.getStore().updateRole(role);
140          if (role==null) {
141             return new IDMError(404,"role id not found :" + id,"").response();
142          }
143          IdmLightProxy.clearClaimCache();
144          return Response.status(200).entity(role).build();
145       }
146       catch (IDMStoreException se) {
147          return new IDMError(500,"internal error putting role",se.getMessage()).response();
148       }
149    }
150
151    @DELETE
152    @Path("/{id}")
153    public Response deleteRole(@Context UriInfo info,@PathParam("id") String id) {
154       logger.info("Delete /roles/" + id);
155
156       try {
157          Role role = ServiceLocator.INSTANCE.getStore().deleteRole(id);
158          if (role==null) {
159             return new IDMError(404,"role id not found :" + id,"").response();
160          }
161       }
162       catch (IDMStoreException se) {
163          return new IDMError(500,"internal error deleting role",se.getMessage()).response();
164       }
165       IdmLightProxy.clearClaimCache();
166       return Response.status(204).build();
167    }
168
169 }