blob: 73c4e513d90ecfc5bd951627fff3eff0dd1948f5 [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;
6import java.util.Collections;
7import java.util.Comparator;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.concurrent.ConcurrentLinkedQueue;
11
12import net.floodlightcontroller.util.MACAddress;
13
14import net.onrc.onos.graph.GraphDBOperation;
15
16import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
17import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
19import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
20import net.onrc.onos.ofcontroller.util.*;
21
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * Class for performing Flow-related operations on the Database.
27 */
28class FlowDatabaseOperation {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070029 private final static Logger log = LoggerFactory.getLogger(FlowDatabaseOperation.class);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070030
31 /**
32 * Add a flow.
33 *
34 * @param flowManager the Flow Manager to use.
35 * @param dbHandler the Graph Database handler to use.
36 * @param flowPath the Flow Path to install.
37 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070038 * @return true on success, otherwise false.
39 */
40 static boolean addFlow(FlowManager flowManager,
41 GraphDBOperation dbHandler,
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -070042 FlowPath flowPath, FlowId flowId) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070043 IFlowPath flowObj = null;
44 boolean found = false;
45 try {
46 if ((flowObj = dbHandler.searchFlowPath(flowPath.flowId())) != null) {
47 found = true;
48 } else {
49 flowObj = dbHandler.newFlowPath();
50 }
51 } catch (Exception e) {
52 dbHandler.rollback();
53
54 StringWriter sw = new StringWriter();
55 e.printStackTrace(new PrintWriter(sw));
56 String stacktrace = sw.toString();
57
58 log.error(":addFlow FlowId:{} failed: {}",
59 flowPath.flowId().toString(),
60 stacktrace);
61 return false;
62 }
63 if (flowObj == null) {
64 log.error(":addFlow FlowId:{} failed: Flow object not created",
65 flowPath.flowId().toString());
66 dbHandler.rollback();
67 return false;
68 }
69
70 //
71 // Set the Flow key:
72 // - flowId
73 //
74 flowObj.setFlowId(flowPath.flowId().toString());
75 flowObj.setType("flow");
76
77 //
78 // Set the Flow attributes:
79 // - flowPath.installerId()
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -070080 // - flowPath.flowPathType()
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -070081 // - flowPath.flowPathUserState()
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070082 // - flowPath.flowPathFlags()
83 // - flowPath.dataPath().srcPort()
84 // - flowPath.dataPath().dstPort()
85 // - flowPath.matchSrcMac()
86 // - flowPath.matchDstMac()
87 // - flowPath.matchEthernetFrameType()
88 // - flowPath.matchVlanId()
89 // - flowPath.matchVlanPriority()
90 // - flowPath.matchSrcIPv4Net()
91 // - flowPath.matchDstIPv4Net()
92 // - flowPath.matchIpProto()
93 // - flowPath.matchIpToS()
94 // - flowPath.matchSrcTcpUdpPort()
95 // - flowPath.matchDstTcpUdpPort()
96 // - flowPath.flowEntryActions()
97 //
98 flowObj.setInstallerId(flowPath.installerId().toString());
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -070099 flowObj.setFlowPathType(flowPath.flowPathType().toString());
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700100 flowObj.setFlowPathUserState(flowPath.flowPathUserState().toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700101 flowObj.setFlowPathFlags(flowPath.flowPathFlags().flags());
102 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
103 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
104 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
105 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
106 if (flowPath.flowEntryMatch().matchSrcMac()) {
107 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
108 }
109 if (flowPath.flowEntryMatch().matchDstMac()) {
110 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
111 }
112 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
113 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
114 }
115 if (flowPath.flowEntryMatch().matchVlanId()) {
116 flowObj.setMatchVlanId(flowPath.flowEntryMatch().vlanId());
117 }
118 if (flowPath.flowEntryMatch().matchVlanPriority()) {
119 flowObj.setMatchVlanPriority(flowPath.flowEntryMatch().vlanPriority());
120 }
121 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
122 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
123 }
124 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
125 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
126 }
127 if (flowPath.flowEntryMatch().matchIpProto()) {
128 flowObj.setMatchIpProto(flowPath.flowEntryMatch().ipProto());
129 }
130 if (flowPath.flowEntryMatch().matchIpToS()) {
131 flowObj.setMatchIpToS(flowPath.flowEntryMatch().ipToS());
132 }
133 if (flowPath.flowEntryMatch().matchSrcTcpUdpPort()) {
134 flowObj.setMatchSrcTcpUdpPort(flowPath.flowEntryMatch().srcTcpUdpPort());
135 }
136 if (flowPath.flowEntryMatch().matchDstTcpUdpPort()) {
137 flowObj.setMatchDstTcpUdpPort(flowPath.flowEntryMatch().dstTcpUdpPort());
138 }
139 if (! flowPath.flowEntryActions().actions().isEmpty()) {
140 flowObj.setActions(flowPath.flowEntryActions().toString());
141 }
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700142 flowObj.setDataPathSummary(flowPath.dataPath().dataPathSummary());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700143
144 if (found)
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700145 flowObj.setFlowPathUserState("FP_USER_MODIFY");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700146 else
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700147 flowObj.setFlowPathUserState("FP_USER_ADD");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700148
149 // Flow edges:
150 // HeadFE
151
152
153 //
154 // Flow Entries:
155 // flowPath.dataPath().flowEntries()
156 //
157 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
158 if (addFlowEntry(flowManager, dbHandler, flowObj, flowEntry) == null) {
159 dbHandler.rollback();
160 return false;
161 }
162 }
163 dbHandler.commit();
164
165 //
166 // TODO: We need a proper Flow ID allocation mechanism.
167 //
168 flowId.setValue(flowPath.flowId().value());
169
170 return true;
171 }
172
173 /**
174 * Add a flow entry to the Network MAP.
175 *
176 * @param flowManager the Flow Manager to use.
177 * @param dbHandler the Graph Database handler to use.
178 * @param flowObj the corresponding Flow Path object for the Flow Entry.
179 * @param flowEntry the Flow Entry to install.
180 * @return the added Flow Entry object on success, otherwise null.
181 */
182 static IFlowEntry addFlowEntry(FlowManager flowManager,
183 GraphDBOperation dbHandler,
184 IFlowPath flowObj,
185 FlowEntry flowEntry) {
186 // Flow edges
187 // HeadFE (TODO)
188
189 //
190 // Assign the FlowEntry ID.
191 //
192 if ((flowEntry.flowEntryId() == null) ||
193 (flowEntry.flowEntryId().value() == 0)) {
194 long id = flowManager.getNextFlowEntryId();
195 flowEntry.setFlowEntryId(new FlowEntryId(id));
196 }
197
198 IFlowEntry flowEntryObj = null;
199 boolean found = false;
200 try {
201 if ((flowEntryObj =
202 dbHandler.searchFlowEntry(flowEntry.flowEntryId())) != null) {
203 found = true;
204 } else {
205 flowEntryObj = dbHandler.newFlowEntry();
206 }
207 } catch (Exception e) {
208 log.error(":addFlow FlowEntryId:{} failed",
209 flowEntry.flowEntryId().toString());
210 return null;
211 }
212 if (flowEntryObj == null) {
213 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
214 flowEntry.flowEntryId().toString());
215 return null;
216 }
217
218 //
219 // Set the Flow Entry key:
220 // - flowEntry.flowEntryId()
221 //
222 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
223 flowEntryObj.setType("flow_entry");
224
225 //
226 // Set the Flow Entry Edges and attributes:
227 // - Switch edge
228 // - InPort edge
229 // - OutPort edge
230 //
231 // - flowEntry.dpid()
232 // - flowEntry.flowEntryUserState()
233 // - flowEntry.flowEntrySwitchState()
234 // - flowEntry.flowEntryErrorState()
235 // - flowEntry.matchInPort()
236 // - flowEntry.matchSrcMac()
237 // - flowEntry.matchDstMac()
238 // - flowEntry.matchEthernetFrameType()
239 // - flowEntry.matchVlanId()
240 // - flowEntry.matchVlanPriority()
241 // - flowEntry.matchSrcIPv4Net()
242 // - flowEntry.matchDstIPv4Net()
243 // - flowEntry.matchIpProto()
244 // - flowEntry.matchIpToS()
245 // - flowEntry.matchSrcTcpUdpPort()
246 // - flowEntry.matchDstTcpUdpPort()
247 // - flowEntry.actionOutputPort()
248 // - flowEntry.actions()
249 //
250 ISwitchObject sw = dbHandler.searchSwitch(flowEntry.dpid().toString());
251 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
252 flowEntryObj.setSwitch(sw);
253 if (flowEntry.flowEntryMatch().matchInPort()) {
254 IPortObject inport =
255 dbHandler.searchPort(flowEntry.dpid().toString(),
256 flowEntry.flowEntryMatch().inPort().value());
257 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
258 flowEntryObj.setInPort(inport);
259 }
260 if (flowEntry.flowEntryMatch().matchSrcMac()) {
261 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
262 }
263 if (flowEntry.flowEntryMatch().matchDstMac()) {
264 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
265 }
266 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
267 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
268 }
269 if (flowEntry.flowEntryMatch().matchVlanId()) {
270 flowEntryObj.setMatchVlanId(flowEntry.flowEntryMatch().vlanId());
271 }
272 if (flowEntry.flowEntryMatch().matchVlanPriority()) {
273 flowEntryObj.setMatchVlanPriority(flowEntry.flowEntryMatch().vlanPriority());
274 }
275 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
276 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
277 }
278 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
279 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
280 }
281 if (flowEntry.flowEntryMatch().matchIpProto()) {
282 flowEntryObj.setMatchIpProto(flowEntry.flowEntryMatch().ipProto());
283 }
284 if (flowEntry.flowEntryMatch().matchIpToS()) {
285 flowEntryObj.setMatchIpToS(flowEntry.flowEntryMatch().ipToS());
286 }
287 if (flowEntry.flowEntryMatch().matchSrcTcpUdpPort()) {
288 flowEntryObj.setMatchSrcTcpUdpPort(flowEntry.flowEntryMatch().srcTcpUdpPort());
289 }
290 if (flowEntry.flowEntryMatch().matchDstTcpUdpPort()) {
291 flowEntryObj.setMatchDstTcpUdpPort(flowEntry.flowEntryMatch().dstTcpUdpPort());
292 }
293
294 for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) {
295 if (fa.actionOutput() != null) {
296 IPortObject outport =
297 dbHandler.searchPort(flowEntry.dpid().toString(),
298 fa.actionOutput().port().value());
299 flowEntryObj.setActionOutputPort(fa.actionOutput().port().value());
300 flowEntryObj.setOutPort(outport);
301 }
302 }
303 if (! flowEntry.flowEntryActions().isEmpty()) {
304 flowEntryObj.setActions(flowEntry.flowEntryActions().toString());
305 }
306
307 // TODO: Hacks with hard-coded state names!
308 if (found)
309 flowEntryObj.setUserState("FE_USER_MODIFY");
310 else
311 flowEntryObj.setUserState("FE_USER_ADD");
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700312 flowEntryObj.setSwitchState(flowEntry.flowEntrySwitchState().toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700313 //
314 // TODO: Take care of the FlowEntryErrorState.
315 //
316
317 // Flow Entries edges:
318 // Flow
319 // NextFE (TODO)
320 if (! found) {
321 flowObj.addFlowEntry(flowEntryObj);
322 flowEntryObj.setFlow(flowObj);
323 }
324
325 return flowEntryObj;
326 }
327
328 /**
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700329 * Delete a flow entry from the Network MAP.
330 *
331 * @param dbHandler the Graph Database handler to use.
332 * @param flowObj the corresponding Flow Path object for the Flow Entry.
333 * @param flowEntry the Flow Entry to delete.
334 * @return true on success, otherwise false.
335 */
336 static boolean deleteFlowEntry(GraphDBOperation dbHandler,
337 IFlowPath flowObj,
338 FlowEntry flowEntry) {
339 IFlowEntry flowEntryObj = null;
340 try {
341 flowEntryObj = dbHandler.searchFlowEntry(flowEntry.flowEntryId());
342 } catch (Exception e) {
343 log.error(":deleteFlowEntry FlowEntryId:{} failed",
344 flowEntry.flowEntryId().toString());
345 return false;
346 }
347 //
348 // TODO: Don't print an error for now, because multiple controller
349 // instances might be deleting the same flow entry.
350 //
351 /*
352 if (flowEntryObj == null) {
353 log.error(":deleteFlowEntry FlowEntryId:{} failed: FlowEntry object not found",
354 flowEntry.flowEntryId().toString());
355 return false;
356 }
357 */
358 if (flowEntryObj == null)
359 return true;
360
361 flowObj.removeFlowEntry(flowEntryObj);
362 dbHandler.removeFlowEntry(flowEntryObj);
363 return true;
364 }
365
366 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700367 * Delete all previously added flows.
368 *
369 * @param dbHandler the Graph Database handler to use.
370 * @return true on success, otherwise false.
371 */
372 static boolean deleteAllFlows(GraphDBOperation dbHandler) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700373 List<FlowId> allFlowIds = new LinkedList<FlowId>();
374
375 // Get all Flow IDs
376 Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
377 for (IFlowPath flowPathObj : allFlowPaths) {
378 if (flowPathObj == null)
379 continue;
380 String flowIdStr = flowPathObj.getFlowId();
381 if (flowIdStr == null)
382 continue;
383 FlowId flowId = new FlowId(flowIdStr);
384 allFlowIds.add(flowId);
385 }
386
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800387 // Delete all flows one-by-one
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700388 for (FlowId flowId : allFlowIds) {
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800389 deleteFlow(dbHandler, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700390 }
391
392 return true;
393 }
394
395 /**
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800396 * Delete a previously added flow.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700397 *
398 * @param dbHandler the Graph Database handler to use.
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800399 * @param flowId the Flow ID of the flow to delete.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700400 * @return true on success, otherwise false.
401 */
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800402 static boolean deleteFlow(GraphDBOperation dbHandler, FlowId flowId) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700403 IFlowPath flowObj = null;
404 try {
405 flowObj = dbHandler.searchFlowPath(flowId);
406 } catch (Exception e) {
407 // TODO: handle exceptions
408 dbHandler.rollback();
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800409 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700410 return false;
411 }
412 if (flowObj == null) {
413 dbHandler.commit();
414 return true; // OK: No such flow
415 }
416
417 //
418 // Remove all Flow Entries
419 //
420 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
421 for (IFlowEntry flowEntryObj : flowEntries) {
422 flowObj.removeFlowEntry(flowEntryObj);
423 dbHandler.removeFlowEntry(flowEntryObj);
424 }
425 // Remove the Flow itself
426 dbHandler.removeFlowPath(flowObj);
427 dbHandler.commit();
428
429 return true;
430 }
431
432 /**
433 * Get a previously added flow.
434 *
435 * @param dbHandler the Graph Database handler to use.
436 * @param flowId the Flow ID of the flow to get.
437 * @return the Flow Path if found, otherwise null.
438 */
439 static FlowPath getFlow(GraphDBOperation dbHandler, FlowId flowId) {
440 IFlowPath flowObj = null;
441 try {
442 flowObj = dbHandler.searchFlowPath(flowId);
443 } catch (Exception e) {
444 // TODO: handle exceptions
445 dbHandler.rollback();
446 log.error(":getFlow FlowId:{} failed", flowId.toString());
447 return null;
448 }
449 if (flowObj == null) {
450 dbHandler.commit();
451 return null; // Flow not found
452 }
453
454 //
455 // Extract the Flow state
456 //
457 FlowPath flowPath = extractFlowPath(flowObj);
458 dbHandler.commit();
459
460 return flowPath;
461 }
462
463 /**
464 * Get all installed flows by all installers.
465 *
466 * @param dbHandler the Graph Database handler to use.
467 * @return the Flow Paths if found, otherwise null.
468 */
469 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler) {
470 Iterable<IFlowPath> flowPathsObj = null;
471 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
472
473 try {
474 flowPathsObj = dbHandler.getAllFlowPaths();
475 } catch (Exception e) {
476 // TODO: handle exceptions
477 dbHandler.rollback();
478 log.error(":getAllFlowPaths failed");
479 return flowPaths;
480 }
481 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
482 dbHandler.commit();
483 return flowPaths; // No Flows found
484 }
485
486 for (IFlowPath flowObj : flowPathsObj) {
487 //
488 // Extract the Flow state
489 //
490 FlowPath flowPath = extractFlowPath(flowObj);
491 if (flowPath != null)
492 flowPaths.add(flowPath);
493 }
494
495 dbHandler.commit();
496
497 return flowPaths;
498 }
499
500 /**
501 * Get all previously added flows by a specific installer for a given
502 * data path endpoints.
503 *
504 * @param dbHandler the Graph Database handler to use.
505 * @param installerId the Caller ID of the installer of the flow to get.
506 * @param dataPathEndpoints the data path endpoints of the flow to get.
507 * @return the Flow Paths if found, otherwise null.
508 */
509 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler,
510 CallerId installerId,
511 DataPathEndpoints dataPathEndpoints) {
512 //
513 // TODO: The implementation below is not optimal:
514 // We fetch all flows, and then return only the subset that match
515 // the query conditions.
516 // We should use the appropriate Titan/Gremlin query to filter-out
517 // the flows as appropriate.
518 //
519 ArrayList<FlowPath> allFlows = getAllFlows(dbHandler);
520 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
521
522 if (allFlows == null)
523 return flowPaths;
524
525 for (FlowPath flow : allFlows) {
526 //
527 // TODO: String-based comparison is sub-optimal.
528 // We are using it for now to save us the extra work of
529 // implementing the "equals()" and "hashCode()" methods.
530 //
531 if (! flow.installerId().toString().equals(installerId.toString()))
532 continue;
533 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
534 continue;
535 }
536 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
537 continue;
538 }
539 flowPaths.add(flow);
540 }
541
542 return flowPaths;
543 }
544
545 /**
546 * Get all installed flows by all installers for given data path endpoints.
547 *
548 * @param dbHandler the Graph Database handler to use.
549 * @param dataPathEndpoints the data path endpoints of the flows to get.
550 * @return the Flow Paths if found, otherwise null.
551 */
552 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler,
553 DataPathEndpoints dataPathEndpoints) {
554 //
555 // TODO: The implementation below is not optimal:
556 // We fetch all flows, and then return only the subset that match
557 // the query conditions.
558 // We should use the appropriate Titan/Gremlin query to filter-out
559 // the flows as appropriate.
560 //
561 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
562 ArrayList<FlowPath> allFlows = getAllFlows(dbHandler);
563
564 if (allFlows == null)
565 return flowPaths;
566
567 for (FlowPath flow : allFlows) {
568 //
569 // TODO: String-based comparison is sub-optimal.
570 // We are using it for now to save us the extra work of
571 // implementing the "equals()" and "hashCode()" methods.
572 //
573 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
574 continue;
575 }
576 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
577 continue;
578 }
579 flowPaths.add(flow);
580 }
581
582 return flowPaths;
583 }
584
585 /**
586 * Get summary of all installed flows by all installers in a given range.
587 *
588 * @param dbHandler the Graph Database handler to use.
589 * @param flowId the Flow ID of the first flow in the flow range to get.
590 * @param maxFlows the maximum number of flows to be returned.
591 * @return the Flow Paths if found, otherwise null.
592 */
593 static ArrayList<IFlowPath> getAllFlowsSummary(GraphDBOperation dbHandler,
594 FlowId flowId,
595 int maxFlows) {
596 //
597 // TODO: The implementation below is not optimal:
598 // We fetch all flows, and then return only the subset that match
599 // the query conditions.
600 // We should use the appropriate Titan/Gremlin query to filter-out
601 // the flows as appropriate.
602 //
603 ArrayList<IFlowPath> flowPathsWithoutFlowEntries =
604 getAllFlowsWithoutFlowEntries(dbHandler);
605
606 Collections.sort(flowPathsWithoutFlowEntries,
607 new Comparator<IFlowPath>() {
608 @Override
609 public int compare(IFlowPath first, IFlowPath second) {
610 long result =
611 new FlowId(first.getFlowId()).value()
612 - new FlowId(second.getFlowId()).value();
613 if (result > 0) {
614 return 1;
615 } else if (result < 0) {
616 return -1;
617 } else {
618 return 0;
619 }
620 }
621 }
622 );
623
624 return flowPathsWithoutFlowEntries;
625 }
626
627 /**
628 * Get all Flows information, without the associated Flow Entries.
629 *
630 * @param dbHandler the Graph Database handler to use.
631 * @return all Flows information, without the associated Flow Entries.
632 */
633 static ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(GraphDBOperation dbHandler) {
634 Iterable<IFlowPath> flowPathsObj = null;
635 ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>();
636
637 // TODO: Remove this op.commit() flow, because it is not needed?
638 dbHandler.commit();
639
640 try {
641 flowPathsObj = dbHandler.getAllFlowPaths();
642 } catch (Exception e) {
643 // TODO: handle exceptions
644 dbHandler.rollback();
645 log.error(":getAllFlowPaths failed");
646 return flowPathsObjArray; // No Flows found
647 }
648 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
649 return flowPathsObjArray; // No Flows found
650 }
651
652 for (IFlowPath flowObj : flowPathsObj)
653 flowPathsObjArray.add(flowObj);
654
655 // conn.endTx(Transaction.COMMIT);
656
657 return flowPathsObjArray;
658 }
659
660 /**
661 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
662 *
663 * @param flowObj the object to extract the Flow Path State from.
664 * @return the extracted Flow Path State.
665 */
666 private static FlowPath extractFlowPath(IFlowPath flowObj) {
667 //
668 // Extract the Flow state
669 //
670 String flowIdStr = flowObj.getFlowId();
671 String installerIdStr = flowObj.getInstallerId();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700672 String flowPathType = flowObj.getFlowPathType();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700673 String flowPathUserState = flowObj.getFlowPathUserState();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700674 Long flowPathFlags = flowObj.getFlowPathFlags();
675 String srcSwitchStr = flowObj.getSrcSwitch();
676 Short srcPortShort = flowObj.getSrcPort();
677 String dstSwitchStr = flowObj.getDstSwitch();
678 Short dstPortShort = flowObj.getDstPort();
679
680 if ((flowIdStr == null) ||
681 (installerIdStr == null) ||
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700682 (flowPathType == null) ||
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700683 (flowPathUserState == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700684 (flowPathFlags == null) ||
685 (srcSwitchStr == null) ||
686 (srcPortShort == null) ||
687 (dstSwitchStr == null) ||
688 (dstPortShort == null)) {
689 // TODO: A work-around, becauuse of some bogus database objects
690 return null;
691 }
692
693 FlowPath flowPath = new FlowPath();
694 flowPath.setFlowId(new FlowId(flowIdStr));
695 flowPath.setInstallerId(new CallerId(installerIdStr));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700696 flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700697 flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState));
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700698 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
699 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
700 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
701 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
702 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
703 //
704 // Extract the match conditions common for all Flow Entries
705 //
706 {
707 FlowEntryMatch match = new FlowEntryMatch();
708 String matchSrcMac = flowObj.getMatchSrcMac();
709 if (matchSrcMac != null)
710 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
711 String matchDstMac = flowObj.getMatchDstMac();
712 if (matchDstMac != null)
713 match.enableDstMac(MACAddress.valueOf(matchDstMac));
714 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
715 if (matchEthernetFrameType != null)
716 match.enableEthernetFrameType(matchEthernetFrameType);
717 Short matchVlanId = flowObj.getMatchVlanId();
718 if (matchVlanId != null)
719 match.enableVlanId(matchVlanId);
720 Byte matchVlanPriority = flowObj.getMatchVlanPriority();
721 if (matchVlanPriority != null)
722 match.enableVlanPriority(matchVlanPriority);
723 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
724 if (matchSrcIPv4Net != null)
725 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
726 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
727 if (matchDstIPv4Net != null)
728 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
729 Byte matchIpProto = flowObj.getMatchIpProto();
730 if (matchIpProto != null)
731 match.enableIpProto(matchIpProto);
732 Byte matchIpToS = flowObj.getMatchIpToS();
733 if (matchIpToS != null)
734 match.enableIpToS(matchIpToS);
735 Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort();
736 if (matchSrcTcpUdpPort != null)
737 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
738 Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort();
739 if (matchDstTcpUdpPort != null)
740 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
741
742 flowPath.setFlowEntryMatch(match);
743 }
744 //
745 // Extract the actions for the first Flow Entry
746 //
747 {
748 String actionsStr = flowObj.getActions();
749 if (actionsStr != null) {
750 FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr);
751 flowPath.setFlowEntryActions(flowEntryActions);
752 }
753 }
754
755 //
756 // Extract all Flow Entries
757 //
758 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
759 for (IFlowEntry flowEntryObj : flowEntries) {
760 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
761 if (flowEntry == null)
762 continue;
763 flowPath.dataPath().flowEntries().add(flowEntry);
764 }
765
766 return flowPath;
767 }
768
769 /**
770 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
771 *
772 * @param flowEntryObj the object to extract the Flow Entry State from.
773 * @return the extracted Flow Entry State.
774 */
Brian O'Connora8e49802013-10-30 20:49:59 -0700775 public static FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700776 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
777 String switchDpidStr = flowEntryObj.getSwitchDpid();
778 String userState = flowEntryObj.getUserState();
779 String switchState = flowEntryObj.getSwitchState();
780
781 if ((flowEntryIdStr == null) ||
782 (switchDpidStr == null) ||
783 (userState == null) ||
784 (switchState == null)) {
Brian O'Connora8e49802013-10-30 20:49:59 -0700785 // TODO: A work-around, because of some bogus database objects
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700786 return null;
787 }
788
789 FlowEntry flowEntry = new FlowEntry();
790 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
791 flowEntry.setDpid(new Dpid(switchDpidStr));
792
793 //
794 // Extract the match conditions
795 //
796 FlowEntryMatch match = new FlowEntryMatch();
797 Short matchInPort = flowEntryObj.getMatchInPort();
798 if (matchInPort != null)
799 match.enableInPort(new Port(matchInPort));
800 String matchSrcMac = flowEntryObj.getMatchSrcMac();
801 if (matchSrcMac != null)
802 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
803 String matchDstMac = flowEntryObj.getMatchDstMac();
804 if (matchDstMac != null)
805 match.enableDstMac(MACAddress.valueOf(matchDstMac));
806 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
807 if (matchEthernetFrameType != null)
808 match.enableEthernetFrameType(matchEthernetFrameType);
809 Short matchVlanId = flowEntryObj.getMatchVlanId();
810 if (matchVlanId != null)
811 match.enableVlanId(matchVlanId);
812 Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority();
813 if (matchVlanPriority != null)
814 match.enableVlanPriority(matchVlanPriority);
815 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
816 if (matchSrcIPv4Net != null)
817 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
818 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
819 if (matchDstIPv4Net != null)
820 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
821 Byte matchIpProto = flowEntryObj.getMatchIpProto();
822 if (matchIpProto != null)
823 match.enableIpProto(matchIpProto);
824 Byte matchIpToS = flowEntryObj.getMatchIpToS();
825 if (matchIpToS != null)
826 match.enableIpToS(matchIpToS);
827 Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort();
828 if (matchSrcTcpUdpPort != null)
829 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
830 Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort();
831 if (matchDstTcpUdpPort != null)
832 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
833 flowEntry.setFlowEntryMatch(match);
834
835 //
836 // Extract the actions
837 //
838 FlowEntryActions actions = new FlowEntryActions();
839 String actionsStr = flowEntryObj.getActions();
840 if (actionsStr != null)
841 actions = new FlowEntryActions(actionsStr);
842 flowEntry.setFlowEntryActions(actions);
843 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
844 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
845 //
846 // TODO: Take care of FlowEntryErrorState.
847 //
848 return flowEntry;
849 }
850}