Log all configuration(add/modify/delete) changes to a new log file audit.log
[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 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class NorthboundUtils {
20
21     private static final Map<StatusCode, Response.Status> ResponseStatusMapping = new HashMap<StatusCode, Response.Status>() {
22         private static final long serialVersionUID = 1L;
23         {
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);
39         }
40     };
41
42     private static final String AUDIT = "audit";
43
44     private static final Logger logger = LoggerFactory.getLogger(AUDIT);
45
46     // Suppress default constructor for noninstantiability
47     private NorthboundUtils() {
48     }
49
50     /**
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,
53      * it returns null.
54      *
55      * @param status
56      *            The Status
57      * @return The Response.Status for a given status
58      */
59     public static Response.Status getResponseStatus(Status status) {
60         return ResponseStatusMapping.get(status.getCode());
61     }
62
63     /**
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.
67      *
68      * @param status
69      *            The Status
70      * @return The Response for a given status.
71      */
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();
75         }
76         return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build();
77     }
78
79     /**
80      * Returns whether the current user has the required privilege on the
81      * specified container
82      *
83      * @param userName
84      *            The user name
85      * @param containerName
86      *            The container name
87      * @param required
88      *            Operation to be performed - READ/WRITE
89      * @param bundle
90      *            Class from where the function is invoked
91      * @return The Status of the request, either Success or Unauthorized
92      */
93     public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) {
94
95         if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
96             IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle);
97
98             switch (required) {
99             case WRITE:
100                 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKADMIN.ordinal());
101             case READ:
102                 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKOPERATOR.ordinal());
103             default:
104                 return false;
105             }
106
107         } else {
108             IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
109                     IContainerAuthorization.class, bundle);
110
111             if (auth == null) {
112                 return false;
113             }
114
115             Privilege current = auth.getResourcePrivilege(userName, containerName);
116             if (required.ordinal() > current.ordinal()) {
117                 return false;
118             }
119         }
120         return true;
121     }
122
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 "
129                     + containerName;
130         } else {
131             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource;
132         }
133         logger.info(auditMsg);
134     }
135
136     public static void auditlog(String moduleName, String user, String action, String resource) {
137         auditlog(moduleName, user, action, resource, null);
138     }
139 }