Merge "Prevent ConfigPusher from killing its thread"
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWebUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.web;
9
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;
22
23 public class DaylightWebUtil {
24
25     private static final String AUDIT = "audit";
26     private static final Logger logger = LoggerFactory.getLogger(AUDIT);
27
28     /**
29      * Returns the access privilege the user has on the specified container
30      *
31      * @param userName
32      *            The user name
33      * @param container
34      *            The container name. If null, the default container will be assumed
35      * @param bundle
36      *            The bundle originating the request
37      * @return The access privilege the user is granted on the container
38      */
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;
43
44         // Retrieve the Container Authorization service
45         IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper
46                 .getGlobalInstance(IContainerAuthorization.class, bundle);
47         if (auth != null) {
48             return auth.getResourcePrivilege(userName, resource);
49         }
50
51         /*
52          * Container Authorization service not available. We can only derive the
53          * access privilege to the default container based on user level
54          */
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)) {
60                 case NETWORKADMIN:
61                     return Privilege.WRITE;
62                 case NETWORKOPERATOR:
63                     return Privilege.READ;
64                 default:
65                     return Privilege.NONE;
66                 }
67             }
68         }
69
70         return Privilege.NONE;
71     }
72
73     public static void auditlog(String moduleName, String user, String action, String resource,
74             String containerName) {
75         String auditMsg = "";
76         String mode = "UI";
77         if (containerName != null) {
78             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource + " in container "
79                     + containerName;
80         } else {
81             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource;
82         }
83         logger.trace(auditMsg);
84     }
85
86     public static void auditlog(String moduleName, String user, String action, String resource) {
87         auditlog(moduleName, user, action, resource, null);
88     }
89
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;
96     }
97
98     public static String getNodeDesc(Node node, String containerName,
99             Object bundle) {
100         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
101                 .getInstance(ISwitchManager.class, containerName, bundle);
102         if (switchManager == null) {
103             return null;
104         }
105
106         return getNodeDesc(node, switchManager);
107     }
108
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) {
114             return null;
115         }
116
117         return getNodeDesc(node, switchManager);
118     }
119
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);
125     }
126
127     public static String getPortName(NodeConnector nodeConnector, Object bundle) {
128         return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
129     }
130
131     public static String getPortName(NodeConnector nodeConnector,
132             ISwitchManager switchManager) {
133         Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
134                 Name.NamePropName));
135         String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
136         nodeConnectorName = nodeConnectorName + "@"
137                 + getNodeDesc(nodeConnector.getNode(), switchManager);
138         return nodeConnectorName.substring(0, nodeConnectorName.length());
139     }
140 }