blob: 8e6a74d6b5d2e8dd5ebf128442ba316a3d61e95a [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 Koideeb88ff62013-06-12 16:46:40 -070029 public ISwitchObject newSwitch() {
Pankaj Berde15193092013-03-21 17:30:14 -070030 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
31 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -070032 if (obj != null) obj.setType("switch");
Pankaj Berde15193092013-03-21 17:30:14 -070033 return obj;
34 }
Pankaj Berdeda809572013-02-22 15:31:20 -080035
36 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070037 public void removeSwitch(ISwitchObject sw) {
Pankaj Berde15193092013-03-21 17:30:14 -070038 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
39 fg.removeVertex(sw.asVertex());
40 }
41
42 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070043 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080044 // TODO Auto-generated method stub
45 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
46
Pankaj Berde62016142013-04-09 15:35:50 -070047 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080048 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
49
50 }
51
52 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070053 public IDeviceObject searchDevice(String macAddr) {
Pankaj Berdeda809572013-02-22 15:31:20 -080054 // TODO Auto-generated method stub
55 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -070056 return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
Pankaj Berdeda809572013-02-22 15:31:20 -080057 IDeviceObject.class).iterator().next() : null;
58
59 }
60
61 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070062 public IPortObject searchPort(String dpid, short number) {
63 ISwitchObject sw = searchSwitch(dpid);
Pankaj Berde62016142013-04-09 15:35:50 -070064 if (sw != null) {
Pankaj Berdec165a522013-06-10 21:28:11 -070065
66 IPortObject port = null;
67
68 // Requires Frames 2.3.0
69
70 try {
71 port = sw.getPort(number);
72 } catch (Exception e) {
73 // TODO Auto-generated catch block
74 e.printStackTrace();
75 }
76
77 return port;
Pankaj Berde62016142013-04-09 15:35:50 -070078 }
Pankaj Berdec165a522013-06-10 21:28:11 -070079
80// if (sw != null) {
81// GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
82// pipe.start(sw.asVertex());
83// pipe.out("on").has("number", number);
84// FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
85// return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
86// }
Pankaj Berde62016142013-04-09 15:35:50 -070087 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080088 }
89
90 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070091 public IPortObject newPort() {
Pankaj Berde15193092013-03-21 17:30:14 -070092 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
93 IPortObject obj = fg.addVertex(null,IPortObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -070094 if (obj != null) obj.setType("port");
Pankaj Berde15193092013-03-21 17:30:14 -070095 return obj;
96 }
97
98 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070099 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -0800100 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
101 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700102 if (obj != null) obj.setType("device");
Pankaj Berdeda809572013-02-22 15:31:20 -0800103 return obj;
104 }
Pankaj Berde15193092013-03-21 17:30:14 -0700105
106 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700107 public void removePort(IPortObject port) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700108 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
109// EventGraph<TitanGraph> eg = conn.getEventGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700110 if (fg != null) fg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -0700111 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800112
113 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700114 public void removeDevice(IDeviceObject dev) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800115 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700116 if (fg != null) fg.removeVertex(dev.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800117 }
118
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800119 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700120 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800121 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700122 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800123 }
124
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800125 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700126 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800127 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
128
129 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
130 fg.getVertices("flow_id", flowId.toString(),
131 IFlowPath.class).iterator().next() : null;
132 }
133
134 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700135 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800136 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
137 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700138 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800139 return flowPath;
140 }
141
142 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700143 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800144 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
145 fg.removeVertex(flowPath.asVertex());
146 }
147
148 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700149 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800150 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
151 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
152 pipe.start(flowEntry.asVertex());
153 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700154 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800155 return r.iterator().hasNext() ? r.iterator().next() : null;
156 }
157
158 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700159 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800160 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700161 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800162
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700163 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
164
165 for (IFlowPath fp: flowPaths) {
166 if (fp.getFlowId() != null) {
167 nonNullFlows.add(fp);
168 }
169 }
170 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800171 }
172
173 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700174 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800175 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
176
177 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
178 fg.getVertices("flow_entry_id", flowEntryId.toString(),
179 IFlowEntry.class).iterator().next() : null;
180 }
181
182 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700183 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800184 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
185 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700186 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800187 return flowEntry;
188 }
189
190 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700191 public void removeFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800192 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
193 fg.removeVertex(flowEntry.asVertex());
194 }
195
196 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700197 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800198 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
199
200 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
201 }
Pankaj Berded1c38592013-04-10 22:46:40 -0700202
203 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700204 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
Pankaj Berded1c38592013-04-10 22:46:40 -0700205 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
206 //TODO: Should use an enum for flow_switch_state
207 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
208 }
Pankaj Berde15193092013-03-21 17:30:14 -0700209
210 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700211 public Iterable<ISwitchObject> getActiveSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700212 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
213 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
214 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
215
216 for (ISwitchObject sw: switches) {
217 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
218 activeSwitches.add(sw);
219 }
220 }
221 return activeSwitches;
222 }
223
224 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700225 public Iterable<ISwitchObject> getAllSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700226 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
227 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
228 return switches;
229 }
230
231 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700232 public Iterable<ISwitchObject> getInactiveSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700233 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
234 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
235 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
236
237 for (ISwitchObject sw: switches) {
238 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
239 inactiveSwitches.add(sw);
240 }
241 }
242 return inactiveSwitches;
243 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700244
245 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700246 public ISwitchObject searchActiveSwitch(String dpid) {
Pankaj Berde61ce9082013-04-09 19:38:28 -0700247
Toshio Koideeb88ff62013-06-12 16:46:40 -0700248 ISwitchObject sw = searchSwitch(dpid);
Pankaj Berde61ce9082013-04-09 19:38:28 -0700249 if ((sw != null) &&
250 sw.getState().equals(SwitchState.ACTIVE.toString())) {
251 return sw;
252 }
253 return null;
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700254 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800255}