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 |
Pavlin Radoslavov | 823f322 | 2013-11-04 21:57:31 -0800 | [diff] [blame] | 124 | reverseLink.me.removeLink(reverseLink.myPort); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 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 | } |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | case ELEMENT_PORT: { |
| 219 | // Add the switch |
| 220 | Node node = getNode(topologyElement.getSwitch()); |
| 221 | if (node == null) { |
| 222 | node = addNode(topologyElement.getSwitch()); |
| 223 | isModified = true; |
| 224 | } |
| 225 | // Add the port for the switch |
| 226 | Integer port = node.getPort(topologyElement.getSwitchPort()); |
| 227 | if (port == null) { |
| 228 | node.addPort(topologyElement.getSwitchPort()); |
| 229 | isModified = true; |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | case ELEMENT_LINK: { |
| 234 | // Add the "from" switch |
| 235 | Node fromNode = getNode(topologyElement.getFromSwitch()); |
| 236 | if (fromNode == null) { |
| 237 | fromNode = addNode(topologyElement.getFromSwitch()); |
| 238 | isModified = true; |
| 239 | } |
| 240 | // Add the "to" switch |
| 241 | Node toNode = getNode(topologyElement.getToSwitch()); |
| 242 | if (toNode == null) { |
| 243 | toNode = addNode(topologyElement.getToSwitch()); |
| 244 | isModified = true; |
| 245 | } |
| 246 | // Add the "from" port |
| 247 | Integer fromPort = fromNode.getPort(topologyElement.getFromPort()); |
| 248 | if (fromPort == null) { |
| 249 | fromNode.addPort(topologyElement.getFromPort()); |
| 250 | isModified = true; |
| 251 | } |
| 252 | // Add the "to" port |
| 253 | Integer toPort = fromNode.getPort(topologyElement.getToPort()); |
| 254 | if (toPort == null) { |
| 255 | toNode.addPort(topologyElement.getToPort()); |
| 256 | isModified = true; |
| 257 | } |
| 258 | Node.Link link = fromNode.getLink(topologyElement.getFromPort()); |
| 259 | if (link == null) { |
| 260 | fromNode.addLink(topologyElement.getFromPort(), |
| 261 | toNode, |
| 262 | topologyElement.getToPort()); |
| 263 | isModified = true; |
| 264 | } |
| 265 | |
| 266 | break; |
| 267 | } |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 268 | case ELEMENT_UNKNOWN: |
| 269 | // TODO: Adding "assert(false);" here can be dangerous |
| 270 | break; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return isModified; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Remove a topology element from the topology. |
| 278 | * |
| 279 | * @param topologyElement the topology element to remove. |
| 280 | * @return true if the topology was modified, otherwise false. |
| 281 | */ |
| 282 | public boolean removeTopologyElement(TopologyElement topologyElement) { |
| 283 | boolean isModified = false; |
| 284 | |
| 285 | switch (topologyElement.getType()) { |
| 286 | case ELEMENT_SWITCH: { |
| 287 | // Remove the switch |
| 288 | Node node = getNode(topologyElement.getSwitch()); |
| 289 | if (node != null) { |
| 290 | removeNode(node); |
| 291 | isModified = true; |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | case ELEMENT_PORT: { |
| 296 | // Find the switch |
| 297 | Node node = getNode(topologyElement.getSwitch()); |
| 298 | if (node == null) |
| 299 | break; |
| 300 | // Remove the port for the switch |
| 301 | Integer port = node.getPort(topologyElement.getSwitchPort()); |
| 302 | if (port != null) { |
| 303 | node.removePort(topologyElement.getSwitchPort()); |
| 304 | isModified = true; |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | case ELEMENT_LINK: { |
| 309 | // Find the "from" switch |
| 310 | Node fromNode = getNode(topologyElement.getFromSwitch()); |
| 311 | if (fromNode == null) |
| 312 | break; |
| 313 | // Remove the link originating from the "from" port |
| 314 | Node.Link link = fromNode.getLink(topologyElement.getFromPort()); |
| 315 | if (link != null) { |
| 316 | fromNode.removeLink(topologyElement.getFromPort()); |
| 317 | isModified = true; |
| 318 | } |
| 319 | break; |
| 320 | } |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 321 | case ELEMENT_UNKNOWN: |
| 322 | // TODO: Adding "assert(false);" here can be dangerous |
| 323 | break; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return isModified; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Get a node for a given Node ID. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 331 | * |
| 332 | * @param nodeId the Node ID to use. |
| 333 | * @return the corresponding Node if found, otherwise null. |
| 334 | */ |
| 335 | Node getNode(long nodeId) { |
| 336 | return nodesMap.get(nodeId); |
| 337 | } |
| 338 | |
| 339 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 340 | * Add a node for a given Node ID. |
| 341 | * |
| 342 | * @param nodeId the Node ID to use. |
| 343 | * @return the added Node. |
| 344 | */ |
| 345 | Node addNode(long nodeId) { |
| 346 | Node node = new Node(nodeId); |
| 347 | nodesMap.put(nodeId, node); |
| 348 | return node; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Remove an existing node. |
| 353 | * |
| 354 | * @param node the Node to remove. |
| 355 | */ |
| 356 | void removeNode(Node node) { |
| 357 | // |
| 358 | // Remove all ports one-by-one. This operation will also remove the |
| 359 | // incoming links originating from the neighbors. |
| 360 | // |
Pavlin Radoslavov | cfe7b40 | 2013-10-30 19:44:22 -0700 | [diff] [blame] | 361 | // NOTE: We have to extract all Port IDs in advance, otherwise we |
| 362 | // cannot loop over the Ports collection and remove entries at the |
| 363 | // same time. |
| 364 | // TODO: If there is a large number of ports, the implementation |
| 365 | // below can be sub-optimal. It should be refactored as follows: |
| 366 | // 1. Modify removePort() to perform all the cleanup, except |
| 367 | // removing the Port entry from the portsMap |
| 368 | // 2. Call portsMap.clear() at the end of this method |
| 369 | // 3. In all other methods: if removePort() is called somewhere else, |
| 370 | // add an explicit removal of the Port entry from the portsMap. |
| 371 | // |
| 372 | List<Integer> allPortIdKeys = new LinkedList<Integer>(); |
| 373 | allPortIdKeys.addAll(node.ports().keySet()); |
| 374 | for (Integer portId : allPortIdKeys) |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 375 | node.removePort(portId); |
| 376 | |
| 377 | nodesMap.remove(node.nodeId); |
| 378 | } |
| 379 | |
| 380 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 381 | * Read topology state from the database. |
| 382 | * |
| 383 | * @param dbHandler the Graph Database handler to use. |
| 384 | */ |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame^] | 385 | public void readFromDatabase(GraphDBOperation dbHandler) { |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 386 | // |
| 387 | // Fetch the relevant info from the Switch and Port vertices |
| 388 | // from the Titan Graph. |
| 389 | // |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame^] | 390 | nodesMap = new TreeMap<Long,Node>(); |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 391 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 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(); |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // TODO Merge into loops in readFromDatabase() can reduce execution time. |
| 458 | /** |
| 459 | * Check given two topology are identical or not. |
| 460 | * @param topo1 |
| 461 | * @param topo2 |
| 462 | * @return true if identical |
| 463 | */ |
| 464 | private boolean compareTopology(Map<Long,Node> topo1, Map<Long,Node> topo2) { |
| 465 | if (topo1.size() != topo2.size()) { |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | for (Map.Entry<Long,Node> nodeEntry : topo1.entrySet()) { |
| 470 | Long dpid = nodeEntry.getKey(); |
| 471 | if (! topo2.containsKey(dpid)) { |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | Node n1 = nodeEntry.getValue(); |
| 476 | Node n2 = topo2.get(dpid); |
| 477 | |
| 478 | // check port identity |
| 479 | if (n1.ports().size() != n2.ports().size()) { |
| 480 | return false; |
| 481 | } |
| 482 | for (Integer port : n1.ports().keySet()) { |
| 483 | if (! n2.ports().containsKey(port)) { |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // check link identity |
| 489 | if (n1.links.size() != n2.links.size()) { |
| 490 | return false; |
| 491 | } |
| 492 | for (Map.Entry<Integer, Node.Link> linkEntry : n1.links.entrySet()) { |
| 493 | Integer p1 = linkEntry.getKey(); |
| 494 | Node.Link l1 = linkEntry.getValue(); |
| 495 | |
| 496 | if (! n2.links.containsKey(p1)) { |
| 497 | return false; |
| 498 | } |
| 499 | Node.Link l2 = n2.links.get(p1); |
| 500 | |
| 501 | // Supposition: Link's "me" and "neighbor" is properly set. |
| 502 | if (l1.myPort != l2.myPort || |
| 503 | l1.neighborPort != l2.neighborPort) { |
| 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | // Only for debug use |
| 512 | @Override |
| 513 | public String toString() { |
| 514 | long numNodes = nodesMap.size(); |
| 515 | long numLinks = 0; |
| 516 | for (Map.Entry<Long, Node> entry : nodesMap.entrySet()) { |
| 517 | Node n = entry.getValue(); |
| 518 | for (Map.Entry<Integer, Node.Link> linkEntry : n.links.entrySet()) { |
| 519 | if (n.nodeId > linkEntry.getValue().neighbor.nodeId) { |
| 520 | ++numLinks; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | return "Topology has " + numNodes + " Nodes and " + numLinks + " Links."; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 525 | } |
| 526 | } |