3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.topology.web;
12 import java.awt.Dimension;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.ObjectInputStream;
16 import java.nio.ByteBuffer;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.LinkedList;
21 import java.util.List;
25 import javax.servlet.http.HttpServletRequest;
27 import org.opendaylight.controller.configuration.IConfigurationAware;
28 import org.opendaylight.controller.sal.authorization.Privilege;
29 import org.opendaylight.controller.sal.core.Bandwidth;
30 import org.opendaylight.controller.sal.core.Edge;
31 import org.opendaylight.controller.sal.core.Host;
32 import org.opendaylight.controller.sal.core.Name;
33 import org.opendaylight.controller.sal.core.Node;
34 import org.opendaylight.controller.sal.core.Node.NodeIDType;
35 import org.opendaylight.controller.sal.core.NodeConnector;
36 import org.opendaylight.controller.sal.core.Property;
37 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
38 import org.opendaylight.controller.sal.utils.GlobalConstants;
39 import org.opendaylight.controller.sal.utils.IObjectReader;
40 import org.opendaylight.controller.sal.utils.ObjectReader;
41 import org.opendaylight.controller.sal.utils.ObjectWriter;
42 import org.opendaylight.controller.sal.utils.ServiceHelper;
43 import org.opendaylight.controller.sal.utils.Status;
44 import org.opendaylight.controller.sal.utils.StatusCode;
45 import org.opendaylight.controller.switchmanager.ISwitchManager;
46 import org.opendaylight.controller.switchmanager.Switch;
47 import org.opendaylight.controller.switchmanager.SwitchConfig;
48 import org.opendaylight.controller.topologymanager.ITopologyManager;
49 import org.opendaylight.controller.web.DaylightWebUtil;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.PathVariable;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestMethod;
54 import org.springframework.web.bind.annotation.RequestParam;
55 import org.springframework.web.bind.annotation.ResponseBody;
57 import edu.uci.ics.jung.algorithms.layout.CircleLayout;
58 import edu.uci.ics.jung.graph.Graph;
59 import edu.uci.ics.jung.graph.SparseMultigraph;
63 public class Topology implements IObjectReader, IConfigurationAware {
64 private static String ROOT = GlobalConstants.STARTUPHOME.toString();
65 private String topologyWebFileName = null;
67 protected Map<String, Map<String, Map<String, Object>>> metaCache = new HashMap<String, Map<String, Map<String, Object>>>();
68 protected Map<String, Map<String, Object>> stagedNodes;
69 protected Map<String, Map<String, Object>> newNodes;
71 protected Map<String, Integer> metaNodeHash = new HashMap<String, Integer>();
72 protected Map<String, Integer> metaHostHash = new HashMap<String, Integer>();
73 protected Map<String, Integer> metaNodeSingleHash = new HashMap<String, Integer>();
74 protected Map<String, Integer> metaNodeConfigurationHash = new HashMap<String, Integer>();
77 ServiceHelper.registerGlobalService(IConfigurationAware.class, this, null);
78 topologyWebFileName = ROOT + "topologyCache.sav";
83 * Topology of nodes and hosts in the network in JSON format.
85 * Mainly intended for consumption by the visual topology.
87 * @return - JSON output for visual topology
89 @RequestMapping(value = "/visual.json", method = RequestMethod.GET)
91 public Collection<Map<String, Object>> getLinkData(@RequestParam(required = false) String container, HttpServletRequest request) {
92 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
94 // Derive the privilege this user has on the current container
95 String userName = request.getUserPrincipal().getName();
96 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
98 if (privilege == Privilege.NONE) {
102 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper
103 .getInstance(ITopologyManager.class, containerName, this);
104 if (topologyManager == null) {
107 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
108 .getInstance(ISwitchManager.class, containerName, this);
109 if (switchManager == null) {
113 Map<Node, Set<Edge>> nodeEdges = topologyManager.getNodeEdges();
114 Map<Node, Set<NodeConnector>> hostEdges = topologyManager
115 .getNodesWithNodeConnectorHost();
116 List<Switch> nodes = switchManager.getNetworkDevices();
118 List<SwitchConfig> switchConfigurations = new ArrayList<SwitchConfig>();
119 for(Switch sw : nodes) {
120 Node n = sw.getNode();
121 SwitchConfig config = switchManager.getSwitchConfig(n.toString());
122 switchConfigurations.add(config);
125 // initialize cache if needed
126 if (!metaCache.containsKey(containerName)) {
127 metaCache.put(containerName, new HashMap<String, Map<String, Object>>());
129 metaNodeHash.put(containerName, null);
130 metaHostHash.put(containerName, null);
131 metaNodeSingleHash.put(containerName, null);
132 metaNodeConfigurationHash.put(containerName, null);
135 // return cache if topology hasn't changed
137 (metaNodeHash.get(containerName) != null && metaHostHash.get(containerName) != null && metaNodeSingleHash.get(containerName) != null && metaNodeConfigurationHash.get(containerName) != null) &&
138 metaNodeHash.get(containerName).equals(nodeEdges.hashCode()) && metaHostHash.get(containerName).equals(hostEdges.hashCode()) && metaNodeSingleHash.get(containerName).equals(nodes.hashCode()) && metaNodeConfigurationHash.get(containerName).equals(switchConfigurations.hashCode())
140 return metaCache.get(containerName).values();
143 // cache has changed, we must assign the new values
144 metaNodeHash.put(containerName, nodeEdges.hashCode());
145 metaHostHash.put(containerName, hostEdges.hashCode());
146 metaNodeSingleHash.put(containerName, nodes.hashCode());
147 metaNodeConfigurationHash.put(containerName, switchConfigurations.hashCode());
149 stagedNodes = new HashMap<String, Map<String, Object>>();
150 newNodes = new HashMap<String, Map<String, Object>>();
152 // nodeEdges addition
153 addNodes(nodeEdges, topologyManager, switchManager, containerName);
155 // single nodes addition
156 addSingleNodes(nodes, switchManager, containerName);
158 // hostNodes addition
159 addHostNodes(hostEdges, topologyManager, containerName);
161 repositionTopology(containerName);
163 return metaCache.get(containerName).values();
167 * Add regular nodes to main topology
169 * @param nodeEdges - node-edges mapping derived from topology
170 * @param topology - the topology instance
172 private void addNodes(Map<Node, Set<Edge>> nodeEdges,
173 ITopologyManager topology, ISwitchManager switchManager, String containerName) {
174 Bandwidth bandwidth = new Bandwidth(0);
175 Map<Edge, Set<Property>> properties = topology.getEdges();
177 for (Map.Entry<Node, Set<Edge>> e : nodeEdges.entrySet()) {
179 String description = switchManager.getNodeDescription(n);
180 NodeBean node = createNodeBean(description, n);
182 // skip production node
187 List<Map<String, Object>> adjacencies = new LinkedList<Map<String, Object>>();
188 Set<Edge> links = e.getValue();
189 for (Edge link : links) {
190 if (edgeIgnore(link)) {
193 for (Property p : properties.get(link)) {
194 if (p instanceof Bandwidth) {
195 bandwidth = (Bandwidth) p;
199 NodeConnector headNodeConnector = link.getHeadNodeConnector();
200 NodeConnector tailNodeConnector = link.getTailNodeConnector();
202 String headDescription = this.getNodeConnectorDescription(headNodeConnector, switchManager);
203 String tailDescription = this.getNodeConnectorDescription(tailNodeConnector, switchManager);
204 String headPortDescription = this.getNodeConnectorPortDescription(headNodeConnector, switchManager);
205 String tailPortDescription = this.getNodeConnectorPortDescription(tailNodeConnector, switchManager);
206 EdgeBean edge = new EdgeBean(link, bandwidth, headDescription, tailDescription, headPortDescription, tailPortDescription);
207 adjacencies.add(edge.out());
210 node.setLinks(adjacencies);
211 if (metaCache.get(containerName).containsKey(node.id())) {
212 // retrieve node from cache
213 Map<String, Object> nodeEntry = metaCache.get(containerName).get(node.id());
215 Map<String, String> data = (Map<String, String>) nodeEntry.get("data");
216 data.put("$desc", description);
217 nodeEntry.put("data", data);
219 // always update adjacencies
220 nodeEntry.put("adjacencies", adjacencies);
221 // stage this cached node (with position)
222 stagedNodes.put(node.id(), nodeEntry);
224 newNodes.put(node.id(), node.out());
230 * Check if this node shouldn't appear in the visual topology
235 private boolean nodeIgnore(Node node) {
236 String nodeType = node.getType();
238 // add other node types to ignore later
239 if (nodeType.equals(NodeIDType.PRODUCTION)) {
247 * Check if this edge shouldn't appear in the visual topology
252 private boolean edgeIgnore(Edge edge) {
253 NodeConnector headNodeConnector = edge.getHeadNodeConnector();
254 Node headNode = headNodeConnector.getNode();
255 if (nodeIgnore(headNode)) {
259 NodeConnector tailNodeConnector = edge.getTailNodeConnector();
260 Node tailNode = tailNodeConnector.getNode();
261 if (nodeIgnore(tailNode)) {
268 protected NodeBean createNodeBean(String description, Node node) {
269 String name = this.getDescription(description, node);
270 return new NodeBean(node.toString(), name, NodeType.NODE);
273 private String getDescription(String description, Node node) {
274 String name = (description == null ||
275 description.trim().isEmpty() ||
276 description.equalsIgnoreCase("none"))?
277 node.toString() : description;
281 private String getNodeConnectorDescription(NodeConnector nodeConnector, ISwitchManager switchManager) {
282 Node node = nodeConnector.getNode();
283 String description = switchManager.getNodeDescription(node);
284 String name = this.getDescription(description, node);
288 private String getNodeConnectorPortDescription(NodeConnector nodeConnector, ISwitchManager switchManager) {
289 Name ncName = (Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName);
290 String nodeConnectorName = nodeConnector.getNodeConnectorIDString();
291 if (ncName != null) {
292 nodeConnectorName = ncName.getValue();
294 return nodeConnectorName;
297 @SuppressWarnings("unchecked")
298 private void addSingleNodes(List<Switch> nodes, ISwitchManager switchManager, String containerName) {
302 for (Switch sw : nodes) {
303 Node n = sw.getNode();
305 // skip production node
310 String description = switchManager.getNodeDescription(n);
312 if ((stagedNodes.containsKey(n.toString()) && metaCache.get(containerName).containsKey(n.toString())) || newNodes.containsKey(n.toString())) {
315 NodeBean node = createNodeBean(description, n);
317 // FIXME still doesn't display standalone node when last remaining link is removed
318 if (metaCache.get(containerName).containsKey(node.id()) && !stagedNodes.containsKey(node.id())) {
319 Map<String, Object> nodeEntry = metaCache.get(containerName).get(node.id());
320 Map<String, String> data = (Map<String, String>) nodeEntry.get("data");
321 data.put("$desc", description);
322 nodeEntry.put("data", data);
323 // clear adjacencies since this is now a single node
324 nodeEntry.put("adjacencies", new LinkedList<Map<String, Object>>());
325 stagedNodes.put(node.id(), nodeEntry);
327 newNodes.put(node.id(), node.out());
333 * Add regular hosts to main topology
335 * @param hostEdges - node-nodeconnectors host-specific mapping from topology
336 * @param topology - topology instance
338 private void addHostNodes(Map<Node, Set<NodeConnector>> hostEdges,
339 ITopologyManager topology, String containerName) {
340 for (Map.Entry<Node, Set<NodeConnector>> e : hostEdges.entrySet()) {
341 for (NodeConnector connector : e.getValue()) {
342 Host host = topology.getHostAttachedToNodeConnector(connector);
343 EthernetAddress dmac = (EthernetAddress) host.getDataLayerAddress();
345 ByteBuffer addressByteBuffer = ByteBuffer.allocate(8);
346 addressByteBuffer.putShort((short) 0);
347 addressByteBuffer.put(dmac.getValue());
348 addressByteBuffer.rewind();
350 long hid = addressByteBuffer.getLong();
351 String hostId = String.valueOf(hid);
353 NodeBean hostBean = new NodeBean(hostId, host.getNetworkAddressAsString(), NodeType.HOST);
354 List<Map<String, Object>> adjacencies = new LinkedList<Map<String, Object>>();
355 EdgeBean edge = new EdgeBean(connector, hid);
356 adjacencies.add(edge.out());
357 hostBean.setLinks(adjacencies);
359 if (metaCache.get(containerName).containsKey(hostId)) {
360 Map<String, Object> hostEntry = metaCache.get(containerName).get(hostId);
361 hostEntry.put("adjacencies", adjacencies);
362 stagedNodes.put(hostId, hostEntry);
364 newNodes.put(String.valueOf(hid), hostBean.out());
371 * Re-position nodes in circular layout
373 private void repositionTopology(String containerName) {
374 Graph<String, String> graph = new SparseMultigraph<String, String>();
376 metaCache.get(containerName).clear();
377 metaCache.get(containerName).putAll(stagedNodes);
378 metaCache.get(containerName).putAll(newNodes);
380 for (Map<String, Object> on : metaCache.get(containerName).values()) {
381 graph.addVertex(on.toString());
383 List<Map<String, Object>> adjacencies = (List<Map<String, Object>>) on.get("adjacencies");
385 for (Map<String, Object> adj : adjacencies) {
387 adj.toString(), adj.get("nodeFrom").toString(),
388 adj.get("nodeTo").toString()
393 CircleLayout<String, String> layout = new CircleLayout<String, String>(graph);
394 layout.setSize(new Dimension(1200, 365));
395 for (Map.Entry<String, Map<String, Object>> v : newNodes.entrySet()) {
396 Double x = layout.transform(v.getKey()).getX();
397 Double y = layout.transform(v.getKey()).getY();
399 Map<String, String> nodeData = (HashMap<String, String>) v.getValue().get("data");
400 nodeData.put("$x", (x - 600) + "");
401 nodeData.put("$y", (y - 225) + "");
403 newNodes.get(v.getKey()).put("data", nodeData);
408 * Update node position
410 * This method is mainly used by the visual topology
412 * @param nodeId - The node to update
413 * @return The node object
415 @RequestMapping(value = "/node/{nodeId}", method = RequestMethod.POST)
417 public Map<String, Object> post(@PathVariable String nodeId, @RequestParam(required = true) String x,
418 @RequestParam(required = true) String y, @RequestParam(required = false) String container,
419 HttpServletRequest request) {
420 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
422 // Derive the privilege this user has on the current container
423 String userName = request.getUserPrincipal().getName();
424 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
426 if (privilege != Privilege.WRITE) {
427 return new HashMap<String, Object>(); // silently disregard new node position
430 String id = new String(nodeId);
432 if (!metaCache.get(containerName).containsKey(id)) {
436 Map<String, Object> node = metaCache.get(containerName).get(id);
437 Map<String, String> data = (Map<String, String>) node.get("data");
442 node.put("data", data);
448 * Node object for visual topology
450 protected class NodeBean {
452 protected String name;
453 protected Map<String, String> data;
454 protected List<Map<String, Object>> links;
457 data = new HashMap<String, String>();
458 links = new ArrayList<Map<String, Object>>();
461 public NodeBean(String id, String name, String type) {
465 data.put("$desc", name);
466 data.put("$type", type);
469 public void setLinks(List<Map<String, Object>> links) {
473 public Map<String, Object> out() {
474 Map<String, Object> node = new HashMap<String, Object>();
475 node.put("id", this.id);
476 node.put("name", this.name);
477 node.put("data", this.data);
478 node.put("adjacencies", this.links);
483 public String name() {
493 * Edge object for visual topology
495 protected class EdgeBean {
496 protected NodeConnector source;
497 protected NodeConnector destination;
498 protected Map<String, String> data;
499 protected Long hostId;
502 data = new HashMap<String, String>();
506 * EdgeBean object that includes complete node description
510 * @param headDescription
511 * @param tailDescription
513 public EdgeBean(Edge link, Bandwidth bandwidth, String headDescription,
514 String tailDescription, String headPortDescription, String tailPortDescription) {
516 this.source = link.getHeadNodeConnector();
517 this.destination = link.getTailNodeConnector();
520 data.put("$bandwidth", bandwidth.toString());
521 data.put("$color", bandwidthColor(bandwidth));
522 data.put("$nodeToPort", destination.getID().toString());
523 data.put("$nodeFromPort", source.getID().toString());
524 data.put("$descFrom", headDescription);
525 data.put("$descTo", tailDescription);
526 data.put("$nodeFromPortName", source.toString());
527 data.put("$nodeToPortName", destination.toString());
528 data.put("$nodeFromPortDescription", headPortDescription);
529 data.put("$nodeToPortDescription", tailPortDescription);
532 public EdgeBean(NodeConnector connector, Long hostId) {
535 this.destination = connector;
536 this.hostId = hostId;
538 data.put("$bandwidth", "N/A");
539 data.put("$color", bandwidthColor(new Bandwidth(0)));
540 data.put("$nodeToPort", connector.getNodeConnectorIDString());
541 data.put("$nodeFromPort", connector.getNodeConnectorIDString());
542 data.put("$descTo", "");
543 data.put("$descFrom", "");
544 data.put("$nodeToPortName", "");
545 data.put("$nodeFromPortName", "");
548 public Map<String, Object> out() {
549 Map<String, Object> edge = new HashMap<String, Object>();
551 edge.put("data", data);
552 if (source == null) {
553 edge.put("nodeFrom", String.valueOf(this.hostId));
555 edge.put("nodeFrom", source.getNode().toString());
557 edge.put("nodeTo", destination.getNode().toString());
563 private String bandwidthColor(Bandwidth bandwidth) {
565 long bandwidthValue = bandwidth.getValue();
567 if (bandwidthValue == 0) {
569 } else if (bandwidthValue < Bandwidth.BW1Kbps) {
571 } else if (bandwidthValue < Bandwidth.BW1Mbps) {
573 } else if (bandwidthValue < Bandwidth.BW1Gbps) {
575 } else if (bandwidthValue < Bandwidth.BW1Tbps) {
577 } else if (bandwidthValue < Bandwidth.BW1Pbps) {
585 protected class NodeType {
586 public static final String NODE = "swtch";
587 public static final String HOST = "host";
590 @SuppressWarnings("unchecked")
591 private void loadConfiguration() {
592 ObjectReader objReader = new ObjectReader();
593 metaCache = (Map<String, Map<String, Map<String, Object>>>) objReader.read(this, topologyWebFileName);
594 if (metaCache == null) {
595 metaCache = new HashMap<String, Map<String, Map<String, Object>>>();
600 public Status saveConfiguration() {
601 ObjectWriter objWriter = new ObjectWriter();
602 objWriter.write(metaCache, topologyWebFileName);
603 return new Status(StatusCode.SUCCESS, null);
607 public Object readObject(ObjectInputStream ois)
608 throws FileNotFoundException, IOException, ClassNotFoundException {
609 // Perform the class deserialization locally, from inside the package where the class is defined
610 return ois.readObject();