blob: 1c40b787fc8667a6d34b4d51d2d44d8c7765a91d [file] [log] [blame]
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -07001package net.onrc.onos.ofcontroller.flowmanager;
2
3import java.io.PrintWriter;
4import java.io.StringWriter;
5import java.util.ArrayList;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -07006import java.util.LinkedList;
7import java.util.List;
Pavlin Radoslavov16b761d2014-01-08 09:47:14 -08008import java.util.Map;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -07009
Pavlin Radoslavov16b761d2014-01-08 09:47:14 -080010import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070011import net.floodlightcontroller.util.MACAddress;
12
13import net.onrc.onos.graph.GraphDBOperation;
14
15import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
16import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
17import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
19import net.onrc.onos.ofcontroller.util.*;
20
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24/**
25 * Class for performing Flow-related operations on the Database.
26 */
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -080027public class FlowDatabaseOperation {
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -080028 static private boolean enableOnrc2014MeasurementsFlows = true;
29
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070030 private final static Logger log = LoggerFactory.getLogger(FlowDatabaseOperation.class);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070031
32 /**
33 * Add a flow.
34 *
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070035 * @param dbHandler the Graph Database handler to use.
36 * @param flowPath the Flow Path to install.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070037 * @return true on success, otherwise false.
38 */
Pavlin Radoslavov051abb42013-12-05 17:24:50 -080039 static boolean addFlow(GraphDBOperation dbHandler, FlowPath flowPath) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070040 IFlowPath flowObj = null;
41 boolean found = false;
42 try {
43 if ((flowObj = dbHandler.searchFlowPath(flowPath.flowId())) != null) {
44 found = true;
45 } else {
46 flowObj = dbHandler.newFlowPath();
47 }
48 } catch (Exception e) {
49 dbHandler.rollback();
50
51 StringWriter sw = new StringWriter();
52 e.printStackTrace(new PrintWriter(sw));
53 String stacktrace = sw.toString();
54
55 log.error(":addFlow FlowId:{} failed: {}",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -080056 flowPath.flowId(),
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070057 stacktrace);
58 return false;
59 }
60 if (flowObj == null) {
61 log.error(":addFlow FlowId:{} failed: Flow object not created",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -080062 flowPath.flowId());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070063 dbHandler.rollback();
64 return false;
65 }
66
67 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080068 // Remove the old Flow Entries
69 //
70 if (found) {
71 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
72 LinkedList<IFlowEntry> deleteFlowEntries =
73 new LinkedList<IFlowEntry>();
74 for (IFlowEntry flowEntryObj : flowEntries)
75 deleteFlowEntries.add(flowEntryObj);
76 for (IFlowEntry flowEntryObj : deleteFlowEntries) {
77 flowObj.removeFlowEntry(flowEntryObj);
78 dbHandler.removeFlowEntry(flowEntryObj);
79 }
80 }
81
82 //
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070083 // Set the Flow key:
84 // - flowId
85 //
86 flowObj.setFlowId(flowPath.flowId().toString());
87 flowObj.setType("flow");
88
89 //
90 // Set the Flow attributes:
91 // - flowPath.installerId()
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -070092 // - flowPath.flowPathType()
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -070093 // - flowPath.flowPathUserState()
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070094 // - flowPath.flowPathFlags()
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -080095 // - flowPath.idleTimeout()
96 // - flowPath.hardTimeout()
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070097 // - flowPath.dataPath().srcPort()
98 // - flowPath.dataPath().dstPort()
99 // - flowPath.matchSrcMac()
100 // - flowPath.matchDstMac()
101 // - flowPath.matchEthernetFrameType()
102 // - flowPath.matchVlanId()
103 // - flowPath.matchVlanPriority()
104 // - flowPath.matchSrcIPv4Net()
105 // - flowPath.matchDstIPv4Net()
106 // - flowPath.matchIpProto()
107 // - flowPath.matchIpToS()
108 // - flowPath.matchSrcTcpUdpPort()
109 // - flowPath.matchDstTcpUdpPort()
110 // - flowPath.flowEntryActions()
111 //
112 flowObj.setInstallerId(flowPath.installerId().toString());
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700113 flowObj.setFlowPathType(flowPath.flowPathType().toString());
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700114 flowObj.setFlowPathUserState(flowPath.flowPathUserState().toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700115 flowObj.setFlowPathFlags(flowPath.flowPathFlags().flags());
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800116 flowObj.setIdleTimeout(flowPath.idleTimeout());
117 flowObj.setHardTimeout(flowPath.hardTimeout());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700118 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
119 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
120 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
121 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
122 if (flowPath.flowEntryMatch().matchSrcMac()) {
123 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
124 }
125 if (flowPath.flowEntryMatch().matchDstMac()) {
126 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
127 }
128 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
129 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
130 }
131 if (flowPath.flowEntryMatch().matchVlanId()) {
132 flowObj.setMatchVlanId(flowPath.flowEntryMatch().vlanId());
133 }
134 if (flowPath.flowEntryMatch().matchVlanPriority()) {
135 flowObj.setMatchVlanPriority(flowPath.flowEntryMatch().vlanPriority());
136 }
137 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
138 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
139 }
140 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
141 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
142 }
143 if (flowPath.flowEntryMatch().matchIpProto()) {
144 flowObj.setMatchIpProto(flowPath.flowEntryMatch().ipProto());
145 }
146 if (flowPath.flowEntryMatch().matchIpToS()) {
147 flowObj.setMatchIpToS(flowPath.flowEntryMatch().ipToS());
148 }
149 if (flowPath.flowEntryMatch().matchSrcTcpUdpPort()) {
150 flowObj.setMatchSrcTcpUdpPort(flowPath.flowEntryMatch().srcTcpUdpPort());
151 }
152 if (flowPath.flowEntryMatch().matchDstTcpUdpPort()) {
153 flowObj.setMatchDstTcpUdpPort(flowPath.flowEntryMatch().dstTcpUdpPort());
154 }
155 if (! flowPath.flowEntryActions().actions().isEmpty()) {
156 flowObj.setActions(flowPath.flowEntryActions().toString());
157 }
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700158 flowObj.setDataPathSummary(flowPath.dataPath().dataPathSummary());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700159
160 if (found)
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700161 flowObj.setFlowPathUserState("FP_USER_MODIFY");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700162 else
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700163 flowObj.setFlowPathUserState("FP_USER_ADD");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700164
165 // Flow edges:
166 // HeadFE
167
168
169 //
170 // Flow Entries:
171 // flowPath.dataPath().flowEntries()
172 //
173 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -0800174 if (! enableOnrc2014MeasurementsFlows) {
175 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE)
176 continue; // Skip: all Flow Entries were deleted earlier
177 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800178
Pavlin Radoslavov67bf7622013-12-04 12:28:23 -0800179 if (addFlowEntry(dbHandler, flowObj, flowEntry) == null) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700180 dbHandler.rollback();
181 return false;
182 }
183 }
184 dbHandler.commit();
185
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700186 return true;
187 }
188
189 /**
190 * Add a flow entry to the Network MAP.
191 *
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700192 * @param dbHandler the Graph Database handler to use.
193 * @param flowObj the corresponding Flow Path object for the Flow Entry.
194 * @param flowEntry the Flow Entry to install.
195 * @return the added Flow Entry object on success, otherwise null.
196 */
Pavlin Radoslavov67bf7622013-12-04 12:28:23 -0800197 static IFlowEntry addFlowEntry(GraphDBOperation dbHandler,
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700198 IFlowPath flowObj,
199 FlowEntry flowEntry) {
200 // Flow edges
201 // HeadFE (TODO)
202
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700203 IFlowEntry flowEntryObj = null;
204 boolean found = false;
205 try {
206 if ((flowEntryObj =
207 dbHandler.searchFlowEntry(flowEntry.flowEntryId())) != null) {
208 found = true;
209 } else {
210 flowEntryObj = dbHandler.newFlowEntry();
211 }
212 } catch (Exception e) {
213 log.error(":addFlow FlowEntryId:{} failed",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800214 flowEntry.flowEntryId());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700215 return null;
216 }
217 if (flowEntryObj == null) {
218 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800219 flowEntry.flowEntryId());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700220 return null;
221 }
222
223 //
224 // Set the Flow Entry key:
225 // - flowEntry.flowEntryId()
226 //
227 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
228 flowEntryObj.setType("flow_entry");
229
230 //
231 // Set the Flow Entry Edges and attributes:
232 // - Switch edge
233 // - InPort edge
234 // - OutPort edge
235 //
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800236 // - flowEntry.idleTimeout()
237 // - flowEntry.hardTimeout()
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700238 // - flowEntry.dpid()
239 // - flowEntry.flowEntryUserState()
240 // - flowEntry.flowEntrySwitchState()
241 // - flowEntry.flowEntryErrorState()
242 // - flowEntry.matchInPort()
243 // - flowEntry.matchSrcMac()
244 // - flowEntry.matchDstMac()
245 // - flowEntry.matchEthernetFrameType()
246 // - flowEntry.matchVlanId()
247 // - flowEntry.matchVlanPriority()
248 // - flowEntry.matchSrcIPv4Net()
249 // - flowEntry.matchDstIPv4Net()
250 // - flowEntry.matchIpProto()
251 // - flowEntry.matchIpToS()
252 // - flowEntry.matchSrcTcpUdpPort()
253 // - flowEntry.matchDstTcpUdpPort()
254 // - flowEntry.actionOutputPort()
255 // - flowEntry.actions()
256 //
257 ISwitchObject sw = dbHandler.searchSwitch(flowEntry.dpid().toString());
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800258 flowEntryObj.setIdleTimeout(flowEntry.idleTimeout());
259 flowEntryObj.setHardTimeout(flowEntry.hardTimeout());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700260 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
261 flowEntryObj.setSwitch(sw);
262 if (flowEntry.flowEntryMatch().matchInPort()) {
Jonathan Hart5b3ad192013-12-06 17:34:46 -0800263 IPortObject inport =
264 dbHandler.searchPort(flowEntry.dpid().toString(),
265 flowEntry.flowEntryMatch().inPort().value());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700266 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
Jonathan Hart5b3ad192013-12-06 17:34:46 -0800267 flowEntryObj.setInPort(inport);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700268 }
269 if (flowEntry.flowEntryMatch().matchSrcMac()) {
270 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
271 }
272 if (flowEntry.flowEntryMatch().matchDstMac()) {
273 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
274 }
275 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
276 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
277 }
278 if (flowEntry.flowEntryMatch().matchVlanId()) {
279 flowEntryObj.setMatchVlanId(flowEntry.flowEntryMatch().vlanId());
280 }
281 if (flowEntry.flowEntryMatch().matchVlanPriority()) {
282 flowEntryObj.setMatchVlanPriority(flowEntry.flowEntryMatch().vlanPriority());
283 }
284 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
285 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
286 }
287 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
288 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
289 }
290 if (flowEntry.flowEntryMatch().matchIpProto()) {
291 flowEntryObj.setMatchIpProto(flowEntry.flowEntryMatch().ipProto());
292 }
293 if (flowEntry.flowEntryMatch().matchIpToS()) {
294 flowEntryObj.setMatchIpToS(flowEntry.flowEntryMatch().ipToS());
295 }
296 if (flowEntry.flowEntryMatch().matchSrcTcpUdpPort()) {
297 flowEntryObj.setMatchSrcTcpUdpPort(flowEntry.flowEntryMatch().srcTcpUdpPort());
298 }
299 if (flowEntry.flowEntryMatch().matchDstTcpUdpPort()) {
300 flowEntryObj.setMatchDstTcpUdpPort(flowEntry.flowEntryMatch().dstTcpUdpPort());
301 }
302
303 for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) {
304 if (fa.actionOutput() != null) {
Jonathan Hart5b3ad192013-12-06 17:34:46 -0800305 IPortObject outport =
306 dbHandler.searchPort(flowEntry.dpid().toString(),
307 fa.actionOutput().port().value());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700308 flowEntryObj.setActionOutputPort(fa.actionOutput().port().value());
Jonathan Hart5b3ad192013-12-06 17:34:46 -0800309 flowEntryObj.setOutPort(outport);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700310 }
311 }
312 if (! flowEntry.flowEntryActions().isEmpty()) {
313 flowEntryObj.setActions(flowEntry.flowEntryActions().toString());
314 }
315
316 // TODO: Hacks with hard-coded state names!
317 if (found)
318 flowEntryObj.setUserState("FE_USER_MODIFY");
319 else
320 flowEntryObj.setUserState("FE_USER_ADD");
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700321 flowEntryObj.setSwitchState(flowEntry.flowEntrySwitchState().toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700322 //
323 // TODO: Take care of the FlowEntryErrorState.
324 //
325
326 // Flow Entries edges:
327 // Flow
328 // NextFE (TODO)
329 if (! found) {
330 flowObj.addFlowEntry(flowEntryObj);
331 flowEntryObj.setFlow(flowObj);
332 }
333
334 return flowEntryObj;
335 }
336
337 /**
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700338 * Delete a flow entry from the Network MAP.
339 *
340 * @param dbHandler the Graph Database handler to use.
341 * @param flowObj the corresponding Flow Path object for the Flow Entry.
342 * @param flowEntry the Flow Entry to delete.
343 * @return true on success, otherwise false.
344 */
345 static boolean deleteFlowEntry(GraphDBOperation dbHandler,
346 IFlowPath flowObj,
347 FlowEntry flowEntry) {
348 IFlowEntry flowEntryObj = null;
349 try {
350 flowEntryObj = dbHandler.searchFlowEntry(flowEntry.flowEntryId());
351 } catch (Exception e) {
352 log.error(":deleteFlowEntry FlowEntryId:{} failed",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800353 flowEntry.flowEntryId());
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700354 return false;
355 }
356 //
357 // TODO: Don't print an error for now, because multiple controller
358 // instances might be deleting the same flow entry.
359 //
360 /*
361 if (flowEntryObj == null) {
362 log.error(":deleteFlowEntry FlowEntryId:{} failed: FlowEntry object not found",
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800363 flowEntry.flowEntryId());
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700364 return false;
365 }
366 */
367 if (flowEntryObj == null)
368 return true;
369
370 flowObj.removeFlowEntry(flowEntryObj);
371 dbHandler.removeFlowEntry(flowEntryObj);
372 return true;
373 }
374
375 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700376 * Delete all previously added flows.
377 *
378 * @param dbHandler the Graph Database handler to use.
379 * @return true on success, otherwise false.
380 */
381 static boolean deleteAllFlows(GraphDBOperation dbHandler) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700382 List<FlowId> allFlowIds = new LinkedList<FlowId>();
383
384 // Get all Flow IDs
385 Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
386 for (IFlowPath flowPathObj : allFlowPaths) {
387 if (flowPathObj == null)
388 continue;
389 String flowIdStr = flowPathObj.getFlowId();
390 if (flowIdStr == null)
391 continue;
392 FlowId flowId = new FlowId(flowIdStr);
393 allFlowIds.add(flowId);
394 }
395
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800396 // Delete all flows one-by-one
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700397 for (FlowId flowId : allFlowIds) {
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800398 deleteFlow(dbHandler, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700399 }
400
401 return true;
402 }
403
404 /**
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800405 * Delete a previously added flow.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700406 *
407 * @param dbHandler the Graph Database handler to use.
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800408 * @param flowId the Flow ID of the flow to delete.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700409 * @return true on success, otherwise false.
410 */
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800411 static boolean deleteFlow(GraphDBOperation dbHandler, FlowId flowId) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700412 IFlowPath flowObj = null;
413 try {
414 flowObj = dbHandler.searchFlowPath(flowId);
415 } catch (Exception e) {
416 // TODO: handle exceptions
417 dbHandler.rollback();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800418 log.error(":deleteFlow FlowId:{} failed", flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700419 return false;
420 }
421 if (flowObj == null) {
422 dbHandler.commit();
423 return true; // OK: No such flow
424 }
425
426 //
427 // Remove all Flow Entries
428 //
429 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
430 for (IFlowEntry flowEntryObj : flowEntries) {
431 flowObj.removeFlowEntry(flowEntryObj);
432 dbHandler.removeFlowEntry(flowEntryObj);
433 }
434 // Remove the Flow itself
435 dbHandler.removeFlowPath(flowObj);
436 dbHandler.commit();
437
438 return true;
439 }
440
441 /**
442 * Get a previously added flow.
443 *
444 * @param dbHandler the Graph Database handler to use.
445 * @param flowId the Flow ID of the flow to get.
446 * @return the Flow Path if found, otherwise null.
447 */
448 static FlowPath getFlow(GraphDBOperation dbHandler, FlowId flowId) {
449 IFlowPath flowObj = null;
450 try {
451 flowObj = dbHandler.searchFlowPath(flowId);
452 } catch (Exception e) {
453 // TODO: handle exceptions
454 dbHandler.rollback();
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800455 log.error(":getFlow FlowId:{} failed", flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700456 return null;
457 }
458 if (flowObj == null) {
459 dbHandler.commit();
460 return null; // Flow not found
461 }
462
463 //
464 // Extract the Flow state
465 //
466 FlowPath flowPath = extractFlowPath(flowObj);
467 dbHandler.commit();
468
469 return flowPath;
470 }
471
472 /**
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800473 * Get a previously added flow entry.
474 *
475 * @param dbHandler the Graph Database handler to use.
476 * @param flowEntryId the Flow Entry ID of the flow entry to get.
477 * @return the Flow Entry if found, otherwise null.
478 */
479 static FlowEntry getFlowEntry(GraphDBOperation dbHandler,
480 FlowEntryId flowEntryId) {
481 IFlowEntry flowEntryObj = null;
482 try {
483 flowEntryObj = dbHandler.searchFlowEntry(flowEntryId);
484 } catch (Exception e) {
485 // TODO: handle exceptions
486 dbHandler.rollback();
487 log.error(":getFlowEntry FlowEntryId:{} failed", flowEntryId);
488 return null;
489 }
490 if (flowEntryObj == null) {
491 dbHandler.commit();
492 return null; // Flow not found
493 }
494
495 //
496 // Extract the Flow Entry state
497 //
498 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
499 dbHandler.commit();
500
501 return flowEntry;
502 }
503
504 /**
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800505 * Get the source switch DPID of a previously added flow.
506 *
507 * @param dbHandler the Graph Database handler to use.
508 * @param flowId the Flow ID of the flow to get.
509 * @return the source switch DPID if found, otherwise null.
510 */
511 static Dpid getFlowSourceDpid(GraphDBOperation dbHandler, FlowId flowId) {
512 IFlowPath flowObj = null;
513 try {
514 flowObj = dbHandler.searchFlowPath(flowId);
515 } catch (Exception e) {
516 // TODO: handle exceptions
517 dbHandler.rollback();
518 log.error(":getFlowSourceDpid FlowId:{} failed", flowId);
519 return null;
520 }
521 if (flowObj == null) {
522 dbHandler.commit();
523 return null; // Flow not found
524 }
525
526 //
527 // Extract the Flow Source DPID
528 //
529 String srcSwitchStr = flowObj.getSrcSwitch();
530 if (srcSwitchStr == null) {
531 // TODO: A work-around, becauuse of some bogus database objects
532 dbHandler.commit();
533 return null;
534 }
535
536 Dpid dpid = new Dpid(srcSwitchStr);
537
538 dbHandler.commit();
539
540 return dpid;
541 }
542
543 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700544 * Get all installed flows by all installers.
545 *
546 * @param dbHandler the Graph Database handler to use.
547 * @return the Flow Paths if found, otherwise null.
548 */
549 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler) {
550 Iterable<IFlowPath> flowPathsObj = null;
551 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
552
553 try {
554 flowPathsObj = dbHandler.getAllFlowPaths();
555 } catch (Exception e) {
556 // TODO: handle exceptions
557 dbHandler.rollback();
558 log.error(":getAllFlowPaths failed");
559 return flowPaths;
560 }
561 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
562 dbHandler.commit();
563 return flowPaths; // No Flows found
564 }
565
566 for (IFlowPath flowObj : flowPathsObj) {
567 //
568 // Extract the Flow state
569 //
570 FlowPath flowPath = extractFlowPath(flowObj);
571 if (flowPath != null)
572 flowPaths.add(flowPath);
573 }
574
575 dbHandler.commit();
576
577 return flowPaths;
578 }
579
580 /**
Pavlin Radoslavov16b761d2014-01-08 09:47:14 -0800581 * Get all installed flows whose Source Switch is controlled by this
582 * instance.
583 *
584 * @param dbHandler the Graph Database handler to use.
585 * @param mySwitches the collection of the switches controlled by this
586 * instance.
587 * @return the Flow Paths if found, otherwise null.
588 */
589 static ArrayList<FlowPath> getAllMyFlows(GraphDBOperation dbHandler,
590 Map<Long, IOFSwitch> mySwitches) {
591 Iterable<IFlowPath> flowPathsObj = null;
592 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
593
594 try {
595 flowPathsObj = dbHandler.getAllFlowPaths();
596 } catch (Exception e) {
597 // TODO: handle exceptions
598 dbHandler.rollback();
599 log.error(":getAllMyFlowPaths failed");
600 return flowPaths;
601 }
602 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
603 dbHandler.commit();
604 return flowPaths; // No Flows found
605 }
606
607 for (IFlowPath flowObj : flowPathsObj) {
608 //
609 // Extract the Source Switch DPID and ignore if the switch
610 // is not controlled by this instance.
611 //
612 String srcSwitchStr = flowObj.getSrcSwitch();
613 if (srcSwitchStr == null) {
614 // TODO: A work-around, becauuse of some bogus database objects
615 continue;
616 }
617 Dpid dpid = new Dpid(srcSwitchStr);
618 if (mySwitches.get(dpid.value()) == null)
619 continue;
620
621 //
622 // Extract the Flow state
623 //
624 FlowPath flowPath = extractFlowPath(flowObj);
625 if (flowPath != null)
626 flowPaths.add(flowPath);
627 }
628
629 dbHandler.commit();
630
631 return flowPaths;
632 }
633
634 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700635 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
636 *
637 * @param flowObj the object to extract the Flow Path State from.
638 * @return the extracted Flow Path State.
639 */
640 private static FlowPath extractFlowPath(IFlowPath flowObj) {
641 //
642 // Extract the Flow state
643 //
644 String flowIdStr = flowObj.getFlowId();
645 String installerIdStr = flowObj.getInstallerId();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700646 String flowPathType = flowObj.getFlowPathType();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700647 String flowPathUserState = flowObj.getFlowPathUserState();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700648 Long flowPathFlags = flowObj.getFlowPathFlags();
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800649 Integer idleTimeout = flowObj.getIdleTimeout();
650 Integer hardTimeout = flowObj.getHardTimeout();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700651 String srcSwitchStr = flowObj.getSrcSwitch();
652 Short srcPortShort = flowObj.getSrcPort();
653 String dstSwitchStr = flowObj.getDstSwitch();
654 Short dstPortShort = flowObj.getDstPort();
655
656 if ((flowIdStr == null) ||
657 (installerIdStr == null) ||
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700658 (flowPathType == null) ||
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700659 (flowPathUserState == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700660 (flowPathFlags == null) ||
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800661 (idleTimeout == null) ||
662 (hardTimeout == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700663 (srcSwitchStr == null) ||
664 (srcPortShort == null) ||
665 (dstSwitchStr == null) ||
666 (dstPortShort == null)) {
667 // TODO: A work-around, becauuse of some bogus database objects
668 return null;
669 }
670
671 FlowPath flowPath = new FlowPath();
672 flowPath.setFlowId(new FlowId(flowIdStr));
673 flowPath.setInstallerId(new CallerId(installerIdStr));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700674 flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700675 flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState));
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700676 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800677 flowPath.setIdleTimeout(idleTimeout);
678 flowPath.setHardTimeout(hardTimeout);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700679 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
680 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
681 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
682 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
683 //
684 // Extract the match conditions common for all Flow Entries
685 //
686 {
687 FlowEntryMatch match = new FlowEntryMatch();
688 String matchSrcMac = flowObj.getMatchSrcMac();
689 if (matchSrcMac != null)
690 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
691 String matchDstMac = flowObj.getMatchDstMac();
692 if (matchDstMac != null)
693 match.enableDstMac(MACAddress.valueOf(matchDstMac));
694 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
695 if (matchEthernetFrameType != null)
696 match.enableEthernetFrameType(matchEthernetFrameType);
697 Short matchVlanId = flowObj.getMatchVlanId();
698 if (matchVlanId != null)
699 match.enableVlanId(matchVlanId);
700 Byte matchVlanPriority = flowObj.getMatchVlanPriority();
701 if (matchVlanPriority != null)
702 match.enableVlanPriority(matchVlanPriority);
703 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
704 if (matchSrcIPv4Net != null)
705 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
706 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
707 if (matchDstIPv4Net != null)
708 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
709 Byte matchIpProto = flowObj.getMatchIpProto();
710 if (matchIpProto != null)
711 match.enableIpProto(matchIpProto);
712 Byte matchIpToS = flowObj.getMatchIpToS();
713 if (matchIpToS != null)
714 match.enableIpToS(matchIpToS);
715 Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort();
716 if (matchSrcTcpUdpPort != null)
717 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
718 Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort();
719 if (matchDstTcpUdpPort != null)
720 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
721
722 flowPath.setFlowEntryMatch(match);
723 }
724 //
725 // Extract the actions for the first Flow Entry
726 //
727 {
728 String actionsStr = flowObj.getActions();
729 if (actionsStr != null) {
730 FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr);
731 flowPath.setFlowEntryActions(flowEntryActions);
732 }
733 }
734
735 //
736 // Extract all Flow Entries
737 //
738 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
739 for (IFlowEntry flowEntryObj : flowEntries) {
740 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
741 if (flowEntry == null)
742 continue;
743 flowPath.dataPath().flowEntries().add(flowEntry);
744 }
745
746 return flowPath;
747 }
748
749 /**
750 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
751 *
752 * @param flowEntryObj the object to extract the Flow Entry State from.
753 * @return the extracted Flow Entry State.
754 */
Brian O'Connora8e49802013-10-30 20:49:59 -0700755 public static FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
Pavlin Radoslavovcf87e532013-12-13 18:17:00 -0800756 IFlowPath flowObj = flowEntryObj.getFlow();
757 if (flowObj == null)
758 return null;
759
760 String flowIdStr = flowObj.getFlowId();
761 //
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700762 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800763 Integer idleTimeout = flowEntryObj.getIdleTimeout();
764 Integer hardTimeout = flowEntryObj.getHardTimeout();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700765 String switchDpidStr = flowEntryObj.getSwitchDpid();
766 String userState = flowEntryObj.getUserState();
767 String switchState = flowEntryObj.getSwitchState();
768
Pavlin Radoslavovcf87e532013-12-13 18:17:00 -0800769 if ((flowIdStr == null) ||
770 (flowEntryIdStr == null) ||
Pavlin Radoslavov5139c0b2013-12-09 18:04:53 -0800771 (idleTimeout == null) ||
772 (hardTimeout == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700773 (switchDpidStr == null) ||
774 (userState == null) ||
775 (switchState == null)) {
Brian O'Connora8e49802013-10-30 20:49:59 -0700776 // TODO: A work-around, because of some bogus database objects
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700777 return null;
778 }
779
780 FlowEntry flowEntry = new FlowEntry();
781 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
Pavlin Radoslavovcf87e532013-12-13 18:17:00 -0800782 flowEntry.setFlowId(new FlowId(flowIdStr));
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700783 flowEntry.setDpid(new Dpid(switchDpidStr));
Pavlin Radoslavovce93a032013-12-13 11:03:02 -0800784 flowEntry.setIdleTimeout(idleTimeout);
785 flowEntry.setHardTimeout(hardTimeout);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700786
787 //
788 // Extract the match conditions
789 //
790 FlowEntryMatch match = new FlowEntryMatch();
791 Short matchInPort = flowEntryObj.getMatchInPort();
792 if (matchInPort != null)
793 match.enableInPort(new Port(matchInPort));
794 String matchSrcMac = flowEntryObj.getMatchSrcMac();
795 if (matchSrcMac != null)
796 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
797 String matchDstMac = flowEntryObj.getMatchDstMac();
798 if (matchDstMac != null)
799 match.enableDstMac(MACAddress.valueOf(matchDstMac));
800 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
801 if (matchEthernetFrameType != null)
802 match.enableEthernetFrameType(matchEthernetFrameType);
803 Short matchVlanId = flowEntryObj.getMatchVlanId();
804 if (matchVlanId != null)
805 match.enableVlanId(matchVlanId);
806 Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority();
807 if (matchVlanPriority != null)
808 match.enableVlanPriority(matchVlanPriority);
809 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
810 if (matchSrcIPv4Net != null)
811 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
812 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
813 if (matchDstIPv4Net != null)
814 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
815 Byte matchIpProto = flowEntryObj.getMatchIpProto();
816 if (matchIpProto != null)
817 match.enableIpProto(matchIpProto);
818 Byte matchIpToS = flowEntryObj.getMatchIpToS();
819 if (matchIpToS != null)
820 match.enableIpToS(matchIpToS);
821 Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort();
822 if (matchSrcTcpUdpPort != null)
823 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
824 Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort();
825 if (matchDstTcpUdpPort != null)
826 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
827 flowEntry.setFlowEntryMatch(match);
828
829 //
830 // Extract the actions
831 //
832 FlowEntryActions actions = new FlowEntryActions();
833 String actionsStr = flowEntryObj.getActions();
834 if (actionsStr != null)
835 actions = new FlowEntryActions(actionsStr);
836 flowEntry.setFlowEntryActions(actions);
837 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
838 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
839 //
840 // TODO: Take care of FlowEntryErrorState.
841 //
842 return flowEntry;
843 }
844}