blob: 5bc35d2fc1ab1403dc153d1f54181ca3301a36c4 [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");
312 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
313 //
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 /**
329 * Delete all previously added flows.
330 *
331 * @param dbHandler the Graph Database handler to use.
332 * @return true on success, otherwise false.
333 */
334 static boolean deleteAllFlows(GraphDBOperation dbHandler) {
335 final ConcurrentLinkedQueue<FlowId> concurrentAllFlowIds =
336 new ConcurrentLinkedQueue<FlowId>();
337
338 // Get all Flow IDs
339 Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
340 for (IFlowPath flowPathObj : allFlowPaths) {
341 if (flowPathObj == null)
342 continue;
343 String flowIdStr = flowPathObj.getFlowId();
344 if (flowIdStr == null)
345 continue;
346 FlowId flowId = new FlowId(flowIdStr);
347 concurrentAllFlowIds.add(flowId);
348 }
349
350 // Delete all flows one-by-one
351 for (FlowId flowId : concurrentAllFlowIds)
352 deleteFlow(dbHandler, flowId);
353
354 /*
355 * TODO: A faster mechanism to delete the Flow Paths by using
356 * a number of threads. Commented-out for now.
357 */
358 /*
359 //
360 // Create the threads to delete the Flow Paths
361 //
362 List<Thread> threads = new LinkedList<Thread>();
363 for (int i = 0; i < 10; i++) {
364 Thread thread = new Thread(new Runnable() {
365 @Override
366 public void run() {
367 while (true) {
368 FlowId flowId = concurrentAllFlowIds.poll();
369 if (flowId == null)
370 return;
371 deleteFlow(dbHandler, flowId);
372 }
373 }}, "Delete All Flow Paths");
374 threads.add(thread);
375 }
376
377 // Start processing
378 for (Thread thread : threads) {
379 thread.start();
380 }
381
382 // Wait for all threads to complete
383 for (Thread thread : threads) {
384 try {
385 thread.join();
386 } catch (InterruptedException e) {
387 log.debug("Exception waiting for a thread to delete a Flow Path: ", e);
388 }
389 }
390 */
391
392 return true;
393 }
394
395 /**
396 * Delete a previously added flow.
397 *
398 * @param dbHandler the Graph Database handler to use.
399 * @param flowId the Flow ID of the flow to delete.
400 * @return true on success, otherwise false.
401 */
402 static boolean deleteFlow(GraphDBOperation dbHandler, FlowId flowId) {
403 IFlowPath flowObj = null;
404 //
405 // We just mark the entries for deletion,
406 // and let the switches remove each individual entry after
407 // it has been removed from the switches.
408 //
409 try {
410 flowObj = dbHandler.searchFlowPath(flowId);
411 } catch (Exception e) {
412 // TODO: handle exceptions
413 dbHandler.rollback();
414 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
415 return false;
416 }
417 if (flowObj == null) {
418 dbHandler.commit();
419 return true; // OK: No such flow
420 }
421
422 //
423 // Find and mark for deletion all Flow Entries,
424 // and the Flow itself.
425 //
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700426 flowObj.setFlowPathUserState("FP_USER_DELETE");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700427 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
428 boolean empty = true; // TODO: an ugly hack
429 for (IFlowEntry flowEntryObj : flowEntries) {
430 empty = false;
431 // flowObj.removeFlowEntry(flowEntryObj);
432 // conn.utils().removeFlowEntry(conn, flowEntryObj);
433 flowEntryObj.setUserState("FE_USER_DELETE");
434 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
435 }
436 // Remove from the database empty flows
437 if (empty)
438 dbHandler.removeFlowPath(flowObj);
439 dbHandler.commit();
440
441 return true;
442 }
443
444 /**
445 * Clear the state for all previously added flows.
446 *
447 * @param dbHandler the Graph Database handler to use.
448 * @return true on success, otherwise false.
449 */
450 static boolean clearAllFlows(GraphDBOperation dbHandler) {
451 List<FlowId> allFlowIds = new LinkedList<FlowId>();
452
453 // Get all Flow IDs
454 Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
455 for (IFlowPath flowPathObj : allFlowPaths) {
456 if (flowPathObj == null)
457 continue;
458 String flowIdStr = flowPathObj.getFlowId();
459 if (flowIdStr == null)
460 continue;
461 FlowId flowId = new FlowId(flowIdStr);
462 allFlowIds.add(flowId);
463 }
464
465 // Clear all flows one-by-one
466 for (FlowId flowId : allFlowIds) {
467 clearFlow(dbHandler, flowId);
468 }
469
470 return true;
471 }
472
473 /**
474 * Clear the state for a previously added flow.
475 *
476 * @param dbHandler the Graph Database handler to use.
477 * @param flowId the Flow ID of the flow to clear.
478 * @return true on success, otherwise false.
479 */
480 static boolean clearFlow(GraphDBOperation dbHandler, FlowId flowId) {
481 IFlowPath flowObj = null;
482 try {
483 flowObj = dbHandler.searchFlowPath(flowId);
484 } catch (Exception e) {
485 // TODO: handle exceptions
486 dbHandler.rollback();
487 log.error(":clearFlow FlowId:{} failed", flowId.toString());
488 return false;
489 }
490 if (flowObj == null) {
491 dbHandler.commit();
492 return true; // OK: No such flow
493 }
494
495 //
496 // Remove all Flow Entries
497 //
498 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
499 for (IFlowEntry flowEntryObj : flowEntries) {
500 flowObj.removeFlowEntry(flowEntryObj);
501 dbHandler.removeFlowEntry(flowEntryObj);
502 }
503 // Remove the Flow itself
504 dbHandler.removeFlowPath(flowObj);
505 dbHandler.commit();
506
507 return true;
508 }
509
510 /**
511 * Get a previously added flow.
512 *
513 * @param dbHandler the Graph Database handler to use.
514 * @param flowId the Flow ID of the flow to get.
515 * @return the Flow Path if found, otherwise null.
516 */
517 static FlowPath getFlow(GraphDBOperation dbHandler, FlowId flowId) {
518 IFlowPath flowObj = null;
519 try {
520 flowObj = dbHandler.searchFlowPath(flowId);
521 } catch (Exception e) {
522 // TODO: handle exceptions
523 dbHandler.rollback();
524 log.error(":getFlow FlowId:{} failed", flowId.toString());
525 return null;
526 }
527 if (flowObj == null) {
528 dbHandler.commit();
529 return null; // Flow not found
530 }
531
532 //
533 // Extract the Flow state
534 //
535 FlowPath flowPath = extractFlowPath(flowObj);
536 dbHandler.commit();
537
538 return flowPath;
539 }
540
541 /**
542 * Get all installed flows by all installers.
543 *
544 * @param dbHandler the Graph Database handler to use.
545 * @return the Flow Paths if found, otherwise null.
546 */
547 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler) {
548 Iterable<IFlowPath> flowPathsObj = null;
549 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
550
551 try {
552 flowPathsObj = dbHandler.getAllFlowPaths();
553 } catch (Exception e) {
554 // TODO: handle exceptions
555 dbHandler.rollback();
556 log.error(":getAllFlowPaths failed");
557 return flowPaths;
558 }
559 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
560 dbHandler.commit();
561 return flowPaths; // No Flows found
562 }
563
564 for (IFlowPath flowObj : flowPathsObj) {
565 //
566 // Extract the Flow state
567 //
568 FlowPath flowPath = extractFlowPath(flowObj);
569 if (flowPath != null)
570 flowPaths.add(flowPath);
571 }
572
573 dbHandler.commit();
574
575 return flowPaths;
576 }
577
578 /**
579 * Get all previously added flows by a specific installer for a given
580 * data path endpoints.
581 *
582 * @param dbHandler the Graph Database handler to use.
583 * @param installerId the Caller ID of the installer of the flow to get.
584 * @param dataPathEndpoints the data path endpoints of the flow to get.
585 * @return the Flow Paths if found, otherwise null.
586 */
587 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler,
588 CallerId installerId,
589 DataPathEndpoints dataPathEndpoints) {
590 //
591 // TODO: The implementation below is not optimal:
592 // We fetch all flows, and then return only the subset that match
593 // the query conditions.
594 // We should use the appropriate Titan/Gremlin query to filter-out
595 // the flows as appropriate.
596 //
597 ArrayList<FlowPath> allFlows = getAllFlows(dbHandler);
598 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
599
600 if (allFlows == null)
601 return flowPaths;
602
603 for (FlowPath flow : allFlows) {
604 //
605 // TODO: String-based comparison is sub-optimal.
606 // We are using it for now to save us the extra work of
607 // implementing the "equals()" and "hashCode()" methods.
608 //
609 if (! flow.installerId().toString().equals(installerId.toString()))
610 continue;
611 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
612 continue;
613 }
614 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
615 continue;
616 }
617 flowPaths.add(flow);
618 }
619
620 return flowPaths;
621 }
622
623 /**
624 * Get all installed flows by all installers for given data path endpoints.
625 *
626 * @param dbHandler the Graph Database handler to use.
627 * @param dataPathEndpoints the data path endpoints of the flows to get.
628 * @return the Flow Paths if found, otherwise null.
629 */
630 static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler,
631 DataPathEndpoints dataPathEndpoints) {
632 //
633 // TODO: The implementation below is not optimal:
634 // We fetch all flows, and then return only the subset that match
635 // the query conditions.
636 // We should use the appropriate Titan/Gremlin query to filter-out
637 // the flows as appropriate.
638 //
639 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
640 ArrayList<FlowPath> allFlows = getAllFlows(dbHandler);
641
642 if (allFlows == null)
643 return flowPaths;
644
645 for (FlowPath flow : allFlows) {
646 //
647 // TODO: String-based comparison is sub-optimal.
648 // We are using it for now to save us the extra work of
649 // implementing the "equals()" and "hashCode()" methods.
650 //
651 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
652 continue;
653 }
654 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
655 continue;
656 }
657 flowPaths.add(flow);
658 }
659
660 return flowPaths;
661 }
662
663 /**
664 * Get summary of all installed flows by all installers in a given range.
665 *
666 * @param dbHandler the Graph Database handler to use.
667 * @param flowId the Flow ID of the first flow in the flow range to get.
668 * @param maxFlows the maximum number of flows to be returned.
669 * @return the Flow Paths if found, otherwise null.
670 */
671 static ArrayList<IFlowPath> getAllFlowsSummary(GraphDBOperation dbHandler,
672 FlowId flowId,
673 int maxFlows) {
674 //
675 // TODO: The implementation below is not optimal:
676 // We fetch all flows, and then return only the subset that match
677 // the query conditions.
678 // We should use the appropriate Titan/Gremlin query to filter-out
679 // the flows as appropriate.
680 //
681 ArrayList<IFlowPath> flowPathsWithoutFlowEntries =
682 getAllFlowsWithoutFlowEntries(dbHandler);
683
684 Collections.sort(flowPathsWithoutFlowEntries,
685 new Comparator<IFlowPath>() {
686 @Override
687 public int compare(IFlowPath first, IFlowPath second) {
688 long result =
689 new FlowId(first.getFlowId()).value()
690 - new FlowId(second.getFlowId()).value();
691 if (result > 0) {
692 return 1;
693 } else if (result < 0) {
694 return -1;
695 } else {
696 return 0;
697 }
698 }
699 }
700 );
701
702 return flowPathsWithoutFlowEntries;
703 }
704
705 /**
706 * Get all Flows information, without the associated Flow Entries.
707 *
708 * @param dbHandler the Graph Database handler to use.
709 * @return all Flows information, without the associated Flow Entries.
710 */
711 static ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(GraphDBOperation dbHandler) {
712 Iterable<IFlowPath> flowPathsObj = null;
713 ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>();
714
715 // TODO: Remove this op.commit() flow, because it is not needed?
716 dbHandler.commit();
717
718 try {
719 flowPathsObj = dbHandler.getAllFlowPaths();
720 } catch (Exception e) {
721 // TODO: handle exceptions
722 dbHandler.rollback();
723 log.error(":getAllFlowPaths failed");
724 return flowPathsObjArray; // No Flows found
725 }
726 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
727 return flowPathsObjArray; // No Flows found
728 }
729
730 for (IFlowPath flowObj : flowPathsObj)
731 flowPathsObjArray.add(flowObj);
732
733 // conn.endTx(Transaction.COMMIT);
734
735 return flowPathsObjArray;
736 }
737
738 /**
739 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
740 *
741 * @param flowObj the object to extract the Flow Path State from.
742 * @return the extracted Flow Path State.
743 */
744 private static FlowPath extractFlowPath(IFlowPath flowObj) {
745 //
746 // Extract the Flow state
747 //
748 String flowIdStr = flowObj.getFlowId();
749 String installerIdStr = flowObj.getInstallerId();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700750 String flowPathType = flowObj.getFlowPathType();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700751 String flowPathUserState = flowObj.getFlowPathUserState();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700752 Long flowPathFlags = flowObj.getFlowPathFlags();
753 String srcSwitchStr = flowObj.getSrcSwitch();
754 Short srcPortShort = flowObj.getSrcPort();
755 String dstSwitchStr = flowObj.getDstSwitch();
756 Short dstPortShort = flowObj.getDstPort();
757
758 if ((flowIdStr == null) ||
759 (installerIdStr == null) ||
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700760 (flowPathType == null) ||
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700761 (flowPathUserState == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700762 (flowPathFlags == null) ||
763 (srcSwitchStr == null) ||
764 (srcPortShort == null) ||
765 (dstSwitchStr == null) ||
766 (dstPortShort == null)) {
767 // TODO: A work-around, becauuse of some bogus database objects
768 return null;
769 }
770
771 FlowPath flowPath = new FlowPath();
772 flowPath.setFlowId(new FlowId(flowIdStr));
773 flowPath.setInstallerId(new CallerId(installerIdStr));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700774 flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700775 flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState));
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700776 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
777 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
778 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
779 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
780 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
781 //
782 // Extract the match conditions common for all Flow Entries
783 //
784 {
785 FlowEntryMatch match = new FlowEntryMatch();
786 String matchSrcMac = flowObj.getMatchSrcMac();
787 if (matchSrcMac != null)
788 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
789 String matchDstMac = flowObj.getMatchDstMac();
790 if (matchDstMac != null)
791 match.enableDstMac(MACAddress.valueOf(matchDstMac));
792 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
793 if (matchEthernetFrameType != null)
794 match.enableEthernetFrameType(matchEthernetFrameType);
795 Short matchVlanId = flowObj.getMatchVlanId();
796 if (matchVlanId != null)
797 match.enableVlanId(matchVlanId);
798 Byte matchVlanPriority = flowObj.getMatchVlanPriority();
799 if (matchVlanPriority != null)
800 match.enableVlanPriority(matchVlanPriority);
801 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
802 if (matchSrcIPv4Net != null)
803 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
804 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
805 if (matchDstIPv4Net != null)
806 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
807 Byte matchIpProto = flowObj.getMatchIpProto();
808 if (matchIpProto != null)
809 match.enableIpProto(matchIpProto);
810 Byte matchIpToS = flowObj.getMatchIpToS();
811 if (matchIpToS != null)
812 match.enableIpToS(matchIpToS);
813 Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort();
814 if (matchSrcTcpUdpPort != null)
815 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
816 Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort();
817 if (matchDstTcpUdpPort != null)
818 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
819
820 flowPath.setFlowEntryMatch(match);
821 }
822 //
823 // Extract the actions for the first Flow Entry
824 //
825 {
826 String actionsStr = flowObj.getActions();
827 if (actionsStr != null) {
828 FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr);
829 flowPath.setFlowEntryActions(flowEntryActions);
830 }
831 }
832
833 //
834 // Extract all Flow Entries
835 //
836 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
837 for (IFlowEntry flowEntryObj : flowEntries) {
838 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
839 if (flowEntry == null)
840 continue;
841 flowPath.dataPath().flowEntries().add(flowEntry);
842 }
843
844 return flowPath;
845 }
846
847 /**
848 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
849 *
850 * @param flowEntryObj the object to extract the Flow Entry State from.
851 * @return the extracted Flow Entry State.
852 */
853 private static FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
854 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
855 String switchDpidStr = flowEntryObj.getSwitchDpid();
856 String userState = flowEntryObj.getUserState();
857 String switchState = flowEntryObj.getSwitchState();
858
859 if ((flowEntryIdStr == null) ||
860 (switchDpidStr == null) ||
861 (userState == null) ||
862 (switchState == null)) {
863 // TODO: A work-around, becauuse of some bogus database objects
864 return null;
865 }
866
867 FlowEntry flowEntry = new FlowEntry();
868 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
869 flowEntry.setDpid(new Dpid(switchDpidStr));
870
871 //
872 // Extract the match conditions
873 //
874 FlowEntryMatch match = new FlowEntryMatch();
875 Short matchInPort = flowEntryObj.getMatchInPort();
876 if (matchInPort != null)
877 match.enableInPort(new Port(matchInPort));
878 String matchSrcMac = flowEntryObj.getMatchSrcMac();
879 if (matchSrcMac != null)
880 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
881 String matchDstMac = flowEntryObj.getMatchDstMac();
882 if (matchDstMac != null)
883 match.enableDstMac(MACAddress.valueOf(matchDstMac));
884 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
885 if (matchEthernetFrameType != null)
886 match.enableEthernetFrameType(matchEthernetFrameType);
887 Short matchVlanId = flowEntryObj.getMatchVlanId();
888 if (matchVlanId != null)
889 match.enableVlanId(matchVlanId);
890 Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority();
891 if (matchVlanPriority != null)
892 match.enableVlanPriority(matchVlanPriority);
893 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
894 if (matchSrcIPv4Net != null)
895 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
896 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
897 if (matchDstIPv4Net != null)
898 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
899 Byte matchIpProto = flowEntryObj.getMatchIpProto();
900 if (matchIpProto != null)
901 match.enableIpProto(matchIpProto);
902 Byte matchIpToS = flowEntryObj.getMatchIpToS();
903 if (matchIpToS != null)
904 match.enableIpToS(matchIpToS);
905 Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort();
906 if (matchSrcTcpUdpPort != null)
907 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
908 Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort();
909 if (matchDstTcpUdpPort != null)
910 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
911 flowEntry.setFlowEntryMatch(match);
912
913 //
914 // Extract the actions
915 //
916 FlowEntryActions actions = new FlowEntryActions();
917 String actionsStr = flowEntryObj.getActions();
918 if (actionsStr != null)
919 actions = new FlowEntryActions(actionsStr);
920 flowEntry.setFlowEntryActions(actions);
921 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
922 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
923 //
924 // TODO: Take care of FlowEntryErrorState.
925 //
926 return flowEntry;
927 }
928}