blob: bcaa3afafa6cc0cf4c1163bdaf2685f15168e084 [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);
32 return obj;
33 }
Pankaj Berdeda809572013-02-22 15:31:20 -080034
35 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070036 public void removeSwitch(ISwitchObject sw) {
Pankaj Berde15193092013-03-21 17:30:14 -070037 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
38 fg.removeVertex(sw.asVertex());
39 }
40
41 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070042 public ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080043 // TODO Auto-generated method stub
44 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
45
Pankaj Berde62016142013-04-09 15:35:50 -070046 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080047 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
48
49 }
50
51 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070052 public IDeviceObject searchDevice(String macAddr) {
Pankaj Berdeda809572013-02-22 15:31:20 -080053 // TODO Auto-generated method stub
54 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -070055 return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
Pankaj Berdeda809572013-02-22 15:31:20 -080056 IDeviceObject.class).iterator().next() : null;
57
58 }
59
60 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070061 public IPortObject searchPort(String dpid, short number) {
62 ISwitchObject sw = searchSwitch(dpid);
Pankaj Berde62016142013-04-09 15:35:50 -070063 if (sw != null) {
Pankaj Berdec165a522013-06-10 21:28:11 -070064
65 IPortObject port = null;
66
67 // Requires Frames 2.3.0
68
69 try {
70 port = sw.getPort(number);
71 } catch (Exception e) {
72 // TODO Auto-generated catch block
73 e.printStackTrace();
74 }
75
76 return port;
Pankaj Berde62016142013-04-09 15:35:50 -070077 }
Pankaj Berdec165a522013-06-10 21:28:11 -070078
79// if (sw != null) {
80// GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
81// pipe.start(sw.asVertex());
82// pipe.out("on").has("number", number);
83// FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
84// return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
85// }
Pankaj Berde62016142013-04-09 15:35:50 -070086 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080087 }
88
89 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070090 public IPortObject newPort() {
Pankaj Berde15193092013-03-21 17:30:14 -070091 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
92 IPortObject obj = fg.addVertex(null,IPortObject.class);
93 return obj;
94 }
95
96 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -070097 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -080098 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
99 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
100 return obj;
101 }
Pankaj Berde15193092013-03-21 17:30:14 -0700102
103 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700104 public void removePort(IPortObject port) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700105 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
106// EventGraph<TitanGraph> eg = conn.getEventGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700107 if (fg != null) fg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -0700108 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800109
110 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700111 public void removeDevice(IDeviceObject dev) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800112 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700113 if (fg != null) fg.removeVertex(dev.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800114 }
115
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800116 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700117 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800118 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700119 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800120 }
121
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800122 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700123 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800124 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
125
126 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
127 fg.getVertices("flow_id", flowId.toString(),
128 IFlowPath.class).iterator().next() : null;
129 }
130
131 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700132 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800133 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
134 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
135 return flowPath;
136 }
137
138 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700139 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800140 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
141 fg.removeVertex(flowPath.asVertex());
142 }
143
144 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700145 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800146 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
147 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
148 pipe.start(flowEntry.asVertex());
149 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700150 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800151 return r.iterator().hasNext() ? r.iterator().next() : null;
152 }
153
154 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700155 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800156 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700157 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800158
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700159 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
160
161 for (IFlowPath fp: flowPaths) {
162 if (fp.getFlowId() != null) {
163 nonNullFlows.add(fp);
164 }
165 }
166 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800167 }
168
169 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700170 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800171 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
172
173 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
174 fg.getVertices("flow_entry_id", flowEntryId.toString(),
175 IFlowEntry.class).iterator().next() : null;
176 }
177
178 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700179 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800180 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
181 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
182 return flowEntry;
183 }
184
185 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700186 public void removeFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800187 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
188 fg.removeVertex(flowEntry.asVertex());
189 }
190
191 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700192 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800193 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
194
195 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
196 }
Pankaj Berded1c38592013-04-10 22:46:40 -0700197
198 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700199 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
Pankaj Berded1c38592013-04-10 22:46:40 -0700200 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
201 //TODO: Should use an enum for flow_switch_state
202 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
203 }
Pankaj Berde15193092013-03-21 17:30:14 -0700204
205 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700206 public Iterable<ISwitchObject> getActiveSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700207 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
208 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
209 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
210
211 for (ISwitchObject sw: switches) {
212 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
213 activeSwitches.add(sw);
214 }
215 }
216 return activeSwitches;
217 }
218
219 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700220 public Iterable<ISwitchObject> getAllSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700221 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
222 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
223 return switches;
224 }
225
226 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700227 public Iterable<ISwitchObject> getInactiveSwitches() {
Pankaj Berde15193092013-03-21 17:30:14 -0700228 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
229 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
230 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
231
232 for (ISwitchObject sw: switches) {
233 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
234 inactiveSwitches.add(sw);
235 }
236 }
237 return inactiveSwitches;
238 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700239
240 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700241 public ISwitchObject searchActiveSwitch(String dpid) {
Pankaj Berde61ce9082013-04-09 19:38:28 -0700242
Toshio Koideeb88ff62013-06-12 16:46:40 -0700243 ISwitchObject sw = searchSwitch(dpid);
Pankaj Berde61ce9082013-04-09 19:38:28 -0700244 if ((sw != null) &&
245 sw.getState().equals(SwitchState.ACTIVE.toString())) {
246 return sw;
247 }
248 return null;
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700249 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800250}