blob: 03b4c96e82e56d069fa49f98a7cacbdbcce5d9cb [file] [log] [blame]
Pankaj Berde85016ab2013-06-21 11:34:53 -07001package net.onrc.onos.graph;
2
3import java.util.ArrayList;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07004import java.util.Iterator;
Pankaj Berde85016ab2013-06-21 11:34:53 -07005import java.util.List;
6
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07007import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject;
Pankaj Berde85016ab2013-06-21 11:34:53 -07008import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070011import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
Pankaj Berde85016ab2013-06-21 11:34:53 -070012import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
14import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
15import net.onrc.onos.ofcontroller.util.FlowEntryId;
16import net.onrc.onos.ofcontroller.util.FlowId;
17
18import com.thinkaurelius.titan.core.TitanGraph;
19import com.tinkerpop.blueprints.Vertex;
20import com.tinkerpop.frames.FramedGraph;
21import com.tinkerpop.frames.structures.FramedVertexIterable;
22import com.tinkerpop.gremlin.java.GremlinPipeline;
23
24public class GraphDBOperation implements IDBOperation {
25 private GraphDBConnection conn;
26
27 /**
28 * Create a GraphDBOperation instance from specified GraphDBConnection's instance.
29 * @param dbConnection an instance of GraphDBConnection
30 */
31 public GraphDBOperation(GraphDBConnection dbConnection) {
32 this.conn = dbConnection;
33 }
34
35 /**
36 * Create a GraphDBOperation instance from database configuration path.
37 * @param dbConfPath a path for database configuration file.
38 */
39 public GraphDBOperation(final String dbConfPath) {
40 this.conn = GraphDBConnection.getInstance(dbConfPath);
41 }
42
43 /**
44 * Create a new switch and return the created switch object.
45 * @param dpid DPID of the switch
46 */
47 public ISwitchObject newSwitch(String dpid) {
48 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
49 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
50 if (obj != null) {
51 obj.setType("switch");
52 obj.setDPID(dpid);
53 }
54 return obj;
55 }
56
57 /**
58 * Search and get a switch object with DPID.
59 * @param dpid DPID of the switch
60 */
61 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdebbd38612013-06-22 05:59:12 -070062
Pankaj Berde85016ab2013-06-21 11:34:53 -070063 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
64
65 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
66 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
67
68 }
69
70 /**
71 * Search and get an active switch object with DPID.
72 * @param dpid DPID of the switch
73 */
74 public ISwitchObject searchActiveSwitch(String dpid) {
75
76 ISwitchObject sw = searchSwitch(dpid);
77 if ((sw != null) &&
78 sw.getState().equals(SwitchState.ACTIVE.toString())) {
79 return sw;
80 }
81 return null;
82 }
83
84 /**
85 * Get all switch objects.
86 */
87 public Iterable<ISwitchObject> getAllSwitches() {
88 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
89 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
90 return switches;
91 }
92
93 /**
94 * Get all active switch objects.
95 */
96 public Iterable<ISwitchObject> getActiveSwitches() {
97 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
98 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
99 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
100
101 for (ISwitchObject sw: switches) {
102 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
103 activeSwitches.add(sw);
104 }
105 }
106 return activeSwitches;
107 }
108
109 /**
110 * Get all inactive switch objects.
111 */
112 public Iterable<ISwitchObject> getInactiveSwitches() {
113 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
114 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
115 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
116
117 for (ISwitchObject sw: switches) {
118 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
119 inactiveSwitches.add(sw);
120 }
121 }
122 return inactiveSwitches;
123 }
124
125 /**
126 * Get all flow entries' objects where their switches are not updated.
127 */
128 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
129 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
130 //TODO: Should use an enum for flow_switch_state
131 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
132 }
133
134 /**
135 * Remove specified switch.
136 * @param sw switch object to remove
137 */
138 public void removeSwitch(ISwitchObject sw) {
139 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
140 fg.removeVertex(sw.asVertex());
141 }
Pankaj Berdebbd38612013-06-22 05:59:12 -0700142
143 @Override
144 public IPortObject newPort(String dpid, 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 String id = dpid + portNumber.toString();
150 obj.setPortId(id);
151 obj.setNumber(portNumber);
152 }
153 return obj;
154
155 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700156
157 /**
158 * Create a port having specified port number.
159 * @param portNumber port number
160 */
Pankaj Berdebbd38612013-06-22 05:59:12 -0700161 @Deprecated
Pankaj Berde85016ab2013-06-21 11:34:53 -0700162 public IPortObject newPort(Short portNumber) {
163 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
164 IPortObject obj = fg.addVertex(null,IPortObject.class);
165 if (obj != null) {
166 obj.setType("port");
167 obj.setNumber(portNumber);
168 }
169 return obj;
170 }
171
172 /**
173 * Search and get a port object of specified switch and port number.
174 * @param dpid DPID of a switch
175 * @param number port number of the switch's port
176 */
Pankaj Berdebbd38612013-06-22 05:59:12 -0700177 public IPortObject searchPort(String dpid, Short number) {
178 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Yuta HIGUCHI6039faf2013-12-11 16:28:11 -0800179 if ( fg == null ) return null;
Pankaj Berdebbd38612013-06-22 05:59:12 -0700180 String id = dpid + number.toString();
Yuta HIGUCHI6039faf2013-12-11 16:28:11 -0800181 Iterator<IPortObject> ports = fg.getVertices("port_id",id,IPortObject.class).iterator();
182 if ( ports.hasNext() ) {
183 return ports.next();
184 } else {
185 return null;
186 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700187 }
188
189 /**
190 * Remove the specified switch port.
191 * @param port switch port object to remove
192 */
193 public void removePort(IPortObject port) {
194 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
195// EventGraph<TitanGraph> eg = conn.getEventGraph();
196 if (fg != null) fg.removeVertex(port.asVertex());
197 }
198
199 /**
200 * Create and return a device object.
201 */
202 public IDeviceObject newDevice() {
203 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
204 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
205 if (obj != null) obj.setType("device");
206 return obj;
207 }
208
209 /**
210 * Search and get a device object having specified MAC address.
211 * @param macAddr MAC address to search and get
212 */
213 public IDeviceObject searchDevice(String macAddr) {
Yuta HIGUCHI6039faf2013-12-11 16:28:11 -0800214 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
215 if ( fg == null ) return null;
216 Iterator<IDeviceObject> devices = fg.getVertices("dl_addr",macAddr, IDeviceObject.class).iterator();
217 if ( devices.hasNext() ) {
218 return devices.next();
219 } else {
220 return null;
221 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700222 }
223
224 /**
225 * Get all devices.
226 */
227 public Iterable<IDeviceObject> getDevices() {
228 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
229 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
230 }
231
232 /**
233 * Remove the specified device.
234 * @param dev a device object to remove
235 */
236 public void removeDevice(IDeviceObject dev) {
237 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
238 if (fg != null) fg.removeVertex(dev.asVertex());
239 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700240
241 public IIpv4Address newIpv4Address() {
242 return newVertex("ipv4Address", IIpv4Address.class);
243 }
244
245 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
246 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
247 T newVertex = fg.addVertex(null, vertexType);
248 if (newVertex != null) {
249 newVertex.setType(type);
250 }
251 return newVertex;
252 }
253
254 public IIpv4Address searchIpv4Address(int intIpv4Address) {
255 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
256 }
257
258 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
259 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
260 if (fg != null) {
261 Iterator<T> it =
262 fg.getVertices(propertyName, propertyValue, vertexType).iterator();
263 if (it.hasNext()) {
264 return it.next();
265 }
266 }
267 return null;
268 }
269
270 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
271 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
272 if (ipv4Vertex == null) {
273 ipv4Vertex = newIpv4Address();
274 ipv4Vertex.setIpv4Address(intIpv4Address);
275 }
276 return ipv4Vertex;
277 }
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700278
279 public void removeIpv4Address(IIpv4Address ipv4Address) {
280 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
281 fg.removeVertex(ipv4Address.asVertex());
282 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700283
284 /**
285 * Create and return a flow path object.
286 */
287 public IFlowPath newFlowPath() {
288 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
289 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
290 if (flowPath != null) flowPath.setType("flow");
291 return flowPath;
292 }
293
294 /**
295 * Search and get a flow path object with specified flow ID.
296 * @param flowId flow ID to search
297 */
298 public IFlowPath searchFlowPath(FlowId flowId) {
299 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Yuta HIGUCHI6039faf2013-12-11 16:28:11 -0800300 if ( fg == null ) return null;
301 Iterator<IFlowPath> flowpaths = fg.getVertices("flow_id", flowId.toString(), IFlowPath.class).iterator();
302 if ( flowpaths.hasNext() ) {
303 return flowpaths.next();
304 } else {
305 return null;
306 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700307 }
308
309 /**
310 * Get a flow path object with a flow entry.
311 * @param flowEntry flow entry object
312 */
313 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Naoki Shiota991093a2013-12-10 14:47:18 -0800314 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>();
315 pipe.start(flowEntry.asVertex()).out("flow");
Naoki Shiotae2f4da72013-12-09 16:34:17 -0800316 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable<IFlowPath>(conn.getFramedGraph(),
Naoki Shiota991093a2013-12-10 14:47:18 -0800317 (Iterable<Vertex>) pipe, IFlowPath.class);
Pankaj Berde85016ab2013-06-21 11:34:53 -0700318 return r.iterator().hasNext() ? r.iterator().next() : null;
319 }
320
321 /**
322 * Get all flow path objects.
323 */
324 public Iterable<IFlowPath> getAllFlowPaths() {
325 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
326 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
327
328 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
329
330 for (IFlowPath fp: flowPaths) {
331 if (fp.getFlowId() != null) {
332 nonNullFlows.add(fp);
333 }
334 }
335 return nonNullFlows;
336 }
337
338 /**
339 * Remove the specified flow path.
340 * @param flowPath flow path object to remove
341 */
342 public void removeFlowPath(IFlowPath flowPath) {
343 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
344 fg.removeVertex(flowPath.asVertex());
345 }
346
347 /**
348 * Create and return a flow entry object.
349 */
350 public IFlowEntry newFlowEntry() {
351 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
352 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
353 if (flowEntry != null) flowEntry.setType("flow_entry");
354 return flowEntry;
355 }
356
357 /**
358 * Search and get a flow entry object with flow entry ID.
359 * @param flowEntryId flow entry ID to search
360 */
361 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
362 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Yuta HIGUCHI6039faf2013-12-11 16:28:11 -0800363 if ( fg == null ) return null;
364 Iterator<IFlowEntry> flowentries = fg.getVertices("flow_entry_id", flowEntryId.toString(), IFlowEntry.class).iterator();
365 if ( flowentries.hasNext() ) {
366 return flowentries.next();
367 } else {
368 return null;
369 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700370 }
371
372 /**
373 * Get all flow entry objects.
374 */
375 public Iterable<IFlowEntry> getAllFlowEntries() {
376 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
377
378 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
379 }
380
381 /**
382 * Remove the specified flow entry.
383 * @param flowEntry flow entry object to remove
384 */
385 public void removeFlowEntry(IFlowEntry flowEntry) {
386 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
387 fg.removeVertex(flowEntry.asVertex());
388 }
389
390 /**
391 * Get the instance of GraphDBConnection assigned to this class.
392 */
393 public IDBConnection getDBConnection() {
394 return conn;
395 }
396
397 /**
398 * Commit changes for the graph.
399 */
400 public void commit() {
401 conn.commit();
402 }
403
404 /**
405 * Rollback changes for the graph.
406 */
407 public void rollback() {
408 conn.rollback();
409 }
410
411 /**
412 * Close the connection of the assigned GraphDBConnection.
413 */
414 public void close() {
415 conn.close();
416 }
Pankaj Berdebbd38612013-06-22 05:59:12 -0700417
418
Pankaj Berde85016ab2013-06-21 11:34:53 -0700419}