Merge "UI support for multiple host per port"
[controller.git] / opendaylight / web / topology / src / main / java / org / opendaylight / controller / topology / web / Topology.java
index 62b64a51849ea2ee08c4b9f43cffca8439bd5cad..a6c390d799bb1fa54ac1329436ab2a3b47d2b037 100644 (file)
@@ -25,12 +25,12 @@ import java.util.Set;
 import javax.servlet.http.HttpServletRequest;
 
 import org.opendaylight.controller.configuration.IConfigurationAware;
-import org.opendaylight.controller.containermanager.IContainerAuthorization;
-import org.opendaylight.controller.sal.authorization.Resource;
-import org.opendaylight.controller.sal.authorization.UserLevel;
+import org.opendaylight.controller.sal.authorization.Privilege;
 import org.opendaylight.controller.sal.core.Bandwidth;
+import org.opendaylight.controller.sal.core.Description;
 import org.opendaylight.controller.sal.core.Edge;
 import org.opendaylight.controller.sal.core.Host;
+import org.opendaylight.controller.sal.core.Name;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.Node.NodeIDType;
 import org.opendaylight.controller.sal.core.NodeConnector;
@@ -47,9 +47,7 @@ import org.opendaylight.controller.switchmanager.ISwitchManager;
 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.opendaylight.controller.web.DaylightWebUtil;
-import org.opendaylight.controller.web.IDaylightWeb;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -92,7 +90,15 @@ public class Topology implements IObjectReader, IConfigurationAware {
     @RequestMapping(value = "/visual.json", method = RequestMethod.GET)
     @ResponseBody
     public Collection<Map<String, Object>> getLinkData(@RequestParam(required = false) String container, HttpServletRequest request) {
-        String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
+        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);
+
+        if (privilege == Privilege.NONE) {
+            return null;
+        }
 
         ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
                 .getInstance(ITopologyManager.class, containerName, this);
@@ -171,7 +177,8 @@ public class Topology implements IObjectReader, IConfigurationAware {
 
         for (Map.Entry<Node, Set<Edge>> e : nodeEdges.entrySet()) {
             Node n = e.getKey();
-            String description = switchManager.getNodeDescription(n);
+            String description = getNodeDesc(n, switchManager);
+
             NodeBean node = createNodeBean(description, n);
 
             // skip production node
@@ -191,7 +198,14 @@ public class Topology implements IObjectReader, IConfigurationAware {
                         break;
                     }
                 }
-                EdgeBean edge = new EdgeBean(link, bandwidth);
+                NodeConnector headNodeConnector = link.getHeadNodeConnector();
+                NodeConnector tailNodeConnector = link.getTailNodeConnector();
+
+                String headDescription = this.getNodeConnectorDescription(headNodeConnector, switchManager);
+                String tailDescription = this.getNodeConnectorDescription(tailNodeConnector, switchManager);
+                String headPortDescription = this.getNodeConnectorPortDescription(headNodeConnector, switchManager);
+                String tailPortDescription = this.getNodeConnectorPortDescription(tailNodeConnector, switchManager);
+                EdgeBean edge = new EdgeBean(link, bandwidth, headDescription, tailDescription, headPortDescription, tailPortDescription);
                 adjacencies.add(edge.out());
             }
 
@@ -254,11 +268,31 @@ public class Topology implements IObjectReader, IConfigurationAware {
     }
 
     protected NodeBean createNodeBean(String description, Node node) {
+        String name = this.getDescription(description, node);
+        return  new NodeBean(node.toString(), name, NodeType.NODE);
+    }
+
+    private String getDescription(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);
+                description.trim().isEmpty() ||
+                description.equalsIgnoreCase("none"))?
+                                node.toString() : description;
+        return name;
+    }
+
+    private String getNodeConnectorDescription(NodeConnector nodeConnector, ISwitchManager switchManager) {
+        Node node = nodeConnector.getNode();
+        String name = this.getDescription(getNodeDesc(node, switchManager), node);
+        return name;
+    }
+
+    private String getNodeConnectorPortDescription(NodeConnector nodeConnector, ISwitchManager switchManager) {
+        Name ncName = (Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName);
+        String nodeConnectorName = nodeConnector.getNodeConnectorIDString();
+        if (ncName != null) {
+            nodeConnectorName = ncName.getValue();
+        }
+        return nodeConnectorName;
     }
 
     @SuppressWarnings("unchecked")
