Merge "Change target to ${project.build.target} in a bunch of pom file. Add some...
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWebUtil.java
1 package org.opendaylight.controller.web;
2
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;
15
16 public class DaylightWebUtil {
17
18     private static final String AUDIT = "audit";
19     private static final Logger logger = LoggerFactory.getLogger(AUDIT);
20
21     /**
22      * Returns the access privilege the user has on the specified container
23      *
24      * @param userName
25      *            The user name
26      * @param container
27      *            The container name. If null, the default container will be assumed
28      * @param bundle
29      *            The bundle originating the request
30      * @return The access privilege the user is granted on the container
31      */
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;
36
37         // Retrieve the Container Authorization service
38         IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper
39                 .getGlobalInstance(IContainerAuthorization.class, bundle);
40         if (auth != null) {
41             return auth.getResourcePrivilege(userName, resource);
42         }
43
44         /*
45          * Container Authorization service not available. We can only derive the
46          * access privilege to the default container based on user level
47          */
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)) {
53                 case NETWORKADMIN:
54                     return Privilege.WRITE;
55                 case NETWORKOPERATOR:
56                     return Privilege.READ;
57                 default:
58                     return Privilege.NONE;
59                 }
60             }
61         }
62
63         return Privilege.NONE;
64     }
65
66     public static void auditlog(String moduleName, String user, String action, String resource,
67             String containerName) {
68         String auditMsg = "";
69         String mode = "UI";
70         if (containerName != null) {
71             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource + " in container "
72                     + containerName;
73         } else {
74             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource;
75         }
76         logger.trace(auditMsg);
77     }
78
79     public static void auditlog(String moduleName, String user, String action, String resource) {
80         auditlog(moduleName, user, action, resource, null);
81     }
82
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;
89     }
90
91     public static String getNodeDesc(Node node, String containerName,
92             Object bundle) {
93         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
94                 .getInstance(ISwitchManager.class, containerName, bundle);
95         if (switchManager == null) {
96             return null;
97         }
98
99         return getNodeDesc(node, switchManager);
100     }
101
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) {
107             return null;
108         }
109
110         return getNodeDesc(node, switchManager);
111     }
112
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);
118     }
119
120     public static String getPortName(NodeConnector nodeConnector, Object bundle) {
121         return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
122     }
123
124     public static String getPortName(NodeConnector nodeConnector,
125             ISwitchManager switchManager) {
126         Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
127                 Name.NamePropName));
128         String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
129         nodeConnectorName = nodeConnectorName + "@"
130                 + getNodeDesc(nodeConnector.getNode(), switchManager);
131         return nodeConnectorName.substring(0, nodeConnectorName.length());
132     }
133 }