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
11 * @file DijkstraImplementation.java
14 * @brief Implementation of a routing engine using
15 * dijkstra. Implementation of dijkstra come from Jung2 library
18 package org.opendaylight.controller.routing.dijkstra_implementation.internal;
20 import org.opendaylight.controller.sal.core.Bandwidth;
21 import org.opendaylight.controller.sal.core.ConstructionException;
22 import org.opendaylight.controller.sal.core.Edge;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.core.Path;
26 import org.opendaylight.controller.sal.core.Property;
27 import org.opendaylight.controller.sal.core.UpdateType;
28 import org.opendaylight.controller.sal.reader.IReadService;
29 import org.opendaylight.controller.sal.routing.IListenRoutingUpdates;
30 import org.opendaylight.controller.sal.routing.IRouting;
31 import org.opendaylight.controller.switchmanager.ISwitchManager;
32 import org.opendaylight.controller.topologymanager.ITopologyManager;
33 import org.opendaylight.controller.topologymanager.ITopologyManagerAware;
35 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
36 import edu.uci.ics.jung.graph.Graph;
37 import edu.uci.ics.jung.graph.SparseMultigraph;
38 import edu.uci.ics.jung.graph.util.EdgeType;
39 import java.lang.Exception;
40 import java.lang.IllegalArgumentException;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.List;
46 import java.util.concurrent.ConcurrentHashMap;
47 import java.util.concurrent.ConcurrentMap;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.apache.commons.collections15.Transformer;
52 public class DijkstraImplementation implements IRouting, ITopologyManagerAware {
53 private static Logger log = LoggerFactory
54 .getLogger(DijkstraImplementation.class);
55 private ConcurrentMap<Short, Graph<Node, Edge>> topologyBWAware;
56 private ConcurrentMap<Short, DijkstraShortestPath<Node, Edge>> sptBWAware;
57 DijkstraShortestPath<Node, Edge> mtp; //Max Throughput Path
58 private Set<IListenRoutingUpdates> routingAware;
59 private ISwitchManager switchManager;
60 private ITopologyManager topologyManager;
61 private IReadService readService;
62 private static final long DEFAULT_LINK_SPEED = Bandwidth.BW1Gbps;
64 public void setListenRoutingUpdates(IListenRoutingUpdates i) {
65 if (this.routingAware == null) {
66 this.routingAware = new HashSet<IListenRoutingUpdates>();
68 if (this.routingAware != null) {
69 log.debug("Adding routingAware listener: {}", i);
70 this.routingAware.add(i);
74 public void unsetListenRoutingUpdates(IListenRoutingUpdates i) {
75 if (this.routingAware == null) {
78 log.debug("Removing routingAware listener");
79 this.routingAware.remove(i);
80 if (this.routingAware.isEmpty()) {
81 // We don't have any listener lets dereference
82 this.routingAware = null;
87 public synchronized void initMaxThroughput(
88 final Map<Edge, Number> EdgeWeightMap) {
90 log.error("Max Throughput Dijkstra is already enabled!");
93 Transformer<Edge, ? extends Number> mtTransformer = null;
94 if (EdgeWeightMap == null) {
95 mtTransformer = new Transformer<Edge, Double>() {
96 public Double transform(Edge e) {
97 if (switchManager == null) {
98 log.error("switchManager is null");
101 NodeConnector srcNC = e.getTailNodeConnector();
102 NodeConnector dstNC = e.getHeadNodeConnector();
103 if ((srcNC == null) || (dstNC == null)) {
104 log.error("srcNC:{} or dstNC:{} is null", srcNC, dstNC);
107 Bandwidth bwSrc = (Bandwidth) switchManager
108 .getNodeConnectorProp(srcNC,
109 Bandwidth.BandwidthPropName);
110 Bandwidth bwDst = (Bandwidth) switchManager
111 .getNodeConnectorProp(dstNC,
112 Bandwidth.BandwidthPropName);
114 long srcLinkSpeed = 0, dstLinkSpeed = 0;
115 if ((bwSrc == null) || ((srcLinkSpeed = bwSrc.getValue()) == 0)) {
116 log.debug("srcNC: {} - Setting srcLinkSpeed to Default!",srcNC);
117 srcLinkSpeed = DEFAULT_LINK_SPEED;
120 if ((bwDst == null) || ((dstLinkSpeed = bwDst.getValue()) == 0)) {
121 log.debug("dstNC: {} - Setting dstLinkSpeed to Default!",dstNC);
122 dstLinkSpeed = DEFAULT_LINK_SPEED;
125 long avlSrcThruPut = srcLinkSpeed
126 - readService.getTransmitRate(srcNC);
127 long avlDstThruPut = dstLinkSpeed
128 - readService.getTransmitRate(dstNC);
130 //Use lower of the 2 available thruput as the available thruput
131 long avlThruPut = avlSrcThruPut < avlDstThruPut ? avlSrcThruPut
134 if (avlThruPut <= 0) {
135 log.debug("Edge {}: Available Throughput {} <= 0!",
139 return (double) (Bandwidth.BW1Pbps / avlThruPut);
143 mtTransformer = new Transformer<Edge, Number>() {
144 public Number transform(Edge e) {
145 return EdgeWeightMap.get(e);
149 Short baseBW = Short.valueOf((short) 0);
150 //Initialize mtp also using the default topo
151 Graph<Node, Edge> g = this.topologyBWAware.get(baseBW);
153 log.error("Default Topology Graph is null");
156 mtp = new DijkstraShortestPath<Node, Edge>(g, mtTransformer);
160 public Path getRoute(Node src, Node dst) {
161 if (src == null || dst == null) {
164 return getRoute(src, dst, (short) 0);
168 public synchronized Path getMaxThroughputRoute(Node src, Node dst) {
170 log.error("Max Throughput Path Calculation Uninitialized!");
176 path = mtp.getMaxThroughputPath(src, dst);
177 } catch (IllegalArgumentException ie) {
178 log.debug("A vertex is yet not known between {} {}", src.toString(),
184 res = new Path(path);
185 } catch (ConstructionException e) {
186 log.debug("A vertex is yet not known between {} {}", src.toString(),
194 public synchronized Path getRoute(Node src, Node dst, Short Bw) {
195 DijkstraShortestPath<Node, Edge> spt = this.sptBWAware.get(Bw);
200 path = spt.getPath(src, dst);
201 } catch (IllegalArgumentException ie) {
202 log.debug("A vertex is yet not known between {} {}", src.toString(),
208 res = new Path(path);
209 } catch (ConstructionException e) {
210 log.debug("A vertex is yet not known between {} {}", src.toString(),
218 public synchronized void clear() {
219 DijkstraShortestPath<Node, Edge> spt;
220 for (Short bw : this.sptBWAware.keySet()) {
221 spt = this.sptBWAware.get(bw);
226 clearMaxThroughput();
230 public synchronized void clearMaxThroughput() {
232 mtp.reset(); //reset maxthruput path
236 @SuppressWarnings( { "rawtypes", "unchecked" })
237 private synchronized boolean updateTopo(Edge edge, Short bw, boolean added) {
238 Graph<Node, Edge> topo = this.topologyBWAware.get(bw);
239 DijkstraShortestPath<Node, Edge> spt = this.sptBWAware.get(bw);
240 boolean edgePresentInGraph = false;
241 Short baseBW = Short.valueOf((short) 0);
244 // Create topology for this BW
245 Graph<Node, Edge> g = new SparseMultigraph();
246 this.topologyBWAware.put(bw, g);
247 topo = this.topologyBWAware.get(bw);
248 this.sptBWAware.put(bw, new DijkstraShortestPath(g));
249 spt = this.sptBWAware.get(bw);
253 NodeConnector src = edge.getTailNodeConnector();
254 NodeConnector dst = edge.getHeadNodeConnector();
256 spt = new DijkstraShortestPath(topo);
257 this.sptBWAware.put(bw, spt);
261 // Make sure the vertex are there before adding the edge
262 topo.addVertex(src.getNode());
263 topo.addVertex(dst.getNode());
264 // Add the link between
265 edgePresentInGraph = topo.containsEdge(edge);
266 if (edgePresentInGraph == false) {
268 topo.addEdge(new Edge(src, dst), src
270 .getNode(), EdgeType.DIRECTED);
271 } catch (ConstructionException e) {
273 return edgePresentInGraph;
279 topo.removeEdge(new Edge(src, dst));
280 } catch (ConstructionException e) {
282 return edgePresentInGraph;
285 // If the src and dst vertex don't have incoming or
286 // outgoing links we can get ride of them
287 if (topo.containsVertex(src.getNode())
288 && topo.inDegree(src.getNode()) == 0
289 && topo.outDegree(src.getNode()) == 0) {
290 log.debug("Removing vertex {}", src);
291 topo.removeVertex(src.getNode());
294 if (topo.containsVertex(dst.getNode())
295 && topo.inDegree(dst.getNode()) == 0
296 && topo.outDegree(dst.getNode()) == 0) {
297 log.debug("Removing vertex {}", dst);
298 topo.removeVertex(dst.getNode());
302 if (bw.equals(baseBW)) {
303 clearMaxThroughput();
306 log.error("Cannot find topology for BW {} this is unexpected!", bw);
308 return edgePresentInGraph;
312 public void edgeUpdate(Edge e, UpdateType type, Set<Property> props) {
313 String srcType = null;
314 String dstType = null;
316 if (e == null || type == null) {
317 log.error("Edge or Update type are null!");
320 srcType = e.getTailNodeConnector().getType();
321 dstType = e.getHeadNodeConnector().getType();
323 if (srcType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
324 log.debug("Skip updates for {}", e);
328 if (dstType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
329 log.debug("Skip updates for {}", e);
334 Bandwidth bw = new Bandwidth(0);
335 boolean newEdge = false;
339 log.debug("edgeUpdate: {} bw: {}", e.toString(), bw.getValue());
341 Short baseBW = Short.valueOf((short) 0);
342 boolean add = (type == UpdateType.ADDED) ? true : false;
344 newEdge = !updateTopo(e, baseBW, add);
345 if (newEdge == true) {
346 if (bw.getValue() != baseBW) {
348 updateTopo(e, (short) bw.getValue(), add);
350 if (this.routingAware != null) {
351 for (IListenRoutingUpdates ra : this.routingAware) {
353 ra.recalculateDone();
354 } catch (Exception ex) {
355 log.error("Exception on routingAware listener call", e);
363 * Function called by the dependency manager when all the required
364 * dependencies are satisfied
367 @SuppressWarnings({ "unchecked", "rawtypes" })
369 log.debug("Routing init() is called");
370 this.topologyBWAware = (ConcurrentMap<Short, Graph<Node, Edge>>) new ConcurrentHashMap();
371 this.sptBWAware = (ConcurrentMap<Short, DijkstraShortestPath<Node, Edge>>) new ConcurrentHashMap();
372 // Now create the default topology, which doesn't consider the
373 // BW, also create the corresponding Dijkstra calculation
374 Graph<Node, Edge> g = new SparseMultigraph();
375 Short sZero = Short.valueOf((short) 0);
376 this.topologyBWAware.put(sZero, g);
377 this.sptBWAware.put(sZero, new DijkstraShortestPath(g));
378 // Topologies for other BW will be added on a needed base
381 * Function called by the dependency manager when at least one dependency
382 * become unsatisfied or when the component is shutting down because for
383 * example bundle is being stopped.
387 log.debug("Routing destroy() is called");
391 * Function called by dependency manager after "init ()" is called
392 * and after the services provided by the class are registered in
393 * the service registry
397 log.debug("Routing start() is called");
398 // build the routing database from the topology if it exists.
399 Map<Edge, Set<Property>> edges = topologyManager.getEdges();
400 if (edges.isEmpty()) {
403 log.debug("Creating routing database from the topology");
404 for (Iterator<Map.Entry<Edge,Set<Property>>> i = edges.entrySet().iterator(); i.hasNext();) {
405 Map.Entry<Edge, Set<Property>> entry = i.next();
406 Edge e = entry.getKey();
407 Set<Property> props = entry.getValue();
408 edgeUpdate(e, UpdateType.ADDED, props);
413 * Function called by the dependency manager before the services exported by
414 * the component are unregistered, this will be followed by a "destroy ()"
419 log.debug("Routing stop() is called");
423 public void edgeOverUtilized(Edge edge) {
424 // TODO Auto-generated method stub
429 public void edgeUtilBackToNormal(Edge edge) {
430 // TODO Auto-generated method stub
434 public void setSwitchManager(ISwitchManager switchManager) {
435 this.switchManager = switchManager;
438 public void unsetSwitchManager(ISwitchManager switchManager) {
439 if (this.switchManager == switchManager) {
440 this.switchManager = null;
444 public void setReadService(IReadService readService) {
445 this.readService = readService;
448 public void unsetReadService(IReadService readService) {
449 if (this.readService == readService) {
450 this.readService = null;
454 public void setTopologyManager(ITopologyManager tm) {
455 this.topologyManager = tm;
458 public void unsetTopologyManager(ITopologyManager tm) {
459 if (this.topologyManager == tm) {
460 this.topologyManager = null;