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