blob: 4512b48473a13907031b7f73701d30c88db5267d [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) {
yoshi89eacab2013-12-09 17:29:08 -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() {
yoshi89eacab2013-12-09 17:29:08 -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() {
yoshi89eacab2013-12-09 17:29:08 -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) {
yoshi89eacab2013-12-09 17:29:08 -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) {
yoshi89eacab2013-12-09 17:29:08 -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) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800145 FramedGraph fg = conn.getFramedGraph();
146 if ( fg == null ) return null;
yoshic455c012013-11-27 10:35:50 -0800147 String id = dpid + number.toString();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800148 Iterator<IPortObject> it = conn.getFramedGraph().getVertices("port_id", id, IPortObject.class).iterator();
149 return (it.hasNext()) ? it.next() : null;
yoshi0451f282013-11-22 15:48:55 -0800150
yoshic455c012013-11-27 10:35:50 -0800151 }
yoshi0451f282013-11-22 15:48:55 -0800152
yoshi2dd767c2013-11-27 23:39:06 -0800153 /**
154 * Remove the specified switch port.
155 * @param port switch port object to remove
156 */
yoshic455c012013-11-27 10:35:50 -0800157 @Override
158 public void removePort(IPortObject port) {
yoshi89eacab2013-12-09 17:29:08 -0800159 //System.out.println("removeProt");
yoshic455c012013-11-27 10:35:50 -0800160 if (conn.getFramedGraph() != null) {
161 conn.getFramedGraph().removeVertex(port.asVertex());
162 }
163 }
164
yoshi2dd767c2013-11-27 23:39:06 -0800165 /**
166 * Create and return a device object.
167 */
yoshic455c012013-11-27 10:35:50 -0800168 @Override
169 public IDeviceObject newDevice() {
yoshi89eacab2013-12-09 17:29:08 -0800170 //System.out.println("newDevice");
yoshic455c012013-11-27 10:35:50 -0800171 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
172 if (obj != null) {
173 obj.setType("device");
174 }
175 return obj;
176 }
177
yoshi2dd767c2013-11-27 23:39:06 -0800178 /**
179 * Get all devices.
180 */
yoshic455c012013-11-27 10:35:50 -0800181 @Override
182 public Iterable<IDeviceObject> getDevices() {
yoshi89eacab2013-12-09 17:29:08 -0800183 //System.out.println("getDeiveces");
yoshic455c012013-11-27 10:35:50 -0800184 return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null;
185 }
yoshi0451f282013-11-22 15:48:55 -0800186
yoshi2dd767c2013-11-27 23:39:06 -0800187 /**
188 * Remove the specified device.
189 * @param dev a device object to remove
190 */
yoshic455c012013-11-27 10:35:50 -0800191 @Override
192 public void removeDevice(IDeviceObject dev) {
yoshi89eacab2013-12-09 17:29:08 -0800193 //System.out.println("removeDevice");
yoshic455c012013-11-27 10:35:50 -0800194 if (conn.getFramedGraph() != null) {
195 conn.getFramedGraph().removeVertex(dev.asVertex());
196 }
197 }
198
199 /**
200 * Create and return a flow path object.
201 */
yoshic455c012013-11-27 10:35:50 -0800202 @Override
203 public IFlowPath newFlowPath() {
yoshi89eacab2013-12-09 17:29:08 -0800204 //System.out.println("newFlowPath");
yoshic455c012013-11-27 10:35:50 -0800205 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
yoshi89eacab2013-12-09 17:29:08 -0800206 //System.out.println("flowPath : " + flowPath);
yoshic455c012013-11-27 10:35:50 -0800207 if (flowPath != null) {
208 flowPath.setType("flow");
209 }
210 return flowPath;
211 }
yoshi0451f282013-11-22 15:48:55 -0800212
yoshi2dd767c2013-11-27 23:39:06 -0800213 /**
214 * Get a flow path object with a flow entry.
215 * @param flowEntry flow entry object
216 */
yoshic455c012013-11-27 10:35:50 -0800217 @Override
218 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
219 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
220 pipe.start(flowEntry.asVertex());
221 pipe.out("flow");
222 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
223 return r.iterator().hasNext() ? r.iterator().next() : null;
224 }
yoshi0451f282013-11-22 15:48:55 -0800225
yoshi0451f282013-11-22 15:48:55 -0800226
yoshic455c012013-11-27 10:35:50 -0800227 /**
228 * Search and get a switch object with DPID.
229 *
230 * @param dpid DPID of the switch
231 */
232 @Override
233 public ISwitchObject searchSwitch(final String dpid) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800234 FramedGraph fg = conn.getFramedGraph();
235 if ( fg == null ) return null;
236 Iterator<ISwitchObject> it = conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator();
237 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800238 }
yoshi0451f282013-11-22 15:48:55 -0800239
yoshi2dd767c2013-11-27 23:39:06 -0800240 /**
241 * Get all active switch objects.
242 */
yoshic455c012013-11-27 10:35:50 -0800243 @Override
244 public Iterable<ISwitchObject> getActiveSwitches() {
245 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
246 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800247
yoshic455c012013-11-27 10:35:50 -0800248 for (ISwitchObject sw : switches) {
249 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
250 activeSwitches.add(sw);
251 }
252 }
253 return activeSwitches;
254 }
yoshi0451f282013-11-22 15:48:55 -0800255
yoshi2dd767c2013-11-27 23:39:06 -0800256 /**
257 * Search and get a device object having specified MAC address.
258 * @param macAddr MAC address to search and get
259 */
yoshic455c012013-11-27 10:35:50 -0800260 @Override
261 public IDeviceObject searchDevice(String macAddr) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800262 FramedGraph fg = conn.getFramedGraph();
263 if ( fg == null ) return null;
264 Iterator<IDeviceObject> it = conn.getFramedGraph().getVertices("dl_addr", macAddr, IDeviceObject.class).iterator();
265 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800266 }
267
yoshi2dd767c2013-11-27 23:39:06 -0800268 /**
269 * Search and get a flow path object with specified flow ID.
270 * @param flowId flow ID to search
271 */
yoshib3c83c12013-12-03 00:58:13 -0800272 @Override
273 public IFlowPath searchFlowPath(final FlowId flowId) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800274 FramedGraph fg = conn.getFramedGraph();
275 if ( fg == null ) return null;
276 Iterator<IFlowPath> it = conn.getFramedGraph().getVertices("flow_id", flowId.toString(), IFlowPath.class).iterator();
277 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800278 }
279
yoshi2dd767c2013-11-27 23:39:06 -0800280 /**
281 * Get all flow path objects.
282 */
yoshib3c83c12013-12-03 00:58:13 -0800283 @Override
284 public Iterable<IFlowPath> getAllFlowPaths() {
yoshi89eacab2013-12-09 17:29:08 -0800285 //System.out.println("getAllFlowPaths");
yoshib3c83c12013-12-03 00:58:13 -0800286 Iterable<IFlowPath> flowPaths = conn.getFramedGraph().getVertices("type", "flow", IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800287
288 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
289
290 for (IFlowPath fp : flowPaths) {
291 if (fp.getFlowId() != null) {
292 nonNullFlows.add(fp);
293 }
294 }
295 return nonNullFlows;
296 }
yoshi2dd767c2013-11-27 23:39:06 -0800297
298 /**
yoshib3c83c12013-12-03 00:58:13 -0800299 * Remove the specified flow path.
300 * @param flowPath flow path object to remove
301 */
302 @Override
303 public void removeFlowPath(IFlowPath flowPath) {
yoshi89eacab2013-12-09 17:29:08 -0800304 //System.out.println("removeFlowPath");
yoshib3c83c12013-12-03 00:58:13 -0800305 conn.getFramedGraph().removeVertex(flowPath.asVertex());
306 }
307
308 /**
309 * Search and get a flow entry object with flow entry ID.
310 * @param flowEntryId flow entry ID to search
311 */
312 @Override
313 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800314 FramedGraph fg = conn.getFramedGraph();
315 if ( fg == null ) return null;
316 Iterator<IFlowEntry> it = conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString(), IFlowEntry.class).iterator();
317 return (it.hasNext()) ? it.next() : null;
yoshib3c83c12013-12-03 00:58:13 -0800318 }
319
320 /**
321 * Get all flow entry objects.
322 */
323 @Override
324 public Iterable<IFlowEntry> getAllFlowEntries() {
325 return conn.getFramedGraph().getVertices("type", "flow_entry", IFlowEntry.class);
326 }
327
328 /**
329 * Remove the specified flow entry.
330 * @param flowEntry flow entry object to remove
331 */
332 @Override
333 public void removeFlowEntry(IFlowEntry flowEntry) {
yoshi89eacab2013-12-09 17:29:08 -0800334 //System.out.println("removeFlowEntry");
yoshib3c83c12013-12-03 00:58:13 -0800335 conn.getFramedGraph().removeVertex(flowEntry.asVertex());
336 }
337
338 /**
yoshi2dd767c2013-11-27 23:39:06 -0800339 * Create and return a flow entry object.
340 */
yoshic455c012013-11-27 10:35:50 -0800341 @Override
342 public IFlowEntry newFlowEntry() {
yoshi89eacab2013-12-09 17:29:08 -0800343 //System.out.println("newFlowEntry");
yoshic455c012013-11-27 10:35:50 -0800344 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
345 if (flowEntry != null) {
346 flowEntry.setType("flow_entry");
347 }
348 return flowEntry;
349 }
350
351
yoshitomob292c622013-11-23 14:35:58 -0800352 public IIpv4Address newIpv4Address() {
353 return newVertex("ipv4Address", IIpv4Address.class);
354 }
yoshi2dd767c2013-11-27 23:39:06 -0800355
yoshitomob292c622013-11-23 14:35:58 -0800356 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800357 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800358 if (newVertex != null) {
359 newVertex.setType(type);
360 }
361 return newVertex;
362 }
yoshic455c012013-11-27 10:35:50 -0800363
yoshitomob292c622013-11-23 14:35:58 -0800364 public IIpv4Address searchIpv4Address(int intIpv4Address) {
365 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
366 }
367
368
369 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
370 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
371 if (ipv4Vertex == null) {
372 ipv4Vertex = newIpv4Address();
373 ipv4Vertex.setIpv4Address(intIpv4Address);
374 }
375 return ipv4Vertex;
376 }
377
378
379 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
380 if (conn.getFramedGraph() != null) {
381 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
382 if (it.hasNext()) {
383 return it.next();
384 }
385 }
386 return null;
387 }
388
389 public void removeIpv4Address(IIpv4Address ipv4Address) {
yoshi89eacab2013-12-09 17:29:08 -0800390 //System.out.println("removeIpv4Address");
yoshitomob292c622013-11-23 14:35:58 -0800391 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
392 }
yoshi0451f282013-11-22 15:48:55 -0800393
yoshib3c83c12013-12-03 00:58:13 -0800394 /**
395 * Get the instance of GraphDBConnection assigned to this class.
396 */
yoshi7594aef2013-11-27 09:27:07 -0800397 @Override
398 public IDBConnection getDBConnection() {
399 return conn;
400 }
yoshib3c83c12013-12-03 00:58:13 -0800401
402 @Override
403 public void commit() {
404 conn.commit();
405 }
406
407 @Override
408 public void rollback() {
409 conn.rollback();
410 }
411
412 @Override
413 public void close() {
414 conn.close();
415 }
yoshi0451f282013-11-22 15:48:55 -0800416}