blob: f92260dac4dc664129bc886a61a890b8b322c0fa [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() {
yoshi40210942013-12-03 08:21:02 -0800193 System.out.println("newFlowPath");
yoshic455c012013-11-27 10:35:50 -0800194 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
195 if (flowPath != null) {
196 flowPath.setType("flow");
197 }
198 return flowPath;
199 }
yoshi0451f282013-11-22 15:48:55 -0800200
yoshi2dd767c2013-11-27 23:39:06 -0800201 /**
202 * Get a flow path object with a flow entry.
203 * @param flowEntry flow entry object
204 */
yoshic455c012013-11-27 10:35:50 -0800205 @Override
206 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
207 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
208 pipe.start(flowEntry.asVertex());
209 pipe.out("flow");
210 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
211 return r.iterator().hasNext() ? r.iterator().next() : null;
212 }
yoshi0451f282013-11-22 15:48:55 -0800213
yoshi0451f282013-11-22 15:48:55 -0800214
yoshic455c012013-11-27 10:35:50 -0800215 /**
216 * Search and get a switch object with DPID.
217 *
218 * @param dpid DPID of the switch
219 */
220 @Override
221 public ISwitchObject searchSwitch(final String dpid) {
222 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dpid", dpid).iterator().hasNext())
223 ? (ISwitchObject) (conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
224 }
yoshi0451f282013-11-22 15:48:55 -0800225
yoshi2dd767c2013-11-27 23:39:06 -0800226 /**
227 * Get all active switch objects.
228 */
yoshic455c012013-11-27 10:35:50 -0800229 @Override
230 public Iterable<ISwitchObject> getActiveSwitches() {
231 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
232 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800233
yoshic455c012013-11-27 10:35:50 -0800234 for (ISwitchObject sw : switches) {
235 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
236 activeSwitches.add(sw);
237 }
238 }
239 return activeSwitches;
240 }
yoshi0451f282013-11-22 15:48:55 -0800241
yoshi2dd767c2013-11-27 23:39:06 -0800242 /**
243 * Search and get a device object having specified MAC address.
244 * @param macAddr MAC address to search and get
245 */
yoshic455c012013-11-27 10:35:50 -0800246 @Override
247 public IDeviceObject searchDevice(String macAddr) {
248 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dl_addr", macAddr).iterator().hasNext())
249 ? (IDeviceObject) conn.getFramedGraph().getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
250
251 }
252
yoshi2dd767c2013-11-27 23:39:06 -0800253 /**
254 * Search and get a flow path object with specified flow ID.
255 * @param flowId flow ID to search
256 */
yoshib3c83c12013-12-03 00:58:13 -0800257 @Override
258 public IFlowPath searchFlowPath(final FlowId flowId) {
yoshi40210942013-12-03 08:21:02 -0800259 System.out.println("searchFlowPath");
yoshib3c83c12013-12-03 00:58:13 -0800260 return conn.getFramedGraph().getVertices("flow_id", flowId.toString()).iterator().hasNext()
261 ? (IFlowPath) conn.getFramedGraph().getVertices("flow_id", flowId.toString(),
yoshic455c012013-11-27 10:35:50 -0800262 IFlowPath.class).iterator().next() : null;
263 }
264
yoshi2dd767c2013-11-27 23:39:06 -0800265 /**
266 * Get all flow path objects.
267 */
yoshib3c83c12013-12-03 00:58:13 -0800268 @Override
269 public Iterable<IFlowPath> getAllFlowPaths() {
yoshi40210942013-12-03 08:21:02 -0800270 System.out.println("getAllFlowPaths");
yoshib3c83c12013-12-03 00:58:13 -0800271 Iterable<IFlowPath> flowPaths = conn.getFramedGraph().getVertices("type", "flow", IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800272
273 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
274
275 for (IFlowPath fp : flowPaths) {
276 if (fp.getFlowId() != null) {
277 nonNullFlows.add(fp);
278 }
279 }
280 return nonNullFlows;
281 }
yoshi2dd767c2013-11-27 23:39:06 -0800282
283 /**
yoshib3c83c12013-12-03 00:58:13 -0800284 * Remove the specified flow path.
285 * @param flowPath flow path object to remove
286 */
287 @Override
288 public void removeFlowPath(IFlowPath flowPath) {
289 conn.getFramedGraph().removeVertex(flowPath.asVertex());
290 }
291
292 /**
293 * Search and get a flow entry object with flow entry ID.
294 * @param flowEntryId flow entry ID to search
295 */
296 @Override
297 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
yoshi40210942013-12-03 08:21:02 -0800298 System.out.println("searchFlowEntry");
yoshib3c83c12013-12-03 00:58:13 -0800299 return conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext()
300 ? (IFlowEntry)conn.getFramedGraph().getVertices("flow_entry_id", flowEntryId.toString(),
301 IFlowEntry.class).iterator().next() : null;
302 }
303
304 /**
305 * Get all flow entry objects.
306 */
307 @Override
308 public Iterable<IFlowEntry> getAllFlowEntries() {
309 return conn.getFramedGraph().getVertices("type", "flow_entry", IFlowEntry.class);
310 }
311
312 /**
313 * Remove the specified flow entry.
314 * @param flowEntry flow entry object to remove
315 */
316 @Override
317 public void removeFlowEntry(IFlowEntry flowEntry) {
318 conn.getFramedGraph().removeVertex(flowEntry.asVertex());
319 }
320
321 /**
yoshi2dd767c2013-11-27 23:39:06 -0800322 * Create and return a flow entry object.
323 */
yoshic455c012013-11-27 10:35:50 -0800324 @Override
325 public IFlowEntry newFlowEntry() {
yoshi40210942013-12-03 08:21:02 -0800326 System.out.println("newFlowEntry");
yoshic455c012013-11-27 10:35:50 -0800327 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
328 if (flowEntry != null) {
329 flowEntry.setType("flow_entry");
330 }
331 return flowEntry;
332 }
333
334
yoshitomob292c622013-11-23 14:35:58 -0800335 public IIpv4Address newIpv4Address() {
336 return newVertex("ipv4Address", IIpv4Address.class);
337 }
yoshi2dd767c2013-11-27 23:39:06 -0800338
yoshitomob292c622013-11-23 14:35:58 -0800339 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800340 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800341 if (newVertex != null) {
342 newVertex.setType(type);
343 }
344 return newVertex;
345 }
yoshic455c012013-11-27 10:35:50 -0800346
yoshitomob292c622013-11-23 14:35:58 -0800347 public IIpv4Address searchIpv4Address(int intIpv4Address) {
348 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
349 }
350
351
352 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
353 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
354 if (ipv4Vertex == null) {
355 ipv4Vertex = newIpv4Address();
356 ipv4Vertex.setIpv4Address(intIpv4Address);
357 }
358 return ipv4Vertex;
359 }
360
361
362 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
363 if (conn.getFramedGraph() != null) {
364 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
365 if (it.hasNext()) {
366 return it.next();
367 }
368 }
369 return null;
370 }
371
372 public void removeIpv4Address(IIpv4Address ipv4Address) {
373 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
374 }
yoshi0451f282013-11-22 15:48:55 -0800375
yoshib3c83c12013-12-03 00:58:13 -0800376 /**
377 * Get the instance of GraphDBConnection assigned to this class.
378 */
yoshi7594aef2013-11-27 09:27:07 -0800379 @Override
380 public IDBConnection getDBConnection() {
381 return conn;
382 }
yoshib3c83c12013-12-03 00:58:13 -0800383
384 @Override
385 public void commit() {
386 conn.commit();
387 }
388
389 @Override
390 public void rollback() {
391 conn.rollback();
392 }
393
394 @Override
395 public void close() {
396 conn.close();
397 }
yoshi0451f282013-11-22 15:48:55 -0800398}