blob: 1646fc7061a56c1651672b8219881579608133f3 [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 com.thinkaurelius.titan.core.TitanGraph;
Pankaj Berdeda809572013-02-22 15:31:20 -08007import com.tinkerpop.blueprints.Vertex;
8import com.tinkerpop.frames.FramedGraph;
9import com.tinkerpop.frames.FramedVertexIterable;
10import com.tinkerpop.gremlin.java.GremlinPipeline;
11
12import net.floodlightcontroller.core.INetMapTopologyObjects.IDeviceObject;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080013import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
14import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pankaj Berdeda809572013-02-22 15:31:20 -080015import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
16import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berde15193092013-03-21 17:30:14 -070017import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080018import net.floodlightcontroller.util.FlowEntryId;
19import net.floodlightcontroller.util.FlowId;
Pankaj Berdeda809572013-02-22 15:31:20 -080020
21public 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
Pavlin Radoslavovf6fa7f02013-03-28 16:40:48 -070047 public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
48 ISwitchObject sw = searchSwitch(conn, dpid);
49 if ((sw != null) &&
50 sw.getState().equals(SwitchState.ACTIVE.toString())) {
51 return sw;
52 }
53 return null;
54 }
55
56 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080057 public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
58 // TODO Auto-generated method stub
59 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
60 return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
61 IDeviceObject.class).iterator().next() : null;
62
63 }
64
65 @Override
66 public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
67 ISwitchObject sw = searchSwitch(conn, dpid);
68 GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
69 pipe.start(sw.asVertex());
70 pipe.out("on").has("number", number);
71 FramedVertexIterable<IPortObject> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IPortObject.class);
72 return r.iterator().hasNext() ? r.iterator().next() : null;
73 }
74
75 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070076 public IPortObject newPort(GraphDBConnection conn) {
77 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
78 IPortObject obj = fg.addVertex(null,IPortObject.class);
79 return obj;
80 }
81
82 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080083 public IDeviceObject newDevice(GraphDBConnection conn) {
84 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
85 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
86 return obj;
87 }
Pankaj Berde15193092013-03-21 17:30:14 -070088
89 @Override
90 public void removePort(GraphDBConnection conn, IPortObject port) {
91 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
92 fg.removeVertex(port.asVertex());
93 }
Pankaj Berdeda809572013-02-22 15:31:20 -080094
95 @Override
96 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
97 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
98 fg.removeVertex(dev.asVertex());
99 }
100
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800101 @Override
102 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
103 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
104 return fg.getVertices("type","device",IDeviceObject.class);
105 }
106
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800107 @Override
108 public IFlowPath searchFlowPath(GraphDBConnection conn,
109 FlowId flowId) {
110 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
111
112 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
113 fg.getVertices("flow_id", flowId.toString(),
114 IFlowPath.class).iterator().next() : null;
115 }
116
117 @Override
118 public IFlowPath newFlowPath(GraphDBConnection conn) {
119 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
120 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
121 return flowPath;
122 }
123
124 @Override
125 public void removeFlowPath(GraphDBConnection conn,
126 IFlowPath flowPath) {
127 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
128 fg.removeVertex(flowPath.asVertex());
129 }
130
131 @Override
132 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
133 IFlowEntry flowEntry) {
134 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
135 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
136 pipe.start(flowEntry.asVertex());
137 pipe.out("flow");
138 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IFlowPath.class);
139 return r.iterator().hasNext() ? r.iterator().next() : null;
140 }
141
142 @Override
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700143 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800144 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700145 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800146
Jonathan Hartf5315fb2013-04-05 11:41:56 -0700147 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
148
149 for (IFlowPath fp: flowPaths) {
150 if (fp.getFlowId() != null) {
151 nonNullFlows.add(fp);
152 }
153 }
154 return nonNullFlows;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800155 }
156
157 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800158 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
159 FlowEntryId flowEntryId) {
160 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
161
162 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
163 fg.getVertices("flow_entry_id", flowEntryId.toString(),
164 IFlowEntry.class).iterator().next() : null;
165 }
166
167 @Override
168 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
169 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
170 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
171 return flowEntry;
172 }
173
174 @Override
175 public void removeFlowEntry(GraphDBConnection conn,
176 IFlowEntry flowEntry) {
177 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
178 fg.removeVertex(flowEntry.asVertex());
179 }
180
181 @Override
182 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
183 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
184
185 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
186 }
Pankaj Berde15193092013-03-21 17:30:14 -0700187
188 @Override
189 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
190 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
191 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
192 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
193
194 for (ISwitchObject sw: switches) {
195 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
196 activeSwitches.add(sw);
197 }
198 }
199 return activeSwitches;
200 }
201
202 @Override
203 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
204 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
205 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
206 return switches;
207 }
208
209 @Override
210 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
211 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
212 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
213 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
214
215 for (ISwitchObject sw: switches) {
216 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
217 inactiveSwitches.add(sw);
218 }
219 }
220 return inactiveSwitches;
221 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800222}