Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.topology; |
| 2 | |
Pavlin Radoslavov | cfe7b40 | 2013-10-30 19:44:22 -0700 | [diff] [blame] | 3 | import java.util.List; |
| 4 | import java.util.LinkedList; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 5 | import java.util.Map; |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 6 | import java.util.TreeMap; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 7 | |
| 8 | import net.onrc.onos.graph.GraphDBOperation; |
| 9 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 10 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
| 11 | |
| 12 | import org.openflow.util.HexString; |
| 13 | |
| 14 | import com.tinkerpop.blueprints.Direction; |
| 15 | import com.tinkerpop.blueprints.Vertex; |
| 16 | |
| 17 | /** |
| 18 | * A class for storing Node and Link information for fast computation |
| 19 | * of shortest paths. |
| 20 | */ |
| 21 | class Node { |
| 22 | /** |
| 23 | * A class for storing Link information for fast computation of shortest |
| 24 | * paths. |
| 25 | */ |
| 26 | class Link { |
| 27 | public Node me; // The node this link originates from |
| 28 | public Node neighbor; // The neighbor node on the other side |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 29 | public int myPort; // Local port ID for the link |
| 30 | public int neighborPort; // Neighbor port ID for the link |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Link constructor. |
| 34 | * |
| 35 | * @param me the node this link originates from. |
| 36 | * @param the neighbor node on the other side of the link. |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 37 | * @param myPort local port ID for the link. |
| 38 | * @param neighborPort neighbor port ID for the link. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 39 | */ |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 40 | public Link(Node me, Node neighbor, int myPort, int neighborPort) { |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 41 | this.me = me; |
| 42 | this.neighbor = neighbor; |
| 43 | this.myPort = myPort; |
| 44 | this.neighborPort = neighborPort; |
| 45 | } |
| 46 | }; |
| 47 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 48 | public long nodeId; // The node ID |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 49 | public TreeMap<Integer, Link> links; // The links from this node: |
Pavlin Radoslavov | f9161ca | 2013-10-31 17:08:12 -0700 | [diff] [blame] | 50 | // (src PortID -> Link) |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 51 | private TreeMap<Integer, Link> reverseLinksMap; // The links to this node: |
Pavlin Radoslavov | f9161ca | 2013-10-31 17:08:12 -0700 | [diff] [blame] | 52 | // (dst PortID -> Link) |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 53 | private TreeMap<Integer, Integer> portsMap; // The ports on this node: |
Pavlin Radoslavov | f9161ca | 2013-10-31 17:08:12 -0700 | [diff] [blame] | 54 | // (PortID -> PortID) |
| 55 | // TODO: In the future will be: |
| 56 | // (PortID -> Port) |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Node constructor. |
| 60 | * |
| 61 | * @param nodeId the node ID. |
| 62 | */ |
| 63 | public Node(long nodeId) { |
| 64 | this.nodeId = nodeId; |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 65 | links = new TreeMap<Integer, Link>(); |
| 66 | reverseLinksMap = new TreeMap<Integer, Link>(); |
| 67 | portsMap = new TreeMap<Integer, Integer>(); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 71 | * Get all ports. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 72 | * |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 73 | * @return all ports. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 74 | */ |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 75 | public Map<Integer, Integer> ports() { |
| 76 | return portsMap; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the port for a given Port ID. |
| 81 | * |
| 82 | * Note: For now the port itself is just the Port ID. In the future |
| 83 | * it might contain more information. |
| 84 | * |
| 85 | * @return the port if found, otherwise null. |
| 86 | */ |
| 87 | public Integer getPort(int portId) { |
Yuta HIGUCHI | dbc8c44 | 2013-10-31 10:15:37 -0700 | [diff] [blame] | 88 | return portsMap.get(portId); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Add a port for a given Port ID. |
| 93 | * |
| 94 | * Note: For now the port itself is just the Port ID. In the future |
| 95 | * it might contain more information. |
| 96 | * |
| 97 | * @param portId the Port ID of the port to add. |
| 98 | * @return the added Port. |
| 99 | */ |
| 100 | Integer addPort(int portId) { |
| 101 | Integer port = new Integer(portId); |
| 102 | portsMap.put(portId, port); |
| 103 | return port; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Remove a port for a given Port ID. |
| 108 | * |
| 109 | * NOTE: The outgoing and incoming links using this port are removed as |
| 110 | * well. |
| 111 | */ |
| 112 | void removePort(int portId) { |
| 113 | // Remove the outgoing link |
| 114 | Link link = getLink(portId); |
| 115 | if (link != null) { |
| 116 | link.neighbor.removeReverseLink(link); |
| 117 | removeLink(portId); |
| 118 | } |
| 119 | |
| 120 | // Remove the incoming link |
| 121 | Link reverseLink = reverseLinksMap.get(portId); |
| 122 | if (reverseLink != null) { |
| 123 | // NOTE: reverseLink.myPort is the neighbor's outgoing port |
| 124 | reverseLink.neighbor.removeLink(reverseLink.myPort); |
| 125 | removeReverseLink(reverseLink); |
| 126 | } |
| 127 | |
| 128 | portsMap.remove(portId); |
| 129 | } |
Yuta HIGUCHI | 63ee5a8 | 2013-10-31 10:16:15 -0700 | [diff] [blame] | 130 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Get a link on a port to a neighbor. |
| 133 | * |
| 134 | * @param myPortId the local port ID for the link to the neighbor. |
| 135 | * @return the link if found, otherwise null. |
| 136 | */ |
| 137 | public Link getLink(int myPortId) { |
| 138 | return links.get(myPortId); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Add a link to a neighbor. |
| 143 | * |
| 144 | * @param myPortId the local port ID for the link to the neighbor. |
| 145 | * @param neighbor the neighbor for the link. |
| 146 | * @param neighborPortId the neighbor port ID for the link. |
| 147 | * @return the added Link. |
| 148 | */ |
| 149 | public Link addLink(int myPortId, Node neighbor, int neighborPortId) { |
| 150 | Link link = new Link(this, neighbor, myPortId, neighborPortId); |
| 151 | links.put(myPortId, link); |
| 152 | neighbor.addReverseLink(link); |
| 153 | return link; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Add a reverse link from a neighbor. |
| 158 | * |
| 159 | * @param link the reverse link from a neighbor to add. |
| 160 | */ |
| 161 | private void addReverseLink(Link link) { |
| 162 | // NOTE: link.neghborPort is my port |
| 163 | reverseLinksMap.put(link.neighborPort, link); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Remove a link to a neighbor. |
| 168 | * |
| 169 | * @param myPortId the local port ID for the link to the neighbor. |
| 170 | */ |
| 171 | public void removeLink(int myPortId) { |
| 172 | links.remove(myPortId); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Remove a reverse link from a neighbor. |
| 177 | * |
| 178 | * @param link the reverse link from a neighbor to remove. |
| 179 | */ |
| 180 | private void removeReverseLink(Link link) { |
| 181 | // NOTE: link.neghborPort is my port |
| 182 | reverseLinksMap.remove(link.neighborPort); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 183 | } |
| 184 | }; |
| 185 | |
| 186 | /** |
| 187 | * A class for storing topology information. |
| 188 | */ |
| 189 | public class Topology { |
| 190 | private Map<Long, Node> nodesMap; // The dpid->Node mapping |
| 191 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Default constructor. |
| 194 | */ |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 195 | public Topology() { |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 196 | nodesMap = new TreeMap<Long, Node>(); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 200 | * Add a topology element to the topology. |
| 201 | * |
| 202 | * @param topologyElement the topology element to add. |
| 203 | * @return true if the topology was modified, otherwise false. |
| 204 | */ |
| 205 | public boolean addTopologyElement(TopologyElement topologyElement) { |
| 206 | boolean isModified = false; |
| 207 | |
| 208 | switch (topologyElement.getType()) { |
| 209 | case ELEMENT_SWITCH: { |
| 210 | // Add the switch |
| 211 | Node node = getNode(topologyElement.getSwitch()); |
| 212 | if (node == null) { |
| 213 | node = addNode(topologyElement.getSwitch()); |
| 214 | isModified = true; |
| 215 | } |
| 216 | // Add the ports for the switch |
| 217 | for (Integer portId : topologyElement.getSwitchPorts().values()) { |
| 218 | Integer port = node.getPort(portId); |
| 219 | if (port == null) { |
| 220 | node.addPort(portId); |
| 221 | isModified = true; |
| 222 | } |
| 223 | } |
| 224 | break; |
| 225 | } |
| 226 | case ELEMENT_PORT: { |
| 227 | // Add the switch |
| 228 | Node node = getNode(topologyElement.getSwitch()); |
| 229 | if (node == null) { |
| 230 | node = addNode(topologyElement.getSwitch()); |
| 231 | isModified = true; |
| 232 | } |
| 233 | // Add the port for the switch |
| 234 | Integer port = node.getPort(topologyElement.getSwitchPort()); |
| 235 | if (port == null) { |
| 236 | node.addPort(topologyElement.getSwitchPort()); |
| 237 | isModified = true; |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | case ELEMENT_LINK: { |
| 242 | // Add the "from" switch |
| 243 | Node fromNode = getNode(topologyElement.getFromSwitch()); |
| 244 | if (fromNode == null) { |
| 245 | fromNode = addNode(topologyElement.getFromSwitch()); |
| 246 | isModified = true; |
| 247 | } |
| 248 | // Add the "to" switch |
| 249 | Node toNode = getNode(topologyElement.getToSwitch()); |
| 250 | if (toNode == null) { |
| 251 | toNode = addNode(topologyElement.getToSwitch()); |
| 252 | isModified = true; |
| 253 | } |
| 254 | // Add the "from" port |
| 255 | Integer fromPort = fromNode.getPort(topologyElement.getFromPort()); |
| 256 | if (fromPort == null) { |
| 257 | fromNode.addPort(topologyElement.getFromPort()); |
| 258 | isModified = true; |
| 259 | } |
| 260 | // Add the "to" port |
| 261 | Integer toPort = fromNode.getPort(topologyElement.getToPort()); |
| 262 | if (toPort == null) { |
| 263 | toNode.addPort(topologyElement.getToPort()); |
| 264 | isModified = true; |
| 265 | } |
| 266 | Node.Link link = fromNode.getLink(topologyElement.getFromPort()); |
| 267 | if (link == null) { |
| 268 | fromNode.addLink(topologyElement.getFromPort(), |
| 269 | toNode, |
| 270 | topologyElement.getToPort()); |
| 271 | isModified = true; |
| 272 | } |
| 273 | |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return isModified; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Remove a topology element from the topology. |
| 283 | * |
| 284 | * @param topologyElement the topology element to remove. |
| 285 | * @return true if the topology was modified, otherwise false. |
| 286 | */ |
| 287 | public boolean removeTopologyElement(TopologyElement topologyElement) { |
| 288 | boolean isModified = false; |
| 289 | |
| 290 | switch (topologyElement.getType()) { |
| 291 | case ELEMENT_SWITCH: { |
| 292 | // Remove the switch |
| 293 | Node node = getNode(topologyElement.getSwitch()); |
| 294 | if (node != null) { |
| 295 | removeNode(node); |
| 296 | isModified = true; |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | case ELEMENT_PORT: { |
| 301 | // Find the switch |
| 302 | Node node = getNode(topologyElement.getSwitch()); |
| 303 | if (node == null) |
| 304 | break; |
| 305 | // Remove the port for the switch |
| 306 | Integer port = node.getPort(topologyElement.getSwitchPort()); |
| 307 | if (port != null) { |
| 308 | node.removePort(topologyElement.getSwitchPort()); |
| 309 | isModified = true; |
| 310 | } |
| 311 | break; |
| 312 | } |
| 313 | case ELEMENT_LINK: { |
| 314 | // Find the "from" switch |
| 315 | Node fromNode = getNode(topologyElement.getFromSwitch()); |
| 316 | if (fromNode == null) |
| 317 | break; |
| 318 | // Remove the link originating from the "from" port |
| 319 | Node.Link link = fromNode.getLink(topologyElement.getFromPort()); |
| 320 | if (link != null) { |
| 321 | fromNode.removeLink(topologyElement.getFromPort()); |
| 322 | isModified = true; |
| 323 | } |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return isModified; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Get a node for a given Node ID. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 333 | * |
| 334 | * @param nodeId the Node ID to use. |
| 335 | * @return the corresponding Node if found, otherwise null. |
| 336 | */ |
| 337 | Node getNode(long nodeId) { |
| 338 | return nodesMap.get(nodeId); |
| 339 | } |
| 340 | |
| 341 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 342 | * Add a node for a given Node ID. |
| 343 | * |
| 344 | * @param nodeId the Node ID to use. |
| 345 | * @return the added Node. |
| 346 | */ |
| 347 | Node addNode(long nodeId) { |
| 348 | Node node = new Node(nodeId); |
| 349 | nodesMap.put(nodeId, node); |
| 350 | return node; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Remove an existing node. |
| 355 | * |
| 356 | * @param node the Node to remove. |
| 357 | */ |
| 358 | void removeNode(Node node) { |
| 359 | // |
| 360 | // Remove all ports one-by-one. This operation will also remove the |
| 361 | // incoming links originating from the neighbors. |
| 362 | // |
Pavlin Radoslavov | cfe7b40 | 2013-10-30 19:44:22 -0700 | [diff] [blame] | 363 | // NOTE: We have to extract all Port IDs in advance, otherwise we |
| 364 | // cannot loop over the Ports collection and remove entries at the |
| 365 | // same time. |
| 366 | // TODO: If there is a large number of ports, the implementation |
| 367 | // below can be sub-optimal. It should be refactored as follows: |
| 368 | // 1. Modify removePort() to perform all the cleanup, except |
| 369 | // removing the Port entry from the portsMap |
| 370 | // 2. Call portsMap.clear() at the end of this method |
| 371 | // 3. In all other methods: if removePort() is called somewhere else, |
| 372 | // add an explicit removal of the Port entry from the portsMap. |
| 373 | // |
| 374 | List<Integer> allPortIdKeys = new LinkedList<Integer>(); |
| 375 | allPortIdKeys.addAll(node.ports().keySet()); |
| 376 | for (Integer portId : allPortIdKeys) |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 377 | node.removePort(portId); |
| 378 | |
| 379 | nodesMap.remove(node.nodeId); |
| 380 | } |
| 381 | |
| 382 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 383 | * Read topology state from the database. |
| 384 | * |
| 385 | * @param dbHandler the Graph Database handler to use. |
| 386 | */ |
| 387 | public void readFromDatabase(GraphDBOperation dbHandler) { |
| 388 | // |
| 389 | // Fetch the relevant info from the Switch and Port vertices |
| 390 | // from the Titan Graph. |
| 391 | // |
| 392 | Iterable<ISwitchObject> activeSwitches = dbHandler.getActiveSwitches(); |
| 393 | for (ISwitchObject switchObj : activeSwitches) { |
| 394 | Vertex nodeVertex = switchObj.asVertex(); |
| 395 | // |
| 396 | // The Switch info |
| 397 | // |
| 398 | String nodeDpid = nodeVertex.getProperty("dpid").toString(); |
| 399 | long nodeId = HexString.toLong(nodeDpid); |
| 400 | Node me = nodesMap.get(nodeId); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 401 | if (me == null) |
| 402 | me = addNode(nodeId); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 403 | |
| 404 | // |
| 405 | // The local Port info |
| 406 | // |
| 407 | for (Vertex myPortVertex : nodeVertex.getVertices(Direction.OUT, "on")) { |
| 408 | // Ignore inactive ports |
| 409 | if (! myPortVertex.getProperty("state").toString().equals("ACTIVE")) |
| 410 | continue; |
| 411 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 412 | int myPort = 0; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 413 | Object obj = myPortVertex.getProperty("number"); |
| 414 | if (obj instanceof Short) { |
| 415 | myPort = (Short)obj; |
| 416 | } else if (obj instanceof Integer) { |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 417 | myPort = (Integer)obj; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // |
| 421 | // The neighbor Port info |
| 422 | // |
| 423 | for (Vertex neighborPortVertex : myPortVertex.getVertices(Direction.OUT, "link")) { |
| 424 | // Ignore inactive ports |
| 425 | if (! neighborPortVertex.getProperty("state").toString().equals("ACTIVE")) |
| 426 | continue; |
| 427 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 428 | int neighborPort = 0; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 429 | obj = neighborPortVertex.getProperty("number"); |
| 430 | if (obj instanceof Short) { |
| 431 | neighborPort = (Short)obj; |
| 432 | } else if (obj instanceof Integer) { |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 433 | neighborPort = (Integer)obj; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 434 | } |
| 435 | // |
| 436 | // The neighbor Switch info |
| 437 | // |
| 438 | for (Vertex neighborVertex : neighborPortVertex.getVertices(Direction.IN, "on")) { |
| 439 | // Ignore inactive switches |
| 440 | String state = neighborVertex.getProperty("state").toString(); |
| 441 | if (! state.equals(SwitchState.ACTIVE.toString())) |
| 442 | continue; |
| 443 | |
| 444 | String neighborDpid = neighborVertex.getProperty("dpid").toString(); |
| 445 | long neighborId = HexString.toLong(neighborDpid); |
| 446 | Node neighbor = nodesMap.get(neighborId); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 447 | if (neighbor == null) |
| 448 | neighbor = addNode(neighborId); |
| 449 | me.addLink(myPort, neighbor, neighborPort); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | dbHandler.commit(); |
| 455 | } |
| 456 | } |