1 package org.opendaylight.controller.northbound.commons.utils;
3 import java.util.HashMap;
6 import javax.ws.rs.core.Response;
8 import org.opendaylight.controller.containermanager.IContainerAuthorization;
9 import org.opendaylight.controller.sal.authorization.Privilege;
10 import org.opendaylight.controller.sal.authorization.UserLevel;
11 import org.opendaylight.controller.sal.utils.GlobalConstants;
12 import org.opendaylight.controller.sal.utils.ServiceHelper;
13 import org.opendaylight.controller.sal.utils.Status;
14 import org.opendaylight.controller.sal.utils.StatusCode;
15 import org.opendaylight.controller.usermanager.IUserManager;
17 public class NorthboundUtils {
19 private static final Map<StatusCode, Response.Status> ResponseStatusMapping = new HashMap<StatusCode, Response.Status>() {
20 private static final long serialVersionUID = 1L;
22 put(StatusCode.SUCCESS, Response.Status.OK);
23 put(StatusCode.BADREQUEST, Response.Status.BAD_REQUEST);
24 put(StatusCode.UNAUTHORIZED, Response.Status.UNAUTHORIZED);
25 put(StatusCode.FORBIDDEN, Response.Status.FORBIDDEN);
26 put(StatusCode.NOTFOUND, Response.Status.NOT_FOUND);
27 put(StatusCode.NOTALLOWED, Response.Status.FORBIDDEN);
28 put(StatusCode.NOTACCEPTABLE, Response.Status.NOT_ACCEPTABLE);
29 put(StatusCode.TIMEOUT, Response.Status.GONE);
30 put(StatusCode.CONFLICT, Response.Status.CONFLICT);
31 put(StatusCode.GONE, Response.Status.GONE);
32 put(StatusCode.UNSUPPORTED, Response.Status.BAD_REQUEST);
33 put(StatusCode.INTERNALERROR, Response.Status.INTERNAL_SERVER_ERROR);
34 put(StatusCode.NOTIMPLEMENTED, Response.Status.NOT_ACCEPTABLE);
35 put(StatusCode.NOSERVICE, Response.Status.SERVICE_UNAVAILABLE);
36 put(StatusCode.UNDEFINED, Response.Status.BAD_REQUEST);
40 // Suppress default constructor for noninstantiability
41 private NorthboundUtils() {
45 * Returns Response.Status for a given status. If the status is null or if
46 * the corresponding StatusCode is not present in the ResponseStatusMapping,
51 * @return The Response.Status for a given status
53 public static Response.Status getResponseStatus(Status status) {
54 return ResponseStatusMapping.get(status.getCode());
58 * Returns Response for a given status. If the status provided is null or if
59 * the corresponding StatusCode is not present in the ResponseStatusMapping,
60 * it returns Response with StatusType as INTERNAL_SERVER_ERROR.
64 * @return The Response for a given status.
66 public static Response getResponse(Status status) {
67 if ((status == null) || (!ResponseStatusMapping.containsKey(status.getCode()))) {
68 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Action Result Unknown").build();
70 return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build();
74 * Returns whether the current user has the required privilege on the
79 * @param containerName
82 * Operation to be performed - READ/WRITE
84 * Class from where the function is invoked
85 * @return The Status of the request, either Success or Unauthorized
87 public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) {
89 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
90 IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle);
94 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKADMIN.ordinal());
96 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKOPERATOR.ordinal());
102 IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
103 IContainerAuthorization.class, bundle);
109 Privilege current = auth.getResourcePrivilege(userName, containerName);
110 if (required.ordinal() > current.ordinal()) {