HIGUCHI Yuta | 4e64f31 | 2013-06-12 14:40:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.routing; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 5 | import java.util.Collections; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 6 | import java.util.HashMap; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 7 | import java.util.HashSet; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 8 | import java.util.LinkedList; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 9 | import java.util.List; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 10 | import java.util.Map; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 11 | import java.util.Queue; |
| 12 | import java.util.Set; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 13 | |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 14 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 15 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 16 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService; |
| 17 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 19 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 20 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 21 | import net.onrc.onos.ofcontroller.util.Port; |
| 22 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 23 | |
| 24 | import org.openflow.util.HexString; |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 27 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 28 | import com.tinkerpop.blueprints.Direction; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 29 | import com.tinkerpop.blueprints.Vertex; |
Pavlin Radoslavov | bfae5e0 | 2013-03-19 23:06:36 -0700 | [diff] [blame] | 30 | import com.tinkerpop.pipes.PipeFunction; |
| 31 | import com.tinkerpop.pipes.branch.LoopPipe.LoopBundle; |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 32 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A class for storing Node and Link information for fast computation |
| 36 | * of shortest paths. |
| 37 | */ |
| 38 | class Node { |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 39 | /** |
| 40 | * A class for storing Link information for fast computation of shortest |
| 41 | * paths. |
| 42 | */ |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 43 | class Link { |
| 44 | public Node me; // The node this link originates from |
| 45 | public Node neighbor; // The neighbor node on the other side |
| 46 | public short myPort; // Local port number for the link |
| 47 | public short neighborPort; // Neighbor port number for the link |
| 48 | |
| 49 | /** |
| 50 | * Link constructor. |
| 51 | * |
| 52 | * @param me the node this link originates from. |
| 53 | * @param the neighbor node on the other side of the link. |
| 54 | * @param myPort local port number for the link. |
| 55 | * @param neighborPort neighrobr port number for the link. |
| 56 | */ |
| 57 | public Link(Node me, Node neighbor, short myPort, short neighborPort) { |
| 58 | this.me = me; |
| 59 | this.neighbor = neighbor; |
| 60 | this.myPort = myPort; |
| 61 | this.neighborPort = neighborPort; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | public long nodeId; // The node ID |
Pavlin Radoslavov | 16cbd37 | 2013-04-08 14:14:50 -0700 | [diff] [blame] | 66 | public HashMap<Short, Link> links; // The links originating from this node |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Node constructor. |
| 70 | * |
| 71 | * @param nodeId the node ID. |
| 72 | */ |
| 73 | public Node(long nodeId) { |
| 74 | this.nodeId = nodeId; |
Pavlin Radoslavov | 16cbd37 | 2013-04-08 14:14:50 -0700 | [diff] [blame] | 75 | links = new HashMap<Short, Link>(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Add a neighbor. |
| 80 | * |
| 81 | * A new link to the neighbor will be created. |
| 82 | * |
| 83 | * @param neighbor the neighbor to add. |
| 84 | * @param myPort the local port number for the link to the neighbor. |
| 85 | * @param neighborPort the neighbor port number for the link. |
| 86 | */ |
| 87 | public void addNeighbor(Node neighbor, short myPort, short neighborPort) { |
| 88 | Link link = new Link(this, neighbor, myPort, neighborPort); |
Pavlin Radoslavov | 16cbd37 | 2013-04-08 14:14:50 -0700 | [diff] [blame] | 89 | links.put(myPort, link); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 90 | } |
| 91 | }; |
| 92 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 93 | /** |
| 94 | * A class for implementing Topology Route Service. |
| 95 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 96 | public class TopoRouteService implements ITopoRouteService { |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 97 | |
| 98 | /** The logger. */ |
Pavlin Radoslavov | 1934343 | 2013-03-06 10:43:18 -0800 | [diff] [blame] | 99 | private static Logger log = |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 100 | LoggerFactory.getLogger(TopoRouteService.class); |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 101 | |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 102 | protected GraphDBOperation op; |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 103 | |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 104 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 105 | /** |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 106 | * Default constructor. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 107 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 108 | public TopoRouteService() { |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 111 | /** |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 112 | * Constructor for given database configuration file. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 113 | * |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 114 | * @param config the database configuration file to use for |
| 115 | * the initialization. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 116 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 117 | public TopoRouteService(String config) { |
| 118 | this.init(config); |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Init the module. |
| 123 | * |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 124 | * @param config the database configuration file to use for |
| 125 | * the initialization. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 126 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 127 | public void init(String config) { |
| 128 | try { |
| 129 | op = new GraphDBOperation(config); |
| 130 | } catch (Exception e) { |
| 131 | log.error(e.getMessage()); |
| 132 | } |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 135 | /** |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 136 | * Close the service. It will close the corresponding database connection. |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 137 | */ |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 138 | public void close() { |
| 139 | op.close(); |
Pavlin Radoslavov | d7d8b79 | 2013-02-22 10:24:38 -0800 | [diff] [blame] | 140 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 141 | |
Pavlin Radoslavov | c5d2fbc | 2013-06-27 18:15:56 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Set the database operation handler. |
| 144 | * |
| 145 | * @param init_op the database operation handler to use for the |
| 146 | * initialization. |
| 147 | */ |
Pavlin Radoslavov | cec899a | 2013-06-27 15:47:50 -0700 | [diff] [blame] | 148 | public void setDbOperationHandler(GraphDBOperation init_op) { |
| 149 | op = init_op; |
| 150 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 151 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Fetch the Switch and Ports info from the Titan Graph |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 154 | * and return it for fast access during the shortest path |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 155 | * computation. |
| 156 | * |
| 157 | * After fetching the state, method @ref getTopoShortestPath() |
| 158 | * can be used for fast shortest path computation. |
| 159 | * |
| 160 | * Note: There is certain cost to fetch the state, hence it should |
| 161 | * be used only when there is a large number of shortest path |
| 162 | * computations that need to be done on the same topology. |
| 163 | * Typically, a single call to @ref prepareShortestPathTopo() |
| 164 | * should be followed by a large number of calls to |
| 165 | * method @ref getTopoShortestPath(). |
| 166 | * After the last @ref getTopoShortestPath() call, |
| 167 | * method @ref dropShortestPathTopo() should be used to release |
| 168 | * the internal state that is not needed anymore: |
| 169 | * |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 170 | * Map<Long, ?> shortestPathTopo; |
| 171 | * shortestPathTopo = prepareShortestPathTopo(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 172 | * for (int i = 0; i < 10000; i++) { |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 173 | * dataPath = getTopoShortestPath(shortestPathTopo, ...); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 174 | * ... |
| 175 | * } |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 176 | * dropShortestPathTopo(shortestPathTopo); |
| 177 | * |
| 178 | * @return the Shortest Path info handler stored in a map. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 179 | */ |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 180 | public Map<Long, ?> prepareShortestPathTopo() { |
| 181 | Map<Long, Node> shortestPathTopo = new HashMap<Long, Node>(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 182 | |
| 183 | // |
| 184 | // Fetch the relevant info from the Switch and Port vertices |
| 185 | // from the Titan Graph. |
| 186 | // |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 187 | Iterable<ISwitchObject> nodes = op.getActiveSwitches(); |
Pavlin Radoslavov | 010f286 | 2013-03-28 16:44:20 -0700 | [diff] [blame] | 188 | for (ISwitchObject switchObj : nodes) { |
| 189 | Vertex nodeVertex = switchObj.asVertex(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 190 | // |
| 191 | // The Switch info |
| 192 | // |
| 193 | String nodeDpid = nodeVertex.getProperty("dpid").toString(); |
| 194 | long nodeId = HexString.toLong(nodeDpid); |
| 195 | Node me = shortestPathTopo.get(nodeId); |
| 196 | if (me == null) { |
| 197 | me = new Node(nodeId); |
| 198 | shortestPathTopo.put(nodeId, me); |
| 199 | } |
| 200 | |
| 201 | // |
| 202 | // The local Port info |
| 203 | // |
| 204 | for (Vertex myPortVertex : nodeVertex.getVertices(Direction.OUT, "on")) { |
| 205 | short myPort = 0; |
| 206 | Object obj = myPortVertex.getProperty("number"); |
| 207 | if (obj instanceof Short) { |
| 208 | myPort = (Short)obj; |
| 209 | } else if (obj instanceof Integer) { |
| 210 | Integer int_nodeId = (Integer)obj; |
| 211 | myPort = int_nodeId.shortValue(); |
| 212 | } |
| 213 | |
| 214 | // |
| 215 | // The neighbor Port info |
| 216 | // |
| 217 | for (Vertex neighborPortVertex : myPortVertex.getVertices(Direction.OUT, "link")) { |
| 218 | short neighborPort = 0; |
| 219 | obj = neighborPortVertex.getProperty("number"); |
| 220 | if (obj instanceof Short) { |
| 221 | neighborPort = (Short)obj; |
| 222 | } else if (obj instanceof Integer) { |
| 223 | Integer int_nodeId = (Integer)obj; |
| 224 | neighborPort = int_nodeId.shortValue(); |
| 225 | } |
| 226 | // |
| 227 | // The neighbor Switch info |
| 228 | // |
| 229 | for (Vertex neighborVertex : neighborPortVertex.getVertices(Direction.IN, "on")) { |
Pavlin Radoslavov | 010f286 | 2013-03-28 16:44:20 -0700 | [diff] [blame] | 230 | // Ignore inactive switches |
| 231 | String state = neighborVertex.getProperty("state").toString(); |
| 232 | if (! state.equals(SwitchState.ACTIVE.toString())) |
| 233 | continue; |
| 234 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 235 | String neighborDpid = neighborVertex.getProperty("dpid").toString(); |
| 236 | long neighborId = HexString.toLong(neighborDpid); |
| 237 | Node neighbor = shortestPathTopo.get(neighborId); |
| 238 | if (neighbor == null) { |
| 239 | neighbor = new Node(neighborId); |
| 240 | shortestPathTopo.put(neighborId, neighbor); |
| 241 | } |
| 242 | me.addNeighbor(neighbor, myPort, neighborPort); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 247 | op.commit(); |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 248 | |
| 249 | return shortestPathTopo; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Release the state that was populated by |
| 254 | * method @ref prepareShortestPathTopo(). |
| 255 | * |
| 256 | * See the documentation for method @ref prepareShortestPathTopo() |
| 257 | * for additional information and usage. |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 258 | * |
Pavlin Radoslavov | ed13a24 | 2013-06-20 17:37:20 -0700 | [diff] [blame] | 259 | * @param shortestPathTopo the Shortest Path info handler to release. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 260 | */ |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 261 | public void dropShortestPathTopo(Map<Long, ?> shortestPathTopo) { |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 262 | shortestPathTopo = null; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Get the shortest path from a source to a destination by |
| 267 | * using the pre-populated local topology state prepared |
| 268 | * by method @ref prepareShortestPathTopo(). |
| 269 | * |
| 270 | * See the documentation for method @ref prepareShortestPathTopo() |
| 271 | * for additional information and usage. |
| 272 | * |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 273 | * @param shortestPathTopoHandler the Shortest Path info handler |
| 274 | * to use. |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 275 | * @param src the source in the shortest path computation. |
| 276 | * @param dest the destination in the shortest path computation. |
| 277 | * @return the data path with the computed shortest path if |
| 278 | * found, otherwise null. |
| 279 | */ |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 280 | public DataPath getTopoShortestPath(Map<Long, ?> shortestPathTopoHandler, |
| 281 | SwitchPort src, SwitchPort dest) { |
| 282 | @SuppressWarnings("unchecked") |
| 283 | Map<Long, Node> shortestPathTopo = (Map)shortestPathTopoHandler; |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 284 | DataPath result_data_path = new DataPath(); |
| 285 | |
| 286 | // Initialize the source and destination in the data path to return |
| 287 | result_data_path.setSrcPort(src); |
| 288 | result_data_path.setDstPort(dest); |
| 289 | |
| 290 | String dpid_src = src.dpid().toString(); |
| 291 | String dpid_dest = dest.dpid().toString(); |
| 292 | |
| 293 | // Get the source vertex |
| 294 | Node v_src = shortestPathTopo.get(src.dpid().value()); |
| 295 | if (v_src == null) { |
| 296 | return null; // Source vertex not found |
| 297 | } |
| 298 | |
| 299 | // Get the destination vertex |
| 300 | Node v_dest = shortestPathTopo.get(dest.dpid().value()); |
| 301 | if (v_dest == null) { |
| 302 | return null; // Destination vertex not found |
| 303 | } |
| 304 | |
| 305 | // |
| 306 | // Test whether we are computing a path from/to the same DPID. |
| 307 | // If "yes", then just add a single flow entry in the return result. |
| 308 | // |
| 309 | if (dpid_src.equals(dpid_dest)) { |
| 310 | FlowEntry flowEntry = new FlowEntry(); |
| 311 | flowEntry.setDpid(src.dpid()); |
| 312 | flowEntry.setInPort(src.port()); |
| 313 | flowEntry.setOutPort(dest.port()); |
| 314 | result_data_path.flowEntries().add(flowEntry); |
| 315 | return result_data_path; |
| 316 | } |
| 317 | |
| 318 | // |
| 319 | // Implement the Shortest Path computation by using Breath First Search |
| 320 | // |
| 321 | Set<Node> visitedSet = new HashSet<Node>(); |
| 322 | Queue<Node> processingList = new LinkedList<Node>(); |
| 323 | Map<Node, Node.Link> previousVertexMap = new HashMap<Node, Node.Link>(); |
| 324 | processingList.add(v_src); |
| 325 | visitedSet.add(v_src); |
| 326 | Boolean path_found = false; |
| 327 | while (! processingList.isEmpty()) { |
| 328 | Node nextVertex = processingList.poll(); |
| 329 | if (v_dest == nextVertex) { |
| 330 | path_found = true; |
| 331 | break; |
| 332 | } |
Pavlin Radoslavov | 16cbd37 | 2013-04-08 14:14:50 -0700 | [diff] [blame] | 333 | for (Node.Link link : nextVertex.links.values()) { |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 334 | Node child = link.neighbor; |
| 335 | if (! visitedSet.contains(child)) { |
| 336 | previousVertexMap.put(child, link); |
| 337 | visitedSet.add(child); |
| 338 | processingList.add(child); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | if (! path_found) |
| 343 | return null; // No path found |
| 344 | |
| 345 | // Collect the path as a list of links |
| 346 | List<Node.Link> resultPath = new LinkedList<Node.Link>(); |
| 347 | Node previousVertex = v_dest; |
| 348 | while (! v_src.equals(previousVertex)) { |
| 349 | Node.Link currentLink = previousVertexMap.get(previousVertex); |
| 350 | resultPath.add(currentLink); |
| 351 | previousVertex = currentLink.me; |
| 352 | } |
| 353 | Collections.reverse(resultPath); |
| 354 | |
| 355 | // |
| 356 | // Loop through the result and prepare the return result |
| 357 | // as a list of Flow Entries. |
| 358 | // |
| 359 | Port inPort = new Port(src.port().value()); |
| 360 | Port outPort; |
| 361 | for (Node.Link link: resultPath) { |
| 362 | // Setup the outgoing port, and add the Flow Entry |
Pavlin Radoslavov | a1ff119 | 2013-03-29 04:11:32 -0700 | [diff] [blame] | 363 | outPort = new Port(link.myPort); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 364 | |
| 365 | FlowEntry flowEntry = new FlowEntry(); |
| 366 | flowEntry.setDpid(new Dpid(link.me.nodeId)); |
| 367 | flowEntry.setInPort(inPort); |
| 368 | flowEntry.setOutPort(outPort); |
| 369 | result_data_path.flowEntries().add(flowEntry); |
| 370 | |
| 371 | // Setup the next incoming port |
| 372 | inPort = new Port(link.neighborPort); |
| 373 | } |
| 374 | if (resultPath.size() > 0) { |
| 375 | // Add the last Flow Entry |
| 376 | FlowEntry flowEntry = new FlowEntry(); |
| 377 | flowEntry.setDpid(new Dpid(dest.dpid().value())); |
| 378 | flowEntry.setInPort(inPort); |
| 379 | flowEntry.setOutPort(dest.port()); |
| 380 | result_data_path.flowEntries().add(flowEntry); |
| 381 | } |
| 382 | |
| 383 | if (result_data_path.flowEntries().size() > 0) |
| 384 | return result_data_path; |
| 385 | |
| 386 | return null; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Get the shortest path from a source to a destination. |
| 391 | * |
| 392 | * @param src the source in the shortest path computation. |
| 393 | * @param dest the destination in the shortest path computation. |
| 394 | * @return the data path with the computed shortest path if |
| 395 | * found, otherwise null. |
| 396 | */ |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 397 | @Override |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 398 | public DataPath getShortestPath(SwitchPort src, SwitchPort dest) { |
| 399 | DataPath result_data_path = new DataPath(); |
| 400 | |
| 401 | // Initialize the source and destination in the data path to return |
| 402 | result_data_path.setSrcPort(src); |
| 403 | result_data_path.setDstPort(dest); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 404 | |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 405 | String dpid_src = src.dpid().toString(); |
| 406 | String dpid_dest = dest.dpid().toString(); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 407 | |
Pavlin Radoslavov | 010f286 | 2013-03-28 16:44:20 -0700 | [diff] [blame] | 408 | // Get the source and destination switches |
| 409 | ISwitchObject srcSwitch = |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 410 | op.searchActiveSwitch(dpid_src); |
Pavlin Radoslavov | 010f286 | 2013-03-28 16:44:20 -0700 | [diff] [blame] | 411 | ISwitchObject destSwitch = |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 412 | op.searchActiveSwitch(dpid_dest); |
Pavlin Radoslavov | 010f286 | 2013-03-28 16:44:20 -0700 | [diff] [blame] | 413 | if (srcSwitch == null || destSwitch == null) { |
| 414 | return null; |
| 415 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 416 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 417 | // |
| 418 | // Test whether we are computing a path from/to the same DPID. |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 419 | // If "yes", then just add a single flow entry in the return result. |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 420 | // |
| 421 | if (dpid_src.equals(dpid_dest)) { |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 422 | FlowEntry flowEntry = new FlowEntry(); |
| 423 | flowEntry.setDpid(src.dpid()); |
| 424 | flowEntry.setInPort(src.port()); |
| 425 | flowEntry.setOutPort(dest.port()); |
| 426 | result_data_path.flowEntries().add(flowEntry); |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 427 | op.commit(); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 428 | return result_data_path; |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 429 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 430 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 431 | Vertex v_src = srcSwitch.asVertex(); |
| 432 | Vertex v_dest = destSwitch.asVertex(); |
| 433 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 434 | // |
| 435 | // Implement the Shortest Path computation by using Breath First Search |
| 436 | // |
| 437 | Set<Vertex> visitedSet = new HashSet<Vertex>(); |
| 438 | Queue<Vertex> processingList = new LinkedList<Vertex>(); |
| 439 | Map<Vertex, Vertex> previousVertexMap = new HashMap<Vertex, Vertex>(); |
| 440 | |
| 441 | processingList.add(v_src); |
| 442 | visitedSet.add(v_src); |
| 443 | Boolean path_found = false; |
| 444 | while (! processingList.isEmpty()) { |
| 445 | Vertex nextVertex = processingList.poll(); |
| 446 | if (v_dest.equals(nextVertex)) { |
| 447 | path_found = true; |
| 448 | break; |
| 449 | } |
| 450 | for (Vertex parentPort : nextVertex.getVertices(Direction.OUT, "on")) { |
| 451 | for (Vertex childPort : parentPort.getVertices(Direction.OUT, "link")) { |
| 452 | for (Vertex child : childPort.getVertices(Direction.IN, "on")) { |
Pavlin Radoslavov | 010f286 | 2013-03-28 16:44:20 -0700 | [diff] [blame] | 453 | // Ignore inactive switches |
| 454 | String state = child.getProperty("state").toString(); |
| 455 | if (! state.equals(SwitchState.ACTIVE.toString())) |
| 456 | continue; |
| 457 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 458 | if (! visitedSet.contains(child)) { |
| 459 | previousVertexMap.put(parentPort, nextVertex); |
| 460 | previousVertexMap.put(childPort, parentPort); |
| 461 | previousVertexMap.put(child, childPort); |
| 462 | visitedSet.add(child); |
| 463 | processingList.add(child); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | if (! path_found) |
| 470 | return null; // No path found |
| 471 | |
| 472 | List<Vertex> resultPath = new LinkedList<Vertex>(); |
| 473 | Vertex previousVertex = v_dest; |
| 474 | resultPath.add(v_dest); |
| 475 | while (! v_src.equals(previousVertex)) { |
| 476 | Vertex currentVertex = previousVertexMap.get(previousVertex); |
| 477 | resultPath.add(currentVertex); |
| 478 | previousVertex = currentVertex; |
| 479 | } |
| 480 | Collections.reverse(resultPath); |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 481 | |
Pankaj Berde | 1519309 | 2013-03-21 17:30:14 -0700 | [diff] [blame] | 482 | |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 483 | // |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 484 | // Loop through the result and prepare the return result |
| 485 | // as a list of Flow Entries. |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 486 | // |
| 487 | long nodeId = 0; |
| 488 | short portId = 0; |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 489 | Port inPort = new Port(src.port().value()); |
| 490 | Port outPort = new Port(); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 491 | int idx = 0; |
| 492 | for (Vertex v: resultPath) { |
| 493 | String type = v.getProperty("type").toString(); |
| 494 | // System.out.println("type: " + type); |
| 495 | if (type.equals("port")) { |
| 496 | String number = v.getProperty("number").toString(); |
| 497 | // System.out.println("number: " + number); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 498 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 499 | Object obj = v.getProperty("number"); |
| 500 | // String class_str = obj.getClass().toString(); |
| 501 | if (obj instanceof Short) { |
| 502 | portId = (Short)obj; |
| 503 | } else if (obj instanceof Integer) { |
| 504 | Integer int_nodeId = (Integer)obj; |
| 505 | portId = int_nodeId.shortValue(); |
| 506 | // int int_nodeId = (Integer)obj; |
| 507 | // portId = (short)int_nodeId.; |
| 508 | } |
| 509 | } else if (type.equals("switch")) { |
| 510 | String dpid = v.getProperty("dpid").toString(); |
| 511 | nodeId = HexString.toLong(dpid); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 512 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 513 | // System.out.println("dpid: " + dpid); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 514 | } |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 515 | idx++; |
| 516 | if (idx == 1) { |
| 517 | continue; |
| 518 | } |
| 519 | int mod = idx % 3; |
| 520 | if (mod == 0) { |
| 521 | // Setup the incoming port |
| 522 | inPort = new Port(portId); |
| 523 | continue; |
| 524 | } |
| 525 | if (mod == 2) { |
| 526 | // Setup the outgoing port, and add the Flow Entry |
| 527 | outPort = new Port(portId); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 528 | |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 529 | FlowEntry flowEntry = new FlowEntry(); |
| 530 | flowEntry.setDpid(new Dpid(nodeId)); |
| 531 | flowEntry.setInPort(inPort); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 532 | flowEntry.setOutPort(outPort); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 533 | result_data_path.flowEntries().add(flowEntry); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 534 | continue; |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 535 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 536 | } |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 537 | if (idx > 0) { |
| 538 | // Add the last Flow Entry |
| 539 | FlowEntry flowEntry = new FlowEntry(); |
| 540 | flowEntry.setDpid(new Dpid(nodeId)); |
| 541 | flowEntry.setInPort(inPort); |
| 542 | flowEntry.setOutPort(dest.port()); |
| 543 | result_data_path.flowEntries().add(flowEntry); |
| 544 | } |
| 545 | |
Toshio Koide | 18e4720 | 2013-06-13 14:07:13 -0700 | [diff] [blame] | 546 | op.commit(); |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 547 | if (result_data_path.flowEntries().size() > 0) |
| 548 | return result_data_path; |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 549 | |
| 550 | return null; |
| 551 | } |
| 552 | |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 553 | /** |
| 554 | * Test whether a route exists from a source to a destination. |
| 555 | * |
| 556 | * @param src the source node for the test. |
| 557 | * @param dest the destination node for the test. |
| 558 | * @return true if a route exists, otherwise false. |
| 559 | */ |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 560 | @Override |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 561 | public Boolean routeExists(SwitchPort src, SwitchPort dest) { |
| 562 | DataPath dataPath = getShortestPath(src, dest); |
Pavlin Radoslavov | a5f167b | 2013-03-21 11:39:27 -0700 | [diff] [blame] | 563 | return (dataPath != null); |
Pavlin Radoslavov | f34c290 | 2013-02-22 10:33:34 -0800 | [diff] [blame] | 564 | } |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 565 | } |