blob: c291544f0f234de2617295104864f55f0e428668 [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;
29import net.onrc.onos.ofcontroller.core.ISwitchStorage;
yoshib3c83c12013-12-03 00:58:13 -080030import net.onrc.onos.ofcontroller.util.FlowEntryId;
yoshi0451f282013-11-22 15:48:55 -080031import net.onrc.onos.ofcontroller.util.FlowId;
32
yoshi0451f282013-11-22 15:48:55 -080033public abstract class DBOperation implements IDBOperation {
34
yoshic455c012013-11-27 10:35:50 -080035 protected DBConnection conn;
Toshio Koide1de920a2014-01-07 15:43:18 -080036 private final static Logger log = LoggerFactory.getLogger(DBOperation.class);
Toshio Koide3f233542014-01-07 14:19:09 -080037
yoshi0451f282013-11-22 15:48:55 -080038
yoshi2dd767c2013-11-27 23:39:06 -080039 /**
40 * Search and get an active switch object with DPID.
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -080041 * @param dpid DPID of the switch
yoshi2dd767c2013-11-27 23:39:06 -080042 */
yoshic455c012013-11-27 10:35:50 -080043 @Override
44 public ISwitchObject searchActiveSwitch(String dpid) {
45 ISwitchObject sw = searchSwitch(dpid);
46 if ((sw != null)
47 && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
48 return sw;
49 }
50 return null;
51 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -080052
yoshi2dd767c2013-11-27 23:39:06 -080053 /**
54 * Create a new switch and return the created switch object.
55 * @param dpid DPID of the switch
56 */
yoshic455c012013-11-27 10:35:50 -080057 @Override
58 public ISwitchObject newSwitch(final String dpid) {
59 ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class);
60 if (obj != null) {
61 obj.setType("switch");
62 obj.setDPID(dpid);
63 }
64 return obj;
65 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -080066
yoshi2dd767c2013-11-27 23:39:06 -080067 /**
Yuta HIGUCHI2cef9ba2014-01-09 19:33:22 -080068 * Get all port objects.
69 */
70 @Override
71 public Iterable<IPortObject> getAllPorts() {
72 Iterable<IPortObject> ports = conn.getFramedGraph().getVertices("type", "port", IPortObject.class);
73 return ports;
74 }
75
76 /**
yoshi2dd767c2013-11-27 23:39:06 -080077 * Get all switch objects.
78 */
yoshic455c012013-11-27 10:35:50 -080079 @Override
80 public Iterable<ISwitchObject> getAllSwitches() {
81 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
82 return switches;
83 }
yoshi0451f282013-11-22 15:48:55 -080084
yoshi2dd767c2013-11-27 23:39:06 -080085 /**
86 * Get all inactive switch objects.
87 */
yoshic455c012013-11-27 10:35:50 -080088 @Override
89 public Iterable<ISwitchObject> getInactiveSwitches() {
90 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
91 List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -080092
yoshic455c012013-11-27 10:35:50 -080093 for (ISwitchObject sw : switches) {
94 if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) {
95 inactiveSwitches.add(sw);
96 }
97 }
98 return inactiveSwitches;
99 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800100
yoshi2dd767c2013-11-27 23:39:06 -0800101 /**
102 * Get all flow entries objects where their switches are not updated.
103 */
yoshic455c012013-11-27 10:35:50 -0800104 @Override
105 public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() {
106 //TODO: Should use an enum for flow_switch_state
107 return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class);
yoshi0451f282013-11-22 15:48:55 -0800108
yoshic455c012013-11-27 10:35:50 -0800109 }
yoshi0451f282013-11-22 15:48:55 -0800110
yoshi2dd767c2013-11-27 23:39:06 -0800111 /**
112 * Remove specified switch.
113 * @param sw switch object to remove
114 */
yoshic455c012013-11-27 10:35:50 -0800115 @Override
116 public void removeSwitch(ISwitchObject sw) {
117 conn.getFramedGraph().removeVertex(sw.asVertex());
118 }
yoshi0451f282013-11-22 15:48:55 -0800119
yoshic455c012013-11-27 10:35:50 -0800120 @Override
121 public IPortObject newPort(String dpid, Short portNum) {
122 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
123 if (obj != null) {
124 obj.setType("port");
Yuta HIGUCHI2cef9ba2014-01-09 19:33:22 -0800125 String id = dpid + PORT_ID_DELIM + portNum.toString();
yoshic455c012013-11-27 10:35:50 -0800126 obj.setPortId(id);
127 obj.setNumber(portNum);
128 }
129 return obj;
130 }
yoshi0451f282013-11-22 15:48:55 -0800131
yoshic455c012013-11-27 10:35:50 -0800132 /**
133 * Create a port having specified port number.
134 *
135 * @param portNumber port number
136 */
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800137 @Override
yoshic455c012013-11-27 10:35:50 -0800138 @Deprecated
139 public IPortObject newPort(Short portNumber) {
140 IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class);
141 if (obj != null) {
142 obj.setType("port");
143 obj.setNumber(portNumber);
144 }
145 return obj;
146 }
yoshi0451f282013-11-22 15:48:55 -0800147
yoshi2dd767c2013-11-27 23:39:06 -0800148 /**
149 * Search and get a port object of specified switch and port number.
150 * @param dpid DPID of a switch
151 * @param number port number of the switch's port
152 */
yoshic455c012013-11-27 10:35:50 -0800153 @Override
154 public IPortObject searchPort(String dpid, Short number) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800155 FramedGraph fg = conn.getFramedGraph();
156 if ( fg == null ) return null;
Yuta HIGUCHI2cef9ba2014-01-09 19:33:22 -0800157 String id = dpid + PORT_ID_DELIM + number.toString();
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800158 Iterator<IPortObject> it = fg.getVertices("port_id", id, IPortObject.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800159 return (it.hasNext()) ? it.next() : null;
yoshi0451f282013-11-22 15:48:55 -0800160
yoshic455c012013-11-27 10:35:50 -0800161 }
yoshi0451f282013-11-22 15:48:55 -0800162
yoshi2dd767c2013-11-27 23:39:06 -0800163 /**
164 * Remove the specified switch port.
165 * @param port switch port object to remove
166 */
yoshic455c012013-11-27 10:35:50 -0800167 @Override
168 public void removePort(IPortObject port) {
169 if (conn.getFramedGraph() != null) {
170 conn.getFramedGraph().removeVertex(port.asVertex());
171 }
172 }
173
yoshi2dd767c2013-11-27 23:39:06 -0800174 /**
175 * Create and return a device object.
176 */
yoshic455c012013-11-27 10:35:50 -0800177 @Override
178 public IDeviceObject newDevice() {
179 IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class);
180 if (obj != null) {
181 obj.setType("device");
182 }
183 return obj;
184 }
185
yoshi2dd767c2013-11-27 23:39:06 -0800186 /**
187 * Get all devices.
188 */
yoshic455c012013-11-27 10:35:50 -0800189 @Override
190 public Iterable<IDeviceObject> getDevices() {
191 return conn.getFramedGraph() != null ? conn.getFramedGraph().getVertices("type", "device", IDeviceObject.class) : null;
192 }
yoshi0451f282013-11-22 15:48:55 -0800193
yoshi2dd767c2013-11-27 23:39:06 -0800194 /**
195 * Remove the specified device.
196 * @param dev a device object to remove
197 */
yoshic455c012013-11-27 10:35:50 -0800198 @Override
199 public void removeDevice(IDeviceObject dev) {
200 if (conn.getFramedGraph() != null) {
201 conn.getFramedGraph().removeVertex(dev.asVertex());
202 }
203 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800204
yoshic455c012013-11-27 10:35:50 -0800205 /**
206 * Create and return a flow path object.
207 */
yoshic455c012013-11-27 10:35:50 -0800208 @Override
209 public IFlowPath newFlowPath() {
210 IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class);
211 if (flowPath != null) {
212 flowPath.setType("flow");
213 }
214 return flowPath;
215 }
yoshi0451f282013-11-22 15:48:55 -0800216
yoshi2dd767c2013-11-27 23:39:06 -0800217 /**
218 * Get a flow path object with a flow entry.
219 * @param flowEntry flow entry object
220 */
yoshic455c012013-11-27 10:35:50 -0800221 @Override
222 public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) {
223 GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>();
224 pipe.start(flowEntry.asVertex());
225 pipe.out("flow");
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800226 FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), pipe, IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800227 return r.iterator().hasNext() ? r.iterator().next() : null;
228 }
yoshi0451f282013-11-22 15:48:55 -0800229
yoshi0451f282013-11-22 15:48:55 -0800230
yoshic455c012013-11-27 10:35:50 -0800231 /**
232 * Search and get a switch object with DPID.
233 *
234 * @param dpid DPID of the switch
235 */
236 @Override
237 public ISwitchObject searchSwitch(final String dpid) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800238 FramedGraph fg = conn.getFramedGraph();
239 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800240 Iterator<ISwitchObject> it = fg.getVertices("dpid", dpid, ISwitchObject.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800241 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800242 }
yoshi0451f282013-11-22 15:48:55 -0800243
yoshi2dd767c2013-11-27 23:39:06 -0800244 /**
245 * Get all active switch objects.
246 */
yoshic455c012013-11-27 10:35:50 -0800247 @Override
248 public Iterable<ISwitchObject> getActiveSwitches() {
249 Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class);
250 List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>();
yoshi0451f282013-11-22 15:48:55 -0800251
yoshic455c012013-11-27 10:35:50 -0800252 for (ISwitchObject sw : switches) {
253 if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) {
254 activeSwitches.add(sw);
255 }
256 }
257 return activeSwitches;
258 }
yoshi0451f282013-11-22 15:48:55 -0800259
yoshi2dd767c2013-11-27 23:39:06 -0800260 /**
261 * Search and get a device object having specified MAC address.
262 * @param macAddr MAC address to search and get
263 */
yoshic455c012013-11-27 10:35:50 -0800264 @Override
265 public IDeviceObject searchDevice(String macAddr) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800266 FramedGraph fg = conn.getFramedGraph();
267 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800268 Iterator<IDeviceObject> it = fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800269 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800270 }
271
yoshi2dd767c2013-11-27 23:39:06 -0800272 /**
273 * Search and get a flow path object with specified flow ID.
274 * @param flowId flow ID to search
275 */
yoshib3c83c12013-12-03 00:58:13 -0800276 @Override
277 public IFlowPath searchFlowPath(final FlowId flowId) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800278 FramedGraph fg = conn.getFramedGraph();
279 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800280 Iterator<IFlowPath> it = fg.getVertices("flow_id", flowId.toString(), IFlowPath.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800281 return (it.hasNext()) ? it.next() : null;
yoshic455c012013-11-27 10:35:50 -0800282 }
283
yoshi2dd767c2013-11-27 23:39:06 -0800284 /**
285 * Get all flow path objects.
286 */
yoshib3c83c12013-12-03 00:58:13 -0800287 @Override
288 public Iterable<IFlowPath> getAllFlowPaths() {
289 Iterable<IFlowPath> flowPaths = conn.getFramedGraph().getVertices("type", "flow", IFlowPath.class);
yoshic455c012013-11-27 10:35:50 -0800290
291 List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>();
292
293 for (IFlowPath fp : flowPaths) {
294 if (fp.getFlowId() != null) {
295 nonNullFlows.add(fp);
296 }
297 }
298 return nonNullFlows;
299 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800300
yoshi2dd767c2013-11-27 23:39:06 -0800301 /**
yoshib3c83c12013-12-03 00:58:13 -0800302 * Remove the specified flow path.
303 * @param flowPath flow path object to remove
304 */
305 @Override
306 public void removeFlowPath(IFlowPath flowPath) {
307 conn.getFramedGraph().removeVertex(flowPath.asVertex());
308 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800309
yoshib3c83c12013-12-03 00:58:13 -0800310 /**
311 * Search and get a flow entry object with flow entry ID.
312 * @param flowEntryId flow entry ID to search
313 */
314 @Override
315 public IFlowEntry searchFlowEntry(FlowEntryId flowEntryId) {
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800316 FramedGraph fg = conn.getFramedGraph();
317 if ( fg == null ) return null;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800318 Iterator<IFlowEntry> it = fg.getVertices("flow_entry_id", flowEntryId.toString(), IFlowEntry.class).iterator();
Yuta HIGUCHIfa9bcb12013-12-14 00:14:58 -0800319 return (it.hasNext()) ? it.next() : null;
yoshib3c83c12013-12-03 00:58:13 -0800320 }
321
322 /**
323 * Get all flow entry objects.
324 */
325 @Override
326 public Iterable<IFlowEntry> getAllFlowEntries() {
327 return conn.getFramedGraph().getVertices("type", "flow_entry", IFlowEntry.class);
328 }
329
330 /**
331 * Remove the specified flow entry.
332 * @param flowEntry flow entry object to remove
333 */
334 @Override
335 public void removeFlowEntry(IFlowEntry flowEntry) {
336 conn.getFramedGraph().removeVertex(flowEntry.asVertex());
337 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800338
yoshib3c83c12013-12-03 00:58:13 -0800339 /**
yoshi2dd767c2013-11-27 23:39:06 -0800340 * Create and return a flow entry object.
341 */
yoshic455c012013-11-27 10:35:50 -0800342 @Override
343 public IFlowEntry newFlowEntry() {
344 IFlowEntry flowEntry = (IFlowEntry) conn.getFramedGraph().addVertex(null, IFlowEntry.class);
345 if (flowEntry != null) {
346 flowEntry.setType("flow_entry");
347 }
348 return flowEntry;
349 }
350
351
yoshitomob292c622013-11-23 14:35:58 -0800352 public IIpv4Address newIpv4Address() {
353 return newVertex("ipv4Address", IIpv4Address.class);
354 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800355
yoshitomob292c622013-11-23 14:35:58 -0800356 private <T extends IBaseObject> T newVertex(String type, Class<T> vertexType) {
yoshi9247b812013-11-27 11:26:14 -0800357 T newVertex = (T) conn.getFramedGraph().addVertex(null, vertexType);
yoshitomob292c622013-11-23 14:35:58 -0800358 if (newVertex != null) {
359 newVertex.setType(type);
360 }
361 return newVertex;
362 }
yoshic455c012013-11-27 10:35:50 -0800363
yoshitomob292c622013-11-23 14:35:58 -0800364 public IIpv4Address searchIpv4Address(int intIpv4Address) {
365 return searchForVertex("ipv4_address", intIpv4Address, IIpv4Address.class);
366 }
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800367
368
yoshitomob292c622013-11-23 14:35:58 -0800369 public IIpv4Address ensureIpv4Address(int intIpv4Address) {
370 IIpv4Address ipv4Vertex = searchIpv4Address(intIpv4Address);
371 if (ipv4Vertex == null) {
372 ipv4Vertex = newIpv4Address();
373 ipv4Vertex.setIpv4Address(intIpv4Address);
374 }
375 return ipv4Vertex;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800376 }
yoshitomob292c622013-11-23 14:35:58 -0800377
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800378
yoshitomob292c622013-11-23 14:35:58 -0800379 private <T> T searchForVertex(String propertyName, Object propertyValue, Class<T> vertexType) {
380 if (conn.getFramedGraph() != null) {
381 Iterator<T> it = conn.getFramedGraph().getVertices(propertyName, propertyValue, vertexType).iterator();
382 if (it.hasNext()) {
383 return it.next();
384 }
385 }
386 return null;
387 }
388
389 public void removeIpv4Address(IIpv4Address ipv4Address) {
390 conn.getFramedGraph().removeVertex(ipv4Address.asVertex());
391 }
yoshi0451f282013-11-22 15:48:55 -0800392
yoshib3c83c12013-12-03 00:58:13 -0800393 /**
394 * Get the instance of GraphDBConnection assigned to this class.
395 */
yoshi7594aef2013-11-27 09:27:07 -0800396 @Override
397 public IDBConnection getDBConnection() {
398 return conn;
Yuta HIGUCHI9e580b62014-01-02 12:02:22 -0800399 }
400
yoshib3c83c12013-12-03 00:58:13 -0800401 @Override
402 public void commit() {
403 conn.commit();
404 }
405
406 @Override
407 public void rollback() {
408 conn.rollback();
409 }
410
411 @Override
412 public void close() {
413 conn.close();
414 }
Toshio Koide3f233542014-01-07 14:19:09 -0800415
416 @Override
Toshio Koide3fcebc12014-01-09 22:40:11 -0800417 public void setVertexProperties(Vertex vertex, Map<String, Object> map) {
Toshio Koide3f233542014-01-07 14:19:09 -0800418 log.debug("setProperties start: size {}", map.size());
Toshio Koide3fcebc12014-01-09 22:40:11 -0800419 RamCloudVertex v = (RamCloudVertex) vertex;
Toshio Koide3f233542014-01-07 14:19:09 -0800420 v.setProperties(map);
421 log.debug("setProperties end: size {}, id {}", map.size(), v.getId());
422 }
Toshio Koide3fcebc12014-01-09 22:40:11 -0800423
424 public String toString() {
425 StringBuilder sb = new StringBuilder();
426 for(ISwitchObject sw: getAllSwitches()) {
427 sb.append("sw: " + sw.getDPID() + "\n");
428 for(IPortObject port: sw.getPorts()) {
429 sb.append(" port: " + port.getPortId() + "\n");
430 }
431 }
432 return sb.toString();
433 }
yoshi0451f282013-11-22 15:48:55 -0800434}