blob: dd52874ee031b80d51c6d0c7b282a0e68f9dc2ea [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 Berde5fb27632013-04-05 08:56:12 -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;
Pankaj Berde73ea7e52013-04-04 14:52:50 -070071 /* 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 Berde73ea7e52013-04-04 14:52:50 -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
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800147 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
148 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
149
150 return fg.getVertices("type", "flow", IFlowPath.class);
151 }
152
153 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800154 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
155 FlowEntryId flowEntryId) {
156 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
157
158 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
159 fg.getVertices("flow_entry_id", flowEntryId.toString(),
160 IFlowEntry.class).iterator().next() : null;
161 }
162
163 @Override
164 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
165 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
166 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
167 return flowEntry;
168 }
169
170 @Override
171 public void removeFlowEntry(GraphDBConnection conn,
172 IFlowEntry flowEntry) {
173 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
174 fg.removeVertex(flowEntry.asVertex());
175 }
176
177 @Override
178 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
179 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
180
181 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
182 }
Pankaj Berde15193092013-03-21 17:30:14 -0700183
184 @Override
185 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
186 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
187 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
188 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
189
190 for (ISwitchObject sw: switches) {
191 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
192 activeSwitches.add(sw);
193 }
194 }
195 return activeSwitches;
196 }
197
198 @Override
199 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
200 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
201 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
202 return switches;
203 }
204
205 @Override
206 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
207 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
208 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
209 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
210
211 for (ISwitchObject sw: switches) {
212 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
213 inactiveSwitches.add(sw);
214 }
215 }
216 return inactiveSwitches;
217 }
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700218
219 @Override
220 public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
221 // TODO Auto-generated method stub
222 return null;
223 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800224}