blob: 747e514e298c1ade674d7649e9730c3eef05cdd1 [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;
Toshio Koide3f233542014-01-07 14:19:09 -08008import com.tinkerpop.blueprints.impls.ramcloud.*;
yoshi0451f282013-11-22 15:48:55 -08009import com.tinkerpop.frames.FramedGraph;
10import com.tinkerpop.frames.structures.FramedVertexIterable;
11import com.tinkerpop.gremlin.java.GremlinPipeline;
Toshio Koide3f233542014-01-07 14:19:09 -080012
yoshi0451f282013-11-22 15:48:55 -080013import java.util.ArrayList;
14import java.util.Iterator;
15import java.util.List;
Toshio Koide3f233542014-01-07 14:19:09 -080016import java.util.Map;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
yoshi0451f282013-11-22 15:48:55 -080021import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects;
yoshitomob292c622013-11-23 14:35:58 -080022import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject;
yoshi0451f282013-11-22 15:48:55 -080023import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
24import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
25import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
yoshitomob292c622013-11-23 14:35:58 -080026import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
yoshi0451f282013-11-22 15:48:55 -080027import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
28import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
yoshib3c83c12013-12-03 00:58:13 -080029import net.onrc.onos.ofcontroller.util.FlowEntryId;
yoshi0451f282013-11-22 15:48:55 -080030import net.onrc.onos.ofcontroller.util.FlowId;
31
yoshi0451f282013-11-22 15:48:55 -080032public abstract class DBOperation implements IDBOperation {
33
yoshic455c012013-11-27 10:35:50 -080034 protected DBConnection conn;
Toshio Koide1de920a2014-01-07 15:43:18 -080035 private final static Logger log = LoggerFactory.getLogger(DBOperation.class);
Toshio Koide3f233542014-01-07 14:19:09 -080036
yoshi0451f282013-11-22 15:48:55 -080037
yoshi2dd767c2013-11-27 23:39:06 -080038 /**
39 * Search and get an active switch object with DPID.
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -080040 * @param dpid DPID of the switch
yoshi2dd767c2013-11-27 23:39:06 -080041 */
yoshic455c012013-11-27 10:35:50 -080042 @Override
43 public ISwitchObject searchActiveSwitch(String dpid) {
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -070044 /*
yoshic455c012013-11-27 10:35:50 -080045 ISwitchObject sw = searchSwitch(dpid);
46 if ((sw != null)
47 && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
48 return sw;
49 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -070050 */
yoshic455c012013-11-27 10:35:50 -080051 return null;
52 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -080053
yoshi2dd767c2013-11-27 23:39:06 -080054 /**
55 * Create a new switch and return the created switch object.
56 * @param dpid DPID of the switch
57 */
yoshic455c012013-11-27 10:35:50 -080058 @Override
59 public ISwitchObject newSwitch(final String dpid) {
60 ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
61 if (obj != null) {
62 obj.setType("switch");
63 obj.setDPID(dpid);
64 }
65 return obj;
66 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -080067
yoshi2dd767c2013-11-27 23:39:06 -080068 /**
Yuta HIGUCHI2cef9ba2014-01-09 19:33:22 -080069 * Get all port objects.
70 */
71 @Override
72 public Iterable<IPortObject> getAllPorts() {
73 Iterable<IPortObject> ports = conn.getFramedGraph().getVertices("type", "port", IPortObject.class);
74 return ports;
75 }
76
77 /**
yoshi2dd767c2013-11-27 23:39:06 -080078 * Get all switch objects.
79 */
yoshic455c012013-11-27 10:35:50 -080080 @Override
81 public Iterable<ISwitchObject> getAllSwitches() {
82 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
83 return switches;
84 }
yoshi0451f282013-11-22 15:48:55 -080085
yoshi2dd767c2013-11-27 23:39:06 -080086 /**
87 * Get all inactive switch objects.
88 */
yoshic455c012013-11-27 10:35:50 -080089 @Override
90 public Iterable<ISwitchObject> getInactiveSwitches() {
91 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
92 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -080093
yoshic455c012013-11-27 10:35:50 -080094 for (ISwitchObject sw : switches) {
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -070095 /*
yoshic455c012013-11-27 10:35:50 -080096 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
97 inactiveSwitches.add(sw);
98 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -070099 */
yoshic455c012013-11-27 10:35:50 -0800100 }
101 return inactiveSwitches;
102 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800103
yoshi2dd767c2013-11-27 23:39:06 -0800104 /**
105 * Get all flow entries objects where their switches are not updated.
106 */
yoshic455c012013-11-27 10:35:50 -0800107 @Override
108 public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
109 //TODO: Should use an enum for flow_switch_state
110 return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -0800111
yoshic455c012013-11-27 10:35:50 -0800112 }
yoshi0451f282013-11-22 15:48:55 -0800113
yoshi2dd767c2013-11-27 23:39:06 -0800114 /**
115 * Remove specified switch.
116 * @param sw switch object to remove
117 */
yoshic455c012013-11-27 10:35:50 -0800118 @Override
119 public void removeSwitch(ISwitchObject sw) {
120 conn.getFramedGraph().removeVertex(sw.asVertex());
121 }
yoshi0451f282013-11-22 15:48:55 -0800122
yoshic455c012013-11-27 10:35:50 -0800123 @Override
124 public IPortObject newPort(String dpid, Short portNum) {
125 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
126 if (obj != null) {
127 obj.setType("port");
Yuta HIGUCHI2cef9ba2014-01-09 19:33:22 -0800128 String id = dpid + PORT_ID_DELIM + portNum.toString();
yoshic455c012013-11-27 10:35:50 -0800129 obj.setPortId(id);
130 obj.setNumber(portNum);
131 }
132 return obj;
133 }
yoshi0451f282013-11-22 15:48:55 -0800134
yoshic455c012013-11-27 10:35:50 -0800135 /**
136 * Create a port having specified port number.
137 *
138 * @param portNumber port number
139 */
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800140 @Override
yoshic455c012013-11-27 10:35:50 -0800141 @Deprecated
142 public IPortObject newPort(Short portNumber) {
143 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
144 if (obj != null) {
145 obj.setType("port");
146 obj.setNumber(portNumber);
147 }
148 return obj;
149 }
yoshi0451f282013-11-22 15:48:55 -0800150
yoshi2dd767c2013-11-27 23:39:06 -0800151 /**
152 * Search and get a port object of specified switch and port number.
153 * @param dpid DPID of a switch
154 * @param number port number of the switch's port
155 */
yoshic455c012013-11-27 10:35:50 -0800156 @Override
157 public IPortObject searchPort(String dpid, Short number) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800158 FramedGraph fg = conn.getFramedGraph();
159 if ( fg == null ) return null;
Yuta HIGUCHI2cef9ba2014-01-09 19:33:22 -0800160 String id = dpid + PORT_ID_DELIM + number.toString();
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800161 Iterator<IPortObject> it = fg.getVertices("port_id", id, IPortObject.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800162 return (it.hasNext()) ? it.next() : null;
yoshi0451f282013-11-22 15:48:55 -0800163
yoshic455c012013-11-27 10:35:50 -0800164 }
yoshi0451f282013-11-22 15:48:55 -0800165
yoshi2dd767c2013-11-27 23:39:06 -0800166 /**
167 * Remove the specified switch port.
168 * @param port switch port object to remove
169 */
yoshic455c012013-11-27 10:35:50 -0800170 @Override
171 public void removePort(IPortObject port) {
172 if (conn.getFramedGraph() != null) {
173 conn.getFramedGraph().removeVertex(port.asVertex());
174 }
175 }
176
yoshi2dd767c2013-11-27 23:39:06 -0800177 /**
178 * Create and return a device object.
179 */
yoshic455c012013-11-27 10:35:50 -0800180 @Override
181 public IDeviceObject newDevice() {
182 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
183 if (obj != null) {
184 obj.setType("device");
185 }
186 return obj;
187 }
188
yoshi2dd767c2013-11-27 23:39:06 -0800189 /**
190 * Get all devices.
191 */
yoshic455c012013-11-27 10:35:50 -0800192 @Override
193 public Iterable<IDeviceObject> getDevices() {
194 return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null;
195 }
yoshi0451f282013-11-22 15:48:55 -0800196
yoshi2dd767c2013-11-27 23:39:06 -0800197 /**
198 * Remove the specified device.
199 * @param dev a device object to remove
200 */
yoshic455c012013-11-27 10:35:50 -0800201 @Override
202 public void removeDevice(IDeviceObject dev) {
203 if (conn.getFramedGraph() != null) {
204 conn.getFramedGraph().removeVertex(dev.asVertex());
205 }
206 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800207
yoshic455c012013-11-27 10:35:50 -0800208 /**
209 * Create and return a flow path object.
210 */
yoshic455c012013-11-27 10:35:50 -0800211 @Override
212 public IFlowPath newFlowPath() {
213 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
214 if (flowPath != null) {
215 flowPath.setType("flow");
216 }
217 return flowPath;
218 }
yoshi0451f282013-11-22 15:48:55 -0800219
yoshi2dd767c2013-11-27 23:39:06 -0800220 /**
221 * Get a flow path object with a flow entry.
222 * @param flowEntry flow entry object
223 */
yoshic455c012013-11-27 10:35:50 -0800224 @Override
225 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
226 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
227 pipe.start(flowEntry.asVertex());
228 pipe.out("flow");
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800229 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800230 return r.iterator().hasNext() ? r.iterator().next() : null;
231 }
yoshi0451f282013-11-22 15:48:55 -0800232
yoshi0451f282013-11-22 15:48:55 -0800233
yoshic455c012013-11-27 10:35:50 -0800234 /**
235 * Search and get a switch object with DPID.
236 *
237 * @param dpid DPID of the switch
238 */
239 @Override
240 public ISwitchObject searchSwitch(final String dpid) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800241 FramedGraph fg = conn.getFramedGraph();
242 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800243 Iterator<ISwitchObject> it = fg.getVertices("dpid", dpid, ISwitchObject.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800244 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800245 }
yoshi0451f282013-11-22 15:48:55 -0800246
yoshi2dd767c2013-11-27 23:39:06 -0800247 /**
248 * Get all active switch objects.
249 */
yoshic455c012013-11-27 10:35:50 -0800250 @Override
251 public Iterable<ISwitchObject> getActiveSwitches() {
252 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
253 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800254
yoshic455c012013-11-27 10:35:50 -0800255 for (ISwitchObject sw : switches) {
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700256 /*
yoshic455c012013-11-27 10:35:50 -0800257 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
258 activeSwitches.add(sw);
259 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700260 */
yoshic455c012013-11-27 10:35:50 -0800261 }
262 return activeSwitches;
263 }
yoshi0451f282013-11-22 15:48:55 -0800264
yoshi2dd767c2013-11-27 23:39:06 -0800265 /**
266 * Search and get a device object having specified MAC address.
267 * @param macAddr MAC address to search and get
268 */
yoshic455c012013-11-27 10:35:50 -0800269 @Override
270 public IDeviceObject searchDevice(String macAddr) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800271 FramedGraph fg = conn.getFramedGraph();
272 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800273 Iterator<IDeviceObject> it = fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800274 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800275 }
276
yoshi2dd767c2013-11-27 23:39:06 -0800277 /**
278 * Search and get a flow path object with specified flow ID.
279 * @param flowId flow ID to search
280 */
yoshib3c83c12013-12-03 00:58:13 -0800281 @Override
282 public IFlowPath searchFlowPath(final FlowId flowId) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800283 FramedGraph fg = conn.getFramedGraph();
284 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800285 Iterator<IFlowPath> it = fg.getVertices("flow_id", flowId.toString(), IFlowPath.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800286 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800287 }
288
yoshi2dd767c2013-11-27 23:39:06 -0800289 /**
290 * Get all flow path objects.
291 */
yoshib3c83c12013-12-03 00:58:13 -0800292 @Override
293 public Iterable<IFlowPath> getAllFlowPaths() {
294 Iterable<IFlowPath> flowPaths = conn.getFramedGraph().getVertices("type", "flow", IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800295
296 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
297
298 for (IFlowPath fp : flowPaths) {
299 if (fp.getFlowId() != null) {
300 nonNullFlows.add(fp);
301 }
302 }
303 return nonNullFlows;
304 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800305
yoshi2dd767c2013-11-27 23:39:06 -0800306 /**
yoshib3c83c12013-12-03 00:58:13 -0800307 * Remove the specified flow path.
308 * @param flowPath flow path object to remove
309 */
310 @Override
311 public void removeFlowPath(IFlowPath flowPath) {
312 conn.getFramedGraph().removeVertex(flowPath.asVertex());
313 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800314
yoshib3c83c12013-12-03 00:58:13 -0800315 /**
316 * Search and get a flow entry object with flow entry ID.
317 * @param flowEntryId flow entry ID to search
318 */
319 @Override
320 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800321 FramedGraph fg = conn.getFramedGraph();
322 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800323 Iterator<IFlowEntry> it = fg.getVertices("flow_entry_id", flowEntryId.toString(), IFlowEntry.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800324 return (it.hasNext()) ? it.next() : null;
yoshib3c83c12013-12-03 00:58:13 -0800325 }
326
327 /**
328 * Get all flow entry objects.
329 */
330 @Override
331 public Iterable<IFlowEntry> getAllFlowEntries() {
332 return conn.getFramedGraph().getVertices("type", "flow_entry", IFlowEntry.class);
333 }
334
335 /**
336 * Remove the specified flow entry.
337 * @param flowEntry flow entry object to remove
338 */
339 @Override
340 public void removeFlowEntry(IFlowEntry flowEntry) {
341 conn.getFramedGraph().removeVertex(flowEntry.asVertex());
342 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800343
yoshib3c83c12013-12-03 00:58:13 -0800344 /**
yoshi2dd767c2013-11-27 23:39:06 -0800345 * Create and return a flow entry object.
346 */
yoshic455c012013-11-27 10:35:50 -0800347 @Override
348 public IFlowEntry newFlowEntry() {
349 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
350 if (flowEntry != null) {
351 flowEntry.setType("flow_entry");
352 }
353 return flowEntry;
354 }
355
356
yoshitomob292c622013-11-23 14:35:58 -0800357 public IIpv4Address newIpv4Address() {
358 return newVertex("ipv4Address", IIpv4Address.class);
359 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800360
yoshitomob292c622013-11-23 14:35:58 -0800361 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800362 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800363 if (newVertex != null) {
364 newVertex.setType(type);
365 }
366 return newVertex;
367 }
yoshic455c012013-11-27 10:35:50 -0800368
yoshitomob292c622013-11-23 14:35:58 -0800369 public IIpv4Address searchIpv4Address(int intIpv4Address) {
370 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
371 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800372
373
yoshitomob292c622013-11-23 14:35:58 -0800374 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
375 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
376 if (ipv4Vertex == null) {
377 ipv4Vertex = newIpv4Address();
378 ipv4Vertex.setIpv4Address(intIpv4Address);
379 }
380 return ipv4Vertex;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800381 }
yoshitomob292c622013-11-23 14:35:58 -0800382
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800383
yoshitomob292c622013-11-23 14:35:58 -0800384 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
385 if (conn.getFramedGraph() != null) {
386 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
387 if (it.hasNext()) {
388 return it.next();
389 }
390 }
391 return null;
392 }
393
394 public void removeIpv4Address(IIpv4Address ipv4Address) {
395 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
396 }
yoshi0451f282013-11-22 15:48:55 -0800397
yoshib3c83c12013-12-03 00:58:13 -0800398 /**
399 * Get the instance of GraphDBConnection assigned to this class.
400 */
yoshi7594aef2013-11-27 09:27:07 -0800401 @Override
402 public IDBConnection getDBConnection() {
403 return conn;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800404 }
405
yoshib3c83c12013-12-03 00:58:13 -0800406 @Override
407 public void commit() {
408 conn.commit();
409 }
410
411 @Override
412 public void rollback() {
413 conn.rollback();
414 }
415
416 @Override
417 public void close() {
418 conn.close();
419 }
Toshio Koide3f233542014-01-07 14:19:09 -0800420
421 @Override
Toshio Koide3fcebc12014-01-09 22:40:11 -0800422 public void setVertexProperties(Vertex vertex, Map<String, Object> map) {
Toshio Koide3f233542014-01-07 14:19:09 -0800423 log.debug("setProperties start: size {}", map.size());
Toshio Koide3fcebc12014-01-09 22:40:11 -0800424 RamCloudVertex v = (RamCloudVertex) vertex;
Toshio Koide3f233542014-01-07 14:19:09 -0800425 v.setProperties(map);
426 log.debug("setProperties end: size {}, id {}", map.size(), v.getId());
427 }
Toshio Koide3fcebc12014-01-09 22:40:11 -0800428
429 public String toString() {
430 StringBuilder sb = new StringBuilder();
431 for(ISwitchObject sw: getAllSwitches()) {
432 sb.append("sw: " + sw.getDPID() + "\n");
433 for(IPortObject port: sw.getPorts()) {
434 sb.append(" port: " + port.getPortId() + "\n");
435 }
436 }
437 return sb.toString();
438 }
yoshi0451f282013-11-22 15:48:55 -0800439}