blob: b58198a4d07e2247666b5cc67f5a01c2f3bc0c46 [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.graph;
6
yoshi0451f282013-11-22 15:48:55 -08007import com.tinkerpop.blueprints.Vertex;
8import com.tinkerpop.frames.FramedGraph;
9import com.tinkerpop.frames.structures.FramedVertexIterable;
10import com.tinkerpop.gremlin.java.GremlinPipeline;
11import java.util.ArrayList;
12import java.util.Iterator;
13import java.util.List;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects;
yoshitomob292c622013-11-23 14:35:58 -080015import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject;
yoshi0451f282013-11-22 15:48:55 -080016import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
17import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
yoshitomob292c622013-11-23 14:35:58 -080019import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
yoshi0451f282013-11-22 15:48:55 -080020import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
21import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
22import net.onrc.onos.ofcontroller.core.ISwitchStorage;
23import net.onrc.onos.ofcontroller.util.FlowId;
24
25/**
26 *
27 * @author nickkaranatsios
28 */
29public abstract class DBOperation implements IDBOperation {
30
yoshic455c012013-11-27 10:35:50 -080031 protected DBConnection conn;
yoshi0451f282013-11-22 15:48:55 -080032
yoshi2dd767c2013-11-27 23:39:06 -080033 /**
34 * Search and get an active switch object with DPID.
35 * @param dpid DPID of the switch
36 */
yoshic455c012013-11-27 10:35:50 -080037 @Override
38 public ISwitchObject searchActiveSwitch(String dpid) {
39 ISwitchObject sw = searchSwitch(dpid);
40 if ((sw != null)
41 && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
42 return sw;
43 }
44 return null;
45 }
yoshi2dd767c2013-11-27 23:39:06 -080046
47 /**
48 * Create a new switch and return the created switch object.
49 * @param dpid DPID of the switch
50 */
yoshic455c012013-11-27 10:35:50 -080051 @Override
52 public ISwitchObject newSwitch(final String dpid) {
53 ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
54 if (obj != null) {
55 obj.setType("switch");
56 obj.setDPID(dpid);
57 }
58 return obj;
59 }
yoshi2dd767c2013-11-27 23:39:06 -080060
61 /**
62 * Get all switch objects.
63 */
yoshic455c012013-11-27 10:35:50 -080064 @Override
65 public Iterable<ISwitchObject> getAllSwitches() {
66 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
67 return switches;
68 }
yoshi0451f282013-11-22 15:48:55 -080069
yoshi2dd767c2013-11-27 23:39:06 -080070 /**
71 * Get all inactive switch objects.
72 */
yoshic455c012013-11-27 10:35:50 -080073 @Override
74 public Iterable<ISwitchObject> getInactiveSwitches() {
75 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
76 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -080077
yoshic455c012013-11-27 10:35:50 -080078 for (ISwitchObject sw : switches) {
79 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
80 inactiveSwitches.add(sw);
81 }
82 }
83 return inactiveSwitches;
84 }
yoshi2dd767c2013-11-27 23:39:06 -080085
86 /**
87 * Get all flow entries objects where their switches are not updated.
88 */
yoshic455c012013-11-27 10:35:50 -080089 @Override
90 public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
91 //TODO: Should use an enum for flow_switch_state
92 return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -080093
yoshic455c012013-11-27 10:35:50 -080094 }
yoshi0451f282013-11-22 15:48:55 -080095
yoshi2dd767c2013-11-27 23:39:06 -080096 /**
97 * Remove specified switch.
98 * @param sw switch object to remove
99 */
yoshic455c012013-11-27 10:35:50 -0800100 @Override
101 public void removeSwitch(ISwitchObject sw) {
102 conn.getFramedGraph().removeVertex(sw.asVertex());
103 }
yoshi0451f282013-11-22 15:48:55 -0800104
yoshic455c012013-11-27 10:35:50 -0800105 @Override
106 public IPortObject newPort(String dpid, Short portNum) {
107 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
108 if (obj != null) {
109 obj.setType("port");
110 String id = dpid + portNum.toString();
111 obj.setPortId(id);
112 obj.setNumber(portNum);
113 }
114 return obj;
115 }
yoshi0451f282013-11-22 15:48:55 -0800116
yoshic455c012013-11-27 10:35:50 -0800117 /**
118 * Create a port having specified port number.
119 *
120 * @param portNumber port number
121 */
122 @Deprecated
123 public IPortObject newPort(Short portNumber) {
124 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
125 if (obj != null) {
126 obj.setType("port");
127 obj.setNumber(portNumber);
128 }
129 return obj;
130 }
yoshi0451f282013-11-22 15:48:55 -0800131
yoshi2dd767c2013-11-27 23:39:06 -0800132 /**
133 * Search and get a port object of specified switch and port number.
134 * @param dpid DPID of a switch
135 * @param number port number of the switch's port
136 */
yoshic455c012013-11-27 10:35:50 -0800137 @Override
138 public IPortObject searchPort(String dpid, Short number) {
139 String id = dpid + number.toString();
140 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("port_id", id).iterator().hasNext())
141 ? (IPortObject) conn.getFramedGraph().getVertices("port_id", id, IPortObject.class).iterator().next() : null;
yoshi0451f282013-11-22 15:48:55 -0800142
yoshic455c012013-11-27 10:35:50 -0800143 }
yoshi0451f282013-11-22 15:48:55 -0800144
yoshi2dd767c2013-11-27 23:39:06 -0800145 /**
146 * Remove the specified switch port.
147 * @param port switch port object to remove
148 */
yoshic455c012013-11-27 10:35:50 -0800149 @Override
150 public void removePort(IPortObject port) {
151 if (conn.getFramedGraph() != null) {
152 conn.getFramedGraph().removeVertex(port.asVertex());
153 }
154 }
155
yoshi2dd767c2013-11-27 23:39:06 -0800156 /**
157 * Create and return a device object.
158 */
yoshic455c012013-11-27 10:35:50 -0800159 @Override
160 public IDeviceObject newDevice() {
161 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
162 if (obj != null) {
163 obj.setType("device");
164 }
165 return obj;
166 }
167
yoshi2dd767c2013-11-27 23:39:06 -0800168 /**
169 * Get all devices.
170 */
yoshic455c012013-11-27 10:35:50 -0800171 @Override
172 public Iterable<IDeviceObject> getDevices() {
173 return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null;
174 }
yoshi0451f282013-11-22 15:48:55 -0800175
yoshi2dd767c2013-11-27 23:39:06 -0800176 /**
177 * Remove the specified device.
178 * @param dev a device object to remove
179 */
yoshic455c012013-11-27 10:35:50 -0800180 @Override
181 public void removeDevice(IDeviceObject dev) {
182 if (conn.getFramedGraph() != null) {
183 conn.getFramedGraph().removeVertex(dev.asVertex());
184 }
185 }
186
187 /**
188 * Create and return a flow path object.
189 */
yoshic455c012013-11-27 10:35:50 -0800190 @Override
191 public IFlowPath newFlowPath() {
192 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
193 if (flowPath != null) {
194 flowPath.setType("flow");
195 }
196 return flowPath;
197 }
yoshi0451f282013-11-22 15:48:55 -0800198
yoshi2dd767c2013-11-27 23:39:06 -0800199 /**
200 * Get a flow path object with a flow entry.
201 * @param flowEntry flow entry object
202 */
yoshic455c012013-11-27 10:35:50 -0800203 @Override
204 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
205 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
206 pipe.start(flowEntry.asVertex());
207 pipe.out("flow");
208 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class);
209 return r.iterator().hasNext() ? r.iterator().next() : null;
210 }
yoshi0451f282013-11-22 15:48:55 -0800211
yoshi0451f282013-11-22 15:48:55 -0800212
yoshic455c012013-11-27 10:35:50 -0800213 /**
214 * Search and get a switch object with DPID.
215 *
216 * @param dpid DPID of the switch
217 */
218 @Override
219 public ISwitchObject searchSwitch(final String dpid) {
220 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dpid", dpid).iterator().hasNext())
221 ? (ISwitchObject) (conn.getFramedGraph().getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null;
222 }
yoshi0451f282013-11-22 15:48:55 -0800223
yoshi2dd767c2013-11-27 23:39:06 -0800224 /**
225 * Get all active switch objects.
226 */
yoshic455c012013-11-27 10:35:50 -0800227 @Override
228 public Iterable<ISwitchObject> getActiveSwitches() {
229 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
230 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800231
yoshic455c012013-11-27 10:35:50 -0800232 for (ISwitchObject sw : switches) {
233 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
234 activeSwitches.add(sw);
235 }
236 }
237 return activeSwitches;
238 }
yoshi0451f282013-11-22 15:48:55 -0800239
yoshi2dd767c2013-11-27 23:39:06 -0800240 /**
241 * Search and get a device object having specified MAC address.
242 * @param macAddr MAC address to search and get
243 */
yoshic455c012013-11-27 10:35:50 -0800244 @Override
245 public IDeviceObject searchDevice(String macAddr) {
246 return (conn.getFramedGraph() != null && conn.getFramedGraph().getVertices("dl_addr", macAddr).iterator().hasNext())
247 ? (IDeviceObject) conn.getFramedGraph().getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null;
248
249 }
250
yoshi2dd767c2013-11-27 23:39:06 -0800251 /**
252 * Search and get a flow path object with specified flow ID.
253 * @param flowId flow ID to search
254 */
yoshic455c012013-11-27 10:35:50 -0800255 protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) {
256 return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext()
257 ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(),
258 IFlowPath.class).iterator().next() : null;
259 }
260
yoshi2dd767c2013-11-27 23:39:06 -0800261 /**
262 * Get all flow path objects.
263 */
yoshic455c012013-11-27 10:35:50 -0800264 protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) {
265 Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class);
266
267 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
268
269 for (IFlowPath fp : flowPaths) {
270 if (fp.getFlowId() != null) {
271 nonNullFlows.add(fp);
272 }
273 }
274 return nonNullFlows;
275 }
yoshi2dd767c2013-11-27 23:39:06 -0800276
277 /**
278 * Create and return a flow entry object.
279 */
yoshic455c012013-11-27 10:35:50 -0800280 @Override
281 public IFlowEntry newFlowEntry() {
282 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
283 if (flowEntry != null) {
284 flowEntry.setType("flow_entry");
285 }
286 return flowEntry;
287 }
288
289
yoshitomob292c622013-11-23 14:35:58 -0800290 public IIpv4Address newIpv4Address() {
291 return newVertex("ipv4Address", IIpv4Address.class);
292 }
yoshi2dd767c2013-11-27 23:39:06 -0800293
yoshitomob292c622013-11-23 14:35:58 -0800294 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800295 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800296 if (newVertex != null) {
297 newVertex.setType(type);
298 }
299 return newVertex;
300 }
yoshic455c012013-11-27 10:35:50 -0800301
yoshitomob292c622013-11-23 14:35:58 -0800302 public IIpv4Address searchIpv4Address(int intIpv4Address) {
303 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
304 }
305
306
307 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
308 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
309 if (ipv4Vertex == null) {
310 ipv4Vertex = newIpv4Address();
311 ipv4Vertex.setIpv4Address(intIpv4Address);
312 }
313 return ipv4Vertex;
314 }
315
316
317 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
318 if (conn.getFramedGraph() != null) {
319 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
320 if (it.hasNext()) {
321 return it.next();
322 }
323 }
324 return null;
325 }
326
327 public void removeIpv4Address(IIpv4Address ipv4Address) {
328 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
329 }
yoshi0451f282013-11-22 15:48:55 -0800330
yoshi7594aef2013-11-27 09:27:07 -0800331
332 @Override
333 public IDBConnection getDBConnection() {
334 return conn;
335 }
yoshi0451f282013-11-22 15:48:55 -0800336}