blob: 0718db2f59a481199f5a7016ac9d3c4088a6dfc7 [file] [log] [blame]
Pankaj Berde85016ab2013-06-21 11:34:53 -07001package net.onrc.onos.graph;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
7import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
8import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
11import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
12import net.onrc.onos.ofcontroller.util.FlowEntryId;
13import net.onrc.onos.ofcontroller.util.FlowId;
14
15import com.thinkaurelius.titan.core.TitanGraph;
16import com.tinkerpop.blueprints.Vertex;
17import com.tinkerpop.frames.FramedGraph;
18import com.tinkerpop.frames.structures.FramedVertexIterable;
19import com.tinkerpop.gremlin.java.GremlinPipeline;
20
21public class GraphDBOperation implements IDBOperation {
22 private GraphDBConnection conn;
23
24 /**
25 * Create a GraphDBOperation instance from specified GraphDBConnection's instance.
26 * @param dbConnection an instance of GraphDBConnection
27 */
28 public GraphDBOperation(GraphDBConnection dbConnection) {
29 this.conn = dbConnection;
30 }
31
32 /**
33 * Create a GraphDBOperation instance from database configuration path.
34 * @param dbConfPath a path for database configuration file.
35 */
36 public GraphDBOperation(final String dbConfPath) {
37 this.conn = GraphDBConnection.getInstance(dbConfPath);
38 }
39
40 /**
41 * Create a new switch and return the created switch object.
42 * @param dpid DPID of the switch
43 */
44 public ISwitchObject newSwitch(String dpid) {
45 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
46 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
47 if (obj != null) {
48 obj.setType("switch");
49 obj.setDPID(dpid);
50 }
51 return obj;
52 }
53
54 /**
55 * Search and get a switch object with DPID.
56 * @param dpid DPID of the switch
57 */
58 public ISwitchObject searchSwitch(String dpid) {
59 // TODO Auto-generated method stub
60 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
61
62 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
63 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
64
65 }
66
67 /**
68 * Search and get an active switch object with DPID.
69 * @param dpid DPID of the switch
70 */
71 public ISwitchObject searchActiveSwitch(String dpid) {
72
73 ISwitchObject sw = searchSwitch(dpid);
74 if ((sw != null) &&
75 sw.getState().equals(SwitchState.ACTIVE.toString())) {
76 return sw;
77 }
78 return null;
79 }
80
81 /**
82 * Get all switch objects.
83 */
84 public Iterable<ISwitchObject> getAllSwitches() {
85 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
86 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
87 return switches;
88 }
89
90 /**
91 * Get all active switch objects.
92 */
93 public Iterable<ISwitchObject> getActiveSwitches() {
94 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
95 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
96 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
97
98 for (ISwitchObject sw: switches) {
99 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
100 activeSwitches.add(sw);
101 }
102 }
103 return activeSwitches;
104 }
105
106 /**
107 * Get all inactive switch objects.
108 */
109 public Iterable<ISwitchObject> getInactiveSwitches() {
110 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
111 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
112 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
113
114 for (ISwitchObject sw: switches) {
115 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
116 inactiveSwitches.add(sw);
117 }
118 }
119 return inactiveSwitches;
120 }
121
122 /**
123 * Get all flow entries' objects where their switches are not updated.
124 */
125 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
126 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
127 //TODO: Should use an enum for flow_switch_state
128 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
129 }
130
131 /**
132 * Remove specified switch.
133 * @param sw switch object to remove
134 */
135 public void removeSwitch(ISwitchObject sw) {
136 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
137 fg.removeVertex(sw.asVertex());
138 }
139
140 /**
141 * Create a port having specified port number.
142 * @param portNumber port number
143 */
144 public IPortObject newPort(Short portNumber) {
145 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
146 IPortObject obj = fg.addVertex(null,IPortObject.class);
147 if (obj != null) {
148 obj.setType("port");
149 obj.setNumber(portNumber);
150 }
151 return obj;
152 }
153
154 /**
155 * Search and get a port object of specified switch and port number.
156 * @param dpid DPID of a switch
157 * @param number port number of the switch's port
158 */
159 public IPortObject searchPort(String dpid, short number) {
160 ISwitchObject sw = searchSwitch(dpid);
161 if (sw != null) {
162
163 IPortObject port = null;
164
165 // Requires Frames 2.3.0
166
167 try {
168 port = sw.getPort(number);
169 } catch (Exception e) {
170 // TODO Auto-generated catch block
171 e.printStackTrace();
172 }
173
174 return port;
175 }
176
177 // if (sw != null) {
178 // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
179 // pipe.start(sw.asVertex());
180 // pipe.out("on").has("number", number);
181 // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
182 // return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
183 // }
184 return null;
185 }
186
187 /**
188 * Remove the specified switch port.
189 * @param port switch port object to remove
190 */
191 public void removePort(IPortObject port) {
192 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
193// EventGraph<TitanGraph> eg = conn.getEventGraph();
194 if (fg != null) fg.removeVertex(port.asVertex());
195 }
196
197 /**
198 * Create and return a device object.
199 */
200 public IDeviceObject newDevice() {
201 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
202 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
203 if (obj != null) obj.setType("device");
204 return obj;
205 }
206
207 /**
208 * Search and get a device object having specified MAC address.
209 * @param macAddr MAC address to search and get
210 */
211 public IDeviceObject searchDevice(String macAddr) {
212 // TODO Auto-generated method stub
213 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
214 return (fg != null && fg.getVertices("dl_addr",macAddr).iterator().hasNext()) ?
215 fg.getVertices("dl_addr",macAddr, IDeviceObject.class).iterator().next() : null;
216 }
217
218 /**
219 * Get all devices.
220 */
221 public Iterable<IDeviceObject> getDevices() {
222 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
223 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
224 }
225
226 /**
227 * Remove the specified device.
228 * @param dev a device object to remove
229 */
230 public void removeDevice(IDeviceObject dev) {
231 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
232 if (fg != null) fg.removeVertex(dev.asVertex());
233 }
234
235 /**
236 * Create and return a flow path object.
237 */
238 public IFlowPath newFlowPath() {
239 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
240 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
241 if (flowPath != null) flowPath.setType("flow");
242 return flowPath;
243 }
244
245 /**
246 * Search and get a flow path object with specified flow ID.
247 * @param flowId flow ID to search
248 */
249 public IFlowPath searchFlowPath(FlowId flowId) {
250 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
251
252 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
253 fg.getVertices("flow_id", flowId.toString(),
254 IFlowPath.class).iterator().next() : null;
255 }
256
257 /**
258 * Get a flow path object with a flow entry.
259 * @param flowEntry flow entry object
260 */
261 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
262 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
263 pipe.start(flowEntry.asVertex());
264 pipe.out("flow");
265 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
266 return r.iterator().hasNext() ? r.iterator().next() : null;
267 }
268
269 /**
270 * Get all flow path objects.
271 */
272 public Iterable<IFlowPath> getAllFlowPaths() {
273 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
274 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
275
276 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
277
278 for (IFlowPath fp: flowPaths) {
279 if (fp.getFlowId() != null) {
280 nonNullFlows.add(fp);
281 }
282 }
283 return nonNullFlows;
284 }
285
286 /**
287 * Remove the specified flow path.
288 * @param flowPath flow path object to remove
289 */
290 public void removeFlowPath(IFlowPath flowPath) {
291 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
292 fg.removeVertex(flowPath.asVertex());
293 }
294
295 /**
296 * Create and return a flow entry object.
297 */
298 public IFlowEntry newFlowEntry() {
299 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
300 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
301 if (flowEntry != null) flowEntry.setType("flow_entry");
302 return flowEntry;
303 }
304
305 /**
306 * Search and get a flow entry object with flow entry ID.
307 * @param flowEntryId flow entry ID to search
308 */
309 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
310 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
311
312 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
313 fg.getVertices("flow_entry_id", flowEntryId.toString(),
314 IFlowEntry.class).iterator().next() : null;
315 }
316
317 /**
318 * Get all flow entry objects.
319 */
320 public Iterable<IFlowEntry> getAllFlowEntries() {
321 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
322
323 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
324 }
325
326 /**
327 * Remove the specified flow entry.
328 * @param flowEntry flow entry object to remove
329 */
330 public void removeFlowEntry(IFlowEntry flowEntry) {
331 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
332 fg.removeVertex(flowEntry.asVertex());
333 }
334
335 /**
336 * Get the instance of GraphDBConnection assigned to this class.
337 */
338 public IDBConnection getDBConnection() {
339 return conn;
340 }
341
342 /**
343 * Commit changes for the graph.
344 */
345 public void commit() {
346 conn.commit();
347 }
348
349 /**
350 * Rollback changes for the graph.
351 */
352 public void rollback() {
353 conn.rollback();
354 }
355
356 /**
357 * Close the connection of the assigned GraphDBConnection.
358 */
359 public void close() {
360 conn.close();
361 }
362}