2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 * @file DijkstraImplementation.java
13 * @brief Implementation of a routing engine using
14 * dijkstra. Implementation of dijkstra come from Jung2 library
17 package org.opendaylight.controller.routing.dijkstra_implementation.internal;
19 import org.opendaylight.controller.sal.core.Bandwidth;
20 import org.opendaylight.controller.sal.core.ConstructionException;
21 import org.opendaylight.controller.sal.core.Edge;
22 import org.opendaylight.controller.sal.core.Node;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.core.Path;
25 import org.opendaylight.controller.sal.core.Property;
26 import org.opendaylight.controller.sal.core.UpdateType;
27 import org.opendaylight.controller.sal.reader.IReadService;
28 import org.opendaylight.controller.sal.routing.IListenRoutingUpdates;
30 import org.opendaylight.controller.sal.routing.IRouting;
31 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
32 import org.opendaylight.controller.switchmanager.ISwitchManager;
33 import org.opendaylight.controller.topologymanager.ITopologyManager;
34 import org.opendaylight.controller.topologymanager.ITopologyManagerAware;
36 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
37 import edu.uci.ics.jung.graph.Graph;
38 import edu.uci.ics.jung.graph.SparseMultigraph;
39 import edu.uci.ics.jung.graph.util.EdgeType;
40 import java.lang.Exception;
41 import java.lang.IllegalArgumentException;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.ArrayList;
48 import java.util.concurrent.ConcurrentHashMap;
49 import java.util.concurrent.ConcurrentMap;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.apache.commons.collections15.Transformer;
54 public class DijkstraImplementation implements IRouting, ITopologyManagerAware {
55 private static Logger log = LoggerFactory
56 .getLogger(DijkstraImplementation.class);
57 private ConcurrentMap<Short, Graph<Node, Edge>> topologyBWAware;
58 private ConcurrentMap<Short, DijkstraShortestPath<Node, Edge>> sptBWAware;
59 DijkstraShortestPath<Node, Edge> mtp; // Max Throughput Path
60 private Set<IListenRoutingUpdates> routingAware;
61 private ISwitchManager switchManager;
62 private ITopologyManager topologyManager;
63 private IReadService readService;
64 private static final long DEFAULT_LINK_SPEED = Bandwidth.BW1Gbps;
66 public void setListenRoutingUpdates(IListenRoutingUpdates i) {
67 if (this.routingAware == null) {
68 this.routingAware = new HashSet<IListenRoutingUpdates>();
70 if (this.routingAware != null) {
71 log.debug("Adding routingAware listener: {}", i);
72 this.routingAware.add(i);
76 public void unsetListenRoutingUpdates(IListenRoutingUpdates i) {
77 if (this.routingAware == null) {
80 log.debug("Removing routingAware listener");
81 this.routingAware.remove(i);
82 if (this.routingAware.isEmpty()) {
83 // We don't have any listener lets dereference
84 this.routingAware = null;
89 public synchronized void initMaxThroughput(
90 final Map<Edge, Number> EdgeWeightMap) {
92 log.error("Max Throughput Dijkstra is already enabled!");
95 Transformer<Edge, ? extends Number> mtTransformer = null;
96 if (EdgeWeightMap == null) {
97 mtTransformer = new Transformer<Edge, Double>() {
98 public Double transform(Edge e) {
99 if (switchManager == null) {
100 log.error("switchManager is null");
103 NodeConnector srcNC = e.getTailNodeConnector();
104 NodeConnector dstNC = e.getHeadNodeConnector();
105 if ((srcNC == null) || (dstNC == null)) {
106 log.error("srcNC:{} or dstNC:{} is null", srcNC, dstNC);
109 Bandwidth bwSrc = (Bandwidth) switchManager
110 .getNodeConnectorProp(srcNC,
111 Bandwidth.BandwidthPropName);
112 Bandwidth bwDst = (Bandwidth) switchManager
113 .getNodeConnectorProp(dstNC,
114 Bandwidth.BandwidthPropName);
116 long srcLinkSpeed = 0, dstLinkSpeed = 0;
118 || ((srcLinkSpeed = bwSrc.getValue()) == 0)) {
120 "srcNC: {} - Setting srcLinkSpeed to Default!",
122 srcLinkSpeed = DEFAULT_LINK_SPEED;
126 || ((dstLinkSpeed = bwDst.getValue()) == 0)) {
128 "dstNC: {} - Setting dstLinkSpeed to Default!",
130 dstLinkSpeed = DEFAULT_LINK_SPEED;
133 long avlSrcThruPut = srcLinkSpeed
134 - readService.getTransmitRate(srcNC);
135 long avlDstThruPut = dstLinkSpeed
136 - readService.getTransmitRate(dstNC);
138 // Use lower of the 2 available thruput as the available
140 long avlThruPut = avlSrcThruPut < avlDstThruPut ? avlSrcThruPut
143 if (avlThruPut <= 0) {
144 log.debug("Edge {}: Available Throughput {} <= 0!", e,
148 return (double) (Bandwidth.BW1Pbps / avlThruPut);
152 mtTransformer = new Transformer<Edge, Number>() {
153 public Number transform(Edge e) {
154 return EdgeWeightMap.get(e);
158 Short baseBW = Short.valueOf((short) 0);
159 // Initialize mtp also using the default topo
160 Graph<Node, Edge> g = this.topologyBWAware.get(baseBW);
162 log.error("Default Topology Graph is null");
165 mtp = new DijkstraShortestPath<Node, Edge>(g, mtTransformer);
169 public Path getRoute(Node src, Node dst) {
170 if (src == null || dst == null) {
173 return getRoute(src, dst, (short) 0);
177 public synchronized Path getMaxThroughputRoute(Node src, Node dst) {
179 log.error("Max Throughput Path Calculation Uninitialized!");
185 path = mtp.getMaxThroughputPath(src, dst);
186 } catch (IllegalArgumentException ie) {
187 log.debug("A vertex is yet not known between {} {}", src, dst);
192 res = new Path(path);
193 } catch (ConstructionException e) {
194 log.debug("A vertex is yet not known between {} {}", src, dst);
201 public synchronized Path getRoute(Node src, Node dst, Short Bw) {
202 DijkstraShortestPath<Node, Edge> spt = this.sptBWAware.get(Bw);
207 path = spt.getPath(src, dst);
208 } catch (IllegalArgumentException ie) {
209 log.debug("A vertex is yet not known between {} {}", src, dst);
214 res = new Path(path);
215 } catch (ConstructionException e) {
216 log.debug("A vertex is yet not known between {} {}", src, dst);
223 public synchronized void clear() {
224 DijkstraShortestPath<Node, Edge> spt;
225 for (Short bw : this.sptBWAware.keySet()) {
226 spt = this.sptBWAware.get(bw);
231 clearMaxThroughput();
235 public synchronized void clearMaxThroughput() {
237 mtp.reset(); // reset maxthruput path
241 @SuppressWarnings({ "rawtypes", "unchecked" })
242 private synchronized boolean updateTopo(Edge edge, Short bw, boolean added) {
243 Graph<Node, Edge> topo = this.topologyBWAware.get(bw);
244 DijkstraShortestPath<Node, Edge> spt = this.sptBWAware.get(bw);
245 boolean edgePresentInGraph = false;
246 Short baseBW = Short.valueOf((short) 0);
249 // Create topology for this BW
250 Graph<Node, Edge> g = new SparseMultigraph();
251 this.topologyBWAware.put(bw, g);
252 topo = this.topologyBWAware.get(bw);
253 this.sptBWAware.put(bw, new DijkstraShortestPath(g));
254 spt = this.sptBWAware.get(bw);
258 NodeConnector src = edge.getTailNodeConnector();
259 NodeConnector dst = edge.getHeadNodeConnector();
261 spt = new DijkstraShortestPath(topo);
262 this.sptBWAware.put(bw, spt);
266 // Make sure the vertex are there before adding the edge
267 topo.addVertex(src.getNode());
268 topo.addVertex(dst.getNode());
269 // Add the link between
270 edgePresentInGraph = topo.containsEdge(edge);
271 if (edgePresentInGraph == false) {
273 topo.addEdge(new Edge(src, dst), src.getNode(),
274 dst.getNode(), EdgeType.DIRECTED);
275 } catch (ConstructionException e) {
277 return edgePresentInGraph;
283 topo.removeEdge(new Edge(src, dst));
284 } catch (ConstructionException e) {
286 return edgePresentInGraph;
289 // If the src and dst vertex don't have incoming or
290 // outgoing links we can get ride of them
291 if (topo.containsVertex(src.getNode())
292 && topo.inDegree(src.getNode()) == 0
293 && topo.outDegree(src.getNode()) == 0) {
294 log.debug("Removing vertex {}", src);
295 topo.removeVertex(src.getNode());
298 if (topo.containsVertex(dst.getNode())
299 && topo.inDegree(dst.getNode()) == 0
300 && topo.outDegree(dst.getNode()) == 0) {
301 log.debug("Removing vertex {}", dst);
302 topo.removeVertex(dst.getNode());
306 if (bw.equals(baseBW)) {
307 clearMaxThroughput();
310 log.error("Cannot find topology for BW {} this is unexpected!", bw);
312 return edgePresentInGraph;
315 private boolean edgeUpdate(Edge e, UpdateType type, Set<Property> props) {
316 String srcType = null;
317 String dstType = null;
319 if (e == null || type == null) {
320 log.error("Edge or Update type are null!");
323 srcType = e.getTailNodeConnector().getType();
324 dstType = e.getHeadNodeConnector().getType();
326 if (srcType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
327 log.debug("Skip updates for {}", e);
331 if (dstType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
332 log.debug("Skip updates for {}", e);
337 Bandwidth bw = new Bandwidth(0);
338 boolean newEdge = false;
342 if (log.isDebugEnabled()) {
343 log.debug("edgeUpdate: {} bw: {}", e, bw.getValue());
346 Short baseBW = Short.valueOf((short) 0);
347 boolean add = (type == UpdateType.ADDED) ? true : false;
349 newEdge = !updateTopo(e, baseBW, add);
350 if (newEdge == true) {
351 if (bw.getValue() != baseBW) {
353 updateTopo(e, (short) bw.getValue(), add);
360 public void edgeUpdate(List<TopoEdgeUpdate> topoedgeupdateList) {
361 boolean callListeners = false;
362 for (int i = 0; i < topoedgeupdateList.size(); i++) {
363 Edge e = topoedgeupdateList.get(i).getEdge();
364 Set<Property> p = topoedgeupdateList.get(i).getProperty();
365 UpdateType type = topoedgeupdateList.get(i).getUpdateType();
366 if ((edgeUpdate(e, type, p)) && (!callListeners)) {
367 callListeners = true;
370 if ((callListeners) && (this.routingAware != null)) {
371 for (IListenRoutingUpdates ra : this.routingAware) {
373 ra.recalculateDone();
374 } catch (Exception ex) {
375 log.error("Exception on routingAware listener call", ex);
382 * Function called by the dependency manager when all the required
383 * dependencies are satisfied
386 @SuppressWarnings({ "unchecked", "rawtypes" })
388 log.debug("Routing init() is called");
389 this.topologyBWAware = (ConcurrentMap<Short, Graph<Node, Edge>>) new ConcurrentHashMap();
390 this.sptBWAware = (ConcurrentMap<Short, DijkstraShortestPath<Node, Edge>>) new ConcurrentHashMap();
391 // Now create the default topology, which doesn't consider the
392 // BW, also create the corresponding Dijkstra calculation
393 Graph<Node, Edge> g = new SparseMultigraph();
394 Short sZero = Short.valueOf((short) 0);
395 this.topologyBWAware.put(sZero, g);
396 this.sptBWAware.put(sZero, new DijkstraShortestPath(g));
397 // Topologies for other BW will be added on a needed base
401 * Function called by the dependency manager when at least one dependency
402 * become unsatisfied or when the component is shutting down because for
403 * example bundle is being stopped.
407 log.debug("Routing destroy() is called");
411 * Function called by dependency manager after "init ()" is called and after
412 * the services provided by the class are registered in the service registry
416 log.debug("Routing start() is called");
417 // build the routing database from the topology if it exists.
418 Map<Edge, Set<Property>> edges = topologyManager.getEdges();
419 if (edges.isEmpty()) {
422 List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
423 log.debug("Creating routing database from the topology");
424 for (Iterator<Map.Entry<Edge, Set<Property>>> i = edges.entrySet()
425 .iterator(); i.hasNext();) {
426 Map.Entry<Edge, Set<Property>> entry = i.next();
427 Edge e = entry.getKey();
428 Set<Property> props = entry.getValue();
429 TopoEdgeUpdate topoedgeupdate = new TopoEdgeUpdate(e, props,
431 topoedgeupdateList.add(topoedgeupdate);
433 edgeUpdate(topoedgeupdateList);
437 * Function called by the dependency manager before the services exported by
438 * the component are unregistered, this will be followed by a "destroy ()"
443 log.debug("Routing stop() is called");
447 public void edgeOverUtilized(Edge edge) {
448 // TODO Auto-generated method stub
453 public void edgeUtilBackToNormal(Edge edge) {
454 // TODO Auto-generated method stub
458 public void setSwitchManager(ISwitchManager switchManager) {
459 this.switchManager = switchManager;
462 public void unsetSwitchManager(ISwitchManager switchManager) {
463 if (this.switchManager == switchManager) {
464 this.switchManager = null;
468 public void setReadService(IReadService readService) {
469 this.readService = readService;
472 public void unsetReadService(IReadService readService) {
473 if (this.readService == readService) {
474 this.readService = null;
478 public void setTopologyManager(ITopologyManager tm) {
479 this.topologyManager = tm;
482 public void unsetTopologyManager(ITopologyManager tm) {
483 if (this.topologyManager == tm) {
484 this.topologyManager = null;