Merge "Reorder public/private modifiers as per JLS. (fixes sonar warnings)"
[controller.git] / opendaylight / web / devices / src / main / java / org / opendaylight / controller / devices / web / Devices.java
index 534c2c293c9c9fecc9bf8467fc74d0e237261ac9..6ca60e857d8a86963be038ca1c4714050d7bff39 100644 (file)
@@ -8,33 +8,33 @@
 
 package org.opendaylight.controller.devices.web;
 
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;
 import java.util.concurrent.ConcurrentMap;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.codehaus.jackson.map.ObjectMapper;
-import org.opendaylight.controller.web.DaylightWebUtil;
-import org.opendaylight.controller.web.IDaylightWeb;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseBody;
 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
 import org.opendaylight.controller.sal.authorization.Privilege;
 import org.opendaylight.controller.sal.authorization.UserLevel;
 import org.opendaylight.controller.sal.core.Config;
+import org.opendaylight.controller.sal.core.Description;
+import org.opendaylight.controller.sal.core.ForwardingMode;
 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.core.Property;
+import org.opendaylight.controller.sal.core.State;
 import org.opendaylight.controller.sal.core.Tier;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
 import org.opendaylight.controller.sal.utils.HexEncode;
@@ -46,16 +46,24 @@ import org.opendaylight.controller.switchmanager.SpanConfig;
 import org.opendaylight.controller.switchmanager.SubnetConfig;
 import org.opendaylight.controller.switchmanager.Switch;
 import org.opendaylight.controller.switchmanager.SwitchConfig;
