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;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class NorthboundUtils {
21 private static final Map<StatusCode, Response.Status> ResponseStatusMapping = new HashMap<StatusCode, Response.Status>() {
22 private static final long serialVersionUID = 1L;
24 put(StatusCode.SUCCESS, Response.Status.OK);
25 put(StatusCode.BADREQUEST, Response.Status.BAD_REQUEST);
26 put(StatusCode.UNAUTHORIZED, Response.Status.UNAUTHORIZED);
27 put(StatusCode.FORBIDDEN, Response.Status.FORBIDDEN);
28 put(StatusCode.NOTFOUND, Response.Status.NOT_FOUND);
29 put(StatusCode.NOTALLOWED, Response.Status.FORBIDDEN);
30 put(StatusCode.NOTACCEPTABLE, Response.Status.NOT_ACCEPTABLE);
31 put(StatusCode.TIMEOUT, Response.Status.GONE);
32 put(StatusCode.CONFLICT, Response.Status.CONFLICT);
33 put(StatusCode.GONE, Response.Status.GONE);
34 put(StatusCode.UNSUPPORTED, Response.Status.BAD_REQUEST);
35 put(StatusCode.INTERNALERROR, Response.Status.INTERNAL_SERVER_ERROR);
36 put(StatusCode.NOTIMPLEMENTED, Response.Status.NOT_ACCEPTABLE);
37 put(StatusCode.NOSERVICE, Response.Status.SERVICE_UNAVAILABLE);
38 put(StatusCode.UNDEFINED, Response.Status.BAD_REQUEST);
42 private static final String AUDIT = "audit";
44 private static final Logger logger = LoggerFactory.getLogger(AUDIT);
46 // Suppress default constructor for noninstantiability
47 private NorthboundUtils() {
51 * Returns Response.Status for a given status. If the status is null or if
52 * the corresponding StatusCode is not present in the ResponseStatusMapping,
57 * @return The Response.Status for a given status
59 public static Response.Status getResponseStatus(Status status) {
60 return ResponseStatusMapping.get(status.getCode());
64 * Returns Response for a given status. If the status provided is null or if
65 * the corresponding StatusCode is not present in the ResponseStatusMapping,
66 * it returns Response with StatusType as INTERNAL_SERVER_ERROR.
70 * @return The Response for a given status.
72 public static Response getResponse(Status status) {
73 if ((status == null) || (!ResponseStatusMapping.containsKey(status.getCode()))) {
74 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Action Result Unknown").build();
76 return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build();
80 * Returns whether the current user has the required privilege on the
85 * @param containerName
88 * Operation to be performed - READ/WRITE
90 * Class from where the function is invoked
91 * @return The Status of the request, either Success or Unauthorized
93 public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) {
95 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
96 IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle);
100 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKADMIN.ordinal());
102 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKOPERATOR.ordinal());
108 IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
109 IContainerAuthorization.class, bundle);
115 Privilege current = auth.getResourcePrivilege(userName, containerName);
116 if (required.ordinal() > current.ordinal()) {
123 public static void auditlog(String moduleName, String user, String action, String resource,
124 String containerName) {
125 String auditMsg = "";
126 String mode = "REST";
127 if (containerName != null) {
128 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource + " in container "
131 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource;
133 logger.info(auditMsg);
136 public static void auditlog(String moduleName, String user, String action, String resource) {
137 auditlog(moduleName, user, action, resource, null);