blob: a2f2c2106a15035064da7e418f9e22aeaf100213 [file] [log] [blame]
Pavlin Radoslavov15954d42013-10-19 15:29:04 -07001package net.onrc.onos.ofcontroller.topology;
2
3import java.util.HashMap;
4import java.util.Map;
5
6import net.onrc.onos.graph.GraphDBOperation;
7import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
8import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
9
10import org.openflow.util.HexString;
11
12import com.tinkerpop.blueprints.Direction;
13import com.tinkerpop.blueprints.Vertex;
14
15/**
16 * A class for storing Node and Link information for fast computation
17 * of shortest paths.
18 */
19class Node {
20 /**
21 * A class for storing Link information for fast computation of shortest
22 * paths.
23 */
24 class Link {
25 public Node me; // The node this link originates from
26 public Node neighbor; // The neighbor node on the other side
27 public short myPort; // Local port number for the link
28 public short neighborPort; // Neighbor port number for the link
29
30 /**
31 * Link constructor.
32 *
33 * @param me the node this link originates from.
34 * @param the neighbor node on the other side of the link.
35 * @param myPort local port number for the link.
36 * @param neighborPort neighrobr port number for the link.
37 */
38 public Link(Node me, Node neighbor, short myPort, short neighborPort) {
39 this.me = me;
40 this.neighbor = neighbor;
41 this.myPort = myPort;
42 this.neighborPort = neighborPort;
43 }
44 };
45
46 public long nodeId; // The node ID
47 public HashMap<Short, Link> links; // The links originating from this node
48
49 /**
50 * Node constructor.
51 *
52 * @param nodeId the node ID.
53 */
54 public Node(long nodeId) {
55 this.nodeId = nodeId;
56 links = new HashMap<Short, Link>();
57 }
58
59 /**
60 * Add a neighbor.
61 *
62 * A new link to the neighbor will be created.
63 *
64 * @param neighbor the neighbor to add.
65 * @param myPort the local port number for the link to the neighbor.
66 * @param neighborPort the neighbor port number for the link.
67 */
68 public void addNeighbor(Node neighbor, short myPort, short neighborPort) {
69 Link link = new Link(this, neighbor, myPort, neighborPort);
70 links.put(myPort, link);
71 }
72};
73
74/**
75 * A class for storing topology information.
76 */
77public class Topology {
78 private Map<Long, Node> nodesMap; // The dpid->Node mapping
79
80 public Topology() {
81 nodesMap = new HashMap<Long, Node>();
82 }
83
84 /**
85 * Get a node for a give Node ID.
86 *
87 * @param nodeId the Node ID to use.
88 * @return the corresponding Node if found, otherwise null.
89 */
90 Node getNode(long nodeId) {
91 return nodesMap.get(nodeId);
92 }
93
94 /**
95 * Read topology state from the database.
96 *
97 * @param dbHandler the Graph Database handler to use.
98 */
99 public void readFromDatabase(GraphDBOperation dbHandler) {
100 //
101 // Fetch the relevant info from the Switch and Port vertices
102 // from the Titan Graph.
103 //
104 Iterable<ISwitchObject> activeSwitches = dbHandler.getActiveSwitches();
105 for (ISwitchObject switchObj : activeSwitches) {
106 Vertex nodeVertex = switchObj.asVertex();
107 //
108 // The Switch info
109 //
110 String nodeDpid = nodeVertex.getProperty("dpid").toString();
111 long nodeId = HexString.toLong(nodeDpid);
112 Node me = nodesMap.get(nodeId);
113 if (me == null) {
114 me = new Node(nodeId);
115 nodesMap.put(nodeId, me);
116 }
117
118 //
119 // The local Port info
120 //
121 for (Vertex myPortVertex : nodeVertex.getVertices(Direction.OUT, "on")) {
122 // Ignore inactive ports
123 if (! myPortVertex.getProperty("state").toString().equals("ACTIVE"))
124 continue;
125
126 short myPort = 0;
127 Object obj = myPortVertex.getProperty("number");
128 if (obj instanceof Short) {
129 myPort = (Short)obj;
130 } else if (obj instanceof Integer) {
131 Integer int_nodeId = (Integer)obj;
132 myPort = int_nodeId.shortValue();
133 }
134
135 //
136 // The neighbor Port info
137 //
138 for (Vertex neighborPortVertex : myPortVertex.getVertices(Direction.OUT, "link")) {
139 // Ignore inactive ports
140 if (! neighborPortVertex.getProperty("state").toString().equals("ACTIVE"))
141 continue;
142
143 short neighborPort = 0;
144 obj = neighborPortVertex.getProperty("number");
145 if (obj instanceof Short) {
146 neighborPort = (Short)obj;
147 } else if (obj instanceof Integer) {
148 Integer int_nodeId = (Integer)obj;
149 neighborPort = int_nodeId.shortValue();
150 }
151 //
152 // The neighbor Switch info
153 //
154 for (Vertex neighborVertex : neighborPortVertex.getVertices(Direction.IN, "on")) {
155 // Ignore inactive switches
156 String state = neighborVertex.getProperty("state").toString();
157 if (! state.equals(SwitchState.ACTIVE.toString()))
158 continue;
159
160 String neighborDpid = neighborVertex.getProperty("dpid").toString();
161 long neighborId = HexString.toLong(neighborDpid);
162 Node neighbor = nodesMap.get(neighborId);
163 if (neighbor == null) {
164 neighbor = new Node(neighborId);
165 nodesMap.put(neighborId, neighbor);
166 }
167 me.addNeighbor(neighbor, myPort, neighborPort);
168 }
169 }
170 }
171 }
172 dbHandler.commit();
173 }
174}