blob: 1d5ab20e42ef8c8d4401f5d2a618eb4bc57f6fc0 [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
Pankaj Berdeda809572013-02-22 15:31:20 -08006import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08007import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
8import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pankaj Berdeda809572013-02-22 15:31:20 -08009import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
10import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berde15193092013-03-21 17:30:14 -070011import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080012import net.floodlightcontroller.util.FlowEntryId;
13import net.floodlightcontroller.util.FlowId;
Toshio Koidea41950a2013-06-13 13:09:19 -070014import net.onrc.onos.util.GraphDBConnection.Transaction;
Pankaj Berdeda809572013-02-22 15:31:20 -080015
Pankaj Berde8f036112013-03-28 22:58:47 -070016import com.thinkaurelius.titan.core.TitanGraph;
17import com.tinkerpop.blueprints.Vertex;
Pankaj Berde8f036112013-03-28 22:58:47 -070018import com.tinkerpop.frames.FramedGraph;
Pankaj Berde5d506412013-04-23 15:03:02 -070019import com.tinkerpop.frames.structures.FramedVertexIterable;
Pankaj Berde8f036112013-03-28 22:58:47 -070020import com.tinkerpop.gremlin.java.GremlinPipeline;
21
Toshio Koideeb88ff62013-06-12 16:46:40 -070022public class GraphDBOperation implements IDBOperation {
23 private GraphDBConnection conn;
24
25 public GraphDBOperation(GraphDBConnection dbConnection) {
26 this.conn = dbConnection;
27 }
Toshio Koide53403802013-06-13 10:08:06 -070028
Pankaj Berde15193092013-03-21 17:30:14 -070029 @Override
Toshio Koide53403802013-06-13 10:08:06 -070030 public ISwitchObject newSwitch(String dpid) {
Pankaj Berde15193092013-03-21 17:30:14 -070031 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
32 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -070033 if (obj != null) {
34 obj.setType("switch");
35 obj.setDPID(dpid);
36 }
Pankaj Berde15193092013-03-21 17:30:14 -070037 return obj;
38 }
Pankaj Berdeda809572013-02-22 15:31:20 -080039
40 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070041 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080042 // TODO Auto-generated method stub
43 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
44
Pankaj Berde62016142013-04-09 15:35:50 -070045 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080046 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
Toshio Koide53403802013-06-13 10:08:06 -070047
Pankaj Berdeda809572013-02-22 15:31:20 -080048 }
49
50 @Override
Toshio Koide53403802013-06-13 10:08:06 -070051 public ISwitchObject searchActiveSwitch(String dpid) {
52
53 ISwitchObject sw = searchSwitch(dpid);
54 if ((sw != null) &&
55 sw.getState().equals(SwitchState.ACTIVE.toString())) {
56 return sw;
57 }
58 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080059 }
60
61 @Override
Toshio Koide53403802013-06-13 10:08:06 -070062 public Iterable<ISwitchObject> getAllSwitches() {
63 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
64 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
65 return switches;
66 }
67
68 @Override
69 public Iterable<ISwitchObject> getActiveSwitches() {
70 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
71 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
72 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
73
74 for (ISwitchObject sw: switches) {
75 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
76 activeSwitches.add(sw);
Pankaj Berdec165a522013-06-10 21:28:11 -070077 }
Pankaj Berde62016142013-04-09 15:35:50 -070078 }
Toshio Koide53403802013-06-13 10:08:06 -070079 return activeSwitches;
80 }
81
82 @Override
83 public Iterable<ISwitchObject> getInactiveSwitches() {
84 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
85 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
86 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
87
88 for (ISwitchObject sw: switches) {
89 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
90 inactiveSwitches.add(sw);
91 }
92 }
93 return inactiveSwitches;
94 }
95
96 @Override
97 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
98 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
99 //TODO: Should use an enum for flow_switch_state
100 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
101 }
102
103 @Override
104 public void removeSwitch(ISwitchObject sw) {
105 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
106 fg.removeVertex(sw.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800107 }
108
109 @Override
Toshio Koide12004e62013-06-12 18:17:53 -0700110 public IPortObject newPort(Short portNumber) {
Pankaj Berde15193092013-03-21 17:30:14 -0700111 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
112 IPortObject obj = fg.addVertex(null,IPortObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -0700113 if (obj != null) {
114 obj.setType("port");
115 obj.setNumber(portNumber);
116 }
Pankaj Berde15193092013-03-21 17:30:14 -0700117 return obj;
118 }
Toshio Koide53403802013-06-13 10:08:06 -0700119
120 @Override
121 public IPortObject searchPort(String dpid, short number) {
122 ISwitchObject sw = searchSwitch(dpid);
123 if (sw != null) {
124
125 IPortObject port = null;
126
127 // Requires Frames 2.3.0
128
129 try {
130 port = sw.getPort(number);
131 } catch (Exception e) {
132 // TODO Auto-generated catch block
133 e.printStackTrace();
134 }
135
136 return port;
137 }
138
139 // if (sw != null) {
140 // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
141 // pipe.start(sw.asVertex());
142 // pipe.out("on").has("number", number);
143 // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
144 // return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
145 // }
146 return null;
147 }
148
149 @Override
150 public void removePort(IPortObject port) {
151 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
152 // EventGraph<TitanGraph> eg = conn.getEventGraph();
153 if (fg != null) fg.removeVertex(port.asVertex());
154 }
155
Pankaj Berde15193092013-03-21 17:30:14 -0700156 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700157 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -0800158 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
159 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700160 if (obj != null) obj.setType("device");
Pankaj Berdeda809572013-02-22 15:31:20 -0800161 return obj;
162 }
163
164 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700165 public IDeviceObject searchDevice(String macAddr) {
166 // TODO Auto-generated method stub
Pankaj Berdeda809572013-02-22 15:31:20 -0800167 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700168 return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
169 IDeviceObject.class).iterator().next() : null;
170
Pankaj Berdeda809572013-02-22 15:31:20 -0800171 }
172
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800173 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700174 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800175 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700176 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800177 }
178
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800179 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700180 public void removeDevice(IDeviceObject dev) {
181 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
182 if (fg != null) fg.removeVertex(dev.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800183 }
184
185 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700186 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800187 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
188 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700189 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800190 return flowPath;
191 }
192
193 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700194 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800195 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700196
197 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
198 fg.getVertices("flow_id", flowId.toString(),
199 IFlowPath.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800200 }
201
202 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700203 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800204 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
205 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
206 pipe.start(flowEntry.asVertex());
207 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700208 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800209 return r.iterator().hasNext() ? r.iterator().next() : null;
210 }
211
212 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700213 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800214 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700215 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800216
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700217 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
218
219 for (IFlowPath fp: flowPaths) {
220 if (fp.getFlowId() != null) {
221 nonNullFlows.add(fp);
222 }
223 }
224 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800225 }
226
227 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700228 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800229 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700230 fg.removeVertex(flowPath.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800231 }
232
233 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700234 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800235 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
236 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700237 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800238 return flowEntry;
239 }
240
241 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700242 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800243 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700244
245 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
246 fg.getVertices("flow_entry_id", flowEntryId.toString(),
247 IFlowEntry.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800248 }
249
250 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700251 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800252 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
253
254 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
255 }
Toshio Koide53403802013-06-13 10:08:06 -0700256
Pankaj Berded1c38592013-04-10 22:46:40 -0700257 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700258 public void removeFlowEntry(IFlowEntry flowEntry) {
Pankaj Berded1c38592013-04-10 22:46:40 -0700259 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700260 fg.removeVertex(flowEntry.asVertex());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700261 }
Toshio Koidea41950a2013-06-13 13:09:19 -0700262
263 public IDBConnection getDBConnection() {
264 return conn;
265 }
266
267 public void commit() {
268 conn.endTx(Transaction.COMMIT);
269 }
270
271 public void rollback() {
272 conn.endTx(Transaction.ROLLBACK);
273 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800274}