blob: 92befe6b651333a6cef091936ae16148a5489ca7 [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
Pankaj Berdeda809572013-02-22 15:31:20 -080021public class GraphDBUtils implements IDBUtils {
Pankaj Berde15193092013-03-21 17:30:14 -070022
23 @Override
24 public ISwitchObject newSwitch(GraphDBConnection conn) {
25 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
26 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
27 return obj;
28 }
Pankaj Berdeda809572013-02-22 15:31:20 -080029
30 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070031 public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) {
32 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
33 fg.removeVertex(sw.asVertex());
34 }
35
36 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080037 public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
38 // TODO Auto-generated method stub
39 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
40
Pankaj Berde62016142013-04-09 15:35:50 -070041 return (fg != null && fg.getVertices("dpid",dpid).iterator().hasNext()) ?
Pankaj Berdeda809572013-02-22 15:31:20 -080042 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
43
44 }
45
46 @Override
47 public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
48 // TODO Auto-generated method stub
49 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -070050 return (fg != null && fg.getVertices("dl_address",macAddr).iterator().hasNext()) ? fg.getVertices("dl_address",macAddr,
Pankaj Berdeda809572013-02-22 15:31:20 -080051 IDeviceObject.class).iterator().next() : null;
52
53 }
54
55 @Override
56 public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
57 ISwitchObject sw = searchSwitch(conn, dpid);
Pankaj Berde62016142013-04-09 15:35:50 -070058 if (sw != null) {
Pankaj Berdec165a522013-06-10 21:28:11 -070059
60 IPortObject port = null;
61
62 // Requires Frames 2.3.0
63
64 try {
65 port = sw.getPort(number);
66 } catch (Exception e) {
67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 }
70
71 return port;
Pankaj Berde62016142013-04-09 15:35:50 -070072 }
Pankaj Berdec165a522013-06-10 21:28:11 -070073
74// if (sw != null) {
75// GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
76// pipe.start(sw.asVertex());
77// pipe.out("on").has("number", number);
78// FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
79// return r != null && r.iterator().hasNext() ? r.iterator().next() : null;
80// }
Pankaj Berde62016142013-04-09 15:35:50 -070081 return null;
Pankaj Berdeda809572013-02-22 15:31:20 -080082 }
83
84 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070085 public IPortObject newPort(GraphDBConnection conn) {
86 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
87 IPortObject obj = fg.addVertex(null,IPortObject.class);
88 return obj;
89 }
90
91 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080092 public IDeviceObject newDevice(GraphDBConnection conn) {
93 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
94 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
95 return obj;
96 }
Pankaj Berde15193092013-03-21 17:30:14 -070097
98 @Override
99 public void removePort(GraphDBConnection conn, IPortObject port) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700100 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
101// EventGraph<TitanGraph> eg = conn.getEventGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700102 if (fg != null) fg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -0700103 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800104
105 @Override
106 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
107 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700108 if (fg != null) fg.removeVertex(dev.asVertex());
Pankaj Berdeda809572013-02-22 15:31:20 -0800109 }
110
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800111 @Override
112 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
113 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Pankaj Berde62016142013-04-09 15:35:50 -0700114 return fg != null ? fg.getVertices("type","device",IDeviceObject.class) : null;
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800115 }
116
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800117 @Override
118 public IFlowPath searchFlowPath(GraphDBConnection conn,
119 FlowId flowId) {
120 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
121
122 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
123 fg.getVertices("flow_id", flowId.toString(),
124 IFlowPath.class).iterator().next() : null;
125 }
126
127 @Override
128 public IFlowPath newFlowPath(GraphDBConnection conn) {
129 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
130 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
131 return flowPath;
132 }
133
134 @Override
135 public void removeFlowPath(GraphDBConnection conn,
136 IFlowPath flowPath) {
137 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
138 fg.removeVertex(flowPath.asVertex());
139 }
140
141 @Override
142 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
143 IFlowEntry flowEntry) {
144 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
145 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
146 pipe.start(flowEntry.asVertex());
147 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700148 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800149 return r.iterator().hasNext() ? r.iterator().next() : null;
150 }
151
152 @Override
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700153 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800154 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700155 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800156
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700157 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
158
159 for (IFlowPath fp: flowPaths) {
160 if (fp.getFlowId() != null) {
161 nonNullFlows.add(fp);
162 }
163 }
164 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800165 }
166
167 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800168 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
169 FlowEntryId flowEntryId) {
170 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
171
172 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
173 fg.getVertices("flow_entry_id", flowEntryId.toString(),
174 IFlowEntry.class).iterator().next() : null;
175 }
176
177 @Override
178 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
179 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
180 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
181 return flowEntry;
182 }
183
184 @Override
185 public void removeFlowEntry(GraphDBConnection conn,
186 IFlowEntry flowEntry) {
187 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
188 fg.removeVertex(flowEntry.asVertex());
189 }
190
191 @Override
192 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
193 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
199 public Iterable<IFlowEntry> getAllSwitchNotUpdatedFlowEntries(GraphDBConnection conn) {
200 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
206 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
207 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
220 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
221 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
222 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
223 return switches;
224 }
225
226 @Override
227 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
228 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
241 public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
Pankaj Berde61ce9082013-04-09 19:38:28 -0700242
243 ISwitchObject sw = searchSwitch(conn, dpid);
244 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}