blob: bc8d4049db0e13ec4fbf2d49f85b4e91197ee7b8 [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.graph;
6
yoshi0451f282013-11-22 15:48:55 -08007import com.tinkerpop.blueprints.Vertex;
8import com.tinkerpop.frames.FramedGraph;
9import com.tinkerpop.frames.structures.FramedVertexIterable;
10import com.tinkerpop.gremlin.java.GremlinPipeline;
11import java.util.ArrayList;
12import java.util.Iterator;
13import java.util.List;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects;
yoshitomob292c622013-11-23 14:35:58 -080015import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject;
yoshi0451f282013-11-22 15:48:55 -080016import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
17import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
yoshitomob292c622013-11-23 14:35:58 -080019import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
yoshi0451f282013-11-22 15:48:55 -080020import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
21import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
22import net.onrc.onos.ofcontroller.core.ISwitchStorage;
yoshib3c83c12013-12-03 00:58:13 -080023import net.onrc.onos.ofcontroller.util.FlowEntryId;
yoshi0451f282013-11-22 15:48:55 -080024import net.onrc.onos.ofcontroller.util.FlowId;
25
26/**
27 *
28 * @author nickkaranatsios
29 */
30public abstract class DBOperation implements IDBOperation {
31
yoshic455c012013-11-27 10:35:50 -080032 protected DBConnection conn;
yoshi0451f282013-11-22 15:48:55 -080033
yoshi2dd767c2013-11-27 23:39:06 -080034 /**
35 * Search and get an active switch object with DPID.
36 * @param dpid DPID of the switch
37 */
yoshic455c012013-11-27 10:35:50 -080038 @Override
39 public ISwitchObject searchActiveSwitch(String dpid) {
40 ISwitchObject sw = searchSwitch(dpid);
41 if ((sw != null)
42 && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
43 return sw;
44 }
45 return null;
46 }
yoshi2dd767c2013-11-27 23:39:06 -080047
48 /**
49 * Create a new switch and return the created switch object.
50 * @param dpid DPID of the switch
51 */
yoshic455c012013-11-27 10:35:50 -080052 @Override
53 public ISwitchObject newSwitch(final String dpid) {
yoshi9e5f0ca2013-12-05 08:47:04 -080054 System.out.println("newSwitch");
yoshic455c012013-11-27 10:35:50 -080055 ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
56 if (obj != null) {
57 obj.setType("switch");
58 obj.setDPID(dpid);
59 }
60 return obj;
61 }
yoshi2dd767c2013-11-27 23:39:06 -080062
63 /**
64 * Get all switch objects.
65 */
yoshic455c012013-11-27 10:35:50 -080066 @Override
67 public Iterable<ISwitchObject> getAllSwitches() {
yoshi9e5f0ca2013-12-05 08:47:04 -080068 System.out.println("getAllSwitches");
yoshic455c012013-11-27 10:35:50 -080069 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
70 return switches;
71 }
yoshi0451f282013-11-22 15:48:55 -080072
yoshi2dd767c2013-11-27 23:39:06 -080073 /**
74 * Get all inactive switch objects.
75 */
yoshic455c012013-11-27 10:35:50 -080076 @Override
77 public Iterable<ISwitchObject> getInactiveSwitches() {
yoshi9e5f0ca2013-12-05 08:47:04 -080078 System.out.println("getInactiveSwitches");
yoshic455c012013-11-27 10:35:50 -080079 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
80 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -080081
yoshic455c012013-11-27 10:35:50 -080082 for (ISwitchObject sw : switches) {
83 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
84 inactiveSwitches.add(sw);
85 }
86 }
87 return inactiveSwitches;
88 }
yoshi2dd767c2013-11-27 23:39:06 -080089
90 /**
91 * Get all flow entries objects where their switches are not updated.
92 */
yoshic455c012013-11-27 10:35:50 -080093 @Override
94 public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
95 //TODO: Should use an enum for flow_switch_state
96 return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -080097
yoshic455c012013-11-27 10:35:50 -080098 }
yoshi0451f282013-11-22 15:48:55 -080099
yoshi2dd767c2013-11-27 23:39:06 -0800100 /**
101 * Remove specified switch.
102 * @param sw switch object to remove
103 */
yoshic455c012013-11-27 10:35:50 -0800104 @Override
105 public void removeSwitch(ISwitchObject sw) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800106 System.out.println("removeSwitch");
yoshic455c012013-11-27 10:35:50 -0800107 conn.getFramedGraph().removeVertex(sw.asVertex());
108 }
yoshi0451f282013-11-22 15:48:55 -0800109
yoshic455c012013-11-27 10:35:50 -0800110 @Override
111 public IPortObject newPort(String dpid, Short portNum) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800112 System.out.println("newPort");
yoshic455c012013-11-27 10:35:50 -0800113 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
114 if (obj != null) {
115 obj.setType("port");
116 String id = dpid + portNum.toString();
117 obj.setPortId(id);
118 obj.setNumber(portNum);
119 }
120 return obj;
121 }
yoshi0451f282013-11-22 15:48:55 -0800122
yoshic455c012013-11-27 10:35:50 -0800123 /**
124 * Create a port having specified port number.
125 *
126 * @param portNumber port number
127 */
128 @Deprecated
129 public IPortObject newPort(Short portNumber) {
130 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
131 if (obj != null) {
132 obj.setType("port");
133 obj.setNumber(portNumber);
134 }
135 return obj;
136 }
yoshi0451f282013-11-22 15:48:55 -0800137
yoshi2dd767c2013-11-27 23:39:06 -0800138 /**
139 * Search and get a port object of specified switch and port number.
140 * @param dpid DPID of a switch
141 * @param number port number of the switch's port
142 */
yoshic455c012013-11-27 10:35:50 -0800143 @Override
144 public IPortObject searchPort(String dpid, Short number) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800145 System.out.println("searchPort");
yoshic455c012013-11-27 10:35:50 -0800146 String id = dpid + number.toString();
147 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("port_id", id).iterator().hasNext())
148 ? (IPortObject) conn.getFramedGraph().getVertices("port_id", id, IPortObject.class).iterator().next() : null;
yoshi0451f282013-11-22 15:48:55 -0800149
yoshic455c012013-11-27 10:35:50 -0800150 }
yoshi0451f282013-11-22 15:48:55 -0800151
yoshi2dd767c2013-11-27 23:39:06 -0800152 /**
153 * Remove the specified switch port.
154 * @param port switch port object to remove
155 */
yoshic455c012013-11-27 10:35:50 -0800156 @Override
157 public void removePort(IPortObject port) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800158 System.out.println("removeProt");
yoshic455c012013-11-27 10:35:50 -0800159 if (conn.getFramedGraph() != null) {
160 conn.getFramedGraph().removeVertex(port.asVertex());
161 }
162 }
163
yoshi2dd767c2013-11-27 23:39:06 -0800164 /**
165 * Create and return a device object.
166 */
yoshic455c012013-11-27 10:35:50 -0800167 @Override
168 public IDeviceObject newDevice() {
yoshi9e5f0ca2013-12-05 08:47:04 -0800169 System.out.println("newDevice");
yoshic455c012013-11-27 10:35:50 -0800170 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
171 if (obj != null) {
172 obj.setType("device");
173 }
174 return obj;
175 }
176
yoshi2dd767c2013-11-27 23:39:06 -0800177 /**
178 * Get all devices.
179 */
yoshic455c012013-11-27 10:35:50 -0800180 @Override
181 public Iterable<IDeviceObject> getDevices() {
yoshi9e5f0ca2013-12-05 08:47:04 -0800182 System.out.println("getDeiveces");
yoshic455c012013-11-27 10:35:50 -0800183 return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null;
184 }
yoshi0451f282013-11-22 15:48:55 -0800185
yoshi2dd767c2013-11-27 23:39:06 -0800186 /**
187 * Remove the specified device.
188 * @param dev a device object to remove
189 */
yoshic455c012013-11-27 10:35:50 -0800190 @Override
191 public void removeDevice(IDeviceObject dev) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800192 System.out.println("removeDevice");
yoshic455c012013-11-27 10:35:50 -0800193 if (conn.getFramedGraph() != null) {
194 conn.getFramedGraph().removeVertex(dev.asVertex());
195 }
196 }
197
198 /**
199 * Create and return a flow path object.
200 */
yoshic455c012013-11-27 10:35:50 -0800201 @Override
202 public IFlowPath newFlowPath() {
yoshi40210942013-12-03 08:21:02 -0800203 System.out.println("newFlowPath");
yoshic455c012013-11-27 10:35:50 -0800204 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
205 if (flowPath != null) {
206 flowPath.setType("flow");
207 }
208 return flowPath;
209 }
yoshi0451f282013-11-22 15:48:55 -0800210
yoshi2dd767c2013-11-27 23:39:06 -0800211 /**
212 * Get a flow path object with a flow entry.
213 * @param flowEntry flow entry object
214 */
yoshic455c012013-11-27 10:35:50 -0800215 @Override
216 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
217 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
218 pipe.start(flowEntry.asVertex());
219 pipe.out("flow");
220 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
221 return r.iterator().hasNext() ? r.iterator().next() : null;
222 }
yoshi0451f282013-11-22 15:48:55 -0800223
yoshi0451f282013-11-22 15:48:55 -0800224
yoshic455c012013-11-27 10:35:50 -0800225 /**
226 * Search and get a switch object with DPID.
227 *
228 * @param dpid DPID of the switch
229 */
230 @Override
231 public ISwitchObject searchSwitch(final String dpid) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800232 System.out.println("searchSwitch");
yoshic455c012013-11-27 10:35:50 -0800233 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dpid", dpid).iterator().hasNext())
234 ? (ISwitchObject) (conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
235 }
yoshi0451f282013-11-22 15:48:55 -0800236
yoshi2dd767c2013-11-27 23:39:06 -0800237 /**
238 * Get all active switch objects.
239 */
yoshic455c012013-11-27 10:35:50 -0800240 @Override
241 public Iterable<ISwitchObject> getActiveSwitches() {
242 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
243 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800244
yoshic455c012013-11-27 10:35:50 -0800245 for (ISwitchObject sw : switches) {
246 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
247 activeSwitches.add(sw);
248 }
249 }
250 return activeSwitches;
251 }
yoshi0451f282013-11-22 15:48:55 -0800252
yoshi2dd767c2013-11-27 23:39:06 -0800253 /**
254 * Search and get a device object having specified MAC address.
255 * @param macAddr MAC address to search and get
256 */
yoshic455c012013-11-27 10:35:50 -0800257 @Override
258 public IDeviceObject searchDevice(String macAddr) {
259 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dl_addr", macAddr).iterator().hasNext())
260 ? (IDeviceObject) conn.getFramedGraph().getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
261
262 }
263
yoshi2dd767c2013-11-27 23:39:06 -0800264 /**
265 * Search and get a flow path object with specified flow ID.
266 * @param flowId flow ID to search
267 */
yoshib3c83c12013-12-03 00:58:13 -0800268 @Override
269 public IFlowPath searchFlowPath(final FlowId flowId) {
yoshi40210942013-12-03 08:21:02 -0800270 System.out.println("searchFlowPath");
yoshib3c83c12013-12-03 00:58:13 -0800271 return conn.getFramedGraph().getVertices("flow_id", flowId.toString()).iterator().hasNext()
272 ? (IFlowPath) conn.getFramedGraph().getVertices("flow_id", flowId.toString(),
yoshic455c012013-11-27 10:35:50 -0800273 IFlowPath.class).iterator().next() : null;
274 }
275
yoshi2dd767c2013-11-27 23:39:06 -0800276 /**
277 * Get all flow path objects.
278 */
yoshib3c83c12013-12-03 00:58:13 -0800279 @Override
280 public Iterable<IFlowPath> getAllFlowPaths() {
yoshi40210942013-12-03 08:21:02 -0800281 System.out.println("getAllFlowPaths");
yoshib3c83c12013-12-03 00:58:13 -0800282 Iterable<IFlowPath> flowPaths = conn.getFramedGraph().getVertices("type", "flow", IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800283
284 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
285
286 for (IFlowPath fp : flowPaths) {
287 if (fp.getFlowId() != null) {
288 nonNullFlows.add(fp);
289 }
290 }
291 return nonNullFlows;
292 }
yoshi2dd767c2013-11-27 23:39:06 -0800293
294 /**
yoshib3c83c12013-12-03 00:58:13 -0800295 * Remove the specified flow path.
296 * @param flowPath flow path object to remove
297 */
298 @Override
299 public void removeFlowPath(IFlowPath flowPath) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800300 System.out.println("removeFlowPath");
yoshib3c83c12013-12-03 00:58:13 -0800301 conn.getFramedGraph().removeVertex(flowPath.asVertex());
302 }
303
304 /**
305 * Search and get a flow entry object with flow entry ID.
306 * @param flowEntryId flow entry ID to search
307 */
308 @Override
309 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
yoshi40210942013-12-03 08:21:02 -0800310 System.out.println("searchFlowEntry");
yoshib3c83c12013-12-03 00:58:13 -0800311 return conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext()
312 ? (IFlowEntry)conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString(),
313 IFlowEntry.class).iterator().next() : null;
314 }
315
316 /**
317 * Get all flow entry objects.
318 */
319 @Override
320 public Iterable<IFlowEntry> getAllFlowEntries() {
321 return conn.getFramedGraph().getVertices("type", "flow_entry", IFlowEntry.class);
322 }
323
324 /**
325 * Remove the specified flow entry.
326 * @param flowEntry flow entry object to remove
327 */
328 @Override
329 public void removeFlowEntry(IFlowEntry flowEntry) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800330 System.out.println("removeFlowEntry");
yoshib3c83c12013-12-03 00:58:13 -0800331 conn.getFramedGraph().removeVertex(flowEntry.asVertex());
332 }
333
334 /**
yoshi2dd767c2013-11-27 23:39:06 -0800335 * Create and return a flow entry object.
336 */
yoshic455c012013-11-27 10:35:50 -0800337 @Override
338 public IFlowEntry newFlowEntry() {
yoshi40210942013-12-03 08:21:02 -0800339 System.out.println("newFlowEntry");
yoshic455c012013-11-27 10:35:50 -0800340 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
341 if (flowEntry != null) {
342 flowEntry.setType("flow_entry");
343 }
344 return flowEntry;
345 }
346
347
yoshitomob292c622013-11-23 14:35:58 -0800348 public IIpv4Address newIpv4Address() {
349 return newVertex("ipv4Address", IIpv4Address.class);
350 }
yoshi2dd767c2013-11-27 23:39:06 -0800351
yoshitomob292c622013-11-23 14:35:58 -0800352 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800353 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800354 if (newVertex != null) {
355 newVertex.setType(type);
356 }
357 return newVertex;
358 }
yoshic455c012013-11-27 10:35:50 -0800359
yoshitomob292c622013-11-23 14:35:58 -0800360 public IIpv4Address searchIpv4Address(int intIpv4Address) {
361 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
362 }
363
364
365 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
366 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
367 if (ipv4Vertex == null) {
368 ipv4Vertex = newIpv4Address();
369 ipv4Vertex.setIpv4Address(intIpv4Address);
370 }
371 return ipv4Vertex;
372 }
373
374
375 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
376 if (conn.getFramedGraph() != null) {
377 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
378 if (it.hasNext()) {
379 return it.next();
380 }
381 }
382 return null;
383 }
384
385 public void removeIpv4Address(IIpv4Address ipv4Address) {
yoshi9e5f0ca2013-12-05 08:47:04 -0800386 System.out.println("removeIpv4Address");
yoshitomob292c622013-11-23 14:35:58 -0800387 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
388 }
yoshi0451f282013-11-22 15:48:55 -0800389
yoshib3c83c12013-12-03 00:58:13 -0800390 /**
391 * Get the instance of GraphDBConnection assigned to this class.
392 */
yoshi7594aef2013-11-27 09:27:07 -0800393 @Override
394 public IDBConnection getDBConnection() {
395 return conn;
396 }
yoshib3c83c12013-12-03 00:58:13 -0800397
398 @Override
399 public void commit() {
400 conn.commit();
401 }
402
403 @Override
404 public void rollback() {
405 conn.rollback();
406 }
407
408 @Override
409 public void close() {
410 conn.close();
411 }
yoshi0451f282013-11-22 15:48:55 -0800412}