@@ -274,7 +308,7 @@ public class Topology implements IObjectReader, IConfigurationAware {
                     continue;
                 }
 
-                String description = switchManager.getNodeDescription(n);
+                String description = getNodeDesc(n, switchManager);
 
                 if ((stagedNodes.containsKey(n.toString()) && metaCache.get(containerName).containsKey(n.toString())) || newNodes.containsKey(n.toString())) {
                         continue;
@@ -306,29 +340,31 @@ public class Topology implements IObjectReader, IConfigurationAware {
             ITopologyManager topology, String containerName) {
         for (Map.Entry<Node, Set<NodeConnector>> e : hostEdges.entrySet()) {
             for (NodeConnector connector : e.getValue()) {
-                Host host = topology.getHostAttachedToNodeConnector(connector);
-                EthernetAddress dmac = (EthernetAddress) host.getDataLayerAddress();
+                List<Host> hosts = topology.getHostsAttachedToNodeConnector(connector);
+                for (Host host : hosts) {
+                    EthernetAddress dmac = (EthernetAddress) host.getDataLayerAddress();
 
-                ByteBuffer addressByteBuffer = ByteBuffer.allocate(8);
-                addressByteBuffer.putShort((short) 0);
-                addressByteBuffer.put(dmac.getValue());
-                addressByteBuffer.rewind();
+                    ByteBuffer addressByteBuffer = ByteBuffer.allocate(8);
+                    addressByteBuffer.putShort((short) 0);
+                    addressByteBuffer.put(dmac.getValue());
+                    addressByteBuffer.rewind();
 
-                long hid = addressByteBuffer.getLong();
-                String hostId = String.valueOf(hid);
+                    long hid = addressByteBuffer.getLong();
+                    String hostId = String.valueOf(hid);
 
-                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);
+                    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 (metaCache.get(containerName).containsKey(hostId)) {
+                    if (metaCache.get(containerName).containsKey(hostId)) {
                         Map<String, Object> hostEntry = metaCache.get(containerName).get(hostId);
                         hostEntry.put("adjacencies", adjacencies);
                         stagedNodes.put(hostId, hostEntry);
-                } else {
+                    } else {
                         newNodes.put(String.valueOf(hid), hostBean.out());
+                    }
                 }
             }
         }
@@ -384,11 +420,15 @@ public class Topology implements IObjectReader, IConfigurationAware {
     public Map<String, Object> post(@PathVariable String nodeId, @RequestParam(required = true) String x,
                 @RequestParam(required = true) String y, @RequestParam(required = false) String container,
                 HttpServletRequest request) {
-        if (!authorize(UserLevel.NETWORKADMIN, request)) {
-                return new HashMap<String, Object>(); // silently disregard new node position
-        }
+        String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
 
-        String containerName = getAuthorizedContainer(request, container);
+        // Derive the privilege this user has on the current container
+        String userName = request.getUserPrincipal().getName();
+        Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
+
+        if (privilege != Privilege.WRITE) {
+            return new HashMap<String, Object>(); // silently disregard new node position
+        }
 
         String id = new String(nodeId);
 
@@ -465,36 +505,47 @@ public class Topology implements IObjectReader, IConfigurationAware {
                 data = new HashMap<String, String>();
         }
 
-        public EdgeBean(Edge link, Bandwidth bandwidth) {
-                this();
-                this.source = link.getHeadNodeConnector();
-                this.destination = link.getTailNodeConnector();
-
-                // data
-                data.put("$bandwidth", bandwidth.toString());
-                data.put("$color", bandwidthColor(bandwidth));
-                data.put("$nodeToPort", destination.getID().toString());
-                data.put("$nodeFromPort", source.getID().toString());
-                data.put("$descFrom", source.getNode().toString());
-                data.put("$descTo", destination.getNode().toString());
-                data.put("$nodeFromPortName", source.toString());
-                data.put("$nodeToPortName", destination.toString());
+        /**
+         * EdgeBean object that includes complete node description
+         *
+         * @param link
+         * @param bandwidth
+         * @param headDescription
+         * @param tailDescription
+         */
+        public EdgeBean(Edge link, Bandwidth bandwidth, String headDescription,
+                String tailDescription, String headPortDescription, String tailPortDescription) {
+            this();
+            this.source = link.getHeadNodeConnector();
+            this.destination = link.getTailNodeConnector();
+
+            // data
+            data.put("$bandwidth", bandwidth.toString());
+            data.put("$color", bandwidthColor(bandwidth));
+            data.put("$nodeToPort", destination.getID().toString());
+            data.put("$nodeFromPort", source.getID().toString());
+            data.put("$descFrom", headDescription);
+            data.put("$descTo", tailDescription);
+            data.put("$nodeFromPortName", source.toString());
+            data.put("$nodeToPortName", destination.toString());
+            data.put("$nodeFromPortDescription", headPortDescription);
+            data.put("$nodeToPortDescription", tailPortDescription);
         }
 
         public EdgeBean(NodeConnector connector, Long hostId) {
-                this();
-                this.source = null;
-                this.destination = connector;
-                this.hostId = hostId;
-
-                data.put("$bandwidth", "N/A");
-                data.put("$color", bandwidthColor(new Bandwidth(0)));
-                data.put("$nodeToPort", connector.getNodeConnectorIDString());
-                data.put("$nodeFromPort", connector.getNodeConnectorIDString());
-                data.put("$descTo", "");
-                data.put("$descFrom", "");
-                data.put("$nodeToPortName", "");
-                data.put("$nodeFromPortName", "");
+            this();
+            this.source = null;
+            this.destination = connector;
+            this.hostId = hostId;
+
+            data.put("$bandwidth", "N/A");
+            data.put("$color", bandwidthColor(new Bandwidth(0)));
+            data.put("$nodeToPort", connector.getNodeConnectorIDString());
+            data.put("$nodeFromPort", connector.getNodeConnectorIDString());
+            data.put("$descTo", "");
+            data.put("$descFrom", "");
+            data.put("$nodeToPortName", "");
+            data.put("$nodeFromPortName", "");
         }
 
         public Map<String, Object> out() {
@@ -539,51 +590,13 @@ public class Topology implements IObjectReader, IConfigurationAware {
         public static final String HOST = "host";
     }
 
-    private boolean authorize(UserLevel level, HttpServletRequest request) {
-        IUserManager userManager = (IUserManager) ServiceHelper
-                .getGlobalInstance(IUserManager.class, this);
-        if (userManager == null) {
-                return false;
-        }
-
-        String username = request.getUserPrincipal().getName();
-        UserLevel userLevel = userManager.getUserLevel(username);
-        if (userLevel.toNumber() <= level.toNumber()) {
-                return true;
-        }
-        return false;
-    }
-
-    private String getAuthorizedContainer(HttpServletRequest request, String container) {
-        String username = request.getUserPrincipal().getName();
-        IContainerAuthorization containerAuthorization = (IContainerAuthorization) ServiceHelper.
-                        getGlobalInstance(IContainerAuthorization.class, this);
-        if (containerAuthorization != null) {
-                Set<Resource> resources = containerAuthorization.getAllResourcesforUser(username);
-                if (authorizeContainer(container, resources)) {
-                        return container;
-                }
-        }
-
-        return GlobalConstants.DEFAULT.toString();
-    }
-
-    private boolean authorizeContainer(String container, Set<Resource> resources) {
-        for(Resource resource : resources) {
-                String containerName = (String) resource.getResource();
-                if (containerName.equals(container)) {
-                        return true;
-                }
-        }
-
-        return false;
-    }
-
     @SuppressWarnings("unchecked")
         private void loadConfiguration() {
         ObjectReader objReader = new ObjectReader();
         metaCache = (Map<String, Map<String, Map<String, Object>>>) objReader.read(this, topologyWebFileName);
-        if (metaCache == null) metaCache = new HashMap<String, Map<String, Map<String, Object>>>();
+        if (metaCache == null) {
+            metaCache = new HashMap<String, Map<String, Map<String, Object>>>();
+        }
     }
 
     @Override
@@ -599,4 +612,10 @@ public class Topology implements IObjectReader, IConfigurationAware {
         // Perform the class deserialization locally, from inside the package where the class is defined
         return ois.readObject();
     }
+
+    private String getNodeDesc(Node node, ISwitchManager switchManager) {
+        Description desc = (Description) switchManager.getNodeProp(node, Description.propertyName);
+        return (desc == null) ? "" : desc.getValue();
+    }
+
 }
\ No newline at end of file