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.web;
10 import org.opendaylight.controller.containermanager.IContainerAuthorization;
11 import org.opendaylight.controller.sal.authorization.Privilege;
12 import org.opendaylight.controller.sal.core.Description;
13 import org.opendaylight.controller.sal.core.Name;
14 import org.opendaylight.controller.sal.core.Node;
15 import org.opendaylight.controller.sal.core.NodeConnector;
16 import org.opendaylight.controller.sal.utils.GlobalConstants;
17 import org.opendaylight.controller.sal.utils.ServiceHelper;
18 import org.opendaylight.controller.switchmanager.ISwitchManager;
19 import org.opendaylight.controller.usermanager.IUserManager;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class DaylightWebUtil {
25 private static final String AUDIT = "audit";
26 private static final Logger logger = LoggerFactory.getLogger(AUDIT);
29 * Returns the access privilege the user has on the specified container
34 * The container name. If null, the default container will be assumed
36 * The bundle originating the request
37 * @return The access privilege the user is granted on the container
39 public static Privilege getContainerPrivilege(String userName,
40 String container, Object bundle) {
41 // Derive the target resource
42 String resource = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
44 // Retrieve the Container Authorization service
45 IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper
46 .getGlobalInstance(IContainerAuthorization.class, bundle);
48 return auth.getResourcePrivilege(userName, resource);
52 * Container Authorization service not available. We can only derive the
53 * access privilege to the default container based on user level
55 if (resource.equals(GlobalConstants.DEFAULT.toString())) {
56 IUserManager userManager = (IUserManager) ServiceHelper
57 .getGlobalInstance(IUserManager.class, bundle);
58 if (userManager != null) {
59 switch (userManager.getUserLevel(userName)) {
61 return Privilege.WRITE;
63 return Privilege.READ;
65 return Privilege.NONE;
70 return Privilege.NONE;
73 public static void auditlog(String moduleName, String user, String action, String resource,
74 String containerName) {
77 if (containerName != null) {
78 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource + " in container "
81 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource;
83 logger.trace(auditMsg);
86 public static void auditlog(String moduleName, String user, String action, String resource) {
87 auditlog(moduleName, user, action, resource, null);
90 public static String getNodeDesc(Node node, ISwitchManager switchManager) {
91 Description desc = (Description) switchManager.getNodeProp(node,
92 Description.propertyName);
93 String description = (desc == null) ? "" : desc.getValue();
94 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node
95 .toString() : description;
98 public static String getNodeDesc(Node node, String containerName,
100 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
101 .getInstance(ISwitchManager.class, containerName, bundle);
102 if (switchManager == null) {
106 return getNodeDesc(node, switchManager);
109 public static String getNodeDesc(Node node, Object bundle) {
110 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
111 .getInstance(ISwitchManager.class,
112 GlobalConstants.DEFAULT.toString(), bundle);
113 if (switchManager == null) {
117 return getNodeDesc(node, switchManager);
120 public static String getPortName(NodeConnector nodeConnector,
121 String container, Object bundle) {
122 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
123 .getInstance(ISwitchManager.class, container, bundle);
124 return getPortName(nodeConnector, switchManager);
127 public static String getPortName(NodeConnector nodeConnector, Object bundle) {
128 return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
131 public static String getPortName(NodeConnector nodeConnector,
132 ISwitchManager switchManager) {
133 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
135 String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
136 nodeConnectorName = nodeConnectorName + "@"
137 + getNodeDesc(nodeConnector.getNode(), switchManager);
138 return nodeConnectorName.substring(0, nodeConnectorName.length());