blob: 7283d09420a828b02caf7ca699d76a6dfa2a245d [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
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800143 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
144 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
145
146 return fg.getVertices("type", "flow", IFlowPath.class);
147 }
148
149 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800150 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
151 FlowEntryId flowEntryId) {
152 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
153
154 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
155 fg.getVertices("flow_entry_id", flowEntryId.toString(),
156 IFlowEntry.class).iterator().next() : null;
157 }
158
159 @Override
160 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
161 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
162 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
163 return flowEntry;
164 }
165
166 @Override
167 public void removeFlowEntry(GraphDBConnection conn,
168 IFlowEntry flowEntry) {
169 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
170 fg.removeVertex(flowEntry.asVertex());
171 }
172
173 @Override
174 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
175 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
176
177 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
178 }
Pankaj Berde15193092013-03-21 17:30:14 -0700179
180 @Override
181 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
182 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
183 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
184 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
185
186 for (ISwitchObject sw: switches) {
187 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
188 activeSwitches.add(sw);
189 }
190 }
191 return activeSwitches;
192 }
193
194 @Override
195 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
196 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
197 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
198 return switches;
199 }
200
201 @Override
202 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
203 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
204 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
205 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
206
207 for (ISwitchObject sw: switches) {
208 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
209 inactiveSwitches.add(sw);
210 }
211 }
212 return inactiveSwitches;
213 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800214}