blob: de4b8b46889142eac3f9eb598d5c008b27335162 [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
HIGUCHI Yuta20514902013-06-12 11:24:16 -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;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -070012import net.onrc.onos.ofcontroller.util.FlowEntryId;
13import net.onrc.onos.ofcontroller.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 }
Toshio Koide53403802013-06-13 10:08:06 -070027
Pankaj Berde15193092013-03-21 17:30:14 -070028 @Override
Toshio Koide53403802013-06-13 10:08:06 -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 ISwitchObject searchSwitch(String dpid) {
Pankaj Berdeda809572013-02-22 15:31:20 -080041 // TODO Auto-generated method stub
42 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
43
Pankaj Berde62016142013-04-09 15:35:50 -070044 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080045 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
Toshio Koide53403802013-06-13 10:08:06 -070046
Pankaj Berdeda809572013-02-22 15:31:20 -080047 }
48
49 @Override
Toshio Koide53403802013-06-13 10:08:06 -070050 public ISwitchObject searchActiveSwitch(String dpid) {
51
52 ISwitchObject sw = searchSwitch(dpid);
53 if ((sw != null) &&
54 sw.getState().equals(SwitchState.ACTIVE.toString())) {
55 return sw;
56 }
57 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080058 }
59
60 @Override
Toshio Koide53403802013-06-13 10:08:06 -070061 public Iterable<ISwitchObject> getAllSwitches() {
62 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
63 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
64 return switches;
65 }
66
67 @Override
68 public Iterable<ISwitchObject> getActiveSwitches() {
69 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
70 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
71 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
72
73 for (ISwitchObject sw: switches) {
74 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
75 activeSwitches.add(sw);
Pankaj Berdec165a522013-06-10 21:28:11 -070076 }
Pankaj Berde62016142013-04-09 15:35:50 -070077 }
Toshio Koide53403802013-06-13 10:08:06 -070078 return activeSwitches;
79 }
80
81 @Override
82 public Iterable<ISwitchObject> getInactiveSwitches() {
83 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
84 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
85 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
86
87 for (ISwitchObject sw: switches) {
88 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
89 inactiveSwitches.add(sw);
90 }
91 }
92 return inactiveSwitches;
93 }
94
95 @Override
96 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
97 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
98 //TODO: Should use an enum for flow_switch_state
99 return fg.getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", IFlowEntry.class);
100 }
101
102 @Override
103 public void removeSwitch(ISwitchObject sw) {
104 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
105 fg.removeVertex(sw.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800106 }
107
108 @Override
Toshio Koide12004e62013-06-12 18:17:53 -0700109 public IPortObject newPort(Short portNumber) {
Pankaj Berde15193092013-03-21 17:30:14 -0700110 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
111 IPortObject obj = fg.addVertex(null,IPortObject.class);
Toshio Koide12004e62013-06-12 18:17:53 -0700112 if (obj != null) {
113 obj.setType("port");
114 obj.setNumber(portNumber);
115 }
Pankaj Berde15193092013-03-21 17:30:14 -0700116 return obj;
117 }
Toshio Koide53403802013-06-13 10:08:06 -0700118
119 @Override
120 public IPortObject searchPort(String dpid, short number) {
121 ISwitchObject sw = searchSwitch(dpid);
122 if (sw != null) {
123
124 IPortObject port = null;
125
126 // Requires Frames 2.3.0
127
128 try {
129 port = sw.getPort(number);
130 } catch (Exception e) {
131 // TODO Auto-generated catch block
132 e.printStackTrace();
133 }
134
135 return port;
136 }
137
138 // if (sw != null) {
139 // GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
140 // pipe.start(sw.asVertex());
141 // pipe.out("on").has("number", number);
142 // FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
143 // return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
144 // }
145 return null;
146 }
147
148 @Override
149 public void removePort(IPortObject port) {
150 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
151 // EventGraph<TitanGraph> eg = conn.getEventGraph();
152 if (fg != null) fg.removeVertex(port.asVertex());
153 }
154
Pankaj Berde15193092013-03-21 17:30:14 -0700155 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700156 public IDeviceObject newDevice() {
Pankaj Berdeda809572013-02-22 15:31:20 -0800157 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
158 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700159 if (obj != null) obj.setType("device");
Pankaj Berdeda809572013-02-22 15:31:20 -0800160 return obj;
161 }
162
163 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700164 public IDeviceObject searchDevice(String macAddr) {
165 // TODO Auto-generated method stub
Pankaj Berdeda809572013-02-22 15:31:20 -0800166 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700167 return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
168 IDeviceObject.class).iterator().next() : null;
169
Pankaj Berdeda809572013-02-22 15:31:20 -0800170 }
171
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800172 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700173 public Iterable<IDeviceObject> getDevices() {
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800174 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700175 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800176 }
177
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800178 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700179 public void removeDevice(IDeviceObject dev) {
180 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
181 if (fg != null) fg.removeVertex(dev.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800182 }
183
184 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700185 public IFlowPath newFlowPath() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800186 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
187 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700188 if (flowPath != null) flowPath.setType("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800189 return flowPath;
190 }
191
192 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700193 public IFlowPath searchFlowPath(FlowId flowId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800194 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700195
196 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
197 fg.getVertices("flow_id", flowId.toString(),
198 IFlowPath.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800199 }
200
201 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700202 public IFlowPath getFlowPathByFlowEntry(IFlowEntry flowEntry) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800203 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
204 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
205 pipe.start(flowEntry.asVertex());
206 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700207 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800208 return r.iterator().hasNext() ? r.iterator().next() : null;
209 }
210
211 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700212 public Iterable<IFlowPath> getAllFlowPaths() {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800213 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700214 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800215
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700216 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
217
218 for (IFlowPath fp: flowPaths) {
219 if (fp.getFlowId() != null) {
220 nonNullFlows.add(fp);
221 }
222 }
223 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800224 }
225
226 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700227 public void removeFlowPath(IFlowPath flowPath) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800228 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700229 fg.removeVertex(flowPath.asVertex());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800230 }
231
232 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700233 public IFlowEntry newFlowEntry() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800234 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
235 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
Toshio Koided7b7e102013-06-12 17:13:25 -0700236 if (flowEntry != null) flowEntry.setType("flow_entry");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800237 return flowEntry;
238 }
239
240 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700241 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800242 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700243
244 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
245 fg.getVertices("flow_entry_id", flowEntryId.toString(),
246 IFlowEntry.class).iterator().next() : null;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800247 }
248
249 @Override
Toshio Koideeb88ff62013-06-12 16:46:40 -0700250 public Iterable<IFlowEntry> getAllFlowEntries() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800251 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
252
253 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
254 }
Toshio Koide53403802013-06-13 10:08:06 -0700255
Pankaj Berded1c38592013-04-10 22:46:40 -0700256 @Override
Toshio Koide53403802013-06-13 10:08:06 -0700257 public void removeFlowEntry(IFlowEntry flowEntry) {
Pankaj Berded1c38592013-04-10 22:46:40 -0700258 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Toshio Koide53403802013-06-13 10:08:06 -0700259 fg.removeVertex(flowEntry.asVertex());
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700260 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800261}