X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fweb%2Ftopology%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Ftopology%2Fweb%2FTopology.java;h=62b64a51849ea2ee08c4b9f43cffca8439bd5cad;hp=b4695defdb4468d5e3a1324e723e6bab169513ae;hb=541d0a36997f292bb037a2199463431eee538358;hpb=fad07e38e57f1c0d8d687e3bae01532196d6e99d diff --git a/opendaylight/web/topology/src/main/java/org/opendaylight/controller/topology/web/Topology.java b/opendaylight/web/topology/src/main/java/org/opendaylight/controller/topology/web/Topology.java index b4695defdb..62b64a5184 100644 --- a/opendaylight/web/topology/src/main/java/org/opendaylight/controller/topology/web/Topology.java +++ b/opendaylight/web/topology/src/main/java/org/opendaylight/controller/topology/web/Topology.java @@ -70,77 +70,77 @@ public class Topology implements IObjectReader, IConfigurationAware { protected Map>> metaCache = new HashMap>>(); protected Map> stagedNodes; protected Map> newNodes; - + protected Map metaNodeHash = new HashMap(); protected Map metaHostHash = new HashMap(); protected Map metaNodeSingleHash = new HashMap(); protected Map metaNodeConfigurationHash = new HashMap(); - + public Topology() { - ServiceHelper.registerGlobalService(IConfigurationAware.class, this, null); - topologyWebFileName = ROOT + "topologyCache.sav"; - loadConfiguration(); + ServiceHelper.registerGlobalService(IConfigurationAware.class, this, null); + topologyWebFileName = ROOT + "topologyCache.sav"; + loadConfiguration(); } - + /** * Topology of nodes and hosts in the network in JSON format. - * + * * Mainly intended for consumption by the visual topology. - * + * * @return - JSON output for visual topology */ @RequestMapping(value = "/visual.json", method = RequestMethod.GET) @ResponseBody public Collection> getLinkData(@RequestParam(required = false) String container, HttpServletRequest request) { - String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this); - + String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this); + ITopologyManager topologyManager = (ITopologyManager) ServiceHelper .getInstance(ITopologyManager.class, containerName, this); if (topologyManager == null) { - return null; + return null; } ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); if (switchManager == null) { - return null; + return null; } - + Map> nodeEdges = topologyManager.getNodeEdges(); Map> hostEdges = topologyManager .getNodesWithNodeConnectorHost(); List nodes = switchManager.getNetworkDevices(); - + List switchConfigurations = new ArrayList(); for(Switch sw : nodes) { - Node n = sw.getNode(); - SwitchConfig config = switchManager.getSwitchConfig(n.toString()); - switchConfigurations.add(config); + Node n = sw.getNode(); + SwitchConfig config = switchManager.getSwitchConfig(n.toString()); + switchConfigurations.add(config); } - + // initialize cache if needed if (!metaCache.containsKey(containerName)) { - metaCache.put(containerName, new HashMap>()); - // initialize hashes - metaNodeHash.put(containerName, null); - metaHostHash.put(containerName, null); - metaNodeSingleHash.put(containerName, null); - metaNodeConfigurationHash.put(containerName, null); + metaCache.put(containerName, new HashMap>()); + // initialize hashes + metaNodeHash.put(containerName, null); + metaHostHash.put(containerName, null); + metaNodeSingleHash.put(containerName, null); + metaNodeConfigurationHash.put(containerName, null); } - + // return cache if topology hasn't changed if ( - (metaNodeHash.get(containerName) != null && metaHostHash.get(containerName) != null && metaNodeSingleHash.get(containerName) != null && metaNodeConfigurationHash.get(containerName) != null) && - metaNodeHash.get(containerName).equals(nodeEdges.hashCode()) && metaHostHash.get(containerName).equals(hostEdges.hashCode()) && metaNodeSingleHash.get(containerName).equals(nodes.hashCode()) && metaNodeConfigurationHash.get(containerName).equals(switchConfigurations.hashCode()) + (metaNodeHash.get(containerName) != null && metaHostHash.get(containerName) != null && metaNodeSingleHash.get(containerName) != null && metaNodeConfigurationHash.get(containerName) != null) && + metaNodeHash.get(containerName).equals(nodeEdges.hashCode()) && metaHostHash.get(containerName).equals(hostEdges.hashCode()) && metaNodeSingleHash.get(containerName).equals(nodes.hashCode()) && metaNodeConfigurationHash.get(containerName).equals(switchConfigurations.hashCode()) ) { - return metaCache.get(containerName).values(); + return metaCache.get(containerName).values(); } - + // cache has changed, we must assign the new values metaNodeHash.put(containerName, nodeEdges.hashCode()); metaHostHash.put(containerName, hostEdges.hashCode()); metaNodeSingleHash.put(containerName, nodes.hashCode()); metaNodeConfigurationHash.put(containerName, switchConfigurations.hashCode()); - + stagedNodes = new HashMap>(); newNodes = new HashMap>(); @@ -149,12 +149,12 @@ public class Topology implements IObjectReader, IConfigurationAware { // single nodes addition addSingleNodes(nodes, switchManager, containerName); - + // hostNodes addition addHostNodes(hostEdges, topologyManager, containerName); - + repositionTopology(containerName); - + return metaCache.get(containerName).values(); } @@ -165,20 +165,20 @@ public class Topology implements IObjectReader, IConfigurationAware { * @param topology - the topology instance */ private void addNodes(Map> nodeEdges, - ITopologyManager topology, ISwitchManager switchManager, String containerName) { + ITopologyManager topology, ISwitchManager switchManager, String containerName) { Bandwidth bandwidth = new Bandwidth(0); Map> properties = topology.getEdges(); - + for (Map.Entry> e : nodeEdges.entrySet()) { Node n = e.getKey(); String description = switchManager.getNodeDescription(n); NodeBean node = createNodeBean(description, n); - + // skip production node if (nodeIgnore(n)) { continue; } - + List> adjacencies = new LinkedList>(); Set links = e.getValue(); for (Edge link : links) { @@ -187,53 +187,53 @@ public class Topology implements IObjectReader, IConfigurationAware { } for (Property p : properties.get(link)) { if (p instanceof Bandwidth) { - bandwidth = (Bandwidth) p; + bandwidth = (Bandwidth) p; break; } } EdgeBean edge = new EdgeBean(link, bandwidth); adjacencies.add(edge.out()); } - + node.setLinks(adjacencies); if (metaCache.get(containerName).containsKey(node.id())) { - // retrieve node from cache - Map nodeEntry = metaCache.get(containerName).get(node.id()); - - Map data = (Map) 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); + // retrieve node from cache + Map nodeEntry = metaCache.get(containerName).get(node.id()); + + Map data = (Map) 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()); + newNodes.put(node.id(), node.out()); } } } - + /** * Check if this node shouldn't appear in the visual topology - * + * * @param node * @return */ private boolean nodeIgnore(Node node) { String nodeType = node.getType(); - + // add other node types to ignore later if (nodeType.equals(NodeIDType.PRODUCTION)) { return true; } - + return false; } - + /** * Check if this edge shouldn't appear in the visual topology - * + * * @param edge * @return */ @@ -243,57 +243,57 @@ public class Topology implements IObjectReader, IConfigurationAware { if (nodeIgnore(headNode)) { return true; } - + NodeConnector tailNodeConnector = edge.getTailNodeConnector(); Node tailNode = tailNodeConnector.getNode(); if (nodeIgnore(tailNode)) { return true; } - + return false; } - + 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); + String name = (description == null || + description.trim().isEmpty() || + description.equalsIgnoreCase("none"))? + node.toString() : description; + return new NodeBean(node.toString(), name, NodeType.NODE); } - + @SuppressWarnings("unchecked") - private void addSingleNodes(List nodes, ISwitchManager switchManager, String containerName) { - if (nodes == null) { - return; - } - for (Switch sw : nodes) { - Node n = sw.getNode(); - - // skip production node - if (nodeIgnore(n)) { - continue; - } - - String description = switchManager.getNodeDescription(n); - - if ((stagedNodes.containsKey(n.toString()) && metaCache.get(containerName).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 (metaCache.get(containerName).containsKey(node.id()) && !stagedNodes.containsKey(node.id())) { - Map nodeEntry = metaCache.get(containerName).get(node.id()); - Map data = (Map) nodeEntry.get("data"); - data.put("$desc", description); - nodeEntry.put("data", data); - // clear adjacencies since this is now a single node - nodeEntry.put("adjacencies", new LinkedList>()); - stagedNodes.put(node.id(), nodeEntry); + private void addSingleNodes(List nodes, ISwitchManager switchManager, String containerName) { + if (nodes == null) { + return; + } + for (Switch sw : nodes) { + Node n = sw.getNode(); + + // skip production node + if (nodeIgnore(n)) { + continue; + } + + String description = switchManager.getNodeDescription(n); + + if ((stagedNodes.containsKey(n.toString()) && metaCache.get(containerName).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 (metaCache.get(containerName).containsKey(node.id()) && !stagedNodes.containsKey(node.id())) { + Map nodeEntry = metaCache.get(containerName).get(node.id()); + Map data = (Map) nodeEntry.get("data"); + data.put("$desc", description); + nodeEntry.put("data", data); + // clear adjacencies since this is now a single node + nodeEntry.put("adjacencies", new LinkedList>()); + stagedNodes.put(node.id(), nodeEntry); } else { - newNodes.put(node.id(), node.out()); + newNodes.put(node.id(), node.out()); } - } + } } /** @@ -313,22 +313,22 @@ public class Topology implements IObjectReader, IConfigurationAware { addressByteBuffer.putShort((short) 0); addressByteBuffer.put(dmac.getValue()); addressByteBuffer.rewind(); - + long hid = addressByteBuffer.getLong(); String hostId = String.valueOf(hid); - + NodeBean hostBean = new NodeBean(hostId, host.getNetworkAddressAsString(), NodeType.HOST); List> adjacencies = new LinkedList>(); EdgeBean edge = new EdgeBean(connector, hid); adjacencies.add(edge.out()); hostBean.setLinks(adjacencies); - + if (metaCache.get(containerName).containsKey(hostId)) { - Map hostEntry = metaCache.get(containerName).get(hostId); - hostEntry.put("adjacencies", adjacencies); - stagedNodes.put(hostId, hostEntry); + Map hostEntry = metaCache.get(containerName).get(hostId); + hostEntry.put("adjacencies", adjacencies); + stagedNodes.put(hostId, hostEntry); } else { - newNodes.put(String.valueOf(hid), hostBean.out()); + newNodes.put(String.valueOf(hid), hostBean.out()); } } } @@ -339,24 +339,24 @@ public class Topology implements IObjectReader, IConfigurationAware { */ private void repositionTopology(String containerName) { Graph graph = new SparseMultigraph(); - + metaCache.get(containerName).clear(); metaCache.get(containerName).putAll(stagedNodes); metaCache.get(containerName).putAll(newNodes); - + for (Map on : metaCache.get(containerName).values()) { graph.addVertex(on.toString()); List> adjacencies = (List>) on.get("adjacencies"); - + for (Map adj : adjacencies) { graph.addEdge( - adj.toString(), adj.get("nodeFrom").toString(), - adj.get("nodeTo").toString() + adj.toString(), adj.get("nodeFrom").toString(), + adj.get("nodeTo").toString() ); } } - + CircleLayout layout = new CircleLayout(graph); layout.setSize(new Dimension(1200, 365)); for (Map.Entry> v : newNodes.entrySet()) { @@ -373,7 +373,7 @@ public class Topology implements IObjectReader, IConfigurationAware { /** * Update node position - * + * * This method is mainly used by the visual topology * * @param nodeId - The node to update @@ -382,16 +382,16 @@ public class Topology implements IObjectReader, IConfigurationAware { @RequestMapping(value = "/node/{nodeId}", method = RequestMethod.POST) @ResponseBody public Map 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(); // silently disregard new node position - } - - String containerName = getAuthorizedContainer(request, container); - + @RequestParam(required = true) String y, @RequestParam(required = false) String container, + HttpServletRequest request) { + if (!authorize(UserLevel.NETWORKADMIN, request)) { + return new HashMap(); // silently disregard new node position + } + + String containerName = getAuthorizedContainer(request, container); + String id = new String(nodeId); - + if (!metaCache.get(containerName).containsKey(id)) { return null; } @@ -403,123 +403,123 @@ public class Topology implements IObjectReader, IConfigurationAware { data.put("$y", y); node.put("data", data); - + return node; } - + /** * Node object for visual topology */ protected class NodeBean { - protected String id; - protected String name; - protected Map data; - protected List> links; - - public NodeBean() { - data = new HashMap(); - links = new ArrayList>(); - } - - public NodeBean(String id, String name, String type) { - this(); - this.id = id; - this.name = name; - data.put("$desc", name); - data.put("$type", type); - } - - public void setLinks(List> links) { - this.links = links; - } - - public Map out() { - Map node = new HashMap(); - node.put("id", this.id); - node.put("name", this.name); - node.put("data", this.data); - node.put("adjacencies", this.links); - - return node; - } - - public String name() { - return this.name; - } - - public String id() { - return this.id; - } + protected String id; + protected String name; + protected Map data; + protected List> links; + + public NodeBean() { + data = new HashMap(); + links = new ArrayList>(); + } + + public NodeBean(String id, String name, String type) { + this(); + this.id = id; + this.name = name; + data.put("$desc", name); + data.put("$type", type); + } + + public void setLinks(List> links) { + this.links = links; + } + + public Map out() { + Map node = new HashMap(); + node.put("id", this.id); + node.put("name", this.name); + node.put("data", this.data); + node.put("adjacencies", this.links); + + return node; + } + + public String name() { + return this.name; + } + + public String id() { + return this.id; + } } - + /** * Edge object for visual topology */ protected class EdgeBean { - protected NodeConnector source; - protected NodeConnector destination; - protected Map data; - protected Long hostId; - - public EdgeBean() { - data = new HashMap(); - } - - 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()); - } - - 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", ""); - } - - public Map out() { - Map edge = new HashMap(); - - edge.put("data", data); - if (source == null) { - edge.put("nodeFrom", String.valueOf(this.hostId)); - } else { - edge.put("nodeFrom", source.getNode().toString()); - } - edge.put("nodeTo", destination.getNode().toString()); - - - return edge; - } - - private String bandwidthColor(Bandwidth bandwidth) { - String color = null; - long bandwidthValue = bandwidth.getValue(); - - if (bandwidthValue == 0) { + protected NodeConnector source; + protected NodeConnector destination; + protected Map data; + protected Long hostId; + + public EdgeBean() { + data = new HashMap(); + } + + 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()); + } + + 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", ""); + } + + public Map out() { + Map edge = new HashMap(); + + edge.put("data", data); + if (source == null) { + edge.put("nodeFrom", String.valueOf(this.hostId)); + } else { + edge.put("nodeFrom", source.getNode().toString()); + } + edge.put("nodeTo", destination.getNode().toString()); + + + return edge; + } + + private String bandwidthColor(Bandwidth bandwidth) { + String color = null; + long bandwidthValue = bandwidth.getValue(); + + if (bandwidthValue == 0) { color = "#000"; } else if (bandwidthValue < Bandwidth.BW1Kbps) { - color = "#148AC6"; + color = "#148AC6"; } else if (bandwidthValue < Bandwidth.BW1Mbps) { color = "#2858A0"; } else if (bandwidthValue < Bandwidth.BW1Gbps) { @@ -529,58 +529,58 @@ public class Topology implements IObjectReader, IConfigurationAware { } else if (bandwidthValue < Bandwidth.BW1Pbps) { color = "#F9F464"; } - - return color; + + return color; } } - + protected class NodeType { - public static final String NODE = "swtch"; - public static final String HOST = "host"; + public static final String NODE = "swtch"; + public static final String HOST = "host"; } - + private boolean authorize(UserLevel level, HttpServletRequest request) { - IUserManager userManager = (IUserManager) ServiceHelper + IUserManager userManager = (IUserManager) ServiceHelper .getGlobalInstance(IUserManager.class, this); if (userManager == null) { - return false; + return false; } - + String username = request.getUserPrincipal().getName(); UserLevel userLevel = userManager.getUserLevel(username); if (userLevel.toNumber() <= level.toNumber()) { - return true; + 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 resources = containerAuthorization.getAllResourcesforUser(username); - if (authorizeContainer(container, resources)) { - return container; - } - } - - return GlobalConstants.DEFAULT.toString(); + String username = request.getUserPrincipal().getName(); + IContainerAuthorization containerAuthorization = (IContainerAuthorization) ServiceHelper. + getGlobalInstance(IContainerAuthorization.class, this); + if (containerAuthorization != null) { + Set resources = containerAuthorization.getAllResourcesforUser(username); + if (authorizeContainer(container, resources)) { + return container; + } + } + + return GlobalConstants.DEFAULT.toString(); } - + private boolean authorizeContainer(String container, Set resources) { - for(Resource resource : resources) { - String containerName = (String) resource.getResource(); - if (containerName.equals(container)) { - return true; - } - } - - return false; + for(Resource resource : resources) { + String containerName = (String) resource.getResource(); + if (containerName.equals(container)) { + return true; + } + } + + return false; } @SuppressWarnings("unchecked") - private void loadConfiguration() { + private void loadConfiguration() { ObjectReader objReader = new ObjectReader(); metaCache = (Map>>) objReader.read(this, topologyWebFileName); if (metaCache == null) metaCache = new HashMap>>();