blob: d965ad56f7c103276ea377ba187d6b0511768ba3 [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) {
54 ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
55 if (obj != null) {
56 obj.setType("switch");
57 obj.setDPID(dpid);
58 }
59 return obj;
60 }
yoshi2dd767c2013-11-27 23:39:06 -080061
62 /**
63 * Get all switch objects.
64 */
yoshic455c012013-11-27 10:35:50 -080065 @Override
66 public Iterable<ISwitchObject> getAllSwitches() {
67 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
68 return switches;
69 }
yoshi0451f282013-11-22 15:48:55 -080070
yoshi2dd767c2013-11-27 23:39:06 -080071 /**
72 * Get all inactive switch objects.
73 */
yoshic455c012013-11-27 10:35:50 -080074 @Override
75 public Iterable<ISwitchObject> getInactiveSwitches() {
76 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
77 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -080078
yoshic455c012013-11-27 10:35:50 -080079 for (ISwitchObject sw : switches) {
80 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
81 inactiveSwitches.add(sw);
82 }
83 }
84 return inactiveSwitches;
85 }
yoshi2dd767c2013-11-27 23:39:06 -080086
87 /**
88 * Get all flow entries objects where their switches are not updated.
89 */
yoshic455c012013-11-27 10:35:50 -080090 @Override
91 public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
92 //TODO: Should use an enum for flow_switch_state
93 return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -080094
yoshic455c012013-11-27 10:35:50 -080095 }
yoshi0451f282013-11-22 15:48:55 -080096
yoshi2dd767c2013-11-27 23:39:06 -080097 /**
98 * Remove specified switch.
99 * @param sw switch object to remove
100 */
yoshic455c012013-11-27 10:35:50 -0800101 @Override
102 public void removeSwitch(ISwitchObject sw) {
103 conn.getFramedGraph().removeVertex(sw.asVertex());
104 }
yoshi0451f282013-11-22 15:48:55 -0800105
yoshic455c012013-11-27 10:35:50 -0800106 @Override
107 public IPortObject newPort(String dpid, Short portNum) {
108 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
109 if (obj != null) {
110 obj.setType("port");
111 String id = dpid + portNum.toString();
112 obj.setPortId(id);
113 obj.setNumber(portNum);
114 }
115 return obj;
116 }
yoshi0451f282013-11-22 15:48:55 -0800117
yoshic455c012013-11-27 10:35:50 -0800118 /**
119 * Create a port having specified port number.
120 *
121 * @param portNumber port number
122 */
123 @Deprecated
124 public IPortObject newPort(Short portNumber) {
125 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
126 if (obj != null) {
127 obj.setType("port");
128 obj.setNumber(portNumber);
129 }
130 return obj;
131 }
yoshi0451f282013-11-22 15:48:55 -0800132
yoshi2dd767c2013-11-27 23:39:06 -0800133 /**
134 * Search and get a port object of specified switch and port number.
135 * @param dpid DPID of a switch
136 * @param number port number of the switch's port
137 */
yoshic455c012013-11-27 10:35:50 -0800138 @Override
139 public IPortObject searchPort(String dpid, Short number) {
140 String id = dpid + number.toString();
141 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("port_id", id).iterator().hasNext())
142 ? (IPortObject) conn.getFramedGraph().getVertices("port_id", id, IPortObject.class).iterator().next() : null;
yoshi0451f282013-11-22 15:48:55 -0800143
yoshic455c012013-11-27 10:35:50 -0800144 }
yoshi0451f282013-11-22 15:48:55 -0800145
yoshi2dd767c2013-11-27 23:39:06 -0800146 /**
147 * Remove the specified switch port.
148 * @param port switch port object to remove
149 */
yoshic455c012013-11-27 10:35:50 -0800150 @Override
151 public void removePort(IPortObject port) {
152 if (conn.getFramedGraph() != null) {
153 conn.getFramedGraph().removeVertex(port.asVertex());
154 }
155 }
156
yoshi2dd767c2013-11-27 23:39:06 -0800157 /**
158 * Create and return a device object.
159 */
yoshic455c012013-11-27 10:35:50 -0800160 @Override
161 public IDeviceObject newDevice() {
162 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
163 if (obj != null) {
164 obj.setType("device");
165 }
166 return obj;
167 }
168
yoshi2dd767c2013-11-27 23:39:06 -0800169 /**
170 * Get all devices.
171 */
yoshic455c012013-11-27 10:35:50 -0800172 @Override
173 public Iterable<IDeviceObject> getDevices() {
174 return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null;
175 }
yoshi0451f282013-11-22 15:48:55 -0800176
yoshi2dd767c2013-11-27 23:39:06 -0800177 /**
178 * Remove the specified device.
179 * @param dev a device object to remove
180 */
yoshic455c012013-11-27 10:35:50 -0800181 @Override
182 public void removeDevice(IDeviceObject dev) {
183 if (conn.getFramedGraph() != null) {
184 conn.getFramedGraph().removeVertex(dev.asVertex());
185 }
186 }
187
188 /**
189 * Create and return a flow path object.
190 */
yoshic455c012013-11-27 10:35:50 -0800191 @Override
192 public IFlowPath newFlowPath() {
193 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
194 if (flowPath != null) {
195 flowPath.setType("flow");
196 }
197 return flowPath;
198 }
yoshi0451f282013-11-22 15:48:55 -0800199
yoshi2dd767c2013-11-27 23:39:06 -0800200 /**
201 * Get a flow path object with a flow entry.
202 * @param flowEntry flow entry object
203 */
yoshic455c012013-11-27 10:35:50 -0800204 @Override
205 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
206 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
207 pipe.start(flowEntry.asVertex());
208 pipe.out("flow");
209 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
210 return r.iterator().hasNext() ? r.iterator().next() : null;
211 }
yoshi0451f282013-11-22 15:48:55 -0800212
yoshi0451f282013-11-22 15:48:55 -0800213
yoshic455c012013-11-27 10:35:50 -0800214 /**
215 * Search and get a switch object with DPID.
216 *
217 * @param dpid DPID of the switch
218 */
219 @Override
220 public ISwitchObject searchSwitch(final String dpid) {
221 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dpid", dpid).iterator().hasNext())
222 ? (ISwitchObject) (conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
223 }
yoshi0451f282013-11-22 15:48:55 -0800224
yoshi2dd767c2013-11-27 23:39:06 -0800225 /**
226 * Get all active switch objects.
227 */
yoshic455c012013-11-27 10:35:50 -0800228 @Override
229 public Iterable<ISwitchObject> getActiveSwitches() {
230 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
231 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800232
yoshic455c012013-11-27 10:35:50 -0800233 for (ISwitchObject sw : switches) {
234 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
235 activeSwitches.add(sw);
236 }
237 }
238 return activeSwitches;
239 }
yoshi0451f282013-11-22 15:48:55 -0800240
yoshi2dd767c2013-11-27 23:39:06 -0800241 /**
242 * Search and get a device object having specified MAC address.
243 * @param macAddr MAC address to search and get
244 */
yoshic455c012013-11-27 10:35:50 -0800245 @Override
246 public IDeviceObject searchDevice(String macAddr) {
247 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dl_addr", macAddr).iterator().hasNext())
248 ? (IDeviceObject) conn.getFramedGraph().getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
249
250 }
251
yoshi2dd767c2013-11-27 23:39:06 -0800252 /**
253 * Search and get a flow path object with specified flow ID.
254 * @param flowId flow ID to search
255 */
yoshib3c83c12013-12-03 00:58:13 -0800256 @Override
257 public IFlowPath searchFlowPath(final FlowId flowId) {
258 return conn.getFramedGraph().getVertices("flow_id", flowId.toString()).iterator().hasNext()
259 ? (IFlowPath) conn.getFramedGraph().getVertices("flow_id", flowId.toString(),
yoshic455c012013-11-27 10:35:50 -0800260 IFlowPath.class).iterator().next() : null;
261 }
262
yoshi2dd767c2013-11-27 23:39:06 -0800263 /**
264 * Get all flow path objects.
265 */
yoshib3c83c12013-12-03 00:58:13 -0800266 @Override
267 public Iterable<IFlowPath> getAllFlowPaths() {
268 Iterable<IFlowPath> flowPaths = conn.getFramedGraph().getVertices("type", "flow", IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800269
270 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
271
272 for (IFlowPath fp : flowPaths) {
273 if (fp.getFlowId() != null) {
274 nonNullFlows.add(fp);
275 }
276 }
277 return nonNullFlows;
278 }
yoshi2dd767c2013-11-27 23:39:06 -0800279
280 /**
yoshib3c83c12013-12-03 00:58:13 -0800281 * Remove the specified flow path.
282 * @param flowPath flow path object to remove
283 */
284 @Override
285 public void removeFlowPath(IFlowPath flowPath) {
286 conn.getFramedGraph().removeVertex(flowPath.asVertex());
287 }
288
289 /**
290 * Search and get a flow entry object with flow entry ID.
291 * @param flowEntryId flow entry ID to search
292 */
293 @Override
294 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
295 return conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext()
296 ? (IFlowEntry)conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString(),
297 IFlowEntry.class).iterator().next() : null;
298 }
299
300 /**
301 * Get all flow entry objects.
302 */
303 @Override
304 public Iterable<IFlowEntry> getAllFlowEntries() {
305 return conn.getFramedGraph().getVertices("type", "flow_entry", IFlowEntry.class);
306 }
307
308 /**
309 * Remove the specified flow entry.
310 * @param flowEntry flow entry object to remove
311 */
312 @Override
313 public void removeFlowEntry(IFlowEntry flowEntry) {
314 conn.getFramedGraph().removeVertex(flowEntry.asVertex());
315 }
316
317 /**
yoshi2dd767c2013-11-27 23:39:06 -0800318 * Create and return a flow entry object.
319 */
yoshic455c012013-11-27 10:35:50 -0800320 @Override
321 public IFlowEntry newFlowEntry() {
322 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
323 if (flowEntry != null) {
324 flowEntry.setType("flow_entry");
325 }
326 return flowEntry;
327 }
328
329
yoshitomob292c622013-11-23 14:35:58 -0800330 public IIpv4Address newIpv4Address() {
331 return newVertex("ipv4Address", IIpv4Address.class);
332 }
yoshi2dd767c2013-11-27 23:39:06 -0800333
yoshitomob292c622013-11-23 14:35:58 -0800334 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800335 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800336 if (newVertex != null) {
337 newVertex.setType(type);
338 }
339 return newVertex;
340 }
yoshic455c012013-11-27 10:35:50 -0800341
yoshitomob292c622013-11-23 14:35:58 -0800342 public IIpv4Address searchIpv4Address(int intIpv4Address) {
343 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
344 }
345
346
347 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
348 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
349 if (ipv4Vertex == null) {
350 ipv4Vertex = newIpv4Address();
351 ipv4Vertex.setIpv4Address(intIpv4Address);
352 }
353 return ipv4Vertex;
354 }
355
356
357 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
358 if (conn.getFramedGraph() != null) {
359 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
360 if (it.hasNext()) {
361 return it.next();
362 }
363 }
364 return null;
365 }
366
367 public void removeIpv4Address(IIpv4Address ipv4Address) {
368 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
369 }
yoshi0451f282013-11-22 15:48:55 -0800370
yoshib3c83c12013-12-03 00:58:13 -0800371 /**
372 * Get the instance of GraphDBConnection assigned to this class.
373 */
yoshi7594aef2013-11-27 09:27:07 -0800374 @Override
375 public IDBConnection getDBConnection() {
376 return conn;
377 }
yoshib3c83c12013-12-03 00:58:13 -0800378
379 @Override
380 public void commit() {
381 conn.commit();
382 }
383
384 @Override
385 public void rollback() {
386 conn.rollback();
387 }
388
389 @Override
390 public void close() {
391 conn.close();
392 }
yoshi0451f282013-11-22 15:48:55 -0800393}