blob: cce873a2ed56f28a9be6348858b6eda52584a22b [file] [log] [blame]
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001package net.floodlightcontroller.flowcache;
2
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08003import java.io.IOException;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08004import java.util.ArrayList;
5import java.util.Collection;
Jonathan Hartf5315fb2013-04-05 11:41:56 -07006import java.util.Collections;
7import java.util.Comparator;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08008import java.util.EnumSet;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08009import java.util.HashMap;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +000010import java.util.LinkedList;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080011import java.util.List;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080012import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000013import java.util.Random;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080014import java.util.concurrent.Executors;
15import java.util.concurrent.ScheduledExecutorService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080016import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080017
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080018import net.floodlightcontroller.core.IFloodlightProviderService;
19import net.floodlightcontroller.core.INetMapStorage;
20import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
21import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070022import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
Pankaj Berded0079742013-03-27 17:53:25 -070023import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070024import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080025import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080026import net.floodlightcontroller.core.module.FloodlightModuleContext;
27import net.floodlightcontroller.core.module.FloodlightModuleException;
28import net.floodlightcontroller.core.module.IFloodlightModule;
29import net.floodlightcontroller.core.module.IFloodlightService;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080030import net.floodlightcontroller.flowcache.web.FlowWebRoutable;
31import net.floodlightcontroller.restserver.IRestApiService;
32import net.floodlightcontroller.util.CallerId;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080033import net.floodlightcontroller.util.DataPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080034import net.floodlightcontroller.util.DataPathEndpoints;
Jonathan Hart01f2d272013-04-04 20:03:46 -070035import net.floodlightcontroller.util.Dpid;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080036import net.floodlightcontroller.util.FlowEntry;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070037import net.floodlightcontroller.util.FlowEntryAction;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080038import net.floodlightcontroller.util.FlowEntryId;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070039import net.floodlightcontroller.util.FlowEntryMatch;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080040import net.floodlightcontroller.util.FlowEntrySwitchState;
41import net.floodlightcontroller.util.FlowEntryUserState;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080042import net.floodlightcontroller.util.FlowId;
43import net.floodlightcontroller.util.FlowPath;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070044import net.floodlightcontroller.util.IPv4Net;
45import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080046import net.floodlightcontroller.util.OFMessageDamper;
47import net.floodlightcontroller.util.Port;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070048import net.floodlightcontroller.util.SwitchPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080049import net.onrc.onos.util.GraphDBConnection;
50import net.onrc.onos.util.GraphDBConnection.Transaction;
51
52import org.openflow.protocol.OFFlowMod;
53import org.openflow.protocol.OFMatch;
54import org.openflow.protocol.OFPacketOut;
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070055import org.openflow.protocol.OFPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080056import org.openflow.protocol.OFType;
57import org.openflow.protocol.action.OFAction;
58import org.openflow.protocol.action.OFActionOutput;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080059import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
61
Jonathan Hartf5315fb2013-04-05 11:41:56 -070062
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070063public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080064
65 public GraphDBConnection conn;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080066
67 protected IRestApiService restApi;
Jonathan Hart50a94982013-04-10 14:49:51 -070068 protected volatile IFloodlightProviderService floodlightProvider;
69 protected volatile ITopoRouteService topoRouteService;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070070 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080071
72 protected OFMessageDamper messageDamper;
73
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070074 //
75 // TODO: Values copied from elsewhere (class LearningSwitch).
76 // The local copy should go away!
77 //
78 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
79 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
80 public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity
81 public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite
82 public static final short PRIORITY_DEFAULT = 100;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080083
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000084 // Flow Entry ID generation state
85 private static Random randomGenerator = new Random();
86 private static int nextFlowEntryIdPrefix = 0;
87 private static int nextFlowEntryIdSuffix = 0;
88 private static long nextFlowEntryId = 0;
89
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -070090 // State for measurement purpose
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070091 private static long measurementFlowId = 100000;
92 private static String measurementFlowIdStr = "0x186a0"; // 100000
93 private long modifiedMeasurementFlowTime = 0;
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -070094 //
95 private LinkedList<FlowPath> measurementStoredPaths = new LinkedList<FlowPath>();
96 private LinkedList<FlowPath> measurementProcessingPaths = null;
97 private long measurementStartTimeProcessingPaths = 0;
98 private long measurementEndTimeProcessingPaths = 0;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070099
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800100 /** The logger. */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800101 private static Logger log = LoggerFactory.getLogger(FlowManager.class);
102
103 // The periodic task(s)
Jonathan Hart50a94982013-04-10 14:49:51 -0700104 private ScheduledExecutorService mapReaderScheduler;
105 private ScheduledExecutorService shortestPathReconcileScheduler;
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700106
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700107 final Runnable mapReader = new Runnable() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800108 public void run() {
Pavlin Radoslavova75caea2013-04-10 19:11:26 -0700109 try {
110 runImpl();
111 } catch (Exception e) {
112 log.debug("Exception processing All Flow Entries from the Network MAP: ", e);
113 conn.endTx(Transaction.ROLLBACK);
114 return;
115 }
116 }
117
118 private void runImpl() {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700119 long startTime = System.nanoTime();
120 int counterAllFlowEntries = 0;
121 int counterMyNotUpdatedFlowEntries = 0;
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700122
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800123 if (floodlightProvider == null) {
124 log.debug("FloodlightProvider service not found!");
125 return;
126 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000127 Map<Long, IOFSwitch> mySwitches =
128 floodlightProvider.getSwitches();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700129 LinkedList<IFlowEntry> addFlowEntries =
130 new LinkedList<IFlowEntry>();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000131 LinkedList<IFlowEntry> deleteFlowEntries =
132 new LinkedList<IFlowEntry>();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700133
134 //
Pankaj Berdea2e14a92013-04-15 11:59:15 -0700135 // Fetch all Flow Entries which need to be updated and select only my Flow Entries
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700136 // that need to be updated into the switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700137 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700138 boolean processed_measurement_flow = false;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000139 Iterable<IFlowEntry> allFlowEntries =
Pankaj Berded1c38592013-04-10 22:46:40 -0700140 conn.utils().getAllSwitchNotUpdatedFlowEntries(conn);
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700141 for (IFlowEntry flowEntryObj : allFlowEntries) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700142 counterAllFlowEntries++;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000143
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000144 String dpidStr = flowEntryObj.getSwitchDpid();
145 if (dpidStr == null)
146 continue;
147 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800148 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000149 if (mySwitch == null)
150 continue; // Ignore the entry: not my switch
151
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700152 IFlowPath flowObj =
153 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
154 if (flowObj == null)
155 continue; // Should NOT happen
156 if (flowObj.getFlowId() == null)
157 continue; // Invalid entry
158
159 //
160 // NOTE: For now we process the DELETE before the ADD
161 // to cover the more common scenario.
162 // TODO: This is error prone and needs to be fixed!
163 //
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000164 String userState = flowEntryObj.getUserState();
165 if (userState == null)
166 continue;
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700167 if (userState.equals("FE_USER_DELETE")) {
168 // An entry that needs to be deleted.
169 deleteFlowEntries.add(flowEntryObj);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700170 installFlowEntry(mySwitch, flowObj, flowEntryObj);
171 } else {
172 addFlowEntries.add(flowEntryObj);
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700173 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700174 counterMyNotUpdatedFlowEntries++;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700175 // Code for measurement purpose
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700176 // TODO: Commented-out for now
177 /*
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700178 {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700179 if (flowObj.getFlowId().equals(measurementFlowIdStr)) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700180 processed_measurement_flow = true;
181 }
182 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700183 */
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700184 }
185
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700186 //
187 // Process the Flow Entries that need to be added
188 //
189 for (IFlowEntry flowEntryObj : addFlowEntries) {
190 IFlowPath flowObj =
191 conn.utils().getFlowPathByFlowEntry(conn,
192 flowEntryObj);
193 if (flowObj == null)
194 continue; // Should NOT happen
195 if (flowObj.getFlowId() == null)
196 continue; // Invalid entry
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700197
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700198 Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid());
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700199 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000200 if (mySwitch == null)
201 continue; // Shouldn't happen
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700202 installFlowEntry(mySwitch, flowObj, flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800203 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000204
205 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000206 // Delete all Flow Entries marked for deletion from the
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -0700207 // Network MAP.
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000208 //
209 // TODO: We should use the OpenFlow Barrier mechanism
210 // to check for errors, and delete the Flow Entries after the
211 // Barrier message is received.
212 //
213 while (! deleteFlowEntries.isEmpty()) {
214 IFlowEntry flowEntryObj = deleteFlowEntries.poll();
215 IFlowPath flowObj =
216 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
217 if (flowObj == null) {
218 log.debug("Did not find FlowPath to be deleted");
219 continue;
220 }
221 flowObj.removeFlowEntry(flowEntryObj);
222 conn.utils().removeFlowEntry(conn, flowEntryObj);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000223 }
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700224
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700225 conn.endTx(Transaction.COMMIT);
226
227 if (processed_measurement_flow) {
228 long estimatedTime =
229 System.nanoTime() - modifiedMeasurementFlowTime;
230 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
231 (double)estimatedTime / 1000000000 + " sec";
232 log.debug(logMsg);
233 }
234
235 long estimatedTime = System.nanoTime() - startTime;
236 double rate = 0.0;
237 if (estimatedTime > 0)
238 rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime;
239 String logMsg = "MEASUREMENT: Processed AllFlowEntries: " +
240 counterAllFlowEntries + " MyNotUpdatedFlowEntries: " +
241 counterMyNotUpdatedFlowEntries + " in " +
242 (double)estimatedTime / 1000000000 + " sec: " +
243 rate + " paths/s";
244 log.debug(logMsg);
245 }
246 };
247
248 final Runnable shortestPathReconcile = new Runnable() {
249 public void run() {
Pavlin Radoslavova75caea2013-04-10 19:11:26 -0700250 try {
251 runImpl();
252 } catch (Exception e) {
253 log.debug("Exception processing All Flows from the Network MAP: ", e);
254 conn.endTx(Transaction.ROLLBACK);
255 return;
256 }
257 }
258
259 private void runImpl() {
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700260 long startTime = System.nanoTime();
261 int counterAllFlowPaths = 0;
262 int counterMyFlowPaths = 0;
263
264 if (floodlightProvider == null) {
265 log.debug("FloodlightProvider service not found!");
266 return;
267 }
268 Map<Long, IOFSwitch> mySwitches =
269 floodlightProvider.getSwitches();
270 LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>();
271
272 boolean processed_measurement_flow = false;
273
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700274 //
275 // Fetch and recompute the Shortest Path for those
276 // Flow Paths this controller is responsible for.
277 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700278 topoRouteService.prepareShortestPathTopo();
279 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700280 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700281 counterAllFlowPaths++;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700282 if (flowPathObj == null)
283 continue;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700284
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700285 String srcDpidStr = flowPathObj.getSrcSwitch();
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000286 if (srcDpidStr == null)
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700287 continue;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700288 Dpid srcDpid = new Dpid(srcDpidStr);
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700289 //
290 // Use the source DPID as a heuristic to decide
291 // which controller is responsible for maintaining the
292 // shortest path.
293 // NOTE: This heuristic is error-prone: if the switch
294 // goes away and no controller is responsible for that
295 // switch, then the original Flow Path is not cleaned-up
296 //
297 IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
298 if (mySwitch == null)
299 continue; // Ignore: not my responsibility
300
Pavlin Radoslavov99d1b152013-04-09 22:57:33 -0700301 // Test the Data Path Summary string
302 String dataPathSummaryStr = flowPathObj.getDataPathSummary();
303 if (dataPathSummaryStr == null)
304 continue; // Could be invalid entry?
305 if (dataPathSummaryStr.isEmpty())
306 continue; // No need to maintain this flow
307
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000308 //
309 // Test whether we need to complete the Flow cleanup,
310 // if the Flow has been deleted by the user.
311 //
312 String flowUserState = flowPathObj.getUserState();
313 if ((flowUserState != null)
314 && flowUserState.equals("FE_USER_DELETE")) {
315 Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries();
316 boolean empty = true; // TODO: an ugly hack
317 for (IFlowEntry flowEntryObj : flowEntries) {
318 empty = false;
319 break;
320 }
321 if (empty)
322 deleteFlows.add(flowPathObj);
323 }
324
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000325 // Fetch the fields needed to recompute the shortest path
326 Short srcPortShort = flowPathObj.getSrcPort();
327 String dstDpidStr = flowPathObj.getDstSwitch();
328 Short dstPortShort = flowPathObj.getDstPort();
329 if ((srcPortShort == null) ||
330 (dstDpidStr == null) ||
331 (dstPortShort == null)) {
332 continue;
333 }
334
335 Port srcPort = new Port(srcPortShort);
336 Dpid dstDpid = new Dpid(dstDpidStr);
337 Port dstPort = new Port(dstPortShort);
338 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
339 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
340
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700341 counterMyFlowPaths++;
342
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700343 //
344 // NOTE: Using here the regular getShortestPath() method
345 // won't work here, because that method calls internally
346 // "conn.endTx(Transaction.COMMIT)", and that will
347 // invalidate all handlers to the Titan database.
348 // If we want to experiment with calling here
349 // getShortestPath(), we need to refactor that code
350 // to avoid closing the transaction.
351 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700352 DataPath dataPath =
353 topoRouteService.getTopoShortestPath(srcSwitchPort,
354 dstSwitchPort);
Pavlin Radoslavov4a325822013-04-02 22:16:59 +0000355 if (dataPath == null) {
356 // We need the DataPath to compare the paths
357 dataPath = new DataPath();
358 dataPath.setSrcPort(srcSwitchPort);
359 dataPath.setDstPort(dstSwitchPort);
360 }
361
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700362 String newDataPathSummaryStr = dataPath.dataPathSummary();
363 if (dataPathSummaryStr.equals(newDataPathSummaryStr))
364 continue; // Nothing changed
365
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700366 reconcileFlow(flowPathObj, dataPath);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700367 }
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000368
369 //
370 // Delete all leftover Flows marked for deletion from the
371 // Network MAP.
372 //
373 while (! deleteFlows.isEmpty()) {
374 IFlowPath flowPathObj = deleteFlows.poll();
375 conn.utils().removeFlowPath(conn, flowPathObj);
376 }
377
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700378 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700379
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800380 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700381
382 if (processed_measurement_flow) {
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700383 long estimatedTime =
384 System.nanoTime() - modifiedMeasurementFlowTime;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700385 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
386 (double)estimatedTime / 1000000000 + " sec";
387 log.debug(logMsg);
388 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700389
390 long estimatedTime = System.nanoTime() - startTime;
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700391 double rate = 0.0;
392 if (estimatedTime > 0)
393 rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime;
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700394 String logMsg = "MEASUREMENT: Processed AllFlowPaths: " +
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700395 counterAllFlowPaths + " MyFlowPaths: " +
396 counterMyFlowPaths + " in " +
397 (double)estimatedTime / 1000000000 + " sec: " +
398 rate + " paths/s";
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700399 log.debug(logMsg);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800400 }
401 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700402
Jonathan Hart50a94982013-04-10 14:49:51 -0700403 //final ScheduledFuture<?> mapReaderHandle =
404 //mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800405
Jonathan Hart50a94982013-04-10 14:49:51 -0700406 //final ScheduledFuture<?> shortestPathReconcileHandle =
407 //shortestPathReconcileScheduler.scheduleAtFixedRate(shortestPathReconcile, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700408
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800409 @Override
410 public void init(String conf) {
Jonathan Hart50a94982013-04-10 14:49:51 -0700411 conn = GraphDBConnection.getInstance(conf);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800412 }
413
414 public void finalize() {
415 close();
416 }
417
418 @Override
419 public void close() {
420 conn.close();
421 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800422
423 @Override
424 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
425 Collection<Class<? extends IFloodlightService>> l =
426 new ArrayList<Class<? extends IFloodlightService>>();
427 l.add(IFlowService.class);
428 return l;
429 }
430
431 @Override
432 public Map<Class<? extends IFloodlightService>, IFloodlightService>
433 getServiceImpls() {
434 Map<Class<? extends IFloodlightService>,
435 IFloodlightService> m =
436 new HashMap<Class<? extends IFloodlightService>,
437 IFloodlightService>();
438 m.put(IFlowService.class, this);
439 return m;
440 }
441
442 @Override
443 public Collection<Class<? extends IFloodlightService>>
444 getModuleDependencies() {
445 Collection<Class<? extends IFloodlightService>> l =
446 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800447 l.add(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700448 l.add(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800449 l.add(IRestApiService.class);
450 return l;
451 }
452
453 @Override
454 public void init(FloodlightModuleContext context)
455 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700456 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800457 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700458 topoRouteService = context.getServiceImpl(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800459 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800460 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
461 EnumSet.of(OFType.FLOW_MOD),
462 OFMESSAGE_DAMPER_TIMEOUT);
463 // TODO: An ugly hack!
464 String conf = "/tmp/cassandra.titan";
465 this.init(conf);
Jonathan Hart50a94982013-04-10 14:49:51 -0700466
467 mapReaderScheduler = Executors.newScheduledThreadPool(1);
468 shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800469 }
470
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -0700471 private synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000472 //
473 // Generate the next Flow Entry ID.
474 // NOTE: For now, the higher 32 bits are random, and
475 // the lower 32 bits are sequential.
476 // In the future, we need a better allocation mechanism.
477 //
478 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
479 nextFlowEntryIdPrefix = randomGenerator.nextInt();
480 nextFlowEntryIdSuffix = 0;
481 } else {
482 nextFlowEntryIdSuffix++;
483 }
484 long result = (long)nextFlowEntryIdPrefix << 32;
485 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
486 return result;
487 }
488
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800489 @Override
490 public void startUp(FloodlightModuleContext context) {
Jonathan Hart50a94982013-04-10 14:49:51 -0700491 restApi.addRestletRoutable(new FlowWebRoutable());
492
493 // Initialize the Flow Entry ID generator
494 nextFlowEntryIdPrefix = randomGenerator.nextInt();
495
496 mapReaderScheduler.scheduleAtFixedRate(
Pankaj Berde55f121a2013-04-23 15:42:54 -0700497 mapReader, 1, 1, TimeUnit.SECONDS);
Jonathan Hart50a94982013-04-10 14:49:51 -0700498 shortestPathReconcileScheduler.scheduleAtFixedRate(
Pankaj Berdea2e14a92013-04-15 11:59:15 -0700499 shortestPathReconcile, 100, 100, TimeUnit.MILLISECONDS);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800500 }
501
502 /**
503 * Add a flow.
504 *
505 * Internally, ONOS will automatically register the installer for
506 * receiving Flow Path Notifications for that path.
507 *
508 * @param flowPath the Flow Path to install.
509 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700510 * @param dataPathSummaryStr the data path summary string if the added
511 * flow will be maintained internally, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800512 * @return true on success, otherwise false.
513 */
514 @Override
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700515 public boolean addFlow(FlowPath flowPath, FlowId flowId,
516 String dataPathSummaryStr) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700517 /*
518 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700519 if (flowPath.flowId().value() == measurementFlowId) {
520 modifiedMeasurementFlowTime = System.nanoTime();
521 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700522 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800523
524 IFlowPath flowObj = null;
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000525 boolean found = false;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800526 try {
527 if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId()))
528 != null) {
529 log.debug("Adding FlowPath with FlowId {}: found existing FlowPath",
530 flowPath.flowId().toString());
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000531 found = true;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800532 } else {
533 flowObj = conn.utils().newFlowPath(conn);
534 log.debug("Adding FlowPath with FlowId {}: creating new FlowPath",
535 flowPath.flowId().toString());
536 }
537 } catch (Exception e) {
538 // TODO: handle exceptions
539 conn.endTx(Transaction.ROLLBACK);
540 log.error(":addFlow FlowId:{} failed",
541 flowPath.flowId().toString());
542 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700543 if (flowObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000544 log.error(":addFlow FlowId:{} failed: Flow object not created",
545 flowPath.flowId().toString());
546 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800547 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700548 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800549
550 //
551 // Set the Flow key:
552 // - flowId
553 //
554 flowObj.setFlowId(flowPath.flowId().toString());
555 flowObj.setType("flow");
556
557 //
558 // Set the Flow attributes:
559 // - flowPath.installerId()
560 // - flowPath.dataPath().srcPort()
561 // - flowPath.dataPath().dstPort()
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700562 // - flowPath.matchEthernetFrameType()
563 // - flowPath.matchSrcIPv4Net()
564 // - flowPath.matchDstIPv4Net()
565 // - flowPath.matchSrcMac()
566 // - flowPath.matchDstMac()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800567 //
568 flowObj.setInstallerId(flowPath.installerId().toString());
569 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
570 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
571 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
572 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700573 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
574 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
575 }
576 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
577 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
578 }
579 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
580 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
581 }
582 if (flowPath.flowEntryMatch().matchSrcMac()) {
583 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
584 }
585 if (flowPath.flowEntryMatch().matchDstMac()) {
586 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
587 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800588
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700589 if (dataPathSummaryStr != null) {
590 flowObj.setDataPathSummary(dataPathSummaryStr);
591 } else {
592 flowObj.setDataPathSummary("");
593 }
594
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000595 if (found)
596 flowObj.setUserState("FE_USER_MODIFY");
597 else
598 flowObj.setUserState("FE_USER_ADD");
599
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800600 // Flow edges:
601 // HeadFE
602
603
604 //
605 // Flow Entries:
606 // flowPath.dataPath().flowEntries()
607 //
608 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700609 if (addFlowEntry(flowObj, flowEntry) == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000610 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800611 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700612 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800613 }
614 conn.endTx(Transaction.COMMIT);
615
616 //
617 // TODO: We need a proper Flow ID allocation mechanism.
618 //
619 flowId.setValue(flowPath.flowId().value());
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700620
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800621 return true;
622 }
623
624 /**
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700625 * Add a flow entry to the Network MAP.
626 *
627 * @param flowObj the corresponding Flow Path object for the Flow Entry.
628 * @param flowEntry the Flow Entry to install.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700629 * @return the added Flow Entry object on success, otherwise null.
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700630 */
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700631 private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700632 // Flow edges
633 // HeadFE (TODO)
634
635 //
636 // Assign the FlowEntry ID.
637 //
638 if ((flowEntry.flowEntryId() == null) ||
639 (flowEntry.flowEntryId().value() == 0)) {
640 long id = getNextFlowEntryId();
641 flowEntry.setFlowEntryId(new FlowEntryId(id));
642 }
643
644 IFlowEntry flowEntryObj = null;
645 boolean found = false;
646 try {
647 if ((flowEntryObj =
648 conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) {
649 log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry",
650 flowEntry.flowEntryId().toString());
651 found = true;
652 } else {
653 flowEntryObj = conn.utils().newFlowEntry(conn);
654 log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry",
655 flowEntry.flowEntryId().toString());
656 }
657 } catch (Exception e) {
658 log.error(":addFlow FlowEntryId:{} failed",
659 flowEntry.flowEntryId().toString());
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700660 return null;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700661 }
662 if (flowEntryObj == null) {
663 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
664 flowEntry.flowEntryId().toString());
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700665 return null;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700666 }
667
668 //
669 // Set the Flow Entry key:
670 // - flowEntry.flowEntryId()
671 //
672 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
673 flowEntryObj.setType("flow_entry");
674
675 //
676 // Set the Flow Entry Edges and attributes:
677 // - Switch edge
678 // - InPort edge
679 // - OutPort edge
680 //
681 // - flowEntry.flowEntryMatch()
682 // - flowEntry.flowEntryActions()
683 // - flowEntry.dpid()
684 // - flowEntry.flowEntryUserState()
685 // - flowEntry.flowEntrySwitchState()
686 // - flowEntry.flowEntryErrorState()
687 // - flowEntry.matchInPort()
688 // - flowEntry.matchEthernetFrameType()
689 // - flowEntry.matchSrcIPv4Net()
690 // - flowEntry.matchDstIPv4Net()
691 // - flowEntry.matchSrcMac()
692 // - flowEntry.matchDstMac()
693 // - flowEntry.actionOutput()
694 //
695 ISwitchObject sw =
696 conn.utils().searchSwitch(conn, flowEntry.dpid().toString());
697 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
698 flowEntryObj.setSwitch(sw);
699 if (flowEntry.flowEntryMatch().matchInPort()) {
700 IPortObject inport =
701 conn.utils().searchPort(conn, flowEntry.dpid().toString(),
702 flowEntry.flowEntryMatch().inPort().value());
703 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
704 flowEntryObj.setInPort(inport);
705 }
706 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
707 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
708 }
709 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
710 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
711 }
712 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
713 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
714 }
715 if (flowEntry.flowEntryMatch().matchSrcMac()) {
716 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
717 }
718 if (flowEntry.flowEntryMatch().matchDstMac()) {
719 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
720 }
721
722 for (FlowEntryAction fa : flowEntry.flowEntryActions()) {
723 if (fa.actionOutput() != null) {
724 IPortObject outport =
725 conn.utils().searchPort(conn,
726 flowEntry.dpid().toString(),
727 fa.actionOutput().port().value());
728 flowEntryObj.setActionOutput(fa.actionOutput().port().value());
729 flowEntryObj.setOutPort(outport);
730 }
731 }
732 // TODO: Hacks with hard-coded state names!
733 if (found)
734 flowEntryObj.setUserState("FE_USER_MODIFY");
735 else
736 flowEntryObj.setUserState("FE_USER_ADD");
737 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
738 //
739 // TODO: Take care of the FlowEntryErrorState.
740 //
741
742 // Flow Entries edges:
743 // Flow
744 // NextFE (TODO)
745 if (! found) {
746 flowObj.addFlowEntry(flowEntryObj);
747 flowEntryObj.setFlow(flowObj);
748 }
749
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700750 return flowEntryObj;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700751 }
752
753 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800754 * Delete a previously added flow.
755 *
756 * @param flowId the Flow ID of the flow to delete.
757 * @return true on success, otherwise false.
758 */
759 @Override
760 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700761 /*
762 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700763 if (flowId.value() == measurementFlowId) {
764 modifiedMeasurementFlowTime = System.nanoTime();
765 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700766 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700767
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800768 IFlowPath flowObj = null;
769 //
770 // We just mark the entries for deletion,
771 // and let the switches remove each individual entry after
772 // it has been removed from the switches.
773 //
774 try {
775 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
776 != null) {
777 log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath",
778 flowId.toString());
779 } else {
780 log.debug("Deleting FlowPath with FlowId {}: FlowPath not found",
781 flowId.toString());
782 }
783 } catch (Exception e) {
784 // TODO: handle exceptions
785 conn.endTx(Transaction.ROLLBACK);
786 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
787 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700788 if (flowObj == null) {
789 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800790 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700791 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800792
793 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000794 // Find and mark for deletion all Flow Entries,
795 // and the Flow itself.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800796 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000797 flowObj.setUserState("FE_USER_DELETE");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800798 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
799 boolean empty = true; // TODO: an ugly hack
800 for (IFlowEntry flowEntryObj : flowEntries) {
801 empty = false;
802 // flowObj.removeFlowEntry(flowEntryObj);
803 // conn.utils().removeFlowEntry(conn, flowEntryObj);
804 flowEntryObj.setUserState("FE_USER_DELETE");
805 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
806 }
807 // Remove from the database empty flows
808 if (empty)
809 conn.utils().removeFlowPath(conn, flowObj);
810 conn.endTx(Transaction.COMMIT);
811
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800812 return true;
813 }
814
815 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700816 * Clear the state for a previously added flow.
817 *
818 * @param flowId the Flow ID of the flow to clear.
819 * @return true on success, otherwise false.
820 */
821 @Override
822 public boolean clearFlow(FlowId flowId) {
823 IFlowPath flowObj = null;
824 try {
825 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
826 != null) {
827 log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath",
828 flowId.toString());
829 } else {
830 log.debug("Clearing FlowPath with FlowId {}: FlowPath not found",
831 flowId.toString());
832 }
833 } catch (Exception e) {
834 // TODO: handle exceptions
835 conn.endTx(Transaction.ROLLBACK);
836 log.error(":clearFlow FlowId:{} failed", flowId.toString());
837 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700838 if (flowObj == null) {
839 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700840 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700841 }
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700842
843 //
844 // Remove all Flow Entries
845 //
846 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
847 for (IFlowEntry flowEntryObj : flowEntries) {
848 flowObj.removeFlowEntry(flowEntryObj);
849 conn.utils().removeFlowEntry(conn, flowEntryObj);
850 }
851 // Remove the Flow itself
852 conn.utils().removeFlowPath(conn, flowObj);
853 conn.endTx(Transaction.COMMIT);
854
855 return true;
856 }
857
858 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800859 * Get a previously added flow.
860 *
861 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800862 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800863 */
864 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800865 public FlowPath getFlow(FlowId flowId) {
866 IFlowPath flowObj = null;
867 try {
868 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
869 != null) {
870 log.debug("Get FlowPath with FlowId {}: found existing FlowPath",
871 flowId.toString());
872 } else {
873 log.debug("Get FlowPath with FlowId {}: FlowPath not found",
874 flowId.toString());
875 }
876 } catch (Exception e) {
877 // TODO: handle exceptions
878 conn.endTx(Transaction.ROLLBACK);
879 log.error(":getFlow FlowId:{} failed", flowId.toString());
880 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700881 if (flowObj == null) {
882 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800883 return null; // Flow not found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700884 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800885
886 //
887 // Extract the Flow state
888 //
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800889 FlowPath flowPath = extractFlowPath(flowObj);
890 conn.endTx(Transaction.COMMIT);
891
892 return flowPath;
893 }
894
895 /**
896 * Get all previously added flows by a specific installer for a given
897 * data path endpoints.
898 *
899 * @param installerId the Caller ID of the installer of the flow to get.
900 * @param dataPathEndpoints the data path endpoints of the flow to get.
901 * @return the Flow Paths if found, otherwise null.
902 */
903 @Override
904 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
905 DataPathEndpoints dataPathEndpoints) {
906 //
907 // TODO: The implementation below is not optimal:
908 // We fetch all flows, and then return only the subset that match
909 // the query conditions.
910 // We should use the appropriate Titan/Gremlin query to filter-out
911 // the flows as appropriate.
912 //
913 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700914 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800915
916 if (allFlows == null) {
917 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700918 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800919 }
920
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800921 for (FlowPath flow : allFlows) {
922 //
923 // TODO: String-based comparison is sub-optimal.
924 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -0800925 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800926 //
927 if (! flow.installerId().toString().equals(installerId.toString()))
928 continue;
929 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
930 continue;
931 }
932 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
933 continue;
934 }
935 flowPaths.add(flow);
936 }
937
938 if (flowPaths.isEmpty()) {
939 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800940 } else {
941 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints);
942 }
943
944 return flowPaths;
945 }
946
947 /**
948 * Get all installed flows by all installers for given data path endpoints.
949 *
950 * @param dataPathEndpoints the data path endpoints of the flows to get.
951 * @return the Flow Paths if found, otherwise null.
952 */
953 @Override
954 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
955 //
956 // TODO: The implementation below is not optimal:
957 // We fetch all flows, and then return only the subset that match
958 // the query conditions.
959 // We should use the appropriate Titan/Gremlin query to filter-out
960 // the flows as appropriate.
961 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700962 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
963 ArrayList<FlowPath> allFlows = getAllFlows();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800964
965 if (allFlows == null) {
966 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700967 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800968 }
969
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800970 for (FlowPath flow : allFlows) {
971 //
972 // TODO: String-based comparison is sub-optimal.
973 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -0800974 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800975 //
976 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
977 continue;
978 }
979 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
980 continue;
981 }
982 flowPaths.add(flow);
983 }
984
985 if (flowPaths.isEmpty()) {
986 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800987 } else {
988 log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
989 }
990
991 return flowPaths;
992 }
993
994 /**
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700995 * Get summary of all installed flows by all installers in a given range
996 *
997 * @param flowId the data path endpoints of the flows to get.
998 * @param maxFlows: the maximum number of flows to be returned
999 * @return the Flow Paths if found, otherwise null.
1000 */
1001 @Override
Jonathan Hart01f2d272013-04-04 20:03:46 -07001002 public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) {
Jonathan Hartf5315fb2013-04-05 11:41:56 -07001003
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001004 // TODO: The implementation below is not optimal:
1005 // We fetch all flows, and then return only the subset that match
1006 // the query conditions.
1007 // We should use the appropriate Titan/Gremlin query to filter-out
1008 // the flows as appropriate.
1009 //
Jonathan Hart01f2d272013-04-04 20:03:46 -07001010 //ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001011
Jonathan Hart01f2d272013-04-04 20:03:46 -07001012 ArrayList<IFlowPath> flowPathsWithoutFlowEntries = getAllFlowsWithoutFlowEntries();
1013
Jonathan Hartf5315fb2013-04-05 11:41:56 -07001014 Collections.sort(flowPathsWithoutFlowEntries,
1015 new Comparator<IFlowPath>(){
1016 @Override
1017 public int compare(IFlowPath first, IFlowPath second) {
1018 // TODO Auto-generated method stub
1019 long result = new FlowId(first.getFlowId()).value()
1020 - new FlowId(second.getFlowId()).value();
1021 if (result > 0) return 1;
1022 else if (result < 0) return -1;
1023 else return 0;
1024 }
1025 }
1026 );
1027
Jonathan Hart01f2d272013-04-04 20:03:46 -07001028 return flowPathsWithoutFlowEntries;
1029
1030 /*
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001031 ArrayList<FlowPath> allFlows = getAllFlows();
Jonathan Hart01f2d272013-04-04 20:03:46 -07001032
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001033 if (allFlows == null) {
1034 log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001035 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001036 }
1037
Umesh Krishnaswamy244b4ae2013-03-29 12:05:15 -07001038 Collections.sort(allFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001039
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001040 for (FlowPath flow : allFlows) {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001041 flow.setFlowEntryMatch(null);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001042
Pavlin Radoslavov96b43422013-04-04 19:14:56 -07001043 // start from desired flowId
1044 if (flow.flowId().value() < flowId.value()) {
1045 continue;
1046 }
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001047
1048 // Summarize by making null flow entry fields that are not relevant to report
1049 for (FlowEntry flowEntry : flow.dataPath().flowEntries()) {
1050 flowEntry.setFlowEntryActions(null);
1051 flowEntry.setFlowEntryMatch(null);
1052 }
1053
1054 flowPaths.add(flow);
1055 if (maxFlows != 0 && flowPaths.size() >= maxFlows) {
1056 break;
1057 }
1058 }
1059
1060 if (flowPaths.isEmpty()) {
1061 log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001062 } else {
1063 log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows);
1064 }
1065
1066 return flowPaths;
Jonathan Hart01f2d272013-04-04 20:03:46 -07001067 */
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001068 }
1069
1070 /**
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001071 * Get all installed flows by all installers.
1072 *
1073 * @return the Flow Paths if found, otherwise null.
1074 */
1075 @Override
1076 public ArrayList<FlowPath> getAllFlows() {
1077 Iterable<IFlowPath> flowPathsObj = null;
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001078 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001079
1080 try {
1081 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
1082 log.debug("Get all FlowPaths: found FlowPaths");
1083 } else {
1084 log.debug("Get all FlowPaths: no FlowPaths found");
1085 }
1086 } catch (Exception e) {
1087 // TODO: handle exceptions
1088 conn.endTx(Transaction.ROLLBACK);
1089 log.error(":getAllFlowPaths failed");
1090 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001091 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1092 conn.endTx(Transaction.COMMIT);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001093 return flowPaths; // No Flows found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001094 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001095
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001096 for (IFlowPath flowObj : flowPathsObj) {
1097 //
1098 // Extract the Flow state
1099 //
1100 FlowPath flowPath = extractFlowPath(flowObj);
Pavlin Radoslavov3f2af732013-03-29 15:29:35 -07001101 if (flowPath != null)
1102 flowPaths.add(flowPath);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001103 }
1104
1105 conn.endTx(Transaction.COMMIT);
1106
1107 return flowPaths;
1108 }
Jonathan Hart01f2d272013-04-04 20:03:46 -07001109
1110 public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(){
1111 Iterable<IFlowPath> flowPathsObj = null;
1112 ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>();
1113 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1114
Jonathan Harte6e91872013-04-13 11:10:32 -07001115 conn.endTx(Transaction.COMMIT);
1116
Jonathan Hart01f2d272013-04-04 20:03:46 -07001117 try {
1118 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
1119 log.debug("Get all FlowPaths: found FlowPaths");
1120 } else {
1121 log.debug("Get all FlowPaths: no FlowPaths found");
1122 }
1123 } catch (Exception e) {
1124 // TODO: handle exceptions
1125 conn.endTx(Transaction.ROLLBACK);
1126 log.error(":getAllFlowPaths failed");
1127 }
1128 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1129 return new ArrayList<IFlowPath>(); // No Flows found
1130 }
1131
1132 for (IFlowPath flowObj : flowPathsObj){
1133 flowPathsObjArray.add(flowObj);
1134 }
1135 /*
1136 for (IFlowPath flowObj : flowPathsObj) {
1137 //
1138 // Extract the Flow state
1139 //
1140 FlowPath flowPath = extractFlowPath(flowObj);
1141 if (flowPath != null)
1142 flowPaths.add(flowPath);
1143 }
1144 */
1145
1146 //conn.endTx(Transaction.COMMIT);
1147
1148 return flowPathsObjArray;
1149 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001150
1151 /**
1152 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
1153 *
1154 * @param flowObj the object to extract the Flow Path State from.
1155 * @return the extracted Flow Path State.
1156 */
1157 private FlowPath extractFlowPath(IFlowPath flowObj) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001158 //
1159 // Extract the Flow state
1160 //
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001161 String flowIdStr = flowObj.getFlowId();
1162 String installerIdStr = flowObj.getInstallerId();
1163 String srcSwitchStr = flowObj.getSrcSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001164 Short srcPortShort = flowObj.getSrcPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001165 String dstSwitchStr = flowObj.getDstSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001166 Short dstPortShort = flowObj.getDstPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001167
1168 if ((flowIdStr == null) ||
1169 (installerIdStr == null) ||
1170 (srcSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001171 (srcPortShort == null) ||
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001172 (dstSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001173 (dstPortShort == null)) {
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001174 // TODO: A work-around, becauuse of some bogus database objects
1175 return null;
1176 }
1177
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001178 FlowPath flowPath = new FlowPath();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001179 flowPath.setFlowId(new FlowId(flowIdStr));
1180 flowPath.setInstallerId(new CallerId(installerIdStr));
1181 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001182 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001183 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001184 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001185 //
1186 // Extract the match conditions common for all Flow Entries
1187 //
1188 {
1189 FlowEntryMatch match = new FlowEntryMatch();
1190 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1191 if (matchEthernetFrameType != null)
1192 match.enableEthernetFrameType(matchEthernetFrameType);
1193 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1194 if (matchSrcIPv4Net != null)
1195 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1196 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1197 if (matchDstIPv4Net != null)
1198 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1199 String matchSrcMac = flowObj.getMatchSrcMac();
1200 if (matchSrcMac != null)
1201 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1202 String matchDstMac = flowObj.getMatchDstMac();
1203 if (matchDstMac != null)
1204 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1205 flowPath.setFlowEntryMatch(match);
1206 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001207
1208 //
1209 // Extract all Flow Entries
1210 //
1211 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1212 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001213 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
1214 if (flowEntry == null)
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001215 continue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001216 flowPath.dataPath().flowEntries().add(flowEntry);
1217 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001218
1219 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001220 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001221
1222 /**
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001223 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
1224 *
1225 * @param flowEntryObj the object to extract the Flow Entry State from.
1226 * @return the extracted Flow Entry State.
1227 */
1228 private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
1229 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1230 String switchDpidStr = flowEntryObj.getSwitchDpid();
1231 String userState = flowEntryObj.getUserState();
1232 String switchState = flowEntryObj.getSwitchState();
1233
1234 if ((flowEntryIdStr == null) ||
1235 (switchDpidStr == null) ||
1236 (userState == null) ||
1237 (switchState == null)) {
1238 // TODO: A work-around, becauuse of some bogus database objects
1239 return null;
1240 }
1241
1242 FlowEntry flowEntry = new FlowEntry();
1243 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
1244 flowEntry.setDpid(new Dpid(switchDpidStr));
1245
1246 //
1247 // Extract the match conditions
1248 //
1249 FlowEntryMatch match = new FlowEntryMatch();
1250 Short matchInPort = flowEntryObj.getMatchInPort();
1251 if (matchInPort != null)
1252 match.enableInPort(new Port(matchInPort));
1253 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1254 if (matchEthernetFrameType != null)
1255 match.enableEthernetFrameType(matchEthernetFrameType);
1256 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1257 if (matchSrcIPv4Net != null)
1258 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1259 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1260 if (matchDstIPv4Net != null)
1261 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1262 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1263 if (matchSrcMac != null)
1264 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1265 String matchDstMac = flowEntryObj.getMatchDstMac();
1266 if (matchDstMac != null)
1267 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1268 flowEntry.setFlowEntryMatch(match);
1269
1270 //
1271 // Extract the actions
1272 //
1273 ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>();
1274 Short actionOutputPort = flowEntryObj.getActionOutput();
1275 if (actionOutputPort != null) {
1276 FlowEntryAction action = new FlowEntryAction();
1277 action.setActionOutput(new Port(actionOutputPort));
1278 actions.add(action);
1279 }
1280 flowEntry.setFlowEntryActions(actions);
1281 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
1282 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1283 //
1284 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
1285 // and FlowEntryErrorState.
1286 //
1287 return flowEntry;
1288 }
1289
1290 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001291 * Add and maintain a shortest-path flow.
1292 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001293 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001294 *
1295 * @param flowPath the Flow Path with the endpoints and the match
1296 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001297 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001298 */
1299 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001300 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001301 //
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +00001302 // Don't do the shortest path computation here.
1303 // Instead, let the Flow reconciliation thread take care of it.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001304 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001305
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +00001306 // We need the DataPath to populate the Network MAP
1307 DataPath dataPath = new DataPath();
1308 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1309 dataPath.setDstPort(flowPath.dataPath().dstPort());
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001310
1311 //
1312 // Prepare the computed Flow Path
1313 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001314 FlowPath computedFlowPath = new FlowPath();
1315 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1316 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1317 computedFlowPath.setDataPath(dataPath);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001318 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001319
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001320 FlowId flowId = new FlowId();
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +00001321 String dataPathSummaryStr = dataPath.dataPathSummary();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001322 if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr))
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001323 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001324
1325 // TODO: Mark the flow for maintenance purpose
1326
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001327 return (computedFlowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001328 }
1329
1330 /**
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001331 * Reconcile a flow.
1332 *
1333 * @param flowObj the flow that needs to be reconciliated.
1334 * @param newDataPath the new data path to use.
1335 * @return true on success, otherwise false.
1336 */
1337 public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) {
1338 Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches();
1339
1340 //
1341 // Set the incoming port matching and the outgoing port output
1342 // actions for each flow entry.
1343 //
1344 for (FlowEntry flowEntry : newDataPath.flowEntries()) {
1345 // Set the incoming port matching
1346 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
1347 flowEntry.setFlowEntryMatch(flowEntryMatch);
1348 flowEntryMatch.enableInPort(flowEntry.inPort());
1349
1350 // Set the outgoing port output action
1351 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1352 if (flowEntryActions == null) {
1353 flowEntryActions = new ArrayList<FlowEntryAction>();
1354 flowEntry.setFlowEntryActions(flowEntryActions);
1355 }
1356 FlowEntryAction flowEntryAction = new FlowEntryAction();
1357 flowEntryAction.setActionOutput(flowEntry.outPort());
1358 flowEntryActions.add(flowEntryAction);
1359 }
1360
1361 //
1362 // Remove the old Flow Entries, and add the new Flow Entries
1363 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001364 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1365 LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>();
1366 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001367 flowEntryObj.setUserState("FE_USER_DELETE");
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -07001368 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001369 }
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001370 for (FlowEntry flowEntry : newDataPath.flowEntries()) {
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -07001371 addFlowEntry(flowObj, flowEntry);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001372 }
1373
1374 //
1375 // Set the Data Path Summary
1376 //
1377 String dataPathSummaryStr = newDataPath.dataPathSummary();
1378 flowObj.setDataPathSummary(dataPathSummaryStr);
1379
1380 return true;
1381 }
1382
1383 /**
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001384 * Reconcile all flows in a set.
1385 *
1386 * @param flowObjSet the set of flows that need to be reconciliated.
1387 */
1388 public void reconcileFlows(Iterable<IFlowPath> flowObjSet) {
1389 if (! flowObjSet.iterator().hasNext())
1390 return;
Pavlin Radoslavov0eeb15d2013-04-05 10:23:51 -07001391 // TODO: Not implemented/used yet.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001392 }
1393
1394 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001395 * Install a Flow Entry on a switch.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001396 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001397 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001398 * @param flowObj the flow path object for the flow entry to install.
1399 * @param flowEntryObj the flow entry object to install.
1400 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001401 */
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001402 public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj,
1403 IFlowEntry flowEntryObj) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001404 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1405 if (flowEntryIdStr == null)
1406 return false;
1407 FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr);
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001408 String userState = flowEntryObj.getUserState();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001409 if (userState == null)
1410 return false;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001411
1412 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001413 // Create the Open Flow Flow Modification Entry to push
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001414 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001415 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1416 .getMessage(OFType.FLOW_MOD);
1417 long cookie = flowEntryId.value();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001418
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001419 short flowModCommand = OFFlowMod.OFPFC_ADD;
1420 if (userState.equals("FE_USER_ADD")) {
1421 flowModCommand = OFFlowMod.OFPFC_ADD;
1422 } else if (userState.equals("FE_USER_MODIFY")) {
1423 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1424 } else if (userState.equals("FE_USER_DELETE")) {
1425 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1426 } else {
1427 // Unknown user state. Ignore the entry
1428 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1429 flowEntryId.toString(), userState);
1430 return false;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001431 }
1432
1433 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001434 // Fetch the match conditions.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001435 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001436 // NOTE: The Flow matching conditions common for all Flow Entries are
1437 // used ONLY if a Flow Entry does NOT have the corresponding matching
1438 // condition set.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001439 //
1440 OFMatch match = new OFMatch();
1441 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001442
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001443 // Match the Incoming Port
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001444 Short matchInPort = flowEntryObj.getMatchInPort();
1445 if (matchInPort != null) {
1446 match.setInputPort(matchInPort);
1447 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1448 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001449
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001450 // Match the Ethernet Frame Type
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001451 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1452 if (matchEthernetFrameType == null)
1453 matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1454 if (matchEthernetFrameType != null) {
1455 match.setDataLayerType(matchEthernetFrameType);
1456 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1457 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001458
1459 // Match the Source IPv4 Network prefix
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001460 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1461 if (matchSrcIPv4Net == null)
1462 matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1463 if (matchSrcIPv4Net != null) {
1464 match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC);
1465 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001466
1467 // Natch the Destination IPv4 Network prefix
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001468 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1469 if (matchDstIPv4Net == null)
1470 matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1471 if (matchDstIPv4Net != null) {
1472 match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST);
1473 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001474
1475 // Match the Source MAC address
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001476 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1477 if (matchSrcMac == null)
1478 matchSrcMac = flowObj.getMatchSrcMac();
1479 if (matchSrcMac != null) {
1480 match.setDataLayerSource(matchSrcMac);
1481 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1482 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001483
1484 // Match the Destination MAC address
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001485 String matchDstMac = flowEntryObj.getMatchDstMac();
1486 if (matchDstMac == null)
1487 matchDstMac = flowObj.getMatchDstMac();
1488 if (matchDstMac != null) {
1489 match.setDataLayerDestination(matchDstMac);
1490 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1491 }
1492
1493 //
1494 // Fetch the actions
1495 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001496 // TODO: For now we support only the "OUTPUT" actions.
1497 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001498 List<OFAction> actions = new ArrayList<OFAction>();
1499 Short actionOutputPort = flowEntryObj.getActionOutput();
1500 if (actionOutputPort != null) {
1501 OFActionOutput action = new OFActionOutput();
1502 // XXX: The max length is hard-coded for now
1503 action.setMaxLength((short)0xffff);
1504 action.setPort(actionOutputPort);
1505 actions.add(action);
1506 }
1507
1508 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1509 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1510 .setPriority(PRIORITY_DEFAULT)
1511 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1512 .setCookie(cookie)
1513 .setCommand(flowModCommand)
1514 .setMatch(match)
1515 .setActions(actions)
1516 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1517 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1518 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1519 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1520 if (actionOutputPort != null)
1521 fm.setOutPort(actionOutputPort);
1522 }
1523
1524 //
1525 // TODO: Set the following flag
1526 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1527 // See method ForwardingBase::pushRoute()
1528 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001529
1530 //
1531 // Write the message to the switch
1532 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001533 log.debug("MEASUREMENT: Installing flow entry " + userState +
1534 " into switch DPID: " +
1535 mySwitch.getStringId() +
1536 " flowEntryId: " + flowEntryId.toString() +
1537 " srcMac: " + matchSrcMac + " dstMac: " + matchDstMac +
1538 " inPort: " + matchInPort + " outPort: " + actionOutputPort
1539 );
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001540 try {
1541 messageDamper.write(mySwitch, fm, null);
1542 mySwitch.flush();
1543 //
1544 // TODO: We should use the OpenFlow Barrier mechanism
1545 // to check for errors, and update the SwitchState
1546 // for a flow entry after the Barrier message is
1547 // is received.
1548 //
1549 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
1550 } catch (IOException e) {
1551 log.error("Failure writing flow mod from network map", e);
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001552 return false;
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001553 }
1554
1555 return true;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001556 }
1557
1558 /**
1559 * Install a Flow Entry on a switch.
1560 *
1561 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001562 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001563 * @param flowEntry the flow entry to install.
1564 * @return true on success, otherwise false.
1565 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001566 public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1567 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001568 //
1569 // Create the OpenFlow Flow Modification Entry to push
1570 //
1571 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1572 .getMessage(OFType.FLOW_MOD);
1573 long cookie = flowEntry.flowEntryId().value();
1574
1575 short flowModCommand = OFFlowMod.OFPFC_ADD;
1576 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
1577 flowModCommand = OFFlowMod.OFPFC_ADD;
1578 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
1579 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1580 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
1581 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1582 } else {
1583 // Unknown user state. Ignore the entry
1584 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1585 flowEntry.flowEntryId().toString(),
1586 flowEntry.flowEntryUserState());
1587 return false;
1588 }
1589
1590 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001591 // Fetch the match conditions.
1592 //
1593 // NOTE: The Flow matching conditions common for all Flow Entries are
1594 // used ONLY if a Flow Entry does NOT have the corresponding matching
1595 // condition set.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001596 //
1597 OFMatch match = new OFMatch();
1598 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001599 FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch();
1600 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
1601
1602 // Match the Incoming Port
1603 Port matchInPort = flowEntryMatch.inPort();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001604 if (matchInPort != null) {
1605 match.setInputPort(matchInPort.value());
1606 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1607 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001608
1609 // Match the Ethernet Frame Type
1610 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
1611 if ((matchEthernetFrameType == null) && (flowPathMatch != null)) {
1612 matchEthernetFrameType = flowPathMatch.ethernetFrameType();
1613 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001614 if (matchEthernetFrameType != null) {
1615 match.setDataLayerType(matchEthernetFrameType);
1616 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1617 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001618
1619 // Match the Source IPv4 Network prefix
1620 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
1621 if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) {
1622 matchSrcIPv4Net = flowPathMatch.srcIPv4Net();
1623 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001624 if (matchSrcIPv4Net != null) {
1625 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
1626 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001627
1628 // Natch the Destination IPv4 Network prefix
1629 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
1630 if ((matchDstIPv4Net == null) && (flowPathMatch != null)) {
1631 matchDstIPv4Net = flowPathMatch.dstIPv4Net();
1632 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001633 if (matchDstIPv4Net != null) {
1634 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
1635 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001636
1637 // Match the Source MAC address
1638 MACAddress matchSrcMac = flowEntryMatch.srcMac();
1639 if ((matchSrcMac == null) && (flowPathMatch != null)) {
1640 matchSrcMac = flowPathMatch.srcMac();
1641 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001642 if (matchSrcMac != null) {
1643 match.setDataLayerSource(matchSrcMac.toString());
1644 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1645 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001646
1647 // Match the Destination MAC address
1648 MACAddress matchDstMac = flowEntryMatch.dstMac();
1649 if ((matchDstMac == null) && (flowPathMatch != null)) {
1650 matchDstMac = flowPathMatch.dstMac();
1651 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001652 if (matchDstMac != null) {
1653 match.setDataLayerDestination(matchDstMac.toString());
1654 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1655 }
1656
1657 //
1658 // Fetch the actions
1659 //
1660 // TODO: For now we support only the "OUTPUT" actions.
1661 //
1662 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1663 List<OFAction> actions = new ArrayList<OFAction>();
1664 ArrayList<FlowEntryAction> flowEntryActions =
1665 flowEntry.flowEntryActions();
1666 for (FlowEntryAction flowEntryAction : flowEntryActions) {
1667 FlowEntryAction.ActionOutput actionOutput =
1668 flowEntryAction.actionOutput();
1669 if (actionOutput != null) {
1670 short actionOutputPort = actionOutput.port().value();
1671 OFActionOutput action = new OFActionOutput();
1672 // XXX: The max length is hard-coded for now
1673 action.setMaxLength((short)0xffff);
1674 action.setPort(actionOutputPort);
1675 actions.add(action);
1676 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1677 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1678 fm.setOutPort(actionOutputPort);
1679 }
1680 }
1681 }
1682
1683 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1684 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1685 .setPriority(PRIORITY_DEFAULT)
1686 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1687 .setCookie(cookie)
1688 .setCommand(flowModCommand)
1689 .setMatch(match)
1690 .setActions(actions)
1691 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1692
1693 //
1694 // TODO: Set the following flag
1695 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1696 // See method ForwardingBase::pushRoute()
1697 //
1698
1699 //
1700 // Write the message to the switch
1701 //
1702 try {
1703 messageDamper.write(mySwitch, fm, null);
1704 mySwitch.flush();
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001705 //
1706 // TODO: We should use the OpenFlow Barrier mechanism
1707 // to check for errors, and update the SwitchState
1708 // for a flow entry after the Barrier message is
1709 // is received.
1710 //
1711 // TODO: The FlowEntry Object in Titan should be set
1712 // to FE_SWITCH_UPDATED.
1713 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001714 } catch (IOException e) {
1715 log.error("Failure writing flow mod from network map", e);
1716 return false;
1717 }
1718 return true;
1719 }
1720
1721 /**
1722 * Remove a Flow Entry from a switch.
1723 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001724 * @param mySwitch the switch to remove the Flow Entry from.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001725 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001726 * @param flowEntry the flow entry to remove.
1727 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001728 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001729 public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1730 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001731 //
1732 // The installFlowEntry() method implements both installation
1733 // and removal of flow entries.
1734 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001735 return (installFlowEntry(mySwitch, flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001736 }
1737
1738 /**
1739 * Install a Flow Entry on a remote controller.
1740 *
1741 * TODO: We need it now: Jono
1742 * - For now it will make a REST call to the remote controller.
1743 * - Internally, it needs to know the name of the remote controller.
1744 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001745 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001746 * @param flowEntry the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001747 * @return true on success, otherwise false.
1748 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001749 public boolean installRemoteFlowEntry(FlowPath flowPath,
1750 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001751 // TODO: We need it now: Jono
1752 // - For now it will make a REST call to the remote controller.
1753 // - Internally, it needs to know the name of the remote controller.
1754 return true;
1755 }
1756
1757 /**
1758 * Remove a flow entry on a remote controller.
1759 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001760 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001761 * @param flowEntry the flow entry to remove.
1762 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001763 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001764 public boolean removeRemoteFlowEntry(FlowPath flowPath,
1765 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001766 //
1767 // The installRemoteFlowEntry() method implements both installation
1768 // and removal of flow entries.
1769 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001770 return (installRemoteFlowEntry(flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001771 }
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001772
1773 /**
1774 * Store a path flow for measurement purpose.
1775 *
1776 * NOTE: The Flow Path argument does NOT contain flow entries.
1777 * The Shortest Path is computed, and the corresponding Flow Entries
1778 * are stored in the Flow Path.
1779 *
1780 * @param flowPath the Flow Path with the endpoints and the match
1781 * conditions to store.
1782 * @return the stored shortest-path flow on success, otherwise null.
1783 */
1784 @Override
1785 public FlowPath measurementStorePathFlow(FlowPath flowPath) {
1786 //
1787 // Prepare the Shortest Path computation if the first Flow Path
1788 //
1789 if (measurementStoredPaths.isEmpty())
1790 topoRouteService.prepareShortestPathTopo();
1791
1792 //
1793 // Compute the Shortest Path
1794 //
1795 DataPath dataPath =
1796 topoRouteService.getTopoShortestPath(flowPath.dataPath().srcPort(),
1797 flowPath.dataPath().dstPort());
1798 if (dataPath == null) {
1799 // We need the DataPath to populate the Network MAP
1800 dataPath = new DataPath();
1801 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1802 dataPath.setDstPort(flowPath.dataPath().dstPort());
1803 }
1804
1805 //
1806 // Set the incoming port matching and the outgoing port output
1807 // actions for each flow entry.
1808 //
1809 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1810 // Set the incoming port matching
1811 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
1812 flowEntry.setFlowEntryMatch(flowEntryMatch);
1813 flowEntryMatch.enableInPort(flowEntry.inPort());
1814
1815 // Set the outgoing port output action
1816 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1817 if (flowEntryActions == null) {
1818 flowEntryActions = new ArrayList<FlowEntryAction>();
1819 flowEntry.setFlowEntryActions(flowEntryActions);
1820 }
1821 FlowEntryAction flowEntryAction = new FlowEntryAction();
1822 flowEntryAction.setActionOutput(flowEntry.outPort());
1823 flowEntryActions.add(flowEntryAction);
1824 }
1825
1826 //
1827 // Prepare the computed Flow Path
1828 //
1829 FlowPath computedFlowPath = new FlowPath();
1830 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1831 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1832 computedFlowPath.setDataPath(dataPath);
1833 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
1834
1835 //
1836 // Add the computed Flow Path the the internal storage
1837 //
1838 measurementStoredPaths.add(computedFlowPath);
1839
1840 return (computedFlowPath);
1841 }
1842
1843 /**
1844 * Install path flows for measurement purpose.
1845 *
1846 * @param numThreads the number of threads to use to install the path
1847 * flows.
1848 * @return true on success, otherwise false.
1849 */
1850 @Override
1851 public boolean measurementInstallPaths(Integer numThreads) {
1852 List<Thread> threads = new LinkedList<Thread>();
1853
Pavlin Radoslavove0938f32013-05-07 23:17:22 +00001854 // Create a copy of the paths to install
1855 measurementProcessingPaths = new LinkedList<FlowPath>(measurementStoredPaths);
1856
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001857 //
1858 // Create the threads to install the Flow Paths
1859 //
1860 for (int i = 0; i < numThreads; i++) {
1861 Thread thread = new Thread(new Runnable() {
1862 @Override
1863 public void run() {
1864 while (true) {
1865 FlowPath flowPath = measurementPollFirstFlowPath();
1866 if (flowPath == null)
1867 return;
1868 // Install the Flow Path
1869 FlowId flowId = new FlowId();
1870 String dataPathSummaryStr =
1871 flowPath.dataPath().dataPathSummary();
1872 addFlow(flowPath, flowId, dataPathSummaryStr);
1873 }
1874 }}, "Measurement Add Flow Path");
1875 threads.add(thread);
1876 }
1877
1878 //
1879 // Start processing
1880 //
1881 measurementEndTimeProcessingPaths = 0;
1882 measurementStartTimeProcessingPaths = System.nanoTime();
1883 for (Thread thread : threads) {
1884 thread.start();
1885 }
1886
1887 //
1888 // Wait until the end of processing time
1889 //
1890 while (measurementEndTimeProcessingPaths == 0) {
1891 try {
1892 Thread.sleep(100);
1893 } catch (InterruptedException e) {
1894 // Continue waiting
1895 }
1896 }
1897
1898 return true;
1899 }
1900
1901 /**
1902 * Get the measurement time that took to install the path flows.
1903 *
1904 * @return the measurement time (in nanoseconds) it took to install
1905 * the path flows.
1906 */
1907 @Override
1908 public Long measurementGetInstallPathsTimeNsec() {
1909 return new Long(measurementEndTimeProcessingPaths -
1910 measurementStartTimeProcessingPaths);
1911 }
1912
1913 /**
1914 * Get a Flow Path that needs to be installed for measurement purpose.
1915 *
1916 * If there is no next Flow Path to install, the end time measurement
1917 * is recorded.
1918 *
1919 * @return the next Flow Path to install if exists, otherwise null.
1920 */
1921 private synchronized FlowPath measurementPollFirstFlowPath() {
1922 FlowPath flowPath = measurementProcessingPaths.pollFirst();
1923
1924 // Record the end of processing, if the first call
1925 if ((flowPath == null) && (measurementEndTimeProcessingPaths == 0))
1926 measurementEndTimeProcessingPaths = System.nanoTime();
1927
1928 return flowPath;
1929 }
1930
1931 /**
1932 * Clear the path flows stored for measurement purpose.
1933 *
1934 * @return true on success, otherwise false.
1935 */
1936 @Override
1937 public boolean measurementClearAllPaths() {
1938 measurementStoredPaths.clear();
1939 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavove0938f32013-05-07 23:17:22 +00001940 measurementStartTimeProcessingPaths = 0;
1941 measurementEndTimeProcessingPaths = 0;
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001942
1943 return true;
1944 }
1945
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001946}