Added StatusCode to Response.Status Mapping
[controller.git] / opendaylight / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / utils / NorthboundUtils.java
1 package org.opendaylight.controller.northbound.commons.utils;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.ws.rs.core.Response;
7
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;
16
17 public class NorthboundUtils {
18
19     private static final Map<StatusCode, Response.Status> ResponseStatusMapping = new HashMap<StatusCode, Response.Status>() {
20         private static final long serialVersionUID = 1L;
21         {
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);
37         }
38     };
39
40     // Suppress default constructor for noninstantiability
41     private NorthboundUtils() {
42     }
43
44     /**
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,
47      * it returns null.
48      *
49      * @param status
50      *            The Status
51      * @return The Response.Status for a given status
52      */
53     public static Response.Status getResponseStatus(Status status) {
54         return ResponseStatusMapping.get(status.getCode());
55     }
56
57     /**
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.
61      *
62      * @param status
63      *            The Status
64      * @return The Response for a given status.
65      */
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();
69         }
70         return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build();
71     }
72
73     /**
74      * Returns whether the current user has the required privilege on the
75      * specified container
76      *
77      * @param userName
78      *            The user name
79      * @param containerName
80      *            The container name
81      * @param required
82      *            Operation to be performed - READ/WRITE
83      * @param bundle
84      *            Class from where the function is invoked
85      * @return The Status of the request, either Success or Unauthorized
86      */
87     public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) {
88
89         if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
90             IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle);
91
92             switch (required) {
93             case WRITE:
94                 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKADMIN.ordinal());
95             case READ:
96                 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKOPERATOR.ordinal());
97             default:
98                 return false;
99             }
100
101         } else {
102             IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
103                     IContainerAuthorization.class, bundle);
104
105             if (auth == null) {
106                 return false;
107             }
108
109             Privilege current = auth.getResourcePrivilege(userName, containerName);
110             if (required.ordinal() > current.ordinal()) {
111                 return false;
112             }
113         }
114         return true;
115     }
116
117 }