blob: c43434fb9621d12fb379ea62de4b45b99d086ef5 [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;
17import com.tinkerpop.blueprints.util.wrappers.event.EventGraph;
18import com.tinkerpop.frames.FramedGraph;
19import com.tinkerpop.frames.FramedVertexIterable;
20import com.tinkerpop.gremlin.java.GremlinPipeline;
21
Pankaj Berdeda809572013-02-22 15:31:20 -080022public class GraphDBUtils implements IDBUtils {
Pankaj Berde15193092013-03-21 17:30:14 -070023
24 @Override
25 public ISwitchObject newSwitch(GraphDBConnection conn) {
26 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
27 ISwitchObject obj = fg.addVertex(null,ISwitchObject.class);
28 return obj;
29 }
Pankaj Berdeda809572013-02-22 15:31:20 -080030
31 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070032 public void removeSwitch(GraphDBConnection conn, ISwitchObject sw) {
33 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
34 fg.removeVertex(sw.asVertex());
35 }
36
37 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080038 public ISwitchObject searchSwitch(GraphDBConnection conn, String dpid) {
39 // TODO Auto-generated method stub
40 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
41
42 return fg.getVertices("dpid",dpid).iterator().hasNext() ?
43 fg.getVertices("dpid",dpid,ISwitchObject.class).iterator().next() : null;
44
45 }
46
47 @Override
Pavlin Radoslavovf6fa7f02013-03-28 16:40:48 -070048 public ISwitchObject searchActiveSwitch(GraphDBConnection conn, String dpid) {
49 ISwitchObject sw = searchSwitch(conn, dpid);
50 if ((sw != null) &&
51 sw.getState().equals(SwitchState.ACTIVE.toString())) {
52 return sw;
53 }
54 return null;
55 }
56
57 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080058 public IDeviceObject searchDevice(GraphDBConnection conn, String macAddr) {
59 // TODO Auto-generated method stub
60 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
61 return fg.getVertices("dl_address",macAddr).iterator().hasNext() ? fg.getVertices("dl_address",macAddr,
62 IDeviceObject.class).iterator().next() : null;
63
64 }
65
66 @Override
67 public IPortObject searchPort(GraphDBConnection conn, String dpid, short number) {
68 ISwitchObject sw = searchSwitch(conn, dpid);
69 GremlinPipeline<Vertex, IPortObject> pipe = new GremlinPipeline<Vertex, IPortObject>();
70 pipe.start(sw.asVertex());
71 pipe.out("on").has("number", number);
Pankaj Berde8f036112013-03-28 22:58:47 -070072 FramedVertexIterable<IPortObject> r = new FramedVertexIterable<IPortObject>(conn.getFramedGraph(), (Iterable) pipe, IPortObject.class);
Pankaj Berdeda809572013-02-22 15:31:20 -080073 return r.iterator().hasNext() ? r.iterator().next() : null;
74 }
75
76 @Override
Pankaj Berde15193092013-03-21 17:30:14 -070077 public IPortObject newPort(GraphDBConnection conn) {
78 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
79 IPortObject obj = fg.addVertex(null,IPortObject.class);
80 return obj;
81 }
82
83 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -080084 public IDeviceObject newDevice(GraphDBConnection conn) {
85 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
86 IDeviceObject obj = fg.addVertex(null,IDeviceObject.class);
87 return obj;
88 }
Pankaj Berde15193092013-03-21 17:30:14 -070089
90 @Override
91 public void removePort(GraphDBConnection conn, IPortObject port) {
Pankaj Berde8f036112013-03-28 22:58:47 -070092// FramedGraph<TitanGraph> fg = conn.getFramedGraph();
93 EventGraph<TitanGraph> eg = conn.getEventGraph();
94 eg.removeVertex(port.asVertex());
Pankaj Berde15193092013-03-21 17:30:14 -070095 }
Pankaj Berdeda809572013-02-22 15:31:20 -080096
97 @Override
98 public void removeDevice(GraphDBConnection conn, IDeviceObject dev) {
99 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
100 fg.removeVertex(dev.asVertex());
101 }
102
Pankaj Berdeac1a8c32013-02-26 17:45:57 -0800103 @Override
104 public Iterable<IDeviceObject> getDevices(GraphDBConnection conn) {
105 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
106 return fg.getVertices("type","device",IDeviceObject.class);
107 }
108
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800109 @Override
110 public IFlowPath searchFlowPath(GraphDBConnection conn,
111 FlowId flowId) {
112 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
113
114 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() ?
115 fg.getVertices("flow_id", flowId.toString(),
116 IFlowPath.class).iterator().next() : null;
117 }
118
119 @Override
120 public IFlowPath newFlowPath(GraphDBConnection conn) {
121 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
122 IFlowPath flowPath = fg.addVertex(null, IFlowPath.class);
123 return flowPath;
124 }
125
126 @Override
127 public void removeFlowPath(GraphDBConnection conn,
128 IFlowPath flowPath) {
129 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
130 fg.removeVertex(flowPath.asVertex());
131 }
132
133 @Override
134 public IFlowPath getFlowPathByFlowEntry(GraphDBConnection conn,
135 IFlowEntry flowEntry) {
136 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
137 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
138 pipe.start(flowEntry.asVertex());
139 pipe.out("flow");
Pankaj Berde8f036112013-03-28 22:58:47 -0700140 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800141 return r.iterator().hasNext() ? r.iterator().next() : null;
142 }
143
144 @Override
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800145 public Iterable<IFlowPath> getAllFlowPaths(GraphDBConnection conn) {
146 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
147
148 return fg.getVertices("type", "flow", IFlowPath.class);
149 }
150
151 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800152 public IFlowEntry searchFlowEntry(GraphDBConnection conn,
153 FlowEntryId flowEntryId) {
154 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
155
156 return fg.getVertices("flow_entry_id", flowEntryId.toString()).iterator().hasNext() ?
157 fg.getVertices("flow_entry_id", flowEntryId.toString(),
158 IFlowEntry.class).iterator().next() : null;
159 }
160
161 @Override
162 public IFlowEntry newFlowEntry(GraphDBConnection conn) {
163 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
164 IFlowEntry flowEntry = fg.addVertex(null, IFlowEntry.class);
165 return flowEntry;
166 }
167
168 @Override
169 public void removeFlowEntry(GraphDBConnection conn,
170 IFlowEntry flowEntry) {
171 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
172 fg.removeVertex(flowEntry.asVertex());
173 }
174
175 @Override
176 public Iterable<IFlowEntry> getAllFlowEntries(GraphDBConnection conn) {
177 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
178
179 return fg.getVertices("type", "flow_entry", IFlowEntry.class);
180 }
Pankaj Berde15193092013-03-21 17:30:14 -0700181
182 @Override
183 public Iterable<ISwitchObject> getActiveSwitches(GraphDBConnection conn) {
184 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
185 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
186 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
187
188 for (ISwitchObject sw: switches) {
189 if(sw.getState().equals(SwitchState.ACTIVE.toString())) {
190 activeSwitches.add(sw);
191 }
192 }
193 return activeSwitches;
194 }
195
196 @Override
197 public Iterable<ISwitchObject> getAllSwitches(GraphDBConnection conn) {
198 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
199 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
200 return switches;
201 }
202
203 @Override
204 public Iterable<ISwitchObject> getInactiveSwitches(GraphDBConnection conn) {
205 FramedGraph<TitanGraph> fg = conn.getFramedGraph();
206 Iterable<ISwitchObject> switches = fg.getVertices("type","switch",ISwitchObject.class);
207 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
208
209 for (ISwitchObject sw: switches) {
210 if(sw.getState().equals(SwitchState.INACTIVE.toString())) {
211 inactiveSwitches.add(sw);
212 }
213 }
214 return inactiveSwitches;
215 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800216}