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; |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 9 | import net.onrc.onos.graph.IDBOperation; |
| 10 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 11 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 12 | import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState; |
| 13 | |
| 14 | import org.openflow.util.HexString; |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 17 | |
| 18 | import com.tinkerpop.blueprints.Direction; |
| 19 | import com.tinkerpop.blueprints.Vertex; |
| 20 | |
| 21 | /** |
| 22 | * A class for storing Node and Link information for fast computation |
| 23 | * of shortest paths. |
| 24 | */ |
| 25 | class Node { |
| 26 | /** |
| 27 | * A class for storing Link information for fast computation of shortest |
| 28 | * paths. |
| 29 | */ |
| 30 | class Link { |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 31 | public Node me; // The node this link originates from |
| 32 | public Node neighbor; // The neighbor node on the other side |
| 33 | public int myPort; // Local port ID for the link |
| 34 | public int neighborPort; // Neighbor port ID for the link |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 35 | |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Link constructor. |
| 38 | * |
| 39 | * @param me the node this link originates from. |
| 40 | * @param the neighbor node on the other side of the link. |
| 41 | * @param myPort local port ID for the link. |
| 42 | * @param neighborPort neighbor port ID for the link. |
| 43 | */ |
| 44 | public Link(Node me, Node neighbor, int myPort, int neighborPort) { |
| 45 | this.me = me; |
| 46 | this.neighbor = neighbor; |
| 47 | this.myPort = myPort; |
| 48 | this.neighborPort = neighborPort; |
| 49 | } |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 52 | public long nodeId; // The node ID |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 53 | // TODO Change type of PortNumber to Short |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 54 | public TreeMap<Integer, Link> links; // The links from this node: |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 55 | // (src PortNumber -> Link) |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 56 | private TreeMap<Integer, Link> reverseLinksMap; // The links to this node: |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 57 | // (dst PortNumber -> Link) |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 58 | private TreeMap<Integer, Integer> portsMap; // The ports on this node: |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 59 | // (PortNumber -> PortNumber) |
Pavlin Radoslavov | f9161ca | 2013-10-31 17:08:12 -0700 | [diff] [blame] | 60 | // TODO: In the future will be: |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 61 | // (PortNumber -> Port) |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Node constructor. |
| 65 | * |
| 66 | * @param nodeId the node ID. |
| 67 | */ |
| 68 | public Node(long nodeId) { |
| 69 | this.nodeId = nodeId; |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 70 | links = new TreeMap<Integer, Link>(); |
| 71 | reverseLinksMap = new TreeMap<Integer, Link>(); |
| 72 | portsMap = new TreeMap<Integer, Integer>(); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 76 | * Get all ports. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 77 | * |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 78 | * @return all ports. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 79 | */ |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 80 | public Map<Integer, Integer> ports() { |
| 81 | return portsMap; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the port for a given Port ID. |
| 86 | * |
| 87 | * Note: For now the port itself is just the Port ID. In the future |
| 88 | * it might contain more information. |
| 89 | * |
| 90 | * @return the port if found, otherwise null. |
| 91 | */ |
| 92 | public Integer getPort(int portId) { |
Yuta HIGUCHI | dbc8c44 | 2013-10-31 10:15:37 -0700 | [diff] [blame] | 93 | return portsMap.get(portId); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Add a port for a given Port ID. |
| 98 | * |
| 99 | * Note: For now the port itself is just the Port ID. In the future |
| 100 | * it might contain more information. |
| 101 | * |
| 102 | * @param portId the Port ID of the port to add. |
| 103 | * @return the added Port. |
| 104 | */ |
| 105 | Integer addPort(int portId) { |
| 106 | Integer port = new Integer(portId); |
| 107 | portsMap.put(portId, port); |
| 108 | return port; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Remove a port for a given Port ID. |
| 113 | * |
| 114 | * NOTE: The outgoing and incoming links using this port are removed as |
| 115 | * well. |
| 116 | */ |
| 117 | void removePort(int portId) { |
| 118 | // Remove the outgoing link |
| 119 | Link link = getLink(portId); |
| 120 | if (link != null) { |
| 121 | link.neighbor.removeReverseLink(link); |
| 122 | removeLink(portId); |
| 123 | } |
| 124 | |
| 125 | // Remove the incoming link |
| 126 | Link reverseLink = reverseLinksMap.get(portId); |
| 127 | if (reverseLink != null) { |
| 128 | // NOTE: reverseLink.myPort is the neighbor's outgoing port |
Pavlin Radoslavov | 823f322 | 2013-11-04 21:57:31 -0800 | [diff] [blame] | 129 | reverseLink.me.removeLink(reverseLink.myPort); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 130 | removeReverseLink(reverseLink); |
| 131 | } |
| 132 | |
| 133 | portsMap.remove(portId); |
| 134 | } |
Yuta HIGUCHI | 63ee5a8 | 2013-10-31 10:16:15 -0700 | [diff] [blame] | 135 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Get a link on a port to a neighbor. |
| 138 | * |
| 139 | * @param myPortId the local port ID for the link to the neighbor. |
| 140 | * @return the link if found, otherwise null. |
| 141 | */ |
| 142 | public Link getLink(int myPortId) { |
| 143 | return links.get(myPortId); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Add a link to a neighbor. |
| 148 | * |
| 149 | * @param myPortId the local port ID for the link to the neighbor. |
| 150 | * @param neighbor the neighbor for the link. |
| 151 | * @param neighborPortId the neighbor port ID for the link. |
| 152 | * @return the added Link. |
| 153 | */ |
| 154 | public Link addLink(int myPortId, Node neighbor, int neighborPortId) { |
| 155 | Link link = new Link(this, neighbor, myPortId, neighborPortId); |
| 156 | links.put(myPortId, link); |
| 157 | neighbor.addReverseLink(link); |
| 158 | return link; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Add a reverse link from a neighbor. |
| 163 | * |
| 164 | * @param link the reverse link from a neighbor to add. |
| 165 | */ |
| 166 | private void addReverseLink(Link link) { |
| 167 | // NOTE: link.neghborPort is my port |
| 168 | reverseLinksMap.put(link.neighborPort, link); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Remove a link to a neighbor. |
| 173 | * |
| 174 | * @param myPortId the local port ID for the link to the neighbor. |
| 175 | */ |
| 176 | public void removeLink(int myPortId) { |
| 177 | links.remove(myPortId); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Remove a reverse link from a neighbor. |
| 182 | * |
| 183 | * @param link the reverse link from a neighbor to remove. |
| 184 | */ |
| 185 | private void removeReverseLink(Link link) { |
| 186 | // NOTE: link.neghborPort is my port |
| 187 | reverseLinksMap.remove(link.neighborPort); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
| 191 | /** |
| 192 | * A class for storing topology information. |
| 193 | */ |
| 194 | public class Topology { |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 195 | private final static Logger log = LoggerFactory.getLogger(Topology.class); |
| 196 | |
| 197 | // flag to use optimized readFromDatabase() method. |
| 198 | private static final boolean enableOptimizedRead = false; |
| 199 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 200 | private Map<Long, Node> nodesMap; // The dpid->Node mapping |
| 201 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Default constructor. |
| 204 | */ |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 205 | public Topology() { |
Pavlin Radoslavov | df656d6 | 2013-10-31 17:24:32 -0700 | [diff] [blame] | 206 | nodesMap = new TreeMap<Long, Node>(); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 210 | * Add a topology element to the topology. |
| 211 | * |
| 212 | * @param topologyElement the topology element to add. |
| 213 | * @return true if the topology was modified, otherwise false. |
| 214 | */ |
| 215 | public boolean addTopologyElement(TopologyElement topologyElement) { |
| 216 | boolean isModified = false; |
| 217 | |
| 218 | switch (topologyElement.getType()) { |
| 219 | case ELEMENT_SWITCH: { |
| 220 | // Add the switch |
| 221 | Node node = getNode(topologyElement.getSwitch()); |
| 222 | if (node == null) { |
| 223 | node = addNode(topologyElement.getSwitch()); |
| 224 | isModified = true; |
| 225 | } |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | case ELEMENT_PORT: { |
| 229 | // Add the switch |
| 230 | Node node = getNode(topologyElement.getSwitch()); |
| 231 | if (node == null) { |
| 232 | node = addNode(topologyElement.getSwitch()); |
| 233 | isModified = true; |
| 234 | } |
| 235 | // Add the port for the switch |
| 236 | Integer port = node.getPort(topologyElement.getSwitchPort()); |
| 237 | if (port == null) { |
| 238 | node.addPort(topologyElement.getSwitchPort()); |
| 239 | isModified = true; |
| 240 | } |
| 241 | break; |
| 242 | } |
| 243 | case ELEMENT_LINK: { |
| 244 | // Add the "from" switch |
| 245 | Node fromNode = getNode(topologyElement.getFromSwitch()); |
| 246 | if (fromNode == null) { |
| 247 | fromNode = addNode(topologyElement.getFromSwitch()); |
| 248 | isModified = true; |
| 249 | } |
| 250 | // Add the "to" switch |
| 251 | Node toNode = getNode(topologyElement.getToSwitch()); |
| 252 | if (toNode == null) { |
| 253 | toNode = addNode(topologyElement.getToSwitch()); |
| 254 | isModified = true; |
| 255 | } |
| 256 | // Add the "from" port |
| 257 | Integer fromPort = fromNode.getPort(topologyElement.getFromPort()); |
| 258 | if (fromPort == null) { |
| 259 | fromNode.addPort(topologyElement.getFromPort()); |
| 260 | isModified = true; |
| 261 | } |
| 262 | // Add the "to" port |
| 263 | Integer toPort = fromNode.getPort(topologyElement.getToPort()); |
| 264 | if (toPort == null) { |
| 265 | toNode.addPort(topologyElement.getToPort()); |
| 266 | isModified = true; |
| 267 | } |
| 268 | Node.Link link = fromNode.getLink(topologyElement.getFromPort()); |
| 269 | if (link == null) { |
| 270 | fromNode.addLink(topologyElement.getFromPort(), |
| 271 | toNode, |
| 272 | topologyElement.getToPort()); |
| 273 | isModified = true; |
| 274 | } |
| 275 | |
| 276 | break; |
| 277 | } |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 278 | case ELEMENT_UNKNOWN: |
| 279 | // TODO: Adding "assert(false);" here can be dangerous |
| 280 | break; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return isModified; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Remove a topology element from the topology. |
| 288 | * |
| 289 | * @param topologyElement the topology element to remove. |
| 290 | * @return true if the topology was modified, otherwise false. |
| 291 | */ |
| 292 | public boolean removeTopologyElement(TopologyElement topologyElement) { |
| 293 | boolean isModified = false; |
| 294 | |
| 295 | switch (topologyElement.getType()) { |
| 296 | case ELEMENT_SWITCH: { |
| 297 | // Remove the switch |
| 298 | Node node = getNode(topologyElement.getSwitch()); |
| 299 | if (node != null) { |
| 300 | removeNode(node); |
| 301 | isModified = true; |
| 302 | } |
| 303 | break; |
| 304 | } |
| 305 | case ELEMENT_PORT: { |
| 306 | // Find the switch |
| 307 | Node node = getNode(topologyElement.getSwitch()); |
| 308 | if (node == null) |
| 309 | break; |
| 310 | // Remove the port for the switch |
| 311 | Integer port = node.getPort(topologyElement.getSwitchPort()); |
| 312 | if (port != null) { |
| 313 | node.removePort(topologyElement.getSwitchPort()); |
| 314 | isModified = true; |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | case ELEMENT_LINK: { |
| 319 | // Find the "from" switch |
| 320 | Node fromNode = getNode(topologyElement.getFromSwitch()); |
| 321 | if (fromNode == null) |
| 322 | break; |
| 323 | // Remove the link originating from the "from" port |
| 324 | Node.Link link = fromNode.getLink(topologyElement.getFromPort()); |
| 325 | if (link != null) { |
| 326 | fromNode.removeLink(topologyElement.getFromPort()); |
| 327 | isModified = true; |
| 328 | } |
| 329 | break; |
| 330 | } |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 331 | case ELEMENT_UNKNOWN: |
| 332 | // TODO: Adding "assert(false);" here can be dangerous |
| 333 | break; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | return isModified; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Get a node for a given Node ID. |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 341 | * |
| 342 | * @param nodeId the Node ID to use. |
| 343 | * @return the corresponding Node if found, otherwise null. |
| 344 | */ |
| 345 | Node getNode(long nodeId) { |
| 346 | return nodesMap.get(nodeId); |
| 347 | } |
| 348 | |
| 349 | /** |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 350 | * Add a node for a given Node ID. |
| 351 | * |
| 352 | * @param nodeId the Node ID to use. |
| 353 | * @return the added Node. |
| 354 | */ |
| 355 | Node addNode(long nodeId) { |
| 356 | Node node = new Node(nodeId); |
| 357 | nodesMap.put(nodeId, node); |
| 358 | return node; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Remove an existing node. |
| 363 | * |
| 364 | * @param node the Node to remove. |
| 365 | */ |
| 366 | void removeNode(Node node) { |
| 367 | // |
| 368 | // Remove all ports one-by-one. This operation will also remove the |
| 369 | // incoming links originating from the neighbors. |
| 370 | // |
Pavlin Radoslavov | cfe7b40 | 2013-10-30 19:44:22 -0700 | [diff] [blame] | 371 | // NOTE: We have to extract all Port IDs in advance, otherwise we |
| 372 | // cannot loop over the Ports collection and remove entries at the |
| 373 | // same time. |
| 374 | // TODO: If there is a large number of ports, the implementation |
| 375 | // below can be sub-optimal. It should be refactored as follows: |
| 376 | // 1. Modify removePort() to perform all the cleanup, except |
| 377 | // removing the Port entry from the portsMap |
| 378 | // 2. Call portsMap.clear() at the end of this method |
| 379 | // 3. In all other methods: if removePort() is called somewhere else, |
| 380 | // add an explicit removal of the Port entry from the portsMap. |
| 381 | // |
| 382 | List<Integer> allPortIdKeys = new LinkedList<Integer>(); |
| 383 | allPortIdKeys.addAll(node.ports().keySet()); |
| 384 | for (Integer portId : allPortIdKeys) |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 385 | node.removePort(portId); |
| 386 | |
| 387 | nodesMap.remove(node.nodeId); |
| 388 | } |
| 389 | |
| 390 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 391 | * Read topology state from the database. |
| 392 | * |
| 393 | * @param dbHandler the Graph Database handler to use. |
| 394 | */ |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 395 | public void readFromDatabase(GraphDBOperation dbHandler) { |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 396 | if (enableOptimizedRead) { |
| 397 | readFromDatabaseBodyOptimized(dbHandler); |
| 398 | } else { |
| 399 | readFromDatabaseBody(dbHandler); |
| 400 | } |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 401 | |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 404 | private void readFromDatabaseBody(GraphDBOperation dbHandler) { |
| 405 | // |
| 406 | // Fetch the relevant info from the Switch and Port vertices |
| 407 | // from the Titan Graph. |
| 408 | // |
| 409 | |
| 410 | nodesMap.clear(); |
| 411 | Iterable<ISwitchObject> activeSwitches = dbHandler.getActiveSwitches(); |
| 412 | for (ISwitchObject switchObj : activeSwitches) { |
| 413 | Vertex nodeVertex = switchObj.asVertex(); |
| 414 | // |
| 415 | // The Switch info |
| 416 | // |
| 417 | String nodeDpid = nodeVertex.getProperty("dpid").toString(); |
| 418 | long nodeId = HexString.toLong(nodeDpid); |
| 419 | Node me = nodesMap.get(nodeId); |
| 420 | if (me == null) |
| 421 | me = addNode(nodeId); |
| 422 | |
| 423 | // |
| 424 | // The local Port info |
| 425 | // |
| 426 | for (Vertex myPortVertex : nodeVertex.getVertices(Direction.OUT, "on")) { |
| 427 | // Ignore inactive ports |
| 428 | if (! myPortVertex.getProperty("state").toString().equals("ACTIVE")) |
| 429 | continue; |
| 430 | |
| 431 | int myPort = 0; |
| 432 | Object obj = myPortVertex.getProperty("number"); |
| 433 | if (obj instanceof Short) { |
| 434 | myPort = (Short)obj; |
| 435 | } else if (obj instanceof Integer) { |
| 436 | myPort = (Integer)obj; |
| 437 | } |
| 438 | me.addPort(myPort); |
| 439 | |
| 440 | for (Vertex neighborPortVertex : myPortVertex.getVertices(Direction.OUT, "link")) { |
| 441 | // Ignore inactive ports |
| 442 | if (! neighborPortVertex.getProperty("state").toString().equals("ACTIVE")) { |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | int neighborPort = 0; |
| 447 | obj = neighborPortVertex.getProperty("number"); |
| 448 | if (obj instanceof Short) { |
| 449 | neighborPort = (Short)obj; |
| 450 | } else if (obj instanceof Integer) { |
| 451 | neighborPort = (Integer)obj; |
| 452 | } |
| 453 | // |
| 454 | // The neighbor Switch info |
| 455 | // |
| 456 | for (Vertex neighborVertex : neighborPortVertex.getVertices(Direction.IN, "on")) { |
| 457 | // Ignore inactive switches |
| 458 | String state = neighborVertex.getProperty("state").toString(); |
| 459 | if (! state.equals(SwitchState.ACTIVE.toString())) |
| 460 | continue; |
| 461 | |
| 462 | String neighborDpid = neighborVertex.getProperty("dpid").toString(); |
| 463 | long neighborId = HexString.toLong(neighborDpid); |
| 464 | Node neighbor = nodesMap.get(neighborId); |
| 465 | if (neighbor == null) |
| 466 | neighbor = addNode(neighborId); |
| 467 | neighbor.addPort(neighborPort); |
| 468 | me.addLink(myPort, neighbor, neighborPort); |
| 469 | } |
| 470 | } |
| 471 | } |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 472 | } |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 473 | dbHandler.commit(); |
| 474 | } |
| 475 | |
| 476 | private void readFromDatabaseBodyOptimized(GraphDBOperation dbHandler) { |
| 477 | nodesMap.clear(); |
| 478 | |
| 479 | // Load all switches into Map |
| 480 | Iterable<ISwitchObject> switches = dbHandler.getAllSwitches(); |
| 481 | for (ISwitchObject switchObj : switches) { |
| 482 | // Ignore inactive ports |
| 483 | if (!switchObj.getState().equals(SwitchState.ACTIVE.toString())) { |
| 484 | continue; |
| 485 | } |
| 486 | Vertex nodeVertex = switchObj.asVertex(); |
| 487 | // |
| 488 | // The Switch info |
| 489 | // |
| 490 | String nodeDpid = nodeVertex.getProperty("dpid").toString(); |
| 491 | long nodeId = HexString.toLong(nodeDpid); |
| 492 | addNode(nodeId); |
| 493 | } |
| 494 | |
| 495 | // |
| 496 | // Get All Ports |
| 497 | // |
| 498 | Iterable<IPortObject> ports = dbHandler.getAllPorts(); //TODO: Add to DB operations |
| 499 | for (IPortObject myPortObj : ports) { |
| 500 | Vertex myPortVertex = myPortObj.asVertex(); |
| 501 | |
| 502 | // Ignore inactive ports |
| 503 | if (! myPortVertex.getProperty("state").toString().equals("ACTIVE")) { |
| 504 | continue; |
| 505 | } |
| 506 | |
| 507 | short myPort = 0; |
| 508 | String idStr = myPortObj.getPortId(); |
| 509 | String[] splitter = idStr.split(IDBOperation.PORT_ID_DELIM); |
| 510 | if (splitter.length != 2) { |
| 511 | log.error("Invalid port_id : {}", idStr); |
| 512 | continue; |
| 513 | } |
| 514 | String myDpid = splitter[0]; |
| 515 | myPort = Short.parseShort(splitter[1]); |
| 516 | long myId = HexString.toLong(myDpid); |
| 517 | Node me = nodesMap.get(myId); |
| 518 | |
| 519 | if (me == null) { |
| 520 | // cannot proceed ports and switches are out of sync |
| 521 | //TODO: Restart the whole read |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | if (me.getPort((int)myPort) == null) { |
| 526 | me.addPort((int)myPort); |
| 527 | } else if (me.getLink((int)myPort) != null) { |
| 528 | // Link already added..probably by neighbor |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | // |
| 533 | // The neighbor Port info |
| 534 | // |
| 535 | for (Vertex neighborPortVertex : myPortVertex.getVertices(Direction.OUT, "link")) { |
| 536 | // Ignore inactive ports |
| 537 | if (! neighborPortVertex.getProperty("state").toString().equals("ACTIVE")) { |
| 538 | continue; |
| 539 | } |
| 540 | int neighborPort = 0; |
| 541 | idStr = neighborPortVertex.getProperty("port_id").toString(); |
| 542 | splitter = idStr.split(IDBOperation.PORT_ID_DELIM); |
| 543 | if (splitter.length != 2) { |
| 544 | log.error("Invalid port_id : {}", idStr); |
| 545 | continue; |
| 546 | } |
| 547 | String neighborDpid = splitter[0]; |
| 548 | neighborPort = Short.parseShort(splitter[1]); |
| 549 | long neighborId = HexString.toLong(neighborDpid); |
| 550 | Node neighbor = nodesMap.get(neighborId); |
| 551 | if (neighbor == null) { |
| 552 | continue; |
| 553 | } |
| 554 | if (neighbor.getPort(neighborPort) == null) { |
| 555 | neighbor.addPort(neighborPort); |
| 556 | } |
| 557 | me.addLink(myPort, neighbor, neighborPort); |
| 558 | } |
| 559 | } |
| 560 | dbHandler.commit(); |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // Only for debug use |
| 564 | @Override |
| 565 | public String toString() { |
| 566 | long numNodes = nodesMap.size(); |
| 567 | long numLinks = 0; |
| 568 | for (Map.Entry<Long, Node> entry : nodesMap.entrySet()) { |
| 569 | Node n = entry.getValue(); |
| 570 | for (Map.Entry<Integer, Node.Link> linkEntry : n.links.entrySet()) { |
| 571 | if (n.nodeId > linkEntry.getValue().neighbor.nodeId) { |
| 572 | ++numLinks; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | return "Topology has " + numNodes + " Nodes and " + numLinks + " Links."; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 577 | } |
| 578 | } |