+import org.opendaylight.controller.web.DaylightWebUtil;
+import org.opendaylight.controller.web.IDaylightWeb;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
 
 import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
 
 @Controller
 @RequestMapping("/")
 public class Devices implements IDaylightWeb {
     private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
-    private final String WEB_NAME = "Devices";
-    private final String WEB_ID = "devices";
-    private final short WEB_ORDER = 1;
+    private static final String WEB_NAME = "Devices";
+    private static final String WEB_ID = "devices";
+    private static final short WEB_ORDER = 1;
 
     public Devices() {
         ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
@@ -93,59 +101,67 @@ public class Devices implements IDaylightWeb {
         String userName = request.getUserPrincipal().getName();
         Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
 
-        ISwitchManager switchManager = (ISwitchManager) ServiceHelper
-                .getInstance(ISwitchManager.class, containerName, this);
+        ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
+                this);
         List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
         if (switchManager != null && privilege != Privilege.NONE) {
             for (Switch device : switchManager.getNetworkDevices()) {
                 HashMap<String, String> nodeDatum = new HashMap<String, String>();
                 Node node = device.getNode();
-                Tier tier = (Tier) switchManager.getNodeProp(node,
-                        Tier.TierPropName);
-
+                Tier tier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName);
                 nodeDatum.put("containerName", containerName);
-                nodeDatum.put("nodeName",
-                        switchManager.getNodeDescription(node));
+                Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
+                String desc = (description == null) ? "" : description.getValue();
+                nodeDatum.put("nodeName", desc);
                 nodeDatum.put("nodeId", node.toString());
-                int tierNumber = (tier == null) ? TierHelper.unknownTierNumber
-                        : tier.getValue();
-                nodeDatum.put("tierName", TierHelper.getTierName(tierNumber)
-                        + " (Tier-" + tierNumber + ")");
+                int tierNumber = (tier == null) ? TierHelper.unknownTierNumber : tier.getValue();
+                nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) + " (Tier-" + tierNumber + ")");
                 nodeDatum.put("tier", tierNumber + "");
-                SwitchConfig sc = switchManager.getSwitchConfig(device
-                        .getNode().toString());
-                String modeStr = (sc != null) ? sc.getMode() : "0";
+                String modeStr = "0";
+                ForwardingMode mode = null;
+                if (!containerName.equals(GlobalConstants.DEFAULT.toString())) {
+                    ISwitchManager switchManagerDefault = (ISwitchManager) ServiceHelper.getInstance(
+                            ISwitchManager.class, GlobalConstants.DEFAULT.toString(), this);
+                    mode = (ForwardingMode) switchManagerDefault.getNodeProp(node, ForwardingMode.name);
+                } else {
+                    mode = (ForwardingMode) switchManager.getNodeProp(node, ForwardingMode.name);
+                }
+                if (mode != null) {
+                    modeStr = String.valueOf(mode.getValue());
+                }
                 nodeDatum.put("mode", modeStr);
 
                 nodeDatum.put("json", gson.toJson(nodeDatum));
-                nodeDatum.put("mac", HexEncode.bytesToHexString(device
-                        .getDataLayerAddress()));
+                nodeDatum.put("mac", HexEncode.bytesToHexString(device.getDataLayerAddress()));
                 StringBuffer sb1 = new StringBuffer();
-                Set<NodeConnector> nodeConnectorSet = device
-                        .getNodeConnectors();
+                Set<NodeConnector> nodeConnectorSet = device.getNodeConnectors();
                 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
                     Map<Short, String> portList = new HashMap<Short, String>();
                     List<String> intfList = new ArrayList<String>();
                     for (NodeConnector nodeConnector : nodeConnectorSet) {
-                        String nodeConnectorNumberToStr = nodeConnector.getID()
-                                .toString();
-                        Name ncName = ((Name) switchManager
-                                .getNodeConnectorProp(nodeConnector,
-                                        Name.NamePropName));
+                        String nodeConnectorNumberToStr = nodeConnector.getID().toString();
+                        Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
                         Config portStatus = ((Config) switchManager
                                 .getNodeConnectorProp(nodeConnector,
                                         Config.ConfigPropName));
-
+                        State portState = ((State) switchManager
+                                .getNodeConnectorProp(nodeConnector,
+                                        State.StatePropName));
                         String nodeConnectorName = (ncName != null) ? ncName
                                 .getValue() : "";
                         nodeConnectorName += " (" + nodeConnector.getID() + ")";
 
                         if (portStatus != null) {
                             if (portStatus.getValue() == Config.ADMIN_UP) {
-                                nodeConnectorName = "<span style='color:green;'>"
-                                        + nodeConnectorName + "</span>";
+                                if (portState.getValue() == State.EDGE_UP) {
+                                    nodeConnectorName = "<span class='admin-up'>"
+                                            + nodeConnectorName + "</span>";
+                                } else if (portState.getValue() == State.EDGE_DOWN) {
+                                    nodeConnectorName = "<span class='edge-down'>"
+                                            + nodeConnectorName + "</span>";
+                                }
                             } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
-                                nodeConnectorName = "<span style='color:red;'>"
+                                nodeConnectorName = "<span class='admin-down'>"
                                         + nodeConnectorName + "</span>";
                             }
                         }
@@ -224,11 +240,25 @@ public class Devices implements IDaylightWeb {
         try {
             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                     .getInstance(ISwitchManager.class, containerName, this);
-            SwitchConfig cfg = new SwitchConfig(nodeId, nodeName, tier,
-                    operationMode);
-            switchManager.updateSwitchConfig(cfg);
-            resultBean.setStatus(true);
-            resultBean.setMessage("Updated node information successfully");
+            Map<String, Property> nodeProperties = new HashMap<String, Property>();
+            Property desc = new Description(nodeName);
+            nodeProperties.put(desc.getName(), desc);
+            Property nodeTier = new Tier(Integer.parseInt(tier));
+            nodeProperties.put(nodeTier.getName(), nodeTier);
+            if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
+                Property mode = new ForwardingMode(Integer.parseInt(operationMode));
+                nodeProperties.put(mode.getName(), mode);
+            }
+            SwitchConfig cfg = new SwitchConfig(nodeId, nodeProperties);
+            Status result = switchManager.updateNodeConfig(cfg);
+            if (!result.isSuccess()) {
+                resultBean.setStatus(false);
+                resultBean.setMessage(result.getDescription());
+            } else {
+                resultBean.setStatus(true);
+                resultBean.setMessage("Updated node information successfully");
+                DaylightWebUtil.auditlog("Node", userName, "updated", nodeId + " to "+ nodeName, containerName);
+            }
         } catch (Exception e) {
             resultBean.setStatus(false);
             resultBean.setMessage("Error updating node information. "
@@ -310,6 +340,7 @@ public class Devices implements IDaylightWeb {
             if (addStaticRouteResult.isSuccess()) {
                 result.setStatus(true);
                 result.setMessage("Static Route saved successfully");
+                DaylightWebUtil.auditlog("Static Route", userName, "added", routeName, containerName);
             } else {
                 result.setStatus(false);
                 result.setMessage(addStaticRouteResult.getDescription());
@@ -353,6 +384,7 @@ public class Devices implements IDaylightWeb {
                     resultBean.setMessage(result.getDescription());
                     break;
                 }
+                DaylightWebUtil.auditlog("Static Route", userName, "removed", route, containerName);
             }
         } catch (Exception e) {
             resultBean.setStatus(false);
@@ -369,23 +401,35 @@ public class Devices implements IDaylightWeb {
             @RequestParam(required = false) String container) {
         Gson gson = new Gson();
         List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
-        String containerName = (container == null) ? GlobalConstants.DEFAULT
-                .toString() : container;
+        String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
 
         // Derive the privilege this user has on the current container
         String userName = request.getUserPrincipal().getName();
-        Privilege privilege = DaylightWebUtil.getContainerPrivilege(
-                userName, containerName, this);
+        Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
 
         if (privilege != Privilege.NONE) {
-            ISwitchManager switchManager = (ISwitchManager) ServiceHelper
-                    .getInstance(ISwitchManager.class, containerName, this);
+            ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
+                    containerName, this);
             if (switchManager != null) {
                 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
                     Map<String, String> subnet = new HashMap<String, String>();
                     subnet.put("name", conf.getName());
                     subnet.put("subnet", conf.getSubnet());
-                    subnet.put("json", gson.toJson(conf));
+                    List<SubnetGatewayPortBean> portsList = new ArrayList<SubnetGatewayPortBean>();
+                    Iterator<NodeConnector> itor = conf.getNodeConnectors().iterator();
+                    while(itor.hasNext()) {
+                        SubnetGatewayPortBean bean = new SubnetGatewayPortBean();
+                        NodeConnector nodeConnector = itor.next();
+                        String nodeName = getNodeDesc(nodeConnector.getNode().toString(), containerName);
+                        Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
+                        String nodeConnectorName = (ncName != null) ? ncName.getValue() : "";
+                        bean.setNodeName(nodeName);
+                        bean.setNodePortName(nodeConnectorName);
+                        bean.setNodeId(nodeConnector.getNode().toString());
+                        bean.setNodePortId(nodeConnector.toString());
+                        portsList.add(bean);
+                    }
+                    subnet.put("nodePorts", gson.toJson(portsList));
                     subnets.add(subnet);
                 }
             }
@@ -423,6 +467,7 @@ public class Devices implements IDaylightWeb {
             if (result.isSuccess()) {
                 resultBean.setStatus(true);
                 resultBean.setMessage("Added gateway address successfully");
+                DaylightWebUtil.auditlog("Subnet Gateway", userName, "added", gatewayName, containerName);
             } else {
                 resultBean.setStatus(false);
                 resultBean.setMessage(result.getDescription());
@@ -463,6 +508,7 @@ public class Devices implements IDaylightWeb {
                     resultBean.setMessage(result.getDescription());
                     break;
                 }
+                DaylightWebUtil.auditlog("Subnet Gateway", userName, "removed", subnet, containerName);
             }
         } catch (Exception e) {
             resultBean.setStatus(false);
@@ -491,13 +537,17 @@ public class Devices implements IDaylightWeb {
         try {
             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                     .getInstance(ISwitchManager.class, containerName, this);
-            Status result = switchManager.addPortsToSubnet(portsName, nodeId
-                    + "/" + ports);
+            List<String> toAdd = new ArrayList<String>();
+            for (String port : ports.split(",")) {
+                toAdd.add(port);
+            }
+            Status result = switchManager.addPortsToSubnet(portsName, toAdd);
 
             if (result.isSuccess()) {
                 resultBean.setStatus(true);
                 resultBean
                         .setMessage("Added ports to subnet gateway address successfully");
+                DaylightWebUtil.auditlog("Ports to Subnet Gateway", userName, "added", ports, containerName);
             } else {
                 resultBean.setStatus(false);
                 resultBean.setMessage(result.getDescription());
@@ -529,13 +579,16 @@ public class Devices implements IDaylightWeb {
         try {
             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                     .getInstance(ISwitchManager.class, containerName, this);
-            Status result = switchManager.removePortsFromSubnet(gatewayName,
-                    nodePort);
+            List<String> toRemove = new ArrayList<String>();
+            for (String port : nodePort.split(",")) {
+                toRemove.add(port);
+            }
+            Status result = switchManager.removePortsFromSubnet(gatewayName, toRemove);
 
             if (result.isSuccess()) {
                 resultBean.setStatus(true);
-                resultBean
-                        .setMessage("Deleted port from subnet gateway address successfully");
+                resultBean.setMessage("Deleted port from subnet gateway address successfully");
+                DaylightWebUtil.auditlog("Ports from Subnet Gateway", userName, "removed", nodePort, containerName);
             } else {
                 resultBean.setStatus(false);
                 resultBean.setMessage(result.getDescription());
@@ -580,9 +633,14 @@ public class Devices implements IDaylightWeb {
                     Map<String, String> config = new HashMap<String, String>();
                     for (String name : config_data.keySet()) {
                         config.put(name, config_data.get(name));
-                        // Add switch name value (non-configuration field)
+                        // Add switch portName value (non-configuration field)
                         config.put("nodeName",
                                 getNodeDesc(config_data.get("nodeId"), containerName));
+                        NodeConnector spanPortNodeConnector = NodeConnector.fromString(config_data.get("spanPort"));
+                        Name ncName = ((Name) switchManager.getNodeConnectorProp(spanPortNodeConnector,
+                                Name.NamePropName));
+                        String spanPortName = (ncName != null) ? ncName.getValue() : "";
+                        config.put("spanPortName", spanPortName);
                     }
                     config.put("json", config_json);
                     spanConfigs.add(config);
@@ -601,10 +659,8 @@ public class Devices implements IDaylightWeb {
 
     @RequestMapping(value = "/nodeports")
     @ResponseBody
-    public Map<String, Object> getNodePorts(HttpServletRequest request,
-            @RequestParam(required = false) String container) {
-        String containerName = (container == null) ? GlobalConstants.DEFAULT
-                .toString() : container;
+    public String getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
+        String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
 
         // Derive the privilege this user has on the current container
         String userName = request.getUserPrincipal().getName();
@@ -612,34 +668,31 @@ public class Devices implements IDaylightWeb {
             return null;
         }
 
-
         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                 .getInstance(ISwitchManager.class, containerName, this);
         if (switchManager == null) {
             return null;
         }
-
-        Map<String, Object> nodes = new HashMap<String, Object>();
-        Map<Short, String> port;
+        List<NodeJsonBean> nodeJsonBeans = new ArrayList<NodeJsonBean>();
 
         for (Switch node : switchManager.getNetworkDevices()) {
-            port = new HashMap<Short, String>(); // new port
+            NodeJsonBean nodeJsonBean = new NodeJsonBean();
+            List<PortJsonBean> port = new ArrayList<PortJsonBean>();
             Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
-
             if (nodeConnectorSet != null) {
                 for (NodeConnector nodeConnector : nodeConnectorSet) {
                     String nodeConnectorName = ((Name) switchManager
-                            .getNodeConnectorProp(nodeConnector,
-                                    Name.NamePropName)).getValue();
-                    port.put((Short) nodeConnector.getID(), nodeConnectorName
-                            + "(" + nodeConnector.getID() + ")");
+                            .getNodeConnectorProp(nodeConnector, Name.NamePropName)).getValue();
+                    port.add(new PortJsonBean(nodeConnector.getID().toString(), nodeConnectorName, nodeConnector.toString()));
                 }
             }
-
-            nodes.put(node.getNode().toString(), port);
+            nodeJsonBean.setNodeId(node.getNode().toString());
+            nodeJsonBean.setNodeName(getNodeDesc(node.getNode().toString(), containerName));
+            nodeJsonBean.setNodePorts(port);
+            nodeJsonBeans.add(nodeJsonBean);
         }
 
-        return nodes;
+        return new Gson().toJson(nodeJsonBeans);
     }
 
     @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
@@ -667,6 +720,7 @@ public class Devices implements IDaylightWeb {
             if (result.isSuccess()) {
                 resultBean.setStatus(true);
                 resultBean.setMessage("SPAN Port added successfully");
+                DaylightWebUtil.auditlog("SPAN Port", userName, "added", cfgObject.getNodeId(), containerName);
             } else {
                 resultBean.setStatus(false);
                 resultBean.setMessage(result.getDescription());
@@ -699,21 +753,21 @@ public class Devices implements IDaylightWeb {
             Gson gson = new Gson();
             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                     .getInstance(ISwitchManager.class, containerName, this);
-            String[] spans = spanPortsToDelete.split("###");
-            resultBean.setStatus(true);
-            resultBean.setMessage("SPAN Port(s) deleted successfully");
-            for (String span : spans) {
-                if (!span.isEmpty()) {
-                    SpanConfig cfgObject = gson
-                            .fromJson(span, SpanConfig.class);
-                    Status result = switchManager.removeSpanConfig(cfgObject);
-                    if (!result.isSuccess()) {
-                        resultBean.setStatus(false);
-                        resultBean.setMessage(result.getDescription());
-                        break;
-                    }
+            Type collectionType = new TypeToken<List<SpanPortJsonBean>>() {}.getType();
+            List<SpanPortJsonBean> jsonBeanList = gson.fromJson(spanPortsToDelete, collectionType);
+            for (SpanPortJsonBean jsonBean : jsonBeanList) {
+                SpanConfig cfgObject = gson
+                        .fromJson(gson.toJson(jsonBean), SpanConfig.class);
+                Status result = switchManager.removeSpanConfig(cfgObject);
+                if (!result.isSuccess()) {
+                    resultBean.setStatus(false);
+                    resultBean.setMessage(result.getDescription());
+                    break;
                 }
+                DaylightWebUtil.auditlog("SPAN Port", userName, "removed", cfgObject.getNodeId(), containerName);
             }
+            resultBean.setStatus(true);
+            resultBean.setMessage("SPAN Port(s) deleted successfully");
         } catch (Exception e) {
             resultBean.setStatus(false);
             resultBean.setMessage("Error occurred while deleting span port. "
@@ -727,8 +781,10 @@ public class Devices implements IDaylightWeb {
                 .getInstance(ISwitchManager.class, containerName, this);
         String description = "";
         if (switchManager != null) {
-            description = switchManager.getNodeDescription(Node
-                    .fromString(nodeId));
+            Description desc = (Description) switchManager.getNodeProp(Node.fromString(nodeId), Description.propertyName);
+            if(desc != null) {
+                description = desc.getValue();
+            }
         }
         return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
                 : description;