blob: dbf9adafd4e0fcc0ee4a85ef2685fd755fa65d85 [file] [log] [blame]
Pavlin Radoslavov15954d42013-10-19 15:29:04 -07001package net.onrc.onos.ofcontroller.topology;
2
Pavlin Radoslavovcfe7b402013-10-30 19:44:22 -07003import java.util.List;
4import java.util.LinkedList;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -07005import java.util.Map;
Pavlin Radoslavovdf656d62013-10-31 17:24:32 -07006import java.util.TreeMap;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -07007
8import net.onrc.onos.graph.GraphDBOperation;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
10import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
11
12import org.openflow.util.HexString;
13
14import com.tinkerpop.blueprints.Direction;
15import com.tinkerpop.blueprints.Vertex;
16
17/**
18 * A class for storing Node and Link information for fast computation
19 * of shortest paths.
20 */
21class 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 Radoslavova23e5412013-10-27 19:56:40 -070029 public int myPort; // Local port ID for the link
30 public int neighborPort; // Neighbor port ID for the link
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070031
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 Radoslavova23e5412013-10-27 19:56:40 -070037 * @param myPort local port ID for the link.
38 * @param neighborPort neighbor port ID for the link.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070039 */
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070040 public Link(Node me, Node neighbor, int myPort, int neighborPort) {
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070041 this.me = me;
42 this.neighbor = neighbor;
43 this.myPort = myPort;
44 this.neighborPort = neighborPort;
45 }
46 };
47
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070048 public long nodeId; // The node ID
Pavlin Radoslavovdf656d62013-10-31 17:24:32 -070049 public TreeMap<Integer, Link> links; // The links from this node:
Pavlin Radoslavovf9161ca2013-10-31 17:08:12 -070050 // (src PortID -> Link)
Pavlin Radoslavovdf656d62013-10-31 17:24:32 -070051 private TreeMap<Integer, Link> reverseLinksMap; // The links to this node:
Pavlin Radoslavovf9161ca2013-10-31 17:08:12 -070052 // (dst PortID -> Link)
Pavlin Radoslavovdf656d62013-10-31 17:24:32 -070053 private TreeMap<Integer, Integer> portsMap; // The ports on this node:
Pavlin Radoslavovf9161ca2013-10-31 17:08:12 -070054 // (PortID -> PortID)
55 // TODO: In the future will be:
56 // (PortID -> Port)
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070057
58 /**
59 * Node constructor.
60 *
61 * @param nodeId the node ID.
62 */
63 public Node(long nodeId) {
64 this.nodeId = nodeId;
Pavlin Radoslavovdf656d62013-10-31 17:24:32 -070065 links = new TreeMap<Integer, Link>();
66 reverseLinksMap = new TreeMap<Integer, Link>();
67 portsMap = new TreeMap<Integer, Integer>();
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070068 }
69
70 /**
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070071 * Get all ports.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070072 *
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070073 * @return all ports.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -070074 */
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070075 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 HIGUCHIdbc8c442013-10-31 10:15:37 -070088 return portsMap.get(portId);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070089 }
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 Radoslavov823f3222013-11-04 21:57:31 -0800124 reverseLink.me.removeLink(reverseLink.myPort);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700125 removeReverseLink(reverseLink);
126 }
127
128 portsMap.remove(portId);
129 }
Yuta HIGUCHI63ee5a82013-10-31 10:16:15 -0700130
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700131 /**
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 Radoslavov15954d42013-10-19 15:29:04 -0700183 }
184};
185
186/**
187 * A class for storing topology information.
188 */
189public class Topology {
190 private Map<Long, Node> nodesMap; // The dpid->Node mapping
191
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700192 /**
193 * Default constructor.
194 */
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700195 public Topology() {
Pavlin Radoslavovdf656d62013-10-31 17:24:32 -0700196 nodesMap = new TreeMap<Long, Node>();
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700197 }
198
199 /**
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700200 * 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 Radoslavova23e5412013-10-27 19:56:40 -0700216 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 }
268 }
269
270 return isModified;
271 }
272
273 /**
274 * Remove a topology element from the topology.
275 *
276 * @param topologyElement the topology element to remove.
277 * @return true if the topology was modified, otherwise false.
278 */
279 public boolean removeTopologyElement(TopologyElement topologyElement) {
280 boolean isModified = false;
281
282 switch (topologyElement.getType()) {
283 case ELEMENT_SWITCH: {
284 // Remove the switch
285 Node node = getNode(topologyElement.getSwitch());
286 if (node != null) {
287 removeNode(node);
288 isModified = true;
289 }
290 break;
291 }
292 case ELEMENT_PORT: {
293 // Find the switch
294 Node node = getNode(topologyElement.getSwitch());
295 if (node == null)
296 break;
297 // Remove the port for the switch
298 Integer port = node.getPort(topologyElement.getSwitchPort());
299 if (port != null) {
300 node.removePort(topologyElement.getSwitchPort());
301 isModified = true;
302 }
303 break;
304 }
305 case ELEMENT_LINK: {
306 // Find the "from" switch
307 Node fromNode = getNode(topologyElement.getFromSwitch());
308 if (fromNode == null)
309 break;
310 // Remove the link originating from the "from" port
311 Node.Link link = fromNode.getLink(topologyElement.getFromPort());
312 if (link != null) {
313 fromNode.removeLink(topologyElement.getFromPort());
314 isModified = true;
315 }
316 break;
317 }
318 }
319
320 return isModified;
321 }
322
323 /**
324 * Get a node for a given Node ID.
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700325 *
326 * @param nodeId the Node ID to use.
327 * @return the corresponding Node if found, otherwise null.
328 */
329 Node getNode(long nodeId) {
330 return nodesMap.get(nodeId);
331 }
332
333 /**
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700334 * Add a node for a given Node ID.
335 *
336 * @param nodeId the Node ID to use.
337 * @return the added Node.
338 */
339 Node addNode(long nodeId) {
340 Node node = new Node(nodeId);
341 nodesMap.put(nodeId, node);
342 return node;
343 }
344
345 /**
346 * Remove an existing node.
347 *
348 * @param node the Node to remove.
349 */
350 void removeNode(Node node) {
351 //
352 // Remove all ports one-by-one. This operation will also remove the
353 // incoming links originating from the neighbors.
354 //
Pavlin Radoslavovcfe7b402013-10-30 19:44:22 -0700355 // NOTE: We have to extract all Port IDs in advance, otherwise we
356 // cannot loop over the Ports collection and remove entries at the
357 // same time.
358 // TODO: If there is a large number of ports, the implementation
359 // below can be sub-optimal. It should be refactored as follows:
360 // 1. Modify removePort() to perform all the cleanup, except
361 // removing the Port entry from the portsMap
362 // 2. Call portsMap.clear() at the end of this method
363 // 3. In all other methods: if removePort() is called somewhere else,
364 // add an explicit removal of the Port entry from the portsMap.
365 //
366 List<Integer> allPortIdKeys = new LinkedList<Integer>();
367 allPortIdKeys.addAll(node.ports().keySet());
368 for (Integer portId : allPortIdKeys)
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700369 node.removePort(portId);
370
371 nodesMap.remove(node.nodeId);
372 }
373
374 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700375 * Read topology state from the database.
376 *
377 * @param dbHandler the Graph Database handler to use.
378 */
379 public void readFromDatabase(GraphDBOperation dbHandler) {
380 //
381 // Fetch the relevant info from the Switch and Port vertices
382 // from the Titan Graph.
383 //
384 Iterable<ISwitchObject> activeSwitches = dbHandler.getActiveSwitches();
385 for (ISwitchObject switchObj : activeSwitches) {
386 Vertex nodeVertex = switchObj.asVertex();
387 //
388 // The Switch info
389 //
390 String nodeDpid = nodeVertex.getProperty("dpid").toString();
391 long nodeId = HexString.toLong(nodeDpid);
392 Node me = nodesMap.get(nodeId);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700393 if (me == null)
394 me = addNode(nodeId);
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700395
396 //
397 // The local Port info
398 //
399 for (Vertex myPortVertex : nodeVertex.getVertices(Direction.OUT, "on")) {
400 // Ignore inactive ports
401 if (! myPortVertex.getProperty("state").toString().equals("ACTIVE"))
402 continue;
403
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700404 int myPort = 0;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700405 Object obj = myPortVertex.getProperty("number");
406 if (obj instanceof Short) {
407 myPort = (Short)obj;
408 } else if (obj instanceof Integer) {
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700409 myPort = (Integer)obj;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700410 }
411
412 //
413 // The neighbor Port info
414 //
415 for (Vertex neighborPortVertex : myPortVertex.getVertices(Direction.OUT, "link")) {
416 // Ignore inactive ports
417 if (! neighborPortVertex.getProperty("state").toString().equals("ACTIVE"))
418 continue;
419
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700420 int neighborPort = 0;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700421 obj = neighborPortVertex.getProperty("number");
422 if (obj instanceof Short) {
423 neighborPort = (Short)obj;
424 } else if (obj instanceof Integer) {
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700425 neighborPort = (Integer)obj;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700426 }
427 //
428 // The neighbor Switch info
429 //
430 for (Vertex neighborVertex : neighborPortVertex.getVertices(Direction.IN, "on")) {
431 // Ignore inactive switches
432 String state = neighborVertex.getProperty("state").toString();
433 if (! state.equals(SwitchState.ACTIVE.toString()))
434 continue;
435
436 String neighborDpid = neighborVertex.getProperty("dpid").toString();
437 long neighborId = HexString.toLong(neighborDpid);
438 Node neighbor = nodesMap.get(neighborId);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700439 if (neighbor == null)
440 neighbor = addNode(neighborId);
441 me.addLink(myPort, neighbor, neighborPort);
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700442 }
443 }
444 }
445 }
446 dbHandler.commit();
447 }
448}