Bug fix for ForwardingRulesManager:
[controller.git] / opendaylight / web / topology / src / main / java / org / opendaylight / controller / topology / web / Topology.java
index 33669128f3f4fad6ec490ffd3957ec8674f7375d..285e33f53b91690ef7711bd1352b8956da90a63c 100644 (file)
@@ -19,6 +19,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.opendaylight.controller.sal.authorization.UserLevel;
 import org.opendaylight.controller.sal.core.Bandwidth;
 import org.opendaylight.controller.sal.core.Edge;
@@ -33,7 +35,6 @@ import org.opendaylight.controller.switchmanager.Switch;
 import org.opendaylight.controller.switchmanager.SwitchConfig;
 import org.opendaylight.controller.topologymanager.ITopologyManager;
 import org.opendaylight.controller.usermanager.IUserManager;
-import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -50,7 +51,7 @@ import edu.uci.ics.jung.graph.SparseMultigraph;
 public class Topology {
 
     protected Map<String, Map<String, Object>> cache = new HashMap<String, Map<String, Object>>();
-    protected Map<String, Map<String, Object>> stage;
+    protected Map<String, Map<String, Object>> stagedNodes;
     protected Map<String, Map<String, Object>> newNodes;
     protected Integer nodeHash = null;
     protected Integer hostHash = null;
@@ -82,7 +83,7 @@ public class Topology {
         List<SwitchConfig> switchConfigurations = new ArrayList<SwitchConfig>();
         for(Switch sw : nodes) {
                Node n = sw.getNode();
-               SwitchConfig config = switchManager.getSwitchConfig(n.getNodeIDString());
+               SwitchConfig config = switchManager.getSwitchConfig(n.toString());
                switchConfigurations.add(config);
         }
         
@@ -100,7 +101,7 @@ public class Topology {
         nodeSingleHash = nodes.hashCode();
         nodeConfigurationHash = switchConfigurations.hashCode();
         
-        stage = new HashMap<String, Map<String, Object>>();
+        stagedNodes = new HashMap<String, Map<String, Object>>();
         newNodes = new HashMap<String, Map<String, Object>>();
 
         // nodeEdges addition
@@ -130,8 +131,8 @@ public class Topology {
         
         for (Map.Entry<Node, Set<Edge>> e : nodeEdges.entrySet()) {
             Node n = e.getKey();
-            SwitchConfig config = switchManager.getSwitchConfig(n.getNodeIDString());
-            NodeBean node = createNodeBean(config, n);
+            String description = switchManager.getNodeDescription(n);
+            NodeBean node = createNodeBean(description, n);
             
             List<Map<String, Object>> adjacencies = new LinkedList<Map<String, Object>>();
             Set<Edge> links = e.getValue();
@@ -148,45 +149,51 @@ public class Topology {
             
             node.setLinks(adjacencies);
             if (cache.containsKey(node.id())) {
+               // retrieve node from cache
                Map<String, Object> nodeEntry = cache.get(node.id());
-               if (config != null) {
-                       Map<String, String> data = (Map<String, String>) nodeEntry.get("data");
-                       data.put("$desc", config.getNodeDescription());
-                       nodeEntry.put("data", data);
-               }
-               stage.put(node.id(), nodeEntry);
+
+                       Map<String, String> data = (Map<String, String>) nodeEntry.get("data");
+                       data.put("$desc", description);
+                       nodeEntry.put("data", data);
+                       
+               // always update adjacencies
+               nodeEntry.put("adjacencies", adjacencies);
+               // stage this cached node (with position)
+               stagedNodes.put(node.id(), nodeEntry);
             } else {
                newNodes.put(node.id(), node.out());
             }
         }
     }
     
-    protected NodeBean createNodeBean(SwitchConfig config, Node node) {
-       NodeBean bean = null;
-       if (config != null) {
-               bean = new NodeBean(node.toString(), config.getNodeDescription(), NodeType.NODE);
-       } else {
-               bean = new NodeBean(node.toString(), node.toString(), NodeType.NODE);
-       }
-       
-       return bean;
+    protected NodeBean createNodeBean(String description, Node node) {
+       String name = (description == null || 
+                       description.trim().isEmpty() ||
+                       description.equalsIgnoreCase("none"))?
+                                       node.toString() : description;
+               return  new NodeBean(node.toString(), name, NodeType.NODE);
     }
     
-    private void addSingleNodes(List<Switch> nodes, ISwitchManager switchManager) {
+    @SuppressWarnings("unchecked")
+       private void addSingleNodes(List<Switch> nodes, ISwitchManager switchManager) {
        if (nodes == null) return;
        for (Switch sw : nodes) {
                Node n = sw.getNode();
-               SwitchConfig config = switchManager.getSwitchConfig(n.getNodeIDString());
-               if (cache.containsKey(n.toString()) || newNodes.containsKey(n.toString())) continue;
-               NodeBean node = createNodeBean(config, n);
-               if (cache.containsKey(node.id())) {
+
+               String description = switchManager.getNodeDescription(n);
+               
+               if ((stagedNodes.containsKey(n.toString()) && cache.containsKey(n.toString())) || newNodes.containsKey(n.toString())) {
+                       continue;
+               }
+               NodeBean node = createNodeBean(description, n);
+               
+               // FIXME still doesn't display standalone node when last remaining link is removed
+               if (cache.containsKey(node.id()) && !stagedNodes.containsKey(node.id())) {
                        Map<String, Object> nodeEntry = cache.get(node.id());
-                       if (config != null) {
-                               Map<String, String> data = (Map<String, String>) nodeEntry.get("data");
-                       data.put("$desc", config.getNodeDescription());
-                       nodeEntry.put("data", data);
-                       }
-               stage.put(node.id(), nodeEntry);
+                               Map<String, String> data = (Map<String, String>) nodeEntry.get("data");
+                       data.put("$desc", description);
+                       nodeEntry.put("data", data);
+               stagedNodes.put(node.id(), nodeEntry);
             } else {
                newNodes.put(node.id(), node.out());
             }
@@ -212,15 +219,18 @@ public class Topology {
                 addressByteBuffer.rewind();
                 
                 long hid = addressByteBuffer.getLong();
+                String hostId = String.valueOf(hid);
                 
-                NodeBean hostBean = new NodeBean(String.valueOf(hid), host.getNetworkAddressAsString(), NodeType.HOST);
+                NodeBean hostBean = new NodeBean(hostId, host.getNetworkAddressAsString(), NodeType.HOST);
                 List<Map<String, Object>> adjacencies = new LinkedList<Map<String, Object>>();
                 EdgeBean edge = new EdgeBean(connector, hid);
                 adjacencies.add(edge.out());
                 hostBean.setLinks(adjacencies);
                 
-                if (cache.containsKey(String.valueOf(hid))) {
-                       stage.put(String.valueOf(hid), cache.get(String.valueOf(hid)));
+                if (cache.containsKey(hostId)) {
+                       Map<String, Object> hostEntry = cache.get(hostId);
+                       hostEntry.put("adjacencies", adjacencies);
+                       stagedNodes.put(hostId, hostEntry);
                 } else {
                        newNodes.put(String.valueOf(hid), hostBean.out());
                 }
@@ -234,7 +244,7 @@ public class Topology {
     private void repositionTopology() {
         Graph<String, String> graph = new SparseMultigraph<String, String>();
         cache.clear();
-        cache.putAll(stage);
+        cache.putAll(stagedNodes);
         cache.putAll(newNodes);
         for (Map<String, Object> on : cache.values()) {
             graph.addVertex(on.toString());
@@ -249,7 +259,7 @@ public class Topology {
             }
         }
         
-        CircleLayout layout = new CircleLayout(graph);
+        CircleLayout<String,String> layout = new CircleLayout<String,String>(graph);
         layout.setSize(new Dimension(1200, 365));
         for (Map.Entry<String, Map<String, Object>> v : newNodes.entrySet()) {
             Double x = layout.transform(v.getKey()).getX();
@@ -274,8 +284,8 @@ public class Topology {
     @RequestMapping(value = "/node/{nodeId}", method = RequestMethod.POST)
     @ResponseBody
     public Map<String, Object> post(@PathVariable String nodeId, @RequestParam(required = true) String x,
-               @RequestParam(required = true) String y) {
-       if (!authorize(UserLevel.NETWORKADMIN)) {
+               @RequestParam(required = true) String y, HttpServletRequest request) {
+       if (!authorize(UserLevel.NETWORKADMIN, request)) {
                return new HashMap<String, Object>(); // silently disregard new node position
        }
        
@@ -427,14 +437,14 @@ public class Topology {
        public static final String HOST = "host";
     }
     
-    private boolean authorize(UserLevel level) {
+    private boolean authorize(UserLevel level, HttpServletRequest request) {
        IUserManager userManager = (IUserManager) ServiceHelper
                 .getGlobalInstance(IUserManager.class, this);
         if (userManager == null) {
                return false;
         }
         
-        String username = SecurityContextHolder.getContext().getAuthentication().getName();
+        String username = request.getUserPrincipal().getName();
         UserLevel userLevel = userManager.getUserLevel(username);
         if (userLevel.toNumber() <= level.toNumber()) {
                return true;