blob: d56aaf954bc101ec1d238d5b809dd49917c2ffb8 [file] [log] [blame]
Pavlin Radoslavov15954d42013-10-19 15:29:04 -07001package net.onrc.onos.ofcontroller.topology;
2
3import java.util.HashMap;
Pavlin Radoslavovcfe7b402013-10-30 19:44:22 -07004import java.util.List;
5import java.util.LinkedList;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -07006import java.util.Map;
7
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 Radoslavovf9161ca2013-10-31 17:08:12 -070049 public HashMap<Integer, Link> links; // The links from this node:
50 // (src PortID -> Link)
51 private HashMap<Integer, Link> reverseLinksMap; // The links to this node:
52 // (dst PortID -> Link)
53 private HashMap<Integer, Integer> portsMap; // The ports on this node:
54 // (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 Radoslavova23e5412013-10-27 19:56:40 -070065 links = new HashMap<Integer, Link>();
66 reverseLinksMap = new HashMap<Integer, Link>();
67 portsMap = new HashMap<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
124 reverseLink.neighbor.removeLink(reverseLink.myPort);
125 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() {
196 nodesMap = new HashMap<Long, Node>();
197 }
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 }
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 Radoslavov15954d42013-10-19 15:29:04 -0700333 *
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 Radoslavova23e5412013-10-27 19:56:40 -0700342 * 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 Radoslavovcfe7b402013-10-30 19:44:22 -0700363 // 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 Radoslavova23e5412013-10-27 19:56:40 -0700377 node.removePort(portId);
378
379 nodesMap.remove(node.nodeId);
380 }
381
382 /**
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700383 * 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 Radoslavova23e5412013-10-27 19:56:40 -0700401 if (me == null)
402 me = addNode(nodeId);
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700403
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 Radoslavova23e5412013-10-27 19:56:40 -0700412 int myPort = 0;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700413 Object obj = myPortVertex.getProperty("number");
414 if (obj instanceof Short) {
415 myPort = (Short)obj;
416 } else if (obj instanceof Integer) {
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700417 myPort = (Integer)obj;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700418 }
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 Radoslavova23e5412013-10-27 19:56:40 -0700428 int neighborPort = 0;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700429 obj = neighborPortVertex.getProperty("number");
430 if (obj instanceof Short) {
431 neighborPort = (Short)obj;
432 } else if (obj instanceof Integer) {
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700433 neighborPort = (Integer)obj;
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700434 }
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 Radoslavova23e5412013-10-27 19:56:40 -0700447 if (neighbor == null)
448 neighbor = addNode(neighborId);
449 me.addLink(myPort, neighbor, neighborPort);
Pavlin Radoslavov15954d42013-10-19 15:29:04 -0700450 }
451 }
452 }
453 }
454 dbHandler.commit();
455 }
456}