1 package org.opendaylight.controller.web;
3 import org.opendaylight.controller.containermanager.IContainerAuthorization;
4 import org.opendaylight.controller.sal.authorization.Privilege;
5 import org.opendaylight.controller.sal.core.Description;
6 import org.opendaylight.controller.sal.core.Name;
7 import org.opendaylight.controller.sal.core.Node;
8 import org.opendaylight.controller.sal.core.NodeConnector;
9 import org.opendaylight.controller.sal.utils.GlobalConstants;
10 import org.opendaylight.controller.sal.utils.ServiceHelper;
11 import org.opendaylight.controller.switchmanager.ISwitchManager;
12 import org.opendaylight.controller.usermanager.IUserManager;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 public class DaylightWebUtil {
18 private static final String AUDIT = "audit";
19 private static final Logger logger = LoggerFactory.getLogger(AUDIT);
22 * Returns the access privilege the user has on the specified container
27 * The container name. If null, the default container will be assumed
29 * The bundle originating the request
30 * @return The access privilege the user is granted on the container
32 public static Privilege getContainerPrivilege(String userName,
33 String container, Object bundle) {
34 // Derive the target resource
35 String resource = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
37 // Retrieve the Container Authorization service
38 IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper
39 .getGlobalInstance(IContainerAuthorization.class, bundle);
41 return auth.getResourcePrivilege(userName, resource);
45 * Container Authorization service not available. We can only derive the
46 * access privilege to the default container based on user level
48 if (resource.equals(GlobalConstants.DEFAULT.toString())) {
49 IUserManager userManager = (IUserManager) ServiceHelper
50 .getGlobalInstance(IUserManager.class, bundle);
51 if (userManager != null) {
52 switch (userManager.getUserLevel(userName)) {
54 return Privilege.WRITE;
56 return Privilege.READ;
58 return Privilege.NONE;
63 return Privilege.NONE;
66 public static void auditlog(String moduleName, String user, String action, String resource,
67 String containerName) {
70 if (containerName != null) {
71 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource + " in container "
74 auditMsg = "Mode: " + mode + " User " + user + " " + action + " " + moduleName + " " + resource;
76 logger.trace(auditMsg);
79 public static void auditlog(String moduleName, String user, String action, String resource) {
80 auditlog(moduleName, user, action, resource, null);
83 public static String getNodeDesc(Node node, ISwitchManager switchManager) {
84 Description desc = (Description) switchManager.getNodeProp(node,
85 Description.propertyName);
86 String description = (desc == null) ? "" : desc.getValue();
87 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node
88 .toString() : description;
91 public static String getNodeDesc(Node node, String containerName,
93 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
94 .getInstance(ISwitchManager.class, containerName, bundle);
95 if (switchManager == null) {
99 return getNodeDesc(node, switchManager);
102 public static String getNodeDesc(Node node, Object bundle) {
103 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
104 .getInstance(ISwitchManager.class,
105 GlobalConstants.DEFAULT.toString(), bundle);
106 if (switchManager == null) {
110 return getNodeDesc(node, switchManager);
113 public static String getPortName(NodeConnector nodeConnector,
114 String container, Object bundle) {
115 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
116 .getInstance(ISwitchManager.class, container, bundle);
117 return getPortName(nodeConnector, switchManager);
120 public static String getPortName(NodeConnector nodeConnector, Object bundle) {
121 return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
124 public static String getPortName(NodeConnector nodeConnector,
125 ISwitchManager switchManager) {
126 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
128 String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
129 nodeConnectorName = nodeConnectorName + "@"
130 + getNodeDesc(nodeConnector.getNode(), switchManager);
131 return nodeConnectorName.substring(0, nodeConnectorName.length());