blob: 1699bde9e661a00407a5bac0df0fc51c1f448e91 [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;
Pankaj Berdeda809572013-02-22 15:31:20 -080014
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;
23
24 public GraphDBOperation(GraphDBConnection dbConnection) {
25 this.conn = dbConnection;
26 }
Pankaj Berde15193092013-03-21 17:30:14 -070027
28 @Override
Toshio Koide12004e62013-06-12 18:17:53 -070029 public ISwitchObject newSwitch(String dpid) {
Pankaj Berde15193092013-03-21 17:30:14 -070030 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
31 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -070032 if (obj != null) {
33 obj.setType("switch");
34 obj.setDPID(dpid);
35 }
Pankaj Berde15193092013-03-21 17:30:14 -070036 return obj;
37 }
Pankaj Berdeda809572013-02-22 15:31:20 -080038
39 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070040 public void removeSwitch(ISwitchObject sw) {
Pankaj Berde15193092013-03-21 17:30:14 -070041 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
42 fg.removeVertex(sw.asVertex());
43 }
44
45 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070046 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080047 // TODO Auto-generated method stub
48 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
49
Pankaj Berde62016142013-04-09 15:35:50 -070050 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080051 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
52
53 }
54
55 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070056 public IDeviceObject searchDevice(String macAddr) {
Pankaj Berdeda809572013-02-22 15:31:20 -080057 // TODO Auto-generated method stub
58 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -070059 return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
Pankaj Berdeda809572013-02-22 15:31:20 -080060 IDeviceObject.class).iterator().next() : null;
61
62 }
63
64 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070065 public IPortObject searchPort(String dpid, short number) {
66 ISwitchObject sw = searchSwitch(dpid);
Pankaj Berde62016142013-04-09 15:35:50 -070067 if (sw != null) {
Pankaj Berdec165a522013-06-10 21:28:11 -070068
69 IPortObject port = null;
70
71 // Requires Frames 2.3.0
72
73 try {
74 port = sw.getPort(number);
75 } catch (Exception e) {
76 // TODO Auto-generated catch block
77 e.printStackTrace();
78 }
79
80 return port;
Pankaj Berde62016142013-04-09 15:35:50 -070081 }
Pankaj Berdec165a522013-06-10 21:28:11 -070082
83// if (sw != null) {
84// GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
85// pipe.start(sw.asVertex());
86// pipe.out("on").has("number", number);
87// FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
88// return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
89// }
Pankaj Berde62016142013-04-09 15:35:50 -070090 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080091 }
92
93 @Override
Toshio Koide12004e62013-06-12 18:17:53 -070094 public IPortObject newPort(Short portNumber) {
Pankaj Berde15193092013-03-21 17:30:14 -070095 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
96 IPortObject obj = fg.addVertex(null,IPortObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -070097 if (obj != null) {
98 obj.setType("port");
99 obj.setNumber(portNumber);
100 }
Pankaj Berde15193092013-03-21 17:30:14 -0700101 return obj;
102 }
103
104 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700105 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -0800106 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
107 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700108 if (obj != null) obj.setType("device");
Pankaj Berdeda809572013-02-22 15:31:20 -0800109 return obj;
110 }
Pankaj Berde15193092013-03-21 17:30:14 -0700111
112 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700113 public void removePort(IPortObject port) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700114 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
115// EventGraph<TitanGraph> eg = conn.getEventGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700116 if (fg != null) fg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -0700117 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800118
119 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700120 public void removeDevice(IDeviceObject dev) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800121 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700122 if (fg != null) fg.removeVertex(dev.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800123 }
124
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800125 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700126 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800127 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700128 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800129 }
130
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800131 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700132 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800133 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
134
135 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
136 fg.getVertices("flow_id", flowId.toString(),
137 IFlowPath.class).iterator().next() : null;
138 }
139
140 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700141 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800142 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
143 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700144 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800145 return flowPath;
146 }
147
148 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700149 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800150 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
151 fg.removeVertex(flowPath.asVertex());
152 }
153
154 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700155 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800156 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
157 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
158 pipe.start(flowEntry.asVertex());
159 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700160 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800161 return r.iterator().hasNext() ? r.iterator().next() : null;
162 }
163
164 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700165 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800166 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700167 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800168
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700169 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
170
171 for (IFlowPath fp: flowPaths) {
172 if (fp.getFlowId() != null) {
173 nonNullFlows.add(fp);
174 }
175 }
176 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800177 }
178
179 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700180 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800181 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
182
183 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
184 fg.getVertices("flow_entry_id", flowEntryId.toString(),
185 IFlowEntry.class).iterator().next() : null;
186 }
187
188 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700189 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800190 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
191 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700192 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800193 return flowEntry;
194 }
195
196 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700197 public void removeFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800198 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
199 fg.removeVertex(flowEntry.asVertex());
200 }
201
202 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700203 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800204 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
205
206 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
207 }
Pankaj Berded1c38592013-04-10 22:46:40 -0700208
209 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700210 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
Pankaj Berded1c38592013-04-10 22:46:40 -0700211 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
212 //TODO: Should use an enum for flow_switch_state
213 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
214 }
Pankaj Berde15193092013-03-21 17:30:14 -0700215
216 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700217 public Iterable<ISwitchObject> getActiveSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700218 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
219 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
220 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
221
222 for (ISwitchObject sw: switches) {
223 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
224 activeSwitches.add(sw);
225 }
226 }
227 return activeSwitches;
228 }
229
230 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700231 public Iterable<ISwitchObject> getAllSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700232 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
233 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
234 return switches;
235 }
236
237 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700238 public Iterable<ISwitchObject> getInactiveSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700239 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
240 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
241 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
242
243 for (ISwitchObject sw: switches) {
244 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
245 inactiveSwitches.add(sw);
246 }
247 }
248 return inactiveSwitches;
249 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700250
251 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700252 public ISwitchObject searchActiveSwitch(String dpid) {
Pankaj Berde61ce9082013-04-09 19:38:28 -0700253
Toshio Koideeb88ff62013-06-12 16:46:40 -0700254 ISwitchObject sw = searchSwitch(dpid);
Pankaj Berde61ce9082013-04-09 19:38:28 -0700255 if ((sw != null) &&
256 sw.getState().equals(SwitchState.ACTIVE.toString())) {
257 return sw;
258 }
259 return null;
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700260 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800261}