2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.northbound.commons.utils;
10 import java.util.HashMap;
13 import javax.ws.rs.core.Response;
15 import org.opendaylight.controller.containermanager.IContainerAuthorization;
16 import org.opendaylight.controller.sal.authorization.Privilege;
17 import org.opendaylight.controller.sal.authorization.UserLevel;
18 import org.opendaylight.controller.sal.core.Description;
19 import org.opendaylight.controller.sal.core.Name;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.utils.GlobalConstants;
23 import org.opendaylight.controller.sal.utils.ServiceHelper;
24 import org.opendaylight.controller.sal.utils.Status;
25 import org.opendaylight.controller.sal.utils.StatusCode;
26 import org.opendaylight.controller.switchmanager.ISwitchManager;
27 import org.opendaylight.controller.usermanager.IUserManager;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NorthboundUtils {
33 private static final Map<StatusCode, Response.Status> ResponseStatusMapping = new HashMap<StatusCode, Response.Status>() {
34 private static final long serialVersionUID = 1L;
36 put(StatusCode.SUCCESS, Response.Status.OK);
37 put(StatusCode.BADREQUEST, Response.Status.BAD_REQUEST);
38 put(StatusCode.UNAUTHORIZED, Response.Status.UNAUTHORIZED);
39 put(StatusCode.FORBIDDEN, Response.Status.FORBIDDEN);
40 put(StatusCode.NOTFOUND, Response.Status.NOT_FOUND);
41 put(StatusCode.NOTALLOWED, Response.Status.FORBIDDEN);
42 put(StatusCode.NOTACCEPTABLE, Response.Status.NOT_ACCEPTABLE);
43 put(StatusCode.TIMEOUT, Response.Status.GONE);
44 put(StatusCode.CONFLICT, Response.Status.CONFLICT);
45 put(StatusCode.GONE, Response.Status.GONE);
46 put(StatusCode.UNSUPPORTED, Response.Status.BAD_REQUEST);
47 put(StatusCode.INTERNALERROR, Response.Status.INTERNAL_SERVER_ERROR);
48 put(StatusCode.NOTIMPLEMENTED, Response.Status.NOT_ACCEPTABLE);
49 put(StatusCode.NOSERVICE, Response.Status.SERVICE_UNAVAILABLE);
50 put(StatusCode.UNDEFINED, Response.Status.BAD_REQUEST);
54 private static final String AUDIT = "audit";
56 private static final Logger logger = LoggerFactory.getLogger(AUDIT);
58 // Suppress default constructor for noninstantiability
59 private NorthboundUtils() {
63 * Returns Response.Status for a given status. If the status is null or if
64 * the corresponding StatusCode is not present in the ResponseStatusMapping,
69 * @return The Response.Status for a given status
71 public static Response.Status getResponseStatus(Status status) {
72 return ResponseStatusMapping.get(status.getCode());
76 * Returns Response for a given status. If the status provided is null or if
77 * the corresponding StatusCode is not present in the ResponseStatusMapping,
78 * it returns Response with StatusType as INTERNAL_SERVER_ERROR.
82 * @return The Response for a given status.
84 public static Response getResponse(Status status) {
85 if ((status == null) || (!ResponseStatusMapping.containsKey(status.getCode()))) {
86 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Action Result Unknown").build();
88 return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build();
92 * Returns whether the current user has the required privilege on the
97 * @param containerName
100 * Operation to be performed - READ/WRITE
102 * Class from where the function is invoked
103 * @return The Status of the request, either Success or Unauthorized
105 public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) {
107 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
108 IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle);
112 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKADMIN.ordinal());
114 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKOPERATOR.ordinal());
120 IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
121 IContainerAuthorization.class, bundle);
127 Privilege current = auth.getResourcePrivilege(userName, containerName);
128 if (required.ordinal() > current.ordinal()) {
135 public static void auditlog(String moduleName, String user, String action, String resource,
136 String containerName) {
137 String auditMsg = "";
138 String mode = "REST";
139 if (containerName != null) {
140 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource + " in container "
143 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource;
145 logger.trace(auditMsg);
148 public static void auditlog(String moduleName, String user, String action, String resource) {
149 auditlog(moduleName, user, action, resource, null);
152 public static String getNodeDesc(Node node, ISwitchManager switchManager) {
153 Description desc = (Description) switchManager.getNodeProp(node,
154 Description.propertyName);
155 String description = (desc == null) ? "" : desc.getValue();
156 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node
157 .toString() : description;
160 public static String getNodeDesc(Node node, String containerName,
162 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
163 .getInstance(ISwitchManager.class, containerName, bundle);
164 if (switchManager == null) {
168 return getNodeDesc(node, switchManager);
171 public static String getNodeDesc(Node node, Object bundle) {
172 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
173 .getInstance(ISwitchManager.class,
174 GlobalConstants.DEFAULT.toString(), bundle);
175 if (switchManager == null) {
179 return getNodeDesc(node, switchManager);
182 public static String getPortName(NodeConnector nodeConnector,
183 String container, Object bundle) {
184 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
185 .getInstance(ISwitchManager.class, container, bundle);
186 return getPortName(nodeConnector, switchManager);
189 public static String getPortName(NodeConnector nodeConnector, Object bundle) {
190 return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
193 public static String getPortName(NodeConnector nodeConnector,
194 ISwitchManager switchManager) {
195 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
197 String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
198 nodeConnectorName = nodeConnectorName + "@"
199 + getNodeDesc(nodeConnector.getNode(), switchManager);
200 return nodeConnectorName.substring(0, nodeConnectorName.length());