blob: da40bef71543e5c4adbd3f3c30d9a0d35c836fb1 [file] [log] [blame]
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -07001package net.onrc.onos.ofcontroller.flowmanager;
2
3import java.io.PrintWriter;
4import java.io.StringWriter;
5import java.util.ArrayList;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -07006import java.util.Comparator;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.concurrent.ConcurrentLinkedQueue;
10
11import net.floodlightcontroller.util.MACAddress;
yoshitomob292c622013-11-23 14:35:58 -080012import net.onrc.onos.graph.DBOperation;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070013
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070014import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
15import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
16import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
17import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
18import net.onrc.onos.ofcontroller.util.*;
19
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23/**
24 * Class for performing Flow-related operations on the Database.
25 */
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -080026public class FlowDatabaseOperation {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070027 private final static Logger log = LoggerFactory.getLogger(FlowDatabaseOperation.class);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070028
29 /**
30 * Add a flow.
31 *
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070032 * @param dbHandler the Graph Database handler to use.
33 * @param flowPath the Flow Path to install.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070034 * @return true on success, otherwise false.
35 */
onlab-qa38805cd2013-12-06 20:08:54 -080036 static boolean addFlow(DBOperation dbHandler, FlowPath flowPath) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070037 IFlowPath flowObj = null;
38 boolean found = false;
39 try {
yoshi40210942013-12-03 08:21:02 -080040 flowObj = dbHandler.searchFlowPath(flowPath.flowId());
yoshi40210942013-12-03 08:21:02 -080041 if (flowObj != null) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070042 found = true;
43 } else {
44 flowObj = dbHandler.newFlowPath();
45 }
46 } catch (Exception e) {
47 dbHandler.rollback();
48
49 StringWriter sw = new StringWriter();
50 e.printStackTrace(new PrintWriter(sw));
51 String stacktrace = sw.toString();
52
53 log.error(":addFlow FlowId:{} failed: {}",
54 flowPath.flowId().toString(),
55 stacktrace);
56 return false;
57 }
58 if (flowObj == null) {
59 log.error(":addFlow FlowId:{} failed: Flow object not created",
60 flowPath.flowId().toString());
61 dbHandler.rollback();
62 return false;
63 }
64
65 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080066 // Remove the old Flow Entries
67 //
68 if (found) {
69 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
70 LinkedList<IFlowEntry> deleteFlowEntries =
71 new LinkedList<IFlowEntry>();
72 for (IFlowEntry flowEntryObj : flowEntries)
73 deleteFlowEntries.add(flowEntryObj);
74 for (IFlowEntry flowEntryObj : deleteFlowEntries) {
75 flowObj.removeFlowEntry(flowEntryObj);
76 dbHandler.removeFlowEntry(flowEntryObj);
77 }
78 }
79
80 //
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070081 // Set the Flow key:
82 // - flowId
83 //
84 flowObj.setFlowId(flowPath.flowId().toString());
85 flowObj.setType("flow");
86
87 //
88 // Set the Flow attributes:
89 // - flowPath.installerId()
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -070090 // - flowPath.flowPathType()
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -070091 // - flowPath.flowPathUserState()
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070092 // - flowPath.flowPathFlags()
93 // - flowPath.dataPath().srcPort()
94 // - flowPath.dataPath().dstPort()
95 // - flowPath.matchSrcMac()
96 // - flowPath.matchDstMac()
97 // - flowPath.matchEthernetFrameType()
98 // - flowPath.matchVlanId()
99 // - flowPath.matchVlanPriority()
100 // - flowPath.matchSrcIPv4Net()
101 // - flowPath.matchDstIPv4Net()
102 // - flowPath.matchIpProto()
103 // - flowPath.matchIpToS()
104 // - flowPath.matchSrcTcpUdpPort()
105 // - flowPath.matchDstTcpUdpPort()
106 // - flowPath.flowEntryActions()
107 //
108 flowObj.setInstallerId(flowPath.installerId().toString());
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700109 flowObj.setFlowPathType(flowPath.flowPathType().toString());
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700110 flowObj.setFlowPathUserState(flowPath.flowPathUserState().toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700111 flowObj.setFlowPathFlags(flowPath.flowPathFlags().flags());
112 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
113 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
114 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
115 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
116 if (flowPath.flowEntryMatch().matchSrcMac()) {
117 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
118 }
119 if (flowPath.flowEntryMatch().matchDstMac()) {
120 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
121 }
122 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
123 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
124 }
125 if (flowPath.flowEntryMatch().matchVlanId()) {
126 flowObj.setMatchVlanId(flowPath.flowEntryMatch().vlanId());
127 }
128 if (flowPath.flowEntryMatch().matchVlanPriority()) {
129 flowObj.setMatchVlanPriority(flowPath.flowEntryMatch().vlanPriority());
130 }
131 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
132 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
133 }
134 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
135 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
136 }
137 if (flowPath.flowEntryMatch().matchIpProto()) {
138 flowObj.setMatchIpProto(flowPath.flowEntryMatch().ipProto());
139 }
140 if (flowPath.flowEntryMatch().matchIpToS()) {
141 flowObj.setMatchIpToS(flowPath.flowEntryMatch().ipToS());
142 }
143 if (flowPath.flowEntryMatch().matchSrcTcpUdpPort()) {
144 flowObj.setMatchSrcTcpUdpPort(flowPath.flowEntryMatch().srcTcpUdpPort());
145 }
146 if (flowPath.flowEntryMatch().matchDstTcpUdpPort()) {
147 flowObj.setMatchDstTcpUdpPort(flowPath.flowEntryMatch().dstTcpUdpPort());
148 }
149 if (! flowPath.flowEntryActions().actions().isEmpty()) {
150 flowObj.setActions(flowPath.flowEntryActions().toString());
151 }
Pavlin Radoslavovbcc86ef2013-10-26 12:06:25 -0700152 flowObj.setDataPathSummary(flowPath.dataPath().dataPathSummary());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700153
154 if (found)
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700155 flowObj.setFlowPathUserState("FP_USER_MODIFY");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700156 else
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700157 flowObj.setFlowPathUserState("FP_USER_ADD");
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700158
159 // Flow edges:
160 // HeadFE
161
162
163 //
164 // Flow Entries:
165 // flowPath.dataPath().flowEntries()
166 //
167 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800168 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE)
169 continue; // Skip: all Flow Entries were deleted earlier
170
Pavlin Radoslavov67bf7622013-12-04 12:28:23 -0800171 if (addFlowEntry(dbHandler, flowObj, flowEntry) == null) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700172 dbHandler.rollback();
173 return false;
174 }
175 }
176 dbHandler.commit();
177
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700178 return true;
179 }
180
181 /**
182 * Add a flow entry to the Network MAP.
183 *
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700184 * @param dbHandler the Graph Database handler to use.
185 * @param flowObj the corresponding Flow Path object for the Flow Entry.
186 * @param flowEntry the Flow Entry to install.
187 * @return the added Flow Entry object on success, otherwise null.
188 */
onlab-qa38805cd2013-12-06 20:08:54 -0800189 static IFlowEntry addFlowEntry(DBOperation dbHandler,
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700190 IFlowPath flowObj,
191 FlowEntry flowEntry) {
192 // Flow edges
193 // HeadFE (TODO)
194
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700195 IFlowEntry flowEntryObj = null;
196 boolean found = false;
197 try {
198 if ((flowEntryObj =
199 dbHandler.searchFlowEntry(flowEntry.flowEntryId())) != null) {
200 found = true;
201 } else {
202 flowEntryObj = dbHandler.newFlowEntry();
203 }
204 } catch (Exception e) {
205 log.error(":addFlow FlowEntryId:{} failed",
206 flowEntry.flowEntryId().toString());
207 return null;
208 }
209 if (flowEntryObj == null) {
210 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
211 flowEntry.flowEntryId().toString());
212 return null;
213 }
214
215 //
216 // Set the Flow Entry key:
217 // - flowEntry.flowEntryId()
218 //
219 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
220 flowEntryObj.setType("flow_entry");
221
222 //
223 // Set the Flow Entry Edges and attributes:
224 // - Switch edge
225 // - InPort edge
226 // - OutPort edge
227 //
228 // - flowEntry.dpid()
229 // - flowEntry.flowEntryUserState()
230 // - flowEntry.flowEntrySwitchState()
231 // - flowEntry.flowEntryErrorState()
232 // - flowEntry.matchInPort()
233 // - flowEntry.matchSrcMac()
234 // - flowEntry.matchDstMac()
235 // - flowEntry.matchEthernetFrameType()
236 // - flowEntry.matchVlanId()
237 // - flowEntry.matchVlanPriority()
238 // - flowEntry.matchSrcIPv4Net()
239 // - flowEntry.matchDstIPv4Net()
240 // - flowEntry.matchIpProto()
241 // - flowEntry.matchIpToS()
242 // - flowEntry.matchSrcTcpUdpPort()
243 // - flowEntry.matchDstTcpUdpPort()
244 // - flowEntry.actionOutputPort()
245 // - flowEntry.actions()
246 //
247 ISwitchObject sw = dbHandler.searchSwitch(flowEntry.dpid().toString());
248 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
249 flowEntryObj.setSwitch(sw);
250 if (flowEntry.flowEntryMatch().matchInPort()) {
251 IPortObject inport =
252 dbHandler.searchPort(flowEntry.dpid().toString(),
253 flowEntry.flowEntryMatch().inPort().value());
254 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
255 flowEntryObj.setInPort(inport);
256 }
257 if (flowEntry.flowEntryMatch().matchSrcMac()) {
258 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
259 }
260 if (flowEntry.flowEntryMatch().matchDstMac()) {
261 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
262 }
263 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
264 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
265 }
266 if (flowEntry.flowEntryMatch().matchVlanId()) {
267 flowEntryObj.setMatchVlanId(flowEntry.flowEntryMatch().vlanId());
268 }
269 if (flowEntry.flowEntryMatch().matchVlanPriority()) {
270 flowEntryObj.setMatchVlanPriority(flowEntry.flowEntryMatch().vlanPriority());
271 }
272 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
273 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
274 }
275 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
276 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
277 }
278 if (flowEntry.flowEntryMatch().matchIpProto()) {
279 flowEntryObj.setMatchIpProto(flowEntry.flowEntryMatch().ipProto());
280 }
281 if (flowEntry.flowEntryMatch().matchIpToS()) {
282 flowEntryObj.setMatchIpToS(flowEntry.flowEntryMatch().ipToS());
283 }
284 if (flowEntry.flowEntryMatch().matchSrcTcpUdpPort()) {
285 flowEntryObj.setMatchSrcTcpUdpPort(flowEntry.flowEntryMatch().srcTcpUdpPort());
286 }
287 if (flowEntry.flowEntryMatch().matchDstTcpUdpPort()) {
288 flowEntryObj.setMatchDstTcpUdpPort(flowEntry.flowEntryMatch().dstTcpUdpPort());
289 }
290
291 for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) {
292 if (fa.actionOutput() != null) {
293 IPortObject outport =
294 dbHandler.searchPort(flowEntry.dpid().toString(),
295 fa.actionOutput().port().value());
296 flowEntryObj.setActionOutputPort(fa.actionOutput().port().value());
297 flowEntryObj.setOutPort(outport);
298 }
299 }
300 if (! flowEntry.flowEntryActions().isEmpty()) {
301 flowEntryObj.setActions(flowEntry.flowEntryActions().toString());
302 }
303
304 // TODO: Hacks with hard-coded state names!
305 if (found)
306 flowEntryObj.setUserState("FE_USER_MODIFY");
307 else
308 flowEntryObj.setUserState("FE_USER_ADD");
Pavlin Radoslavovebc8b192013-10-29 15:35:35 -0700309 flowEntryObj.setSwitchState(flowEntry.flowEntrySwitchState().toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700310 //
311 // TODO: Take care of the FlowEntryErrorState.
312 //
313
314 // Flow Entries edges:
315 // Flow
316 // NextFE (TODO)
317 if (! found) {
318 flowObj.addFlowEntry(flowEntryObj);
319 flowEntryObj.setFlow(flowObj);
320 }
321
322 return flowEntryObj;
323 }
324
325 /**
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700326 * Delete a flow entry from the Network MAP.
327 *
328 * @param dbHandler the Graph Database handler to use.
329 * @param flowObj the corresponding Flow Path object for the Flow Entry.
330 * @param flowEntry the Flow Entry to delete.
331 * @return true on success, otherwise false.
332 */
yoshitomob292c622013-11-23 14:35:58 -0800333 static boolean deleteFlowEntry(DBOperation dbHandler,
Pavlin Radoslavov7407ab52013-11-01 22:19:00 -0700334 IFlowPath flowObj,
335 FlowEntry flowEntry) {
336 IFlowEntry flowEntryObj = null;
337 try {
338 flowEntryObj = dbHandler.searchFlowEntry(flowEntry.flowEntryId());
339 } catch (Exception e) {
340 log.error(":deleteFlowEntry FlowEntryId:{} failed",
341 flowEntry.flowEntryId().toString());
342 return false;
343 }
344 //
345 // TODO: Don't print an error for now, because multiple controller
346 // instances might be deleting the same flow entry.
347 //
348 /*
349 if (flowEntryObj == null) {
350 log.error(":deleteFlowEntry FlowEntryId:{} failed: FlowEntry object not found",
351 flowEntry.flowEntryId().toString());
352 return false;
353 }
354 */
355 if (flowEntryObj == null)
356 return true;
357
358 flowObj.removeFlowEntry(flowEntryObj);
359 dbHandler.removeFlowEntry(flowEntryObj);
360 return true;
361 }
362
363 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700364 * Delete all previously added flows.
365 *
366 * @param dbHandler the Graph Database handler to use.
367 * @return true on success, otherwise false.
368 */
yoshitomob292c622013-11-23 14:35:58 -0800369 static boolean deleteAllFlows(DBOperation dbHandler) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700370 List<FlowId> allFlowIds = new LinkedList<FlowId>();
371
372 // Get all Flow IDs
373 Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths();
374 for (IFlowPath flowPathObj : allFlowPaths) {
375 if (flowPathObj == null)
376 continue;
377 String flowIdStr = flowPathObj.getFlowId();
378 if (flowIdStr == null)
379 continue;
380 FlowId flowId = new FlowId(flowIdStr);
381 allFlowIds.add(flowId);
382 }
383
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800384 // Delete all flows one-by-one
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700385 for (FlowId flowId : allFlowIds) {
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800386 deleteFlow(dbHandler, flowId);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700387 }
388
389 return true;
390 }
391
392 /**
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800393 * Delete a previously added flow.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700394 *
395 * @param dbHandler the Graph Database handler to use.
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800396 * @param flowId the Flow ID of the flow to delete.
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700397 * @return true on success, otherwise false.
398 */
yoshitomob292c622013-11-23 14:35:58 -0800399 static boolean deleteFlow(DBOperation dbHandler, FlowId flowId) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700400 IFlowPath flowObj = null;
401 try {
402 flowObj = dbHandler.searchFlowPath(flowId);
403 } catch (Exception e) {
404 // TODO: handle exceptions
405 dbHandler.rollback();
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800406 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700407 return false;
408 }
409 if (flowObj == null) {
410 dbHandler.commit();
411 return true; // OK: No such flow
412 }
413
414 //
415 // Remove all Flow Entries
416 //
417 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
418 for (IFlowEntry flowEntryObj : flowEntries) {
419 flowObj.removeFlowEntry(flowEntryObj);
420 dbHandler.removeFlowEntry(flowEntryObj);
421 }
422 // Remove the Flow itself
423 dbHandler.removeFlowPath(flowObj);
424 dbHandler.commit();
425
426 return true;
427 }
428
429 /**
430 * Get a previously added flow.
431 *
432 * @param dbHandler the Graph Database handler to use.
433 * @param flowId the Flow ID of the flow to get.
434 * @return the Flow Path if found, otherwise null.
435 */
yoshitomob292c622013-11-23 14:35:58 -0800436 static FlowPath getFlow(DBOperation dbHandler, FlowId flowId) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700437 IFlowPath flowObj = null;
438 try {
439 flowObj = dbHandler.searchFlowPath(flowId);
440 } catch (Exception e) {
441 // TODO: handle exceptions
442 dbHandler.rollback();
443 log.error(":getFlow FlowId:{} failed", flowId.toString());
444 return null;
445 }
446 if (flowObj == null) {
447 dbHandler.commit();
448 return null; // Flow not found
449 }
450
451 //
452 // Extract the Flow state
453 //
454 FlowPath flowPath = extractFlowPath(flowObj);
455 dbHandler.commit();
456
457 return flowPath;
458 }
459
460 /**
461 * Get all installed flows by all installers.
462 *
463 * @param dbHandler the Graph Database handler to use.
464 * @return the Flow Paths if found, otherwise null.
465 */
yoshitomob292c622013-11-23 14:35:58 -0800466 static ArrayList<FlowPath> getAllFlows(DBOperation dbHandler) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700467 Iterable<IFlowPath> flowPathsObj = null;
468 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
469
470 try {
471 flowPathsObj = dbHandler.getAllFlowPaths();
472 } catch (Exception e) {
473 // TODO: handle exceptions
474 dbHandler.rollback();
475 log.error(":getAllFlowPaths failed");
476 return flowPaths;
477 }
478 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
479 dbHandler.commit();
480 return flowPaths; // No Flows found
481 }
482
483 for (IFlowPath flowObj : flowPathsObj) {
484 //
485 // Extract the Flow state
486 //
487 FlowPath flowPath = extractFlowPath(flowObj);
488 if (flowPath != null)
489 flowPaths.add(flowPath);
490 }
491
492 dbHandler.commit();
493
494 return flowPaths;
495 }
496
497 /**
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700498 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
499 *
500 * @param flowObj the object to extract the Flow Path State from.
501 * @return the extracted Flow Path State.
502 */
503 private static FlowPath extractFlowPath(IFlowPath flowObj) {
504 //
505 // Extract the Flow state
506 //
507 String flowIdStr = flowObj.getFlowId();
508 String installerIdStr = flowObj.getInstallerId();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700509 String flowPathType = flowObj.getFlowPathType();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700510 String flowPathUserState = flowObj.getFlowPathUserState();
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700511 Long flowPathFlags = flowObj.getFlowPathFlags();
512 String srcSwitchStr = flowObj.getSrcSwitch();
513 Short srcPortShort = flowObj.getSrcPort();
514 String dstSwitchStr = flowObj.getDstSwitch();
515 Short dstPortShort = flowObj.getDstPort();
516
517 if ((flowIdStr == null) ||
518 (installerIdStr == null) ||
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700519 (flowPathType == null) ||
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700520 (flowPathUserState == null) ||
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700521 (flowPathFlags == null) ||
522 (srcSwitchStr == null) ||
523 (srcPortShort == null) ||
524 (dstSwitchStr == null) ||
525 (dstPortShort == null)) {
526 // TODO: A work-around, becauuse of some bogus database objects
527 return null;
528 }
529
530 FlowPath flowPath = new FlowPath();
531 flowPath.setFlowId(new FlowId(flowIdStr));
532 flowPath.setInstallerId(new CallerId(installerIdStr));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700533 flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700534 flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState));
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700535 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
536 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
537 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
538 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
539 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
540 //
541 // Extract the match conditions common for all Flow Entries
542 //
543 {
544 FlowEntryMatch match = new FlowEntryMatch();
545 String matchSrcMac = flowObj.getMatchSrcMac();
546 if (matchSrcMac != null)
547 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
548 String matchDstMac = flowObj.getMatchDstMac();
549 if (matchDstMac != null)
550 match.enableDstMac(MACAddress.valueOf(matchDstMac));
551 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
552 if (matchEthernetFrameType != null)
553 match.enableEthernetFrameType(matchEthernetFrameType);
554 Short matchVlanId = flowObj.getMatchVlanId();
555 if (matchVlanId != null)
556 match.enableVlanId(matchVlanId);
557 Byte matchVlanPriority = flowObj.getMatchVlanPriority();
558 if (matchVlanPriority != null)
559 match.enableVlanPriority(matchVlanPriority);
560 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
561 if (matchSrcIPv4Net != null)
562 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
563 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
564 if (matchDstIPv4Net != null)
565 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
566 Byte matchIpProto = flowObj.getMatchIpProto();
567 if (matchIpProto != null)
568 match.enableIpProto(matchIpProto);
569 Byte matchIpToS = flowObj.getMatchIpToS();
570 if (matchIpToS != null)
571 match.enableIpToS(matchIpToS);
572 Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort();
573 if (matchSrcTcpUdpPort != null)
574 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
575 Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort();
576 if (matchDstTcpUdpPort != null)
577 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
578
579 flowPath.setFlowEntryMatch(match);
580 }
581 //
582 // Extract the actions for the first Flow Entry
583 //
584 {
585 String actionsStr = flowObj.getActions();
586 if (actionsStr != null) {
587 FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr);
588 flowPath.setFlowEntryActions(flowEntryActions);
589 }
590 }
591
592 //
593 // Extract all Flow Entries
594 //
595 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
596 for (IFlowEntry flowEntryObj : flowEntries) {
597 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
598 if (flowEntry == null)
599 continue;
600 flowPath.dataPath().flowEntries().add(flowEntry);
601 }
602
603 return flowPath;
604 }
605
606 /**
607 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
608 *
609 * @param flowEntryObj the object to extract the Flow Entry State from.
610 * @return the extracted Flow Entry State.
611 */
Brian O'Connora8e49802013-10-30 20:49:59 -0700612 public static FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700613 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
614 String switchDpidStr = flowEntryObj.getSwitchDpid();
615 String userState = flowEntryObj.getUserState();
616 String switchState = flowEntryObj.getSwitchState();
617
618 if ((flowEntryIdStr == null) ||
619 (switchDpidStr == null) ||
620 (userState == null) ||
621 (switchState == null)) {
Brian O'Connora8e49802013-10-30 20:49:59 -0700622 // TODO: A work-around, because of some bogus database objects
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -0700623 return null;
624 }
625
626 FlowEntry flowEntry = new FlowEntry();
627 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
628 flowEntry.setDpid(new Dpid(switchDpidStr));
629
630 //
631 // Extract the match conditions
632 //
633 FlowEntryMatch match = new FlowEntryMatch();
634 Short matchInPort = flowEntryObj.getMatchInPort();
635 if (matchInPort != null)
636 match.enableInPort(new Port(matchInPort));
637 String matchSrcMac = flowEntryObj.getMatchSrcMac();
638 if (matchSrcMac != null)
639 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
640 String matchDstMac = flowEntryObj.getMatchDstMac();
641 if (matchDstMac != null)
642 match.enableDstMac(MACAddress.valueOf(matchDstMac));
643 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
644 if (matchEthernetFrameType != null)
645 match.enableEthernetFrameType(matchEthernetFrameType);
646 Short matchVlanId = flowEntryObj.getMatchVlanId();
647 if (matchVlanId != null)
648 match.enableVlanId(matchVlanId);
649 Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority();
650 if (matchVlanPriority != null)
651 match.enableVlanPriority(matchVlanPriority);
652 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
653 if (matchSrcIPv4Net != null)
654 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
655 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
656 if (matchDstIPv4Net != null)
657 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
658 Byte matchIpProto = flowEntryObj.getMatchIpProto();
659 if (matchIpProto != null)
660 match.enableIpProto(matchIpProto);
661 Byte matchIpToS = flowEntryObj.getMatchIpToS();
662 if (matchIpToS != null)
663 match.enableIpToS(matchIpToS);
664 Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort();
665 if (matchSrcTcpUdpPort != null)
666 match.enableSrcTcpUdpPort(matchSrcTcpUdpPort);
667 Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort();
668 if (matchDstTcpUdpPort != null)
669 match.enableDstTcpUdpPort(matchDstTcpUdpPort);
670 flowEntry.setFlowEntryMatch(match);
671
672 //
673 // Extract the actions
674 //
675 FlowEntryActions actions = new FlowEntryActions();
676 String actionsStr = flowEntryObj.getActions();
677 if (actionsStr != null)
678 actions = new FlowEntryActions(actionsStr);
679 flowEntry.setFlowEntryActions(actions);
680 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
681 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
682 //
683 // TODO: Take care of FlowEntryErrorState.
684 //
685 return flowEntry;
686 }
687}