blob: 5a70dbc379647f823947bb39b8c4edcfa5bf3787 [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();
Toshio Koide13260102013-06-17 13:44:27 -0700177 return (fg != null && fg.getVertices("dl_addr",macAddr).iterator().hasNext()) ?
178 fg.getVertices("dl_addr",macAddr, IDeviceObject.class).iterator().next() : null;
Pankaj Berdeda809572013-02-22 15:31:20 -0800179 }
180
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800181 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700182 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800183 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700184 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800185 }
186
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800187 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700188 public void removeDevice(IDeviceObject dev) {
189 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
190 if (fg != null) fg.removeVertex(dev.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800191 }
192
193 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700194 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800195 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
196 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700197 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800198 return flowPath;
199 }
200
201 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700202 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800203 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700204
205 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
206 fg.getVertices("flow_id", flowId.toString(),
207 IFlowPath.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800208 }
209
210 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700211 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800212 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
213 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
214 pipe.start(flowEntry.asVertex());
215 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700216 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800217 return r.iterator().hasNext() ? r.iterator().next() : null;
218 }
219
220 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700221 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800222 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700223 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800224
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700225 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
226
227 for (IFlowPath fp: flowPaths) {
228 if (fp.getFlowId() != null) {
229 nonNullFlows.add(fp);
230 }
231 }
232 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800233 }
234
235 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700236 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800237 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700238 fg.removeVertex(flowPath.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800239 }
240
241 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700242 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800243 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
244 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700245 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800246 return flowEntry;
247 }
248
249 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700250 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800251 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700252
253 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
254 fg.getVertices("flow_entry_id", flowEntryId.toString(),
255 IFlowEntry.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800256 }
257
258 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700259 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800260 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
261
262 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
263 }
Toshio Koide53403802013-06-13 10:08:06 -0700264
Pankaj Berded1c38592013-04-10 22:46:40 -0700265 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700266 public void removeFlowEntry(IFlowEntry flowEntry) {
Pankaj Berded1c38592013-04-10 22:46:40 -0700267 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700268 fg.removeVertex(flowEntry.asVertex());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700269 }
Toshio Koidea41950a2013-06-13 13:09:19 -0700270
271 public IDBConnection getDBConnection() {
272 return conn;
273 }
274
275 public void commit() {
276 conn.endTx(Transaction.COMMIT);
277 }
278
279 public void rollback() {
280 conn.endTx(Transaction.ROLLBACK);
281 }
Toshio Koidef20a5072013-06-13 13:18:22 -0700282
283 public void close() {
284 conn.close();
285 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800286}