blob: 950741d5a3f05b86416a84389c4ef3e1b025c4a9 [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
15//import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
16//import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
17//import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
18//import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
19//import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
20//import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
21//import net.floodlightcontroller.util.FlowEntryId;
22//import net.floodlightcontroller.util.FlowId;
Toshio Koidea41950a2013-06-13 13:09:19 -070023import net.onrc.onos.util.GraphDBConnection.Transaction;
Pankaj Berdeda809572013-02-22 15:31:20 -080024
Pankaj Berde8f036112013-03-28 22:58:47 -070025import com.thinkaurelius.titan.core.TitanGraph;
26import com.tinkerpop.blueprints.Vertex;
Pankaj Berde8f036112013-03-28 22:58:47 -070027import com.tinkerpop.frames.FramedGraph;
Pankaj Berde5d506412013-04-23 15:03:02 -070028import com.tinkerpop.frames.structures.FramedVertexIterable;
Pankaj Berde8f036112013-03-28 22:58:47 -070029import com.tinkerpop.gremlin.java.GremlinPipeline;
30
Toshio Koideeb88ff62013-06-12 16:46:40 -070031public class GraphDBOperation implements IDBOperation {
32 private GraphDBConnection conn;
33
34 public GraphDBOperation(GraphDBConnection dbConnection) {
35 this.conn = dbConnection;
36 }
Toshio Koide53403802013-06-13 10:08:06 -070037
Pankaj Berde15193092013-03-21 17:30:14 -070038 @Override
Toshio Koide53403802013-06-13 10:08:06 -070039 public ISwitchObject newSwitch(String dpid) {
Pankaj Berde15193092013-03-21 17:30:14 -070040 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
41 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -070042 if (obj != null) {
43 obj.setType("switch");
44 obj.setDPID(dpid);
45 }
Pankaj Berde15193092013-03-21 17:30:14 -070046 return obj;
47 }
Pankaj Berdeda809572013-02-22 15:31:20 -080048
49 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070050 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080051 // TODO Auto-generated method stub
52 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
53
Pankaj Berde62016142013-04-09 15:35:50 -070054 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080055 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
Toshio Koide53403802013-06-13 10:08:06 -070056
Pankaj Berdeda809572013-02-22 15:31:20 -080057 }
58
59 @Override
Toshio Koide53403802013-06-13 10:08:06 -070060 public ISwitchObject searchActiveSwitch(String dpid) {
61
62 ISwitchObject sw = searchSwitch(dpid);
63 if ((sw != null) &&
64 sw.getState().equals(SwitchState.ACTIVE.toString())) {
65 return sw;
66 }
67 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080068 }
69
70 @Override
Toshio Koide53403802013-06-13 10:08:06 -070071 public Iterable<ISwitchObject> getAllSwitches() {
72 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
73 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
74 return switches;
75 }
76
77 @Override
78 public Iterable<ISwitchObject> getActiveSwitches() {
79 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
80 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
81 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
82
83 for (ISwitchObject sw: switches) {
84 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
85 activeSwitches.add(sw);
Pankaj Berdec165a522013-06-10 21:28:11 -070086 }
Pankaj Berde62016142013-04-09 15:35:50 -070087 }
Toshio Koide53403802013-06-13 10:08:06 -070088 return activeSwitches;
89 }
90
91 @Override
92 public Iterable<ISwitchObject> getInactiveSwitches() {
93 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
94 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
95 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
96
97 for (ISwitchObject sw: switches) {
98 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
99 inactiveSwitches.add(sw);
100 }
101 }
102 return inactiveSwitches;
103 }
104
105 @Override
106 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
107 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
108 //TODO: Should use an enum for flow_switch_state
109 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
110 }
111
112 @Override
113 public void removeSwitch(ISwitchObject sw) {
114 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
115 fg.removeVertex(sw.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800116 }
117
118 @Override
Toshio Koide12004e62013-06-12 18:17:53 -0700119 public IPortObject newPort(Short portNumber) {
Pankaj Berde15193092013-03-21 17:30:14 -0700120 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
121 IPortObject obj = fg.addVertex(null,IPortObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -0700122 if (obj != null) {
123 obj.setType("port");
124 obj.setNumber(portNumber);
125 }
Pankaj Berde15193092013-03-21 17:30:14 -0700126 return obj;
127 }
Toshio Koide53403802013-06-13 10:08:06 -0700128
129 @Override
130 public IPortObject searchPort(String dpid, short number) {
131 ISwitchObject sw = searchSwitch(dpid);
132 if (sw != null) {
133
134 IPortObject port = null;
135
136 // Requires Frames 2.3.0
137
138 try {
139 port = sw.getPort(number);
140 } catch (Exception e) {
141 // TODO Auto-generated catch block
142 e.printStackTrace();
143 }
144
145 return port;
146 }
147
148 // if (sw != null) {
149 // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
150 // pipe.start(sw.asVertex());
151 // pipe.out("on").has("number", number);
152 // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
153 // return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
154 // }
155 return null;
156 }
157
158 @Override
159 public void removePort(IPortObject port) {
160 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
161 // EventGraph<TitanGraph> eg = conn.getEventGraph();
162 if (fg != null) fg.removeVertex(port.asVertex());
163 }
164
Pankaj Berde15193092013-03-21 17:30:14 -0700165 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700166 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -0800167 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
168 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700169 if (obj != null) obj.setType("device");
Pankaj Berdeda809572013-02-22 15:31:20 -0800170 return obj;
171 }
172
173 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700174 public IDeviceObject searchDevice(String macAddr) {
175 // TODO Auto-generated method stub
Pankaj Berdeda809572013-02-22 15:31:20 -0800176 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Teru80f75132013-06-16 05:16:32 -0700177 return (fg != null && fg.getVertices("dl_addr",macAddr).iterator().hasNext()) ? fg.getVertices("dl_addr",macAddr,
Toshio Koide53403802013-06-13 10:08:06 -0700178 IDeviceObject.class).iterator().next() : null;
179
Pankaj Berdeda809572013-02-22 15:31:20 -0800180 }
181
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800182 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700183 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800184 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700185 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800186 }
187
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800188 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700189 public void removeDevice(IDeviceObject dev) {
190 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
191 if (fg != null) fg.removeVertex(dev.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800192 }
193
194 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700195 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800196 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
197 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700198 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800199 return flowPath;
200 }
201
202 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700203 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800204 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700205
206 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
207 fg.getVertices("flow_id", flowId.toString(),
208 IFlowPath.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800209 }
210
211 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700212 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800213 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
214 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
215 pipe.start(flowEntry.asVertex());
216 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700217 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800218 return r.iterator().hasNext() ? r.iterator().next() : null;
219 }
220
221 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700222 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800223 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700224 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800225
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700226 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
227
228 for (IFlowPath fp: flowPaths) {
229 if (fp.getFlowId() != null) {
230 nonNullFlows.add(fp);
231 }
232 }
233 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800234 }
235
236 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700237 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800238 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700239 fg.removeVertex(flowPath.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800240 }
241
242 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700243 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800244 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
245 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700246 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800247 return flowEntry;
248 }
249
250 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700251 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800252 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700253
254 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
255 fg.getVertices("flow_entry_id", flowEntryId.toString(),
256 IFlowEntry.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800257 }
258
259 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700260 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800261 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
262
263 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
264 }
Toshio Koide53403802013-06-13 10:08:06 -0700265
Pankaj Berded1c38592013-04-10 22:46:40 -0700266 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700267 public void removeFlowEntry(IFlowEntry flowEntry) {
Pankaj Berded1c38592013-04-10 22:46:40 -0700268 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700269 fg.removeVertex(flowEntry.asVertex());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700270 }
Toshio Koidea41950a2013-06-13 13:09:19 -0700271
272 public IDBConnection getDBConnection() {
273 return conn;
274 }
275
276 public void commit() {
277 conn.endTx(Transaction.COMMIT);
278 }
279
280 public void rollback() {
281 conn.endTx(Transaction.ROLLBACK);
282 }
Toshio Koidef20a5072013-06-13 13:18:22 -0700283
284 public void close() {
285 conn.close();
286 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800287}