Fixed inappropriate uses of log level INFO
[controller.git] / opendaylight / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / utils / NorthboundUtils.java
index f98189319becc8ccae7557a7028378f38843b72e..58ba77697001b0e4d0b998098d0d17d15165a736 100644 (file)
@@ -8,11 +8,18 @@ import javax.ws.rs.core.Response;
 import org.opendaylight.controller.containermanager.IContainerAuthorization;
 import org.opendaylight.controller.sal.authorization.Privilege;
 import org.opendaylight.controller.sal.authorization.UserLevel;
+import org.opendaylight.controller.sal.core.Description;
+import org.opendaylight.controller.sal.core.Name;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.controller.sal.utils.StatusCode;
+import org.opendaylight.controller.switchmanager.ISwitchManager;
 import org.opendaylight.controller.usermanager.IUserManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class NorthboundUtils {
 
@@ -37,6 +44,10 @@ public class NorthboundUtils {
         }
     };
 
+    private static final String AUDIT = "audit";
+
+    private static final Logger logger = LoggerFactory.getLogger(AUDIT);
+
     // Suppress default constructor for noninstantiability
     private NorthboundUtils() {
     }
@@ -114,4 +125,71 @@ public class NorthboundUtils {
         return true;
     }
 
+    public static void auditlog(String moduleName, String user, String action, String resource,
+            String containerName) {
+        String auditMsg = "";
+        String mode = "REST";
+        if (containerName != null) {
+            auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource + " in container "
+                    + containerName;
+        } else {
+            auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource;
+        }
+        logger.trace(auditMsg);
+    }
+
+    public static void auditlog(String moduleName, String user, String action, String resource) {
+        auditlog(moduleName, user, action, resource, null);
+    }
+
+    public static String getNodeDesc(Node node, ISwitchManager switchManager) {
+        Description desc = (Description) switchManager.getNodeProp(node,
+                Description.propertyName);
+        String description = (desc == null) ? "" : desc.getValue();
+        return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node
+                .toString() : description;
+    }
+
+    public static String getNodeDesc(Node node, String containerName,
+            Object bundle) {
+        ISwitchManager switchManager = (ISwitchManager) ServiceHelper
+                .getInstance(ISwitchManager.class, containerName, bundle);
+        if (switchManager == null) {
+            return null;
+        }
+
+        return getNodeDesc(node, switchManager);
+    }
+
+    public static String getNodeDesc(Node node, Object bundle) {
+        ISwitchManager switchManager = (ISwitchManager) ServiceHelper
+                .getInstance(ISwitchManager.class,
+                        GlobalConstants.DEFAULT.toString(), bundle);
+        if (switchManager == null) {
+            return null;
+        }
+
+        return getNodeDesc(node, switchManager);
+    }
+
+    public static String getPortName(NodeConnector nodeConnector,
+            String container, Object bundle) {
+        ISwitchManager switchManager = (ISwitchManager) ServiceHelper
+                .getInstance(ISwitchManager.class, container, bundle);
+        return getPortName(nodeConnector, switchManager);
+    }
+
+    public static String getPortName(NodeConnector nodeConnector, Object bundle) {
+        return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
+    }
+
+    public static String getPortName(NodeConnector nodeConnector,
+            ISwitchManager switchManager) {
+        Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
+                Name.NamePropName));
+        String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
+        nodeConnectorName = nodeConnectorName + "@"
+                + getNodeDesc(nodeConnector.getNode(), switchManager);
+        return nodeConnectorName.substring(0, nodeConnectorName.length());
+    }
 }