blob: eea57fd0f1985982559a50e07058fedb0bc83e75 [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 Berde5fb27632013-04-05 08:56:12 -070018import com.tinkerpop.frames.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
41 return fg.getVertices("dpid",dpid).iterator().hasNext() ?
42 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();
50 return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
51 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 Berde86a0d412013-04-05 15:06:26 -070058// if (sw != null) {
59//
60// IPortObject port = null;
61// try {
62// port = sw.getPort(number);
63// } catch (Exception e) {
64// // TODO Auto-generated catch block
65// e.printStackTrace();
66// }
67//
68// return port;
69// }
70// return null;
71 GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
Pankaj Berdeda809572013-02-22 15:31:20 -080072 pipe.start(sw.asVertex());
73 pipe.out("on").has("number", number);
Pankaj Berde8f036112013-03-28 22:58:47 -070074 FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
Pankaj Berde86a0d412013-04-05 15:06:26 -070075 return r.iterator().hasNext() ? r.iterator().next() : null;
Pankaj Berdeda809572013-02-22 15:31:20 -080076 }
77
78 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070079 public IPortObject newPort(GraphDBConnection conn) {
80 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
81 IPortObject obj = fg.addVertex(null,IPortObject.class);
82 return obj;
83 }
84
85 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080086 public IDeviceObject newDevice(GraphDBConnection conn) {
87 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
88 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
89 return obj;
90 }
Pankaj Berde15193092013-03-21 17:30:14 -070091
92 @Override
93 public void removePort(GraphDBConnection conn, IPortObject port) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -070094 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
95// EventGraph<TitanGraph> eg = conn.getEventGraph();
96 fg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -070097 }
Pankaj Berdeda809572013-02-22 15:31:20 -080098
99 @Override
100 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
101 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
102 fg.removeVertex(dev.asVertex());
103 }
104
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800105 @Override
106 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
107 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
108 return fg.getVertices("type","device",IDeviceObject.class);
109 }
110
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800111 @Override
112 public IFlowPath searchFlowPath(GraphDBConnection conn,
113 FlowId flowId) {
114 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
115
116 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
117 fg.getVertices("flow_id", flowId.toString(),
118 IFlowPath.class).iterator().next() : null;
119 }
120
121 @Override
122 public IFlowPath newFlowPath(GraphDBConnection conn) {
123 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
124 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
125 return flowPath;
126 }
127
128 @Override
129 public void removeFlowPath(GraphDBConnection conn,
130 IFlowPath flowPath) {
131 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
132 fg.removeVertex(flowPath.asVertex());
133 }
134
135 @Override
136 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
137 IFlowEntry flowEntry) {
138 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
139 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
140 pipe.start(flowEntry.asVertex());
141 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700142 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800143 return r.iterator().hasNext() ? r.iterator().next() : null;
144 }
145
146 @Override
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700147 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800148 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700149 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800150
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700151 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
152
153 for (IFlowPath fp: flowPaths) {
154 if (fp.getFlowId() != null) {
155 nonNullFlows.add(fp);
156 }
157 }
158 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800159 }
160
161 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800162 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
163 FlowEntryId flowEntryId) {
164 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
165
166 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
167 fg.getVertices("flow_entry_id", flowEntryId.toString(),
168 IFlowEntry.class).iterator().next() : null;
169 }
170
171 @Override
172 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
173 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
174 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
175 return flowEntry;
176 }
177
178 @Override
179 public void removeFlowEntry(GraphDBConnection conn,
180 IFlowEntry flowEntry) {
181 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
182 fg.removeVertex(flowEntry.asVertex());
183 }
184
185 @Override
186 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
187 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
188
189 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
190 }
Pankaj Berde15193092013-03-21 17:30:14 -0700191
192 @Override
193 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
194 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
195 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
196 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
197
198 for (ISwitchObject sw: switches) {
199 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
200 activeSwitches.add(sw);
201 }
202 }
203 return activeSwitches;
204 }
205
206 @Override
207 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
208 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
209 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
210 return switches;
211 }
212
213 @Override
214 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
215 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
216 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
217 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
218
219 for (ISwitchObject sw: switches) {
220 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
221 inactiveSwitches.add(sw);
222 }
223 }
224 return inactiveSwitches;
225 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700226
227 @Override
228 public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
229 // TODO Auto-generated method stub
230 return null;
231 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800232}