blob: bfd9046575b2f63913c625eeb5acf72d3ece6584 [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();
179 String id = dpid + number.toString();
180 return (fg != null && fg.getVertices("port_id",id).iterator().hasNext()) ?
181 fg.getVertices("port_id",id,IPortObject.class).iterator().next() : null;
Pankaj Berde85016ab2013-06-21 11:34:53 -0700182 }
183
184 /**
185 * Remove the specified switch port.
186 * @param port switch port object to remove
187 */
188 public void removePort(IPortObject port) {
189 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
190// EventGraph<TitanGraph> eg = conn.getEventGraph();
191 if (fg != null) fg.removeVertex(port.asVertex());
192 }
193
194 /**
195 * Create and return a device object.
196 */
197 public IDeviceObject newDevice() {
198 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
199 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
200 if (obj != null) obj.setType("device");
201 return obj;
202 }
203
204 /**
205 * Search and get a device object having specified MAC address.
206 * @param macAddr MAC address to search and get
207 */
208 public IDeviceObject searchDevice(String macAddr) {
209 // TODO Auto-generated method stub
210 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
211 return (fg != null && fg.getVertices("dl_addr",macAddr).iterator().hasNext()) ?
212 fg.getVertices("dl_addr",macAddr, IDeviceObject.class).iterator().next() : null;
213 }
214
215 /**
216 * Get all devices.
217 */
218 public Iterable<IDeviceObject> getDevices() {
219 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
220 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
221 }
222
223 /**
224 * Remove the specified device.
225 * @param dev a device object to remove
226 */
227 public void removeDevice(IDeviceObject dev) {
228 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
229 if (fg != null) fg.removeVertex(dev.asVertex());
230 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700231
232 public IIpv4Address newIpv4Address() {
233 return newVertex("ipv4Address", IIpv4Address.class);
234 }
235
236 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
237 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
238 T newVertex = fg.addVertex(null, vertexType);
239 if (newVertex != null) {
240 newVertex.setType(type);
241 }
242 return newVertex;
243 }
244
245 public IIpv4Address searchIpv4Address(int intIpv4Address) {
246 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
247 }
248
249 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
250 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
251 if (fg != null) {
252 Iterator<T> it =
253 fg.getVertices(propertyName, propertyValue, vertexType).iterator();
254 if (it.hasNext()) {
255 return it.next();
256 }
257 }
258 return null;
259 }
260
261 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
262 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
263 if (ipv4Vertex == null) {
264 ipv4Vertex = newIpv4Address();
265 ipv4Vertex.setIpv4Address(intIpv4Address);
266 }
267 return ipv4Vertex;
268 }
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700269
270 public void removeIpv4Address(IIpv4Address ipv4Address) {
271 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
272 fg.removeVertex(ipv4Address.asVertex());
273 }
Pankaj Berde85016ab2013-06-21 11:34:53 -0700274
275 /**
276 * Create and return a flow path object.
277 */
278 public IFlowPath newFlowPath() {
279 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
280 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
281 if (flowPath != null) flowPath.setType("flow");
282 return flowPath;
283 }
284
285 /**
286 * Search and get a flow path object with specified flow ID.
287 * @param flowId flow ID to search
288 */
289 public IFlowPath searchFlowPath(FlowId flowId) {
290 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
291
292 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
293 fg.getVertices("flow_id", flowId.toString(),
294 IFlowPath.class).iterator().next() : null;
295 }
296
297 /**
298 * Get a flow path object with a flow entry.
299 * @param flowEntry flow entry object
300 */
301 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
302 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
303 pipe.start(flowEntry.asVertex());
304 pipe.out("flow");
305 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
306 return r.iterator().hasNext() ? r.iterator().next() : null;
307 }
308
309 /**
310 * Get all flow path objects.
311 */
312 public Iterable<IFlowPath> getAllFlowPaths() {
313 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
314 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
315
316 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
317
318 for (IFlowPath fp: flowPaths) {
319 if (fp.getFlowId() != null) {
320 nonNullFlows.add(fp);
321 }
322 }
323 return nonNullFlows;
324 }
325
326 /**
327 * Remove the specified flow path.
328 * @param flowPath flow path object to remove
329 */
330 public void removeFlowPath(IFlowPath flowPath) {
331 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
332 fg.removeVertex(flowPath.asVertex());
333 }
334
335 /**
336 * Create and return a flow entry object.
337 */
338 public IFlowEntry newFlowEntry() {
339 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
340 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
341 if (flowEntry != null) flowEntry.setType("flow_entry");
342 return flowEntry;
343 }
344
345 /**
346 * Search and get a flow entry object with flow entry ID.
347 * @param flowEntryId flow entry ID to search
348 */
349 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
350 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
351
352 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
353 fg.getVertices("flow_entry_id", flowEntryId.toString(),
354 IFlowEntry.class).iterator().next() : null;
355 }
356
357 /**
358 * Get all flow entry objects.
359 */
360 public Iterable<IFlowEntry> getAllFlowEntries() {
361 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
362
363 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
364 }
365
366 /**
367 * Remove the specified flow entry.
368 * @param flowEntry flow entry object to remove
369 */
370 public void removeFlowEntry(IFlowEntry flowEntry) {
371 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
372 fg.removeVertex(flowEntry.asVertex());
373 }
374
375 /**
376 * Get the instance of GraphDBConnection assigned to this class.
377 */
378 public IDBConnection getDBConnection() {
379 return conn;
380 }
381
382 /**
383 * Commit changes for the graph.
384 */
385 public void commit() {
386 conn.commit();
387 }
388
389 /**
390 * Rollback changes for the graph.
391 */
392 public void rollback() {
393 conn.rollback();
394 }
395
396 /**
397 * Close the connection of the assigned GraphDBConnection.
398 */
399 public void close() {
400 conn.close();
401 }
Pankaj Berdebbd38612013-06-22 05:59:12 -0700402
403
Pankaj Berde85016ab2013-06-21 11:34:53 -0700404}