blob: 92157fbbf8e344e504387594ac9596f9fef6b207 [file] [log] [blame]
Pankaj Berdeda809572013-02-22 15:31:20 -08001package net.onrc.onos.util;
2
Pankaj Berde15193092013-03-21 17:30:14 -07003import java.util.ArrayList;
4import java.util.List;
5
Toshio Koideb29b9b32013-06-13 14:37:46 -07006import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
7import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
8import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
9import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
11import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
12import net.onrc.onos.ofcontroller.util.FlowEntryId;
13import net.onrc.onos.ofcontroller.util.FlowId;
14
Pankaj Berde8f036112013-03-28 22:58:47 -070015import com.thinkaurelius.titan.core.TitanGraph;
16import com.tinkerpop.blueprints.Vertex;
Pankaj Berde8f036112013-03-28 22:58:47 -070017import com.tinkerpop.frames.FramedGraph;
Pankaj Berde5d506412013-04-23 15:03:02 -070018import com.tinkerpop.frames.structures.FramedVertexIterable;
Pankaj Berde8f036112013-03-28 22:58:47 -070019import com.tinkerpop.gremlin.java.GremlinPipeline;
20
Toshio Koideeb88ff62013-06-12 16:46:40 -070021public class GraphDBOperation implements IDBOperation {
22 private GraphDBConnection conn;
Toshio Koide1d1a8562013-06-18 17:19:51 -070023
24 /**
25 * Create a GraphDBOperation instance from specified GraphDBConnection's instance.
26 * @param dbConnection an instance of GraphDBConnection
27 */
Toshio Koideeb88ff62013-06-12 16:46:40 -070028 public GraphDBOperation(GraphDBConnection dbConnection) {
29 this.conn = dbConnection;
30 }
Toshio Koide1d1a8562013-06-18 17:19:51 -070031
32 /**
33 * Create a GraphDBOperation instance from database configuration path.
34 * @param dbConfPath a path for database configuration file.
35 */
Toshio Koidebfe9b922013-06-18 10:56:05 -070036 public GraphDBOperation(final String dbConfPath) {
37 this.conn = GraphDBConnection.getInstance(dbConfPath);
38 }
Toshio Koide53403802013-06-13 10:08:06 -070039
Toshio Koide1d1a8562013-06-18 17:19:51 -070040 /**
41 * Create a new switch and return the created switch object.
42 * @param dpid DPID of the switch
43 */
Toshio Koide53403802013-06-13 10:08:06 -070044 public ISwitchObject newSwitch(String dpid) {
Pankaj Berde15193092013-03-21 17:30:14 -070045 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
46 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -070047 if (obj != null) {
48 obj.setType("switch");
49 obj.setDPID(dpid);
50 }
Pankaj Berde15193092013-03-21 17:30:14 -070051 return obj;
52 }
Pankaj Berdeda809572013-02-22 15:31:20 -080053
Toshio Koide1d1a8562013-06-18 17:19:51 -070054 /**
55 * Search and get a switch object with DPID.
56 * @param dpid DPID of the switch
57 */
Toshio Koideeb88ff62013-06-12 16:46:40 -070058 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080059 // TODO Auto-generated method stub
60 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
61
Pankaj Berde62016142013-04-09 15:35:50 -070062 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080063 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
Toshio Koide53403802013-06-13 10:08:06 -070064
Pankaj Berdeda809572013-02-22 15:31:20 -080065 }
66
Toshio Koide1d1a8562013-06-18 17:19:51 -070067 /**
68 * Search and get an active switch object with DPID.
69 * @param dpid DPID of the switch
70 */
Toshio Koide53403802013-06-13 10:08:06 -070071 public ISwitchObject searchActiveSwitch(String dpid) {
72
73 ISwitchObject sw = searchSwitch(dpid);
74 if ((sw != null) &&
75 sw.getState().equals(SwitchState.ACTIVE.toString())) {
76 return sw;
77 }
78 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080079 }
80
Toshio Koide1d1a8562013-06-18 17:19:51 -070081 /**
82 * Get all switch objects.
83 */
Toshio Koide53403802013-06-13 10:08:06 -070084 public Iterable<ISwitchObject> getAllSwitches() {
85 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
86 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
87 return switches;
88 }
89
Toshio Koide1d1a8562013-06-18 17:19:51 -070090 /**
91 * Get all active switch objects.
92 */
Toshio Koide53403802013-06-13 10:08:06 -070093 public Iterable<ISwitchObject> getActiveSwitches() {
94 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
95 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
96 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
97
98 for (ISwitchObject sw: switches) {
99 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
100 activeSwitches.add(sw);
Pankaj Berdec165a522013-06-10 21:28:11 -0700101 }
Pankaj Berde62016142013-04-09 15:35:50 -0700102 }
Toshio Koide53403802013-06-13 10:08:06 -0700103 return activeSwitches;
104 }
105
Toshio Koide1d1a8562013-06-18 17:19:51 -0700106 /**
107 * Get all inactive switch objects.
108 */
Toshio Koide53403802013-06-13 10:08:06 -0700109 public Iterable<ISwitchObject> getInactiveSwitches() {
110 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
111 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
112 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
113
114 for (ISwitchObject sw: switches) {
115 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
116 inactiveSwitches.add(sw);
117 }
118 }
119 return inactiveSwitches;
120 }
121
Toshio Koide1d1a8562013-06-18 17:19:51 -0700122 /**
123 * Get all flow entries' objects where their switches are not updated.
124 */
Toshio Koide53403802013-06-13 10:08:06 -0700125 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
126 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
127 //TODO: Should use an enum for flow_switch_state
128 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
129 }
130
Toshio Koide1d1a8562013-06-18 17:19:51 -0700131 /**
132 * Remove specified switch.
133 * @param sw switch object to remove
134 */
Toshio Koide53403802013-06-13 10:08:06 -0700135 public void removeSwitch(ISwitchObject sw) {
136 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
137 fg.removeVertex(sw.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800138 }
139
Toshio Koide1d1a8562013-06-18 17:19:51 -0700140 /**
141 * Create a port having specified port number.
142 * @param portNumber port number
143 */
Toshio Koide12004e62013-06-12 18:17:53 -0700144 public IPortObject newPort(Short portNumber) {
Pankaj Berde15193092013-03-21 17:30:14 -0700145 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
146 IPortObject obj = fg.addVertex(null,IPortObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -0700147 if (obj != null) {
148 obj.setType("port");
149 obj.setNumber(portNumber);
150 }
Pankaj Berde15193092013-03-21 17:30:14 -0700151 return obj;
152 }
Toshio Koide53403802013-06-13 10:08:06 -0700153
Toshio Koide1d1a8562013-06-18 17:19:51 -0700154 /**
155 * Search and get a port object of specified switch and port number.
156 * @param dpid DPID of a switch
157 * @param number port number of the switch's port
158 */
159 public IPortObject searchPort(String dpid, short number) {
160 ISwitchObject sw = searchSwitch(dpid);
161 if (sw != null) {
162
163 IPortObject port = null;
164
165 // Requires Frames 2.3.0
166
167 try {
168 port = sw.getPort(number);
169 } catch (Exception e) {
170 // TODO Auto-generated catch block
171 e.printStackTrace();
Toshio Koide53403802013-06-13 10:08:06 -0700172 }
173
Toshio Koide1d1a8562013-06-18 17:19:51 -0700174 return port;
175 }
176
Toshio Koide53403802013-06-13 10:08:06 -0700177 // if (sw != null) {
178 // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
179 // pipe.start(sw.asVertex());
180 // pipe.out("on").has("number", number);
181 // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
182 // return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
183 // }
Toshio Koide1d1a8562013-06-18 17:19:51 -0700184 return null;
185 }
Toshio Koide53403802013-06-13 10:08:06 -0700186
Toshio Koide1d1a8562013-06-18 17:19:51 -0700187 /**
188 * Remove the specified switch port.
189 * @param port switch port object to remove
190 */
191 public void removePort(IPortObject port) {
192 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
193// EventGraph<TitanGraph> eg = conn.getEventGraph();
194 if (fg != null) fg.removeVertex(port.asVertex());
195 }
Toshio Koide53403802013-06-13 10:08:06 -0700196
Toshio Koide1d1a8562013-06-18 17:19:51 -0700197 /**
198 * Create and return a device object.
199 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700200 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -0800201 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
202 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700203 if (obj != null) obj.setType("device");
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 return obj;
205 }
206
Toshio Koide1d1a8562013-06-18 17:19:51 -0700207 /**
208 * Search and get a device object having specified MAC address.
209 * @param macAddr MAC address to search and get
210 */
Toshio Koide53403802013-06-13 10:08:06 -0700211 public IDeviceObject searchDevice(String macAddr) {
212 // TODO Auto-generated method stub
Pankaj Berdeda809572013-02-22 15:31:20 -0800213 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide13260102013-06-17 13:44:27 -0700214 return (fg != null && fg.getVertices("dl_addr",macAddr).iterator().hasNext()) ?
215 fg.getVertices("dl_addr",macAddr, IDeviceObject.class).iterator().next() : null;
Pankaj Berdeda809572013-02-22 15:31:20 -0800216 }
217
Toshio Koide1d1a8562013-06-18 17:19:51 -0700218 /**
219 * Get all devices.
220 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700221 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800222 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700223 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800224 }
225
Toshio Koide1d1a8562013-06-18 17:19:51 -0700226 /**
227 * Remove the specified device.
228 * @param dev a device object to remove
229 */
Toshio Koide53403802013-06-13 10:08:06 -0700230 public void removeDevice(IDeviceObject dev) {
231 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
232 if (fg != null) fg.removeVertex(dev.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800233 }
234
Toshio Koide1d1a8562013-06-18 17:19:51 -0700235 /**
236 * Create and return a flow path object.
237 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700238 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800239 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
240 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700241 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800242 return flowPath;
243 }
244
Toshio Koide1d1a8562013-06-18 17:19:51 -0700245 /**
246 * Search and get a flow path object with specified flow ID.
247 * @param flowId flow ID to search
248 */
Toshio Koide53403802013-06-13 10:08:06 -0700249 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800250 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700251
252 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
253 fg.getVertices("flow_id", flowId.toString(),
254 IFlowPath.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800255 }
256
Toshio Koide1d1a8562013-06-18 17:19:51 -0700257 /**
258 * Get a flow path object with a flow entry.
259 * @param flowEntry flow entry object
260 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700261 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800262 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
263 pipe.start(flowEntry.asVertex());
264 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700265 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800266 return r.iterator().hasNext() ? r.iterator().next() : null;
267 }
268
Toshio Koide1d1a8562013-06-18 17:19:51 -0700269 /**
270 * Get all flow path objects.
271 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700272 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800273 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700274 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800275
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700276 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
277
278 for (IFlowPath fp: flowPaths) {
279 if (fp.getFlowId() != null) {
280 nonNullFlows.add(fp);
281 }
282 }
283 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800284 }
285
Toshio Koide1d1a8562013-06-18 17:19:51 -0700286 /**
287 * Remove the specified flow path.
288 * @param flowPath flow path object to remove
289 */
Toshio Koide53403802013-06-13 10:08:06 -0700290 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800291 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700292 fg.removeVertex(flowPath.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800293 }
294
Toshio Koide1d1a8562013-06-18 17:19:51 -0700295 /**
296 * Create and return a flow entry object.
297 */
Toshio Koideeb88ff62013-06-12 16:46:40 -0700298 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800299 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
300 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700301 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800302 return flowEntry;
303 }
304
Toshio Koide1d1a8562013-06-18 17:19:51 -0700305 /**
306 * Search and get a flow entry object with flow entry ID.
307 * @param flowEntryId flow entry ID to search
308 */
Toshio Koide53403802013-06-13 10:08:06 -0700309 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800310 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700311
312 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
313 fg.getVertices("flow_entry_id", flowEntryId.toString(),
314 IFlowEntry.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800315 }
316
Toshio Koide1d1a8562013-06-18 17:19:51 -0700317 /**
318 * Get all flow entry objects.
319 */
320 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800321 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
322
323 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
324 }
Toshio Koide53403802013-06-13 10:08:06 -0700325
Toshio Koide1d1a8562013-06-18 17:19:51 -0700326 /**
327 * Remove the specified flow entry.
328 * @param flowEntry flow entry object to remove
329 */
Toshio Koide53403802013-06-13 10:08:06 -0700330 public void removeFlowEntry(IFlowEntry flowEntry) {
Pankaj Berded1c38592013-04-10 22:46:40 -0700331 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700332 fg.removeVertex(flowEntry.asVertex());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700333 }
Toshio Koidea41950a2013-06-13 13:09:19 -0700334
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700335 /**
336 * Get the instance of GraphDBConnection assigned to this class.
337 */
Toshio Koidea41950a2013-06-13 13:09:19 -0700338 public IDBConnection getDBConnection() {
339 return conn;
340 }
341
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700342 /**
343 * Commit changes for the graph.
344 */
Toshio Koidea41950a2013-06-13 13:09:19 -0700345 public void commit() {
Toshio Koidedc949442013-06-18 10:35:51 -0700346 conn.commit();
Toshio Koidea41950a2013-06-13 13:09:19 -0700347 }
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700348
349 /**
350 * Rollback changes for the graph.
351 */
Toshio Koidea41950a2013-06-13 13:09:19 -0700352 public void rollback() {
Toshio Koidedc949442013-06-18 10:35:51 -0700353 conn.rollback();
Toshio Koidea41950a2013-06-13 13:09:19 -0700354 }
Toshio Koidef20a5072013-06-13 13:18:22 -0700355
Toshio Koidea7cff3d2013-06-19 11:28:24 -0700356 /**
357 * Close the connection of the assigned GraphDBConnection.
358 */
Toshio Koidef20a5072013-06-13 13:18:22 -0700359 public void close() {
360 conn.close();
361 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800362}