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.core.NodeConnector.NodeConnectorIDType;
29 import org.opendaylight.controller.sal.reader.IReadService;
30 import org.opendaylight.controller.sal.routing.IListenRoutingUpdates;
31 import org.opendaylight.controller.sal.routing.IRouting;
32 import org.opendaylight.controller.sal.topology.IListenTopoUpdates;
33 import org.opendaylight.controller.switchmanager.ISwitchManager;
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.List;
45 import java.util.concurrent.ConcurrentHashMap;
46 import java.util.concurrent.ConcurrentMap;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.apache.commons.collections15.Transformer;
51 public class DijkstraImplementation implements IRouting, IListenTopoUpdates {
52 private static Logger log = LoggerFactory
53 .getLogger(DijkstraImplementation.class);
54 private ConcurrentMap<Short, Graph<Node, Edge>> topologyBWAware;
55 private ConcurrentMap<Short, DijkstraShortestPath<Node, Edge>> sptBWAware;
56 DijkstraShortestPath<Node, Edge> mtp; //Max Throughput Path
57 private Set<IListenRoutingUpdates> routingAware;
58 private ISwitchManager switchManager;
59 private IReadService readService;
60 private static final long DEFAULT_LINK_SPEED = Bandwidth.BW1Gbps;
62 public void setLIstenRoutingUpdates(IListenRoutingUpdates i) {
63 if (this.routingAware == null) {
64 this.routingAware = new HashSet<IListenRoutingUpdates>();
66 if (this.routingAware != null) {
67 log.debug("Adding routingAware listener");
68 this.routingAware.add(i);
72 public void unsetRoutingUpdates(IListenRoutingUpdates i) {
73 if (this.routingAware == null) {
76 log.debug("Removing routingAware listener");
77 this.routingAware.remove(i);
78 if (this.routingAware.isEmpty()) {
79 // We don't have any listener lets dereference
80 this.routingAware = null;
84 @SuppressWarnings( { "unchecked", "rawtypes" })
85 public DijkstraImplementation() {
86 this.topologyBWAware = (ConcurrentMap<Short, Graph<Node, Edge>>) new ConcurrentHashMap();
87 this.sptBWAware = (ConcurrentMap<Short, DijkstraShortestPath<Node, Edge>>) new ConcurrentHashMap();
88 // Now create the default topology, which doesn't consider the
89 // BW, also create the corresponding Dijkstra calculation
90 Graph<Node, Edge> g = new SparseMultigraph();
91 Short sZero = Short.valueOf((short) 0);
92 this.topologyBWAware.put(sZero, g);
93 this.sptBWAware.put(sZero, new DijkstraShortestPath(g));
94 // Topologies for other BW will be added on a needed base
98 public synchronized void initMaxThroughput(
99 final Map<Edge, Number> EdgeWeightMap) {
101 log.error("Max Throughput Dijkstra is already enabled!");
104 Transformer<Edge, ? extends Number> mtTransformer = null;
105 if (EdgeWeightMap == null) {
106 mtTransformer = new Transformer<Edge, Double>() {
107 public Double transform(Edge e) {
108 if (switchManager == null) {
109 log.error("switchManager is null");
112 NodeConnector srcNC = e.getTailNodeConnector();
113 NodeConnector dstNC = e.getHeadNodeConnector();
114 if ((srcNC == null) || (dstNC == null)) {
115 log.error("srcNC:{} or dstNC:{} is null", srcNC, dstNC);
118 Bandwidth bwSrc = (Bandwidth) switchManager
119 .getNodeConnectorProp(srcNC,
120 Bandwidth.BandwidthPropName);
121 Bandwidth bwDst = (Bandwidth) switchManager
122 .getNodeConnectorProp(dstNC,
123 Bandwidth.BandwidthPropName);
125 if ((bwSrc == null) || (bwDst == null)) {
126 log.error("bwSrc:{} or bwDst:{} is null", bwSrc, bwDst);
130 long srcLinkSpeed = bwSrc.getValue();
131 if (srcLinkSpeed == 0) {
132 log.trace("Edge {}: srcLinkSpeed is 0. Setting to {}!",
133 e, DEFAULT_LINK_SPEED);
134 srcLinkSpeed = DEFAULT_LINK_SPEED;
137 long dstLinkSpeed = bwDst.getValue();
138 if (dstLinkSpeed == 0) {
139 log.trace("Edge {}: dstLinkSpeed is 0. Setting to {}!",
140 e, DEFAULT_LINK_SPEED);
141 dstLinkSpeed = DEFAULT_LINK_SPEED;
144 long avlSrcThruPut = srcLinkSpeed
145 - readService.getTransmitRate(srcNC);
146 long avlDstThruPut = dstLinkSpeed
147 - readService.getTransmitRate(dstNC);
149 //Use lower of the 2 available thruput as the available thruput
150 long avlThruPut = avlSrcThruPut < avlDstThruPut ? avlSrcThruPut
153 if (avlThruPut <= 0) {
156 "Edge {}: Available Throughput {} is Zero/Negative",
160 return (double) (Bandwidth.BW1Pbps / avlThruPut);
164 mtTransformer = new Transformer<Edge, Number>() {
165 public Number transform(Edge e) {
166 return EdgeWeightMap.get(e);
170 Short baseBW = Short.valueOf((short) 0);
171 //Initialize mtp also using the default topo
172 Graph<Node, Edge> g = this.topologyBWAware.get(baseBW);
174 log.error("Default Topology Graph is null");
177 mtp = new DijkstraShortestPath<Node, Edge>(g, mtTransformer);
181 public Path getRoute(Node src, Node dst) {
182 if (src == null || dst == null) {
185 return getRoute(src, dst, (short) 0);
189 public synchronized Path getMaxThroughputRoute(Node src, Node dst) {
192 .error("Max Throughput Path Calculation has not been Initialized!");
198 path = mtp.getMaxThroughputPath(src, dst);
199 } catch (IllegalArgumentException ie) {
200 log.debug("A vertex is yet not known between " + src.toString()
201 + " " + dst.toString());
206 res = new Path(path);
207 } catch (ConstructionException e) {
208 log.debug("A vertex is yet not known between " + src.toString()
209 + " " + dst.toString());
216 public synchronized Path getRoute(Node src, Node dst, Short Bw) {
217 DijkstraShortestPath<Node, Edge> spt = this.sptBWAware.get(Bw);
222 path = spt.getPath(src, dst);
223 } catch (IllegalArgumentException ie) {
224 log.debug("A vertex is yet not known between " + src.toString()
225 + " " + dst.toString());
230 res = new Path(path);
231 } catch (ConstructionException e) {
232 log.debug("A vertex is yet not known between " + src.toString()
233 + " " + dst.toString());
240 public synchronized void clear() {
241 DijkstraShortestPath<Node, Edge> spt;
242 for (Short bw : this.sptBWAware.keySet()) {
243 spt = this.sptBWAware.get(bw);
248 clearMaxThroughput();
252 public synchronized void clearMaxThroughput() {
254 mtp.reset(); //reset maxthruput path
258 @SuppressWarnings( { "rawtypes", "unchecked" })
259 private synchronized boolean updateTopo(Edge edge, Short bw, boolean added) {
260 Graph<Node, Edge> topo = this.topologyBWAware.get(bw);
261 DijkstraShortestPath<Node, Edge> spt = this.sptBWAware.get(bw);
262 boolean edgePresentInGraph = false;
263 Short baseBW = Short.valueOf((short) 0);
266 // Create topology for this BW
267 Graph<Node, Edge> g = new SparseMultigraph();
268 this.topologyBWAware.put(bw, g);
269 topo = this.topologyBWAware.get(bw);
270 this.sptBWAware.put(bw, new DijkstraShortestPath(g));
271 spt = this.sptBWAware.get(bw);
275 NodeConnector src = edge.getTailNodeConnector();
276 NodeConnector dst = edge.getHeadNodeConnector();
278 spt = new DijkstraShortestPath(topo);
279 this.sptBWAware.put(bw, spt);
283 // Make sure the vertex are there before adding the edge
284 topo.addVertex(src.getNode());
285 topo.addVertex(dst.getNode());
286 // Add the link between
287 edgePresentInGraph = topo.containsEdge(edge);
288 if (edgePresentInGraph == false) {
290 topo.addEdge(new Edge(src, dst), src
292 .getNode(), EdgeType.DIRECTED);
293 } catch (ConstructionException e) {
295 return edgePresentInGraph;
301 topo.removeEdge(new Edge(src, dst));
302 } catch (ConstructionException e) {
304 return edgePresentInGraph;
307 // If the src and dst vertex don't have incoming or
308 // outgoing links we can get ride of them
309 if (topo.containsVertex(src.getNode())
310 && topo.inDegree(src.getNode()) == 0
311 && topo.outDegree(src.getNode()) == 0) {
312 log.debug("Removing vertex " + src);
313 topo.removeVertex(src.getNode());
316 if (topo.containsVertex(dst.getNode())
317 && topo.inDegree(dst.getNode()) == 0
318 && topo.outDegree(dst.getNode()) == 0) {
319 log.debug("Removing vertex " + dst);
320 topo.removeVertex(dst.getNode());
324 if (bw.equals(baseBW)) {
325 clearMaxThroughput();
328 log.error("Cannot find topology for BW " + bw
329 + " this is unexpected!");
331 return edgePresentInGraph;
335 public void edgeUpdate(Edge e, UpdateType type, Set<Property> props) {
336 String srcType = null;
337 String dstType = null;
339 if (e == null || type == null) {
340 log.error("Edge or Update type are null!");
343 srcType = e.getTailNodeConnector().getType();
344 dstType = e.getHeadNodeConnector().getType();
346 if (srcType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
347 log.debug("Skip updates for {}", e);
351 if (dstType.equals(NodeConnector.NodeConnectorIDType.PRODUCTION)) {
352 log.debug("Skip updates for {}", e);
357 Bandwidth bw = new Bandwidth(0);
358 boolean newEdge = false;
362 log.debug("edgeUpdate: " + e.toString() + " bw: " + bw.getValue());
364 Short baseBW = Short.valueOf((short) 0);
365 boolean add = (type == UpdateType.ADDED) ? true : false;
367 newEdge = !updateTopo(e, baseBW, add);
368 if (newEdge == true) {
369 if (bw.getValue() != baseBW) {
371 updateTopo(e, (short) bw.getValue(), add);
373 if (this.routingAware != null) {
374 log.info("Invoking routingAware listeners");
375 for (IListenRoutingUpdates ra : this.routingAware) {
377 ra.recalculateDone();
378 } catch (Exception ex) {
379 log.error("Exception on routingAware listener call", e);
386 public void startUp() {
387 log.debug(this.getClass().getName() + ":startUp Method Called");
390 public void shutDown() {
391 log.debug(this.getClass().getName() + ":shutDown Method Called");
395 public void edgeOverUtilized(Edge edge) {
396 // TODO Auto-generated method stub
401 public void edgeUtilBackToNormal(Edge edge) {
402 // TODO Auto-generated method stub
406 public void setSwitchManager(ISwitchManager switchManager) {
407 this.switchManager = switchManager;
410 public void unsetSwitchManager(ISwitchManager switchManager) {
411 if (this.switchManager == switchManager) {
412 this.switchManager = null;
416 public void setReadService(IReadService readService) {
417 this.readService = readService;
420 public void unsetReadService(IReadService readService) {
421 if (this.readService == readService) {
422 this.readService = null;