blob: ce2b1c70d3eb6af5d9dcd2a9c33cee48f02b17de [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 */
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800593 static ArrayList<FlowPath> getAllFlowsSummary(GraphDBOperation dbHandler,
594 FlowId flowId,
595 int maxFlows) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700596 //
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 //
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800603 ArrayList<FlowPath> flowPaths = getAllFlowsWithDataPathSummary(dbHandler);
604 Collections.sort(flowPaths);
605 return flowPaths;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700606 }
607
608 /**
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800609 * Get all Flows information, with Data Path summary for the Flow Entries.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700610 *
611 * @param dbHandler the Graph Database handler to use.
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800612 * @return all Flows information, with Data Path summary for the Flow
613 * Entries.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700614 */
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800615 static ArrayList<FlowPath> getAllFlowsWithDataPathSummary(GraphDBOperation dbHandler) {
616 ArrayList<FlowPath> flowPaths = getAllFlows(dbHandler);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700617
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800618 // Truncate each Flow Path and Flow Entry
619 for (FlowPath flowPath : flowPaths) {
620 flowPath.setFlowEntryMatch(null);
621 flowPath.setFlowEntryActions(null);
622 for (FlowEntry flowEntry : flowPath.flowEntries()) {
623 flowEntry.setFlowEntryMatch(null);
624 flowEntry.setFlowEntryActions(null);
625 }
626 }
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700627
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800628 return flowPaths;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700629 }
630
631 /**
632 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
633 *
634 * @param flowObj the object to extract the Flow Path State from.
635 * @return the extracted Flow Path State.
636 */
637 private static FlowPath extractFlowPath(IFlowPath flowObj) {
638 //
639 // Extract the Flow state
640 //
641 String flowIdStr = flowObj.getFlowId();
642 String installerIdStr = flowObj.getInstallerId();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700643 String flowPathType = flowObj.getFlowPathType();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700644 String flowPathUserState = flowObj.getFlowPathUserState();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700645 Long flowPathFlags = flowObj.getFlowPathFlags();
646 String srcSwitchStr = flowObj.getSrcSwitch();
647 Short srcPortShort = flowObj.getSrcPort();
648 String dstSwitchStr = flowObj.getDstSwitch();
649 Short dstPortShort = flowObj.getDstPort();
650
651 if ((flowIdStr == null) ||
652 (installerIdStr == null) ||
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700653 (flowPathType == null) ||
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700654 (flowPathUserState == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700655 (flowPathFlags == null) ||
656 (srcSwitchStr == null) ||
657 (srcPortShort == null) ||
658 (dstSwitchStr == null) ||
659 (dstPortShort == null)) {
660 // TODO: A work-around, becauuse of some bogus database objects
661 return null;
662 }
663
664 FlowPath flowPath = new FlowPath();
665 flowPath.setFlowId(new FlowId(flowIdStr));
666 flowPath.setInstallerId(new CallerId(installerIdStr));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700667 flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700668 flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState));
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700669 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
670 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
671 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
672 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
673 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
674 //
675 // Extract the match conditions common for all Flow Entries
676 //
677 {
678 FlowEntryMatch match = new FlowEntryMatch();
679 String matchSrcMac = flowObj.getMatchSrcMac();
680 if (matchSrcMac != null)
681 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
682 String matchDstMac = flowObj.getMatchDstMac();
683 if (matchDstMac != null)
684 match.enableDstMac(MACAddress.valueOf(matchDstMac));
685 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
686 if (matchEthernetFrameType != null)
687 match.enableEthernetFrameType(matchEthernetFrameType);
688 Short matchVlanId = flowObj.getMatchVlanId();
689 if (matchVlanId != null)
690 match.enableVlanId(matchVlanId);
691 Byte matchVlanPriority = flowObj.getMatchVlanPriority();
692 if (matchVlanPriority != null)
693 match.enableVlanPriority(matchVlanPriority);
694 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
695 if (matchSrcIPv4Net != null)
696 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
697 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
698 if (matchDstIPv4Net != null)
699 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
700 Byte matchIpProto = flowObj.getMatchIpProto();
701 if (matchIpProto != null)
702 match.enableIpProto(matchIpProto);
703 Byte matchIpToS = flowObj.getMatchIpToS();
704 if (matchIpToS != null)
705 match.enableIpToS(matchIpToS);
706 Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort();
707 if (matchSrcTcpUdpPort != null)
708 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
709 Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort();
710 if (matchDstTcpUdpPort != null)
711 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
712
713 flowPath.setFlowEntryMatch(match);
714 }
715 //
716 // Extract the actions for the first Flow Entry
717 //
718 {
719 String actionsStr = flowObj.getActions();
720 if (actionsStr != null) {
721 FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr);
722 flowPath.setFlowEntryActions(flowEntryActions);
723 }
724 }
725
726 //
727 // Extract all Flow Entries
728 //
729 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
730 for (IFlowEntry flowEntryObj : flowEntries) {
731 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
732 if (flowEntry == null)
733 continue;
734 flowPath.dataPath().flowEntries().add(flowEntry);
735 }
736
737 return flowPath;
738 }
739
740 /**
741 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
742 *
743 * @param flowEntryObj the object to extract the Flow Entry State from.
744 * @return the extracted Flow Entry State.
745 */
Brian O'Connora8e49802013-10-30 20:49:59 -0700746 public static FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700747 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
748 String switchDpidStr = flowEntryObj.getSwitchDpid();
749 String userState = flowEntryObj.getUserState();
750 String switchState = flowEntryObj.getSwitchState();
751
752 if ((flowEntryIdStr == null) ||
753 (switchDpidStr == null) ||
754 (userState == null) ||
755 (switchState == null)) {
Brian O'Connora8e49802013-10-30 20:49:59 -0700756 // TODO: A work-around, because of some bogus database objects
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700757 return null;
758 }
759
760 FlowEntry flowEntry = new FlowEntry();
761 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
762 flowEntry.setDpid(new Dpid(switchDpidStr));
763
764 //
765 // Extract the match conditions
766 //
767 FlowEntryMatch match = new FlowEntryMatch();
768 Short matchInPort = flowEntryObj.getMatchInPort();
769 if (matchInPort != null)
770 match.enableInPort(new Port(matchInPort));
771 String matchSrcMac = flowEntryObj.getMatchSrcMac();
772 if (matchSrcMac != null)
773 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
774 String matchDstMac = flowEntryObj.getMatchDstMac();
775 if (matchDstMac != null)
776 match.enableDstMac(MACAddress.valueOf(matchDstMac));
777 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
778 if (matchEthernetFrameType != null)
779 match.enableEthernetFrameType(matchEthernetFrameType);
780 Short matchVlanId = flowEntryObj.getMatchVlanId();
781 if (matchVlanId != null)
782 match.enableVlanId(matchVlanId);
783 Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority();
784 if (matchVlanPriority != null)
785 match.enableVlanPriority(matchVlanPriority);
786 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
787 if (matchSrcIPv4Net != null)
788 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
789 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
790 if (matchDstIPv4Net != null)
791 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
792 Byte matchIpProto = flowEntryObj.getMatchIpProto();
793 if (matchIpProto != null)
794 match.enableIpProto(matchIpProto);
795 Byte matchIpToS = flowEntryObj.getMatchIpToS();
796 if (matchIpToS != null)
797 match.enableIpToS(matchIpToS);
798 Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort();
799 if (matchSrcTcpUdpPort != null)
800 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
801 Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort();
802 if (matchDstTcpUdpPort != null)
803 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
804 flowEntry.setFlowEntryMatch(match);
805
806 //
807 // Extract the actions
808 //
809 FlowEntryActions actions = new FlowEntryActions();
810 String actionsStr = flowEntryObj.getActions();
811 if (actionsStr != null)
812 actions = new FlowEntryActions(actionsStr);
813 flowEntry.setFlowEntryActions(actions);
814 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
815 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
816 //
817 // TODO: Take care of FlowEntryErrorState.
818 //
819 return flowEntry;
820 }
821}