blob: 1020eab4799fdddfaf7d6868cf33ac8571699434 [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 Radoslavov759772f2013-05-20 20:50:00 +00004import java.io.PrintWriter;
5import java.io.StringWriter;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08006import java.util.ArrayList;
7import java.util.Collection;
Jonathan Hartf5315fb2013-04-05 11:41:56 -07008import java.util.Collections;
9import java.util.Comparator;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080010import java.util.EnumSet;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080011import java.util.HashMap;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +000012import java.util.LinkedList;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080013import java.util.List;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080014import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000015import java.util.Random;
Pavlin Radoslavov759772f2013-05-20 20:50:00 +000016import java.util.concurrent.ConcurrentLinkedQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080017import java.util.concurrent.Executors;
18import java.util.concurrent.ScheduledExecutorService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080019import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080020
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080021import net.floodlightcontroller.core.IFloodlightProviderService;
22import net.floodlightcontroller.core.INetMapStorage;
23import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
24import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070025import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
Pankaj Berded0079742013-03-27 17:53:25 -070026import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070027import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080028import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080029import net.floodlightcontroller.core.module.FloodlightModuleContext;
30import net.floodlightcontroller.core.module.FloodlightModuleException;
31import net.floodlightcontroller.core.module.IFloodlightModule;
32import net.floodlightcontroller.core.module.IFloodlightService;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080033import net.floodlightcontroller.flowcache.web.FlowWebRoutable;
34import net.floodlightcontroller.restserver.IRestApiService;
35import net.floodlightcontroller.util.CallerId;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080036import net.floodlightcontroller.util.DataPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080037import net.floodlightcontroller.util.DataPathEndpoints;
Jonathan Hart01f2d272013-04-04 20:03:46 -070038import net.floodlightcontroller.util.Dpid;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080039import net.floodlightcontroller.util.FlowEntry;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070040import net.floodlightcontroller.util.FlowEntryAction;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080041import net.floodlightcontroller.util.FlowEntryId;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070042import net.floodlightcontroller.util.FlowEntryMatch;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080043import net.floodlightcontroller.util.FlowEntrySwitchState;
44import net.floodlightcontroller.util.FlowEntryUserState;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080045import net.floodlightcontroller.util.FlowId;
46import net.floodlightcontroller.util.FlowPath;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070047import net.floodlightcontroller.util.IPv4Net;
48import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080049import net.floodlightcontroller.util.OFMessageDamper;
50import net.floodlightcontroller.util.Port;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070051import net.floodlightcontroller.util.SwitchPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080052import net.onrc.onos.util.GraphDBConnection;
53import net.onrc.onos.util.GraphDBConnection.Transaction;
54
55import org.openflow.protocol.OFFlowMod;
56import org.openflow.protocol.OFMatch;
57import org.openflow.protocol.OFPacketOut;
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070058import org.openflow.protocol.OFPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080059import org.openflow.protocol.OFType;
60import org.openflow.protocol.action.OFAction;
61import org.openflow.protocol.action.OFActionOutput;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080062import org.slf4j.Logger;
63import org.slf4j.LoggerFactory;
64
Jonathan Hartf5315fb2013-04-05 11:41:56 -070065
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070066public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080067
68 public GraphDBConnection conn;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080069
70 protected IRestApiService restApi;
Jonathan Hart50a94982013-04-10 14:49:51 -070071 protected volatile IFloodlightProviderService floodlightProvider;
72 protected volatile ITopoRouteService topoRouteService;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070073 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080074
75 protected OFMessageDamper messageDamper;
76
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070077 //
78 // TODO: Values copied from elsewhere (class LearningSwitch).
79 // The local copy should go away!
80 //
81 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
82 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
83 public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity
84 public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite
85 public static final short PRIORITY_DEFAULT = 100;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080086
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000087 // Flow Entry ID generation state
88 private static Random randomGenerator = new Random();
89 private static int nextFlowEntryIdPrefix = 0;
90 private static int nextFlowEntryIdSuffix = 0;
91 private static long nextFlowEntryId = 0;
92
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -070093 // State for measurement purpose
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070094 private static long measurementFlowId = 100000;
95 private static String measurementFlowIdStr = "0x186a0"; // 100000
96 private long modifiedMeasurementFlowTime = 0;
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -070097 //
98 private LinkedList<FlowPath> measurementStoredPaths = new LinkedList<FlowPath>();
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -070099 private long measurementStartTimeProcessingPaths = 0;
100 private long measurementEndTimeProcessingPaths = 0;
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000101 Map<Long, ?> measurementShortestPathTopo = null;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700102
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800103 /** The logger. */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800104 private static Logger log = LoggerFactory.getLogger(FlowManager.class);
105
106 // The periodic task(s)
Jonathan Hart50a94982013-04-10 14:49:51 -0700107 private ScheduledExecutorService mapReaderScheduler;
108 private ScheduledExecutorService shortestPathReconcileScheduler;
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700109
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700110 final Runnable mapReader = new Runnable() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800111 public void run() {
Pavlin Radoslavova75caea2013-04-10 19:11:26 -0700112 try {
113 runImpl();
114 } catch (Exception e) {
115 log.debug("Exception processing All Flow Entries from the Network MAP: ", e);
116 conn.endTx(Transaction.ROLLBACK);
117 return;
118 }
119 }
120
121 private void runImpl() {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700122 long startTime = System.nanoTime();
123 int counterAllFlowEntries = 0;
124 int counterMyNotUpdatedFlowEntries = 0;
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700125
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800126 if (floodlightProvider == null) {
127 log.debug("FloodlightProvider service not found!");
128 return;
129 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000130 Map<Long, IOFSwitch> mySwitches =
131 floodlightProvider.getSwitches();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700132 LinkedList<IFlowEntry> addFlowEntries =
133 new LinkedList<IFlowEntry>();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000134 LinkedList<IFlowEntry> deleteFlowEntries =
135 new LinkedList<IFlowEntry>();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700136
137 //
Pankaj Berdea2e14a92013-04-15 11:59:15 -0700138 // Fetch all Flow Entries which need to be updated and select only my Flow Entries
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700139 // that need to be updated into the switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700140 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700141 boolean processed_measurement_flow = false;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000142 Iterable<IFlowEntry> allFlowEntries =
Pankaj Berded1c38592013-04-10 22:46:40 -0700143 conn.utils().getAllSwitchNotUpdatedFlowEntries(conn);
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700144 for (IFlowEntry flowEntryObj : allFlowEntries) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700145 counterAllFlowEntries++;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000146
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000147 String dpidStr = flowEntryObj.getSwitchDpid();
148 if (dpidStr == null)
149 continue;
150 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800151 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000152 if (mySwitch == null)
153 continue; // Ignore the entry: not my switch
154
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700155 IFlowPath flowObj =
156 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
157 if (flowObj == null)
158 continue; // Should NOT happen
159 if (flowObj.getFlowId() == null)
160 continue; // Invalid entry
161
162 //
163 // NOTE: For now we process the DELETE before the ADD
164 // to cover the more common scenario.
165 // TODO: This is error prone and needs to be fixed!
166 //
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000167 String userState = flowEntryObj.getUserState();
168 if (userState == null)
169 continue;
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700170 if (userState.equals("FE_USER_DELETE")) {
171 // An entry that needs to be deleted.
172 deleteFlowEntries.add(flowEntryObj);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700173 installFlowEntry(mySwitch, flowObj, flowEntryObj);
174 } else {
175 addFlowEntries.add(flowEntryObj);
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700176 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700177 counterMyNotUpdatedFlowEntries++;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700178 // Code for measurement purpose
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700179 // TODO: Commented-out for now
180 /*
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700181 {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700182 if (flowObj.getFlowId().equals(measurementFlowIdStr)) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700183 processed_measurement_flow = true;
184 }
185 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700186 */
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700187 }
188
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700189 //
190 // Process the Flow Entries that need to be added
191 //
192 for (IFlowEntry flowEntryObj : addFlowEntries) {
193 IFlowPath flowObj =
194 conn.utils().getFlowPathByFlowEntry(conn,
195 flowEntryObj);
196 if (flowObj == null)
197 continue; // Should NOT happen
198 if (flowObj.getFlowId() == null)
199 continue; // Invalid entry
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700200
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700201 Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid());
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700202 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000203 if (mySwitch == null)
204 continue; // Shouldn't happen
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -0700205 installFlowEntry(mySwitch, flowObj, flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800206 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000207
208 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000209 // Delete all Flow Entries marked for deletion from the
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -0700210 // Network MAP.
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000211 //
212 // TODO: We should use the OpenFlow Barrier mechanism
213 // to check for errors, and delete the Flow Entries after the
214 // Barrier message is received.
215 //
216 while (! deleteFlowEntries.isEmpty()) {
217 IFlowEntry flowEntryObj = deleteFlowEntries.poll();
218 IFlowPath flowObj =
219 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
220 if (flowObj == null) {
221 log.debug("Did not find FlowPath to be deleted");
222 continue;
223 }
224 flowObj.removeFlowEntry(flowEntryObj);
225 conn.utils().removeFlowEntry(conn, flowEntryObj);
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000226 }
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700227
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700228 conn.endTx(Transaction.COMMIT);
229
230 if (processed_measurement_flow) {
231 long estimatedTime =
232 System.nanoTime() - modifiedMeasurementFlowTime;
233 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
234 (double)estimatedTime / 1000000000 + " sec";
235 log.debug(logMsg);
236 }
237
238 long estimatedTime = System.nanoTime() - startTime;
239 double rate = 0.0;
240 if (estimatedTime > 0)
241 rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime;
242 String logMsg = "MEASUREMENT: Processed AllFlowEntries: " +
243 counterAllFlowEntries + " MyNotUpdatedFlowEntries: " +
244 counterMyNotUpdatedFlowEntries + " in " +
245 (double)estimatedTime / 1000000000 + " sec: " +
246 rate + " paths/s";
247 log.debug(logMsg);
248 }
249 };
250
251 final Runnable shortestPathReconcile = new Runnable() {
252 public void run() {
Pavlin Radoslavova75caea2013-04-10 19:11:26 -0700253 try {
254 runImpl();
255 } catch (Exception e) {
256 log.debug("Exception processing All Flows from the Network MAP: ", e);
257 conn.endTx(Transaction.ROLLBACK);
258 return;
259 }
260 }
261
262 private void runImpl() {
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700263 long startTime = System.nanoTime();
264 int counterAllFlowPaths = 0;
265 int counterMyFlowPaths = 0;
266
267 if (floodlightProvider == null) {
268 log.debug("FloodlightProvider service not found!");
269 return;
270 }
271 Map<Long, IOFSwitch> mySwitches =
272 floodlightProvider.getSwitches();
273 LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>();
274
275 boolean processed_measurement_flow = false;
276
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700277 //
278 // Fetch and recompute the Shortest Path for those
279 // Flow Paths this controller is responsible for.
280 //
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000281 Map<Long, ?> shortestPathTopo =
282 topoRouteService.prepareShortestPathTopo();
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700283 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700284 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700285 counterAllFlowPaths++;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700286 if (flowPathObj == null)
287 continue;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700288
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700289 String srcDpidStr = flowPathObj.getSrcSwitch();
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000290 if (srcDpidStr == null)
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700291 continue;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700292 Dpid srcDpid = new Dpid(srcDpidStr);
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700293 //
294 // Use the source DPID as a heuristic to decide
295 // which controller is responsible for maintaining the
296 // shortest path.
297 // NOTE: This heuristic is error-prone: if the switch
298 // goes away and no controller is responsible for that
299 // switch, then the original Flow Path is not cleaned-up
300 //
301 IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
302 if (mySwitch == null)
303 continue; // Ignore: not my responsibility
304
Pavlin Radoslavov99d1b152013-04-09 22:57:33 -0700305 // Test the Data Path Summary string
306 String dataPathSummaryStr = flowPathObj.getDataPathSummary();
307 if (dataPathSummaryStr == null)
308 continue; // Could be invalid entry?
309 if (dataPathSummaryStr.isEmpty())
310 continue; // No need to maintain this flow
311
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000312 //
313 // Test whether we need to complete the Flow cleanup,
314 // if the Flow has been deleted by the user.
315 //
316 String flowUserState = flowPathObj.getUserState();
317 if ((flowUserState != null)
318 && flowUserState.equals("FE_USER_DELETE")) {
319 Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries();
320 boolean empty = true; // TODO: an ugly hack
321 for (IFlowEntry flowEntryObj : flowEntries) {
322 empty = false;
323 break;
324 }
325 if (empty)
326 deleteFlows.add(flowPathObj);
327 }
328
Pavlin Radoslavov6db8c6e2013-04-08 00:14:07 +0000329 // Fetch the fields needed to recompute the shortest path
330 Short srcPortShort = flowPathObj.getSrcPort();
331 String dstDpidStr = flowPathObj.getDstSwitch();
332 Short dstPortShort = flowPathObj.getDstPort();
333 if ((srcPortShort == null) ||
334 (dstDpidStr == null) ||
335 (dstPortShort == null)) {
336 continue;
337 }
338
339 Port srcPort = new Port(srcPortShort);
340 Dpid dstDpid = new Dpid(dstDpidStr);
341 Port dstPort = new Port(dstPortShort);
342 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
343 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
344
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700345 counterMyFlowPaths++;
346
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700347 //
348 // NOTE: Using here the regular getShortestPath() method
349 // won't work here, because that method calls internally
350 // "conn.endTx(Transaction.COMMIT)", and that will
351 // invalidate all handlers to the Titan database.
352 // If we want to experiment with calling here
353 // getShortestPath(), we need to refactor that code
354 // to avoid closing the transaction.
355 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700356 DataPath dataPath =
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000357 topoRouteService.getTopoShortestPath(shortestPathTopo,
358 srcSwitchPort,
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700359 dstSwitchPort);
Pavlin Radoslavov4a325822013-04-02 22:16:59 +0000360 if (dataPath == null) {
361 // We need the DataPath to compare the paths
362 dataPath = new DataPath();
363 dataPath.setSrcPort(srcSwitchPort);
364 dataPath.setDstPort(dstSwitchPort);
365 }
366
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700367 String newDataPathSummaryStr = dataPath.dataPathSummary();
368 if (dataPathSummaryStr.equals(newDataPathSummaryStr))
369 continue; // Nothing changed
370
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700371 reconcileFlow(flowPathObj, dataPath);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700372 }
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000373
374 //
375 // Delete all leftover Flows marked for deletion from the
376 // Network MAP.
377 //
378 while (! deleteFlows.isEmpty()) {
379 IFlowPath flowPathObj = deleteFlows.poll();
380 conn.utils().removeFlowPath(conn, flowPathObj);
381 }
382
Pavlin Radoslavov9556b142013-05-20 21:49:04 +0000383 topoRouteService.dropShortestPathTopo(shortestPathTopo);
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700384
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800385 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700386
387 if (processed_measurement_flow) {
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700388 long estimatedTime =
389 System.nanoTime() - modifiedMeasurementFlowTime;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700390 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
391 (double)estimatedTime / 1000000000 + " sec";
392 log.debug(logMsg);
393 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700394
395 long estimatedTime = System.nanoTime() - startTime;
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700396 double rate = 0.0;
397 if (estimatedTime > 0)
398 rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime;
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700399 String logMsg = "MEASUREMENT: Processed AllFlowPaths: " +
Pavlin Radoslavov1552f952013-04-04 17:51:22 -0700400 counterAllFlowPaths + " MyFlowPaths: " +
401 counterMyFlowPaths + " in " +
402 (double)estimatedTime / 1000000000 + " sec: " +
403 rate + " paths/s";
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700404 log.debug(logMsg);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800405 }
406 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700407
Jonathan Hart50a94982013-04-10 14:49:51 -0700408 //final ScheduledFuture<?> mapReaderHandle =
409 //mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800410
Jonathan Hart50a94982013-04-10 14:49:51 -0700411 //final ScheduledFuture<?> shortestPathReconcileHandle =
412 //shortestPathReconcileScheduler.scheduleAtFixedRate(shortestPathReconcile, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -0700413
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800414 @Override
415 public void init(String conf) {
Jonathan Hart50a94982013-04-10 14:49:51 -0700416 conn = GraphDBConnection.getInstance(conf);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800417 }
418
419 public void finalize() {
420 close();
421 }
422
423 @Override
424 public void close() {
425 conn.close();
426 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800427
428 @Override
429 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
430 Collection<Class<? extends IFloodlightService>> l =
431 new ArrayList<Class<? extends IFloodlightService>>();
432 l.add(IFlowService.class);
433 return l;
434 }
435
436 @Override
437 public Map<Class<? extends IFloodlightService>, IFloodlightService>
438 getServiceImpls() {
439 Map<Class<? extends IFloodlightService>,
440 IFloodlightService> m =
441 new HashMap<Class<? extends IFloodlightService>,
442 IFloodlightService>();
443 m.put(IFlowService.class, this);
444 return m;
445 }
446
447 @Override
448 public Collection<Class<? extends IFloodlightService>>
449 getModuleDependencies() {
450 Collection<Class<? extends IFloodlightService>> l =
451 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800452 l.add(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700453 l.add(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800454 l.add(IRestApiService.class);
455 return l;
456 }
457
458 @Override
459 public void init(FloodlightModuleContext context)
460 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700461 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800462 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700463 topoRouteService = context.getServiceImpl(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800464 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800465 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
466 EnumSet.of(OFType.FLOW_MOD),
467 OFMESSAGE_DAMPER_TIMEOUT);
468 // TODO: An ugly hack!
469 String conf = "/tmp/cassandra.titan";
470 this.init(conf);
Jonathan Hart50a94982013-04-10 14:49:51 -0700471
472 mapReaderScheduler = Executors.newScheduledThreadPool(1);
473 shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800474 }
475
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -0700476 private synchronized long getNextFlowEntryId() {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000477 //
478 // Generate the next Flow Entry ID.
479 // NOTE: For now, the higher 32 bits are random, and
480 // the lower 32 bits are sequential.
481 // In the future, we need a better allocation mechanism.
482 //
483 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
484 nextFlowEntryIdPrefix = randomGenerator.nextInt();
485 nextFlowEntryIdSuffix = 0;
486 } else {
487 nextFlowEntryIdSuffix++;
488 }
489 long result = (long)nextFlowEntryIdPrefix << 32;
490 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
491 return result;
492 }
493
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800494 @Override
495 public void startUp(FloodlightModuleContext context) {
Jonathan Hart50a94982013-04-10 14:49:51 -0700496 restApi.addRestletRoutable(new FlowWebRoutable());
497
498 // Initialize the Flow Entry ID generator
499 nextFlowEntryIdPrefix = randomGenerator.nextInt();
500
501 mapReaderScheduler.scheduleAtFixedRate(
Pankaj Berde55f121a2013-04-23 15:42:54 -0700502 mapReader, 1, 1, TimeUnit.SECONDS);
Jonathan Hart50a94982013-04-10 14:49:51 -0700503 shortestPathReconcileScheduler.scheduleAtFixedRate(
Pankaj Berdea2e14a92013-04-15 11:59:15 -0700504 shortestPathReconcile, 100, 100, TimeUnit.MILLISECONDS);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800505 }
506
507 /**
508 * Add a flow.
509 *
510 * Internally, ONOS will automatically register the installer for
511 * receiving Flow Path Notifications for that path.
512 *
513 * @param flowPath the Flow Path to install.
514 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700515 * @param dataPathSummaryStr the data path summary string if the added
516 * flow will be maintained internally, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800517 * @return true on success, otherwise false.
518 */
519 @Override
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700520 public boolean addFlow(FlowPath flowPath, FlowId flowId,
521 String dataPathSummaryStr) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700522 /*
523 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700524 if (flowPath.flowId().value() == measurementFlowId) {
525 modifiedMeasurementFlowTime = System.nanoTime();
526 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700527 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800528
529 IFlowPath flowObj = null;
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000530 boolean found = false;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800531 try {
532 if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId()))
533 != null) {
534 log.debug("Adding FlowPath with FlowId {}: found existing FlowPath",
535 flowPath.flowId().toString());
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000536 found = true;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800537 } else {
538 flowObj = conn.utils().newFlowPath(conn);
539 log.debug("Adding FlowPath with FlowId {}: creating new FlowPath",
540 flowPath.flowId().toString());
541 }
542 } catch (Exception e) {
543 // TODO: handle exceptions
544 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavov759772f2013-05-20 20:50:00 +0000545
546 StringWriter sw = new StringWriter();
547 e.printStackTrace(new PrintWriter(sw));
548 String stacktrace = sw.toString();
549
550 log.error(":addFlow FlowId:{} failed: {}",
551 flowPath.flowId().toString(),
552 stacktrace);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800553 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700554 if (flowObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000555 log.error(":addFlow FlowId:{} failed: Flow object not created",
556 flowPath.flowId().toString());
557 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800558 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700559 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800560
561 //
562 // Set the Flow key:
563 // - flowId
564 //
565 flowObj.setFlowId(flowPath.flowId().toString());
566 flowObj.setType("flow");
567
568 //
569 // Set the Flow attributes:
570 // - flowPath.installerId()
571 // - flowPath.dataPath().srcPort()
572 // - flowPath.dataPath().dstPort()
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700573 // - flowPath.matchEthernetFrameType()
574 // - flowPath.matchSrcIPv4Net()
575 // - flowPath.matchDstIPv4Net()
576 // - flowPath.matchSrcMac()
577 // - flowPath.matchDstMac()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800578 //
579 flowObj.setInstallerId(flowPath.installerId().toString());
580 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
581 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
582 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
583 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700584 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
585 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
586 }
587 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
588 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
589 }
590 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
591 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
592 }
593 if (flowPath.flowEntryMatch().matchSrcMac()) {
594 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
595 }
596 if (flowPath.flowEntryMatch().matchDstMac()) {
597 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
598 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800599
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700600 if (dataPathSummaryStr != null) {
601 flowObj.setDataPathSummary(dataPathSummaryStr);
602 } else {
603 flowObj.setDataPathSummary("");
604 }
605
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000606 if (found)
607 flowObj.setUserState("FE_USER_MODIFY");
608 else
609 flowObj.setUserState("FE_USER_ADD");
610
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800611 // Flow edges:
612 // HeadFE
613
614
615 //
616 // Flow Entries:
617 // flowPath.dataPath().flowEntries()
618 //
619 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700620 if (addFlowEntry(flowObj, flowEntry) == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000621 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800622 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700623 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800624 }
625 conn.endTx(Transaction.COMMIT);
626
627 //
628 // TODO: We need a proper Flow ID allocation mechanism.
629 //
630 flowId.setValue(flowPath.flowId().value());
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700631
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800632 return true;
633 }
634
635 /**
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700636 * Add a flow entry to the Network MAP.
637 *
638 * @param flowObj the corresponding Flow Path object for the Flow Entry.
639 * @param flowEntry the Flow Entry to install.
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700640 * @return the added Flow Entry object on success, otherwise null.
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700641 */
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700642 private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) {
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700643 // Flow edges
644 // HeadFE (TODO)
645
646 //
647 // Assign the FlowEntry ID.
648 //
649 if ((flowEntry.flowEntryId() == null) ||
650 (flowEntry.flowEntryId().value() == 0)) {
651 long id = getNextFlowEntryId();
652 flowEntry.setFlowEntryId(new FlowEntryId(id));
653 }
654
655 IFlowEntry flowEntryObj = null;
656 boolean found = false;
657 try {
658 if ((flowEntryObj =
659 conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) {
660 log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry",
661 flowEntry.flowEntryId().toString());
662 found = true;
663 } else {
664 flowEntryObj = conn.utils().newFlowEntry(conn);
665 log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry",
666 flowEntry.flowEntryId().toString());
667 }
668 } catch (Exception e) {
669 log.error(":addFlow FlowEntryId:{} failed",
670 flowEntry.flowEntryId().toString());
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700671 return null;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700672 }
673 if (flowEntryObj == null) {
674 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
675 flowEntry.flowEntryId().toString());
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700676 return null;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700677 }
678
679 //
680 // Set the Flow Entry key:
681 // - flowEntry.flowEntryId()
682 //
683 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
684 flowEntryObj.setType("flow_entry");
685
686 //
687 // Set the Flow Entry Edges and attributes:
688 // - Switch edge
689 // - InPort edge
690 // - OutPort edge
691 //
692 // - flowEntry.flowEntryMatch()
693 // - flowEntry.flowEntryActions()
694 // - flowEntry.dpid()
695 // - flowEntry.flowEntryUserState()
696 // - flowEntry.flowEntrySwitchState()
697 // - flowEntry.flowEntryErrorState()
698 // - flowEntry.matchInPort()
699 // - flowEntry.matchEthernetFrameType()
700 // - flowEntry.matchSrcIPv4Net()
701 // - flowEntry.matchDstIPv4Net()
702 // - flowEntry.matchSrcMac()
703 // - flowEntry.matchDstMac()
704 // - flowEntry.actionOutput()
705 //
706 ISwitchObject sw =
707 conn.utils().searchSwitch(conn, flowEntry.dpid().toString());
708 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
709 flowEntryObj.setSwitch(sw);
710 if (flowEntry.flowEntryMatch().matchInPort()) {
711 IPortObject inport =
712 conn.utils().searchPort(conn, flowEntry.dpid().toString(),
713 flowEntry.flowEntryMatch().inPort().value());
714 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
715 flowEntryObj.setInPort(inport);
716 }
717 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
718 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
719 }
720 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
721 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
722 }
723 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
724 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
725 }
726 if (flowEntry.flowEntryMatch().matchSrcMac()) {
727 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
728 }
729 if (flowEntry.flowEntryMatch().matchDstMac()) {
730 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
731 }
732
733 for (FlowEntryAction fa : flowEntry.flowEntryActions()) {
734 if (fa.actionOutput() != null) {
735 IPortObject outport =
736 conn.utils().searchPort(conn,
737 flowEntry.dpid().toString(),
738 fa.actionOutput().port().value());
739 flowEntryObj.setActionOutput(fa.actionOutput().port().value());
740 flowEntryObj.setOutPort(outport);
741 }
742 }
743 // TODO: Hacks with hard-coded state names!
744 if (found)
745 flowEntryObj.setUserState("FE_USER_MODIFY");
746 else
747 flowEntryObj.setUserState("FE_USER_ADD");
748 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
749 //
750 // TODO: Take care of the FlowEntryErrorState.
751 //
752
753 // Flow Entries edges:
754 // Flow
755 // NextFE (TODO)
756 if (! found) {
757 flowObj.addFlowEntry(flowEntryObj);
758 flowEntryObj.setFlow(flowObj);
759 }
760
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -0700761 return flowEntryObj;
Pavlin Radoslavov9425f702013-04-04 19:55:07 -0700762 }
763
764 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000765 * Delete all previously added flows.
766 *
767 * @return true on success, otherwise false.
768 */
769 @Override
770 public boolean deleteAllFlows() {
Pavlin Radoslavov759772f2013-05-20 20:50:00 +0000771 List<Thread> threads = new LinkedList<Thread>();
772 final ConcurrentLinkedQueue<FlowId> concurrentAllFlowIds =
773 new ConcurrentLinkedQueue<FlowId>();
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000774
Pavlin Radoslavov759772f2013-05-20 20:50:00 +0000775 // Get all Flow IDs
776 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
777 for (IFlowPath flowPathObj : allFlowPaths) {
778 if (flowPathObj == null)
779 continue;
780 String flowIdStr = flowPathObj.getFlowId();
781 if (flowIdStr == null)
782 continue;
783 FlowId flowId = new FlowId(flowIdStr);
784 concurrentAllFlowIds.add(flowId);
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000785 }
786
Pavlin Radoslavov759772f2013-05-20 20:50:00 +0000787 // Delete all flows one-by-one
788 for (FlowId flowId : concurrentAllFlowIds)
789 deleteFlow(flowId);
790
791 /*
792 * TODO: A faster mechanism to delete the Flow Paths by using
793 * a number of threads. Commented-out for now.
794 */
795 /*
796 //
797 // Create the threads to delete the Flow Paths
798 //
799 for (int i = 0; i < 10; i++) {
800 Thread thread = new Thread(new Runnable() {
801 @Override
802 public void run() {
803 while (true) {
804 FlowId flowId = concurrentAllFlowIds.poll();
805 if (flowId == null)
806 return;
807 deleteFlow(flowId);
808 }
809 }}, "Delete All Flow Paths");
810 threads.add(thread);
811 }
812
813 // Start processing
814 for (Thread thread : threads) {
815 thread.start();
816 }
817
818 // Want for all threads to complete
819 for (Thread thread : threads) {
820 try {
821 thread.join();
822 } catch (InterruptedException e) {
823 log.debug("Exception waiting for a thread to delete a Flow Path: ", e);
824 }
825 }
826 */
827
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000828 return true;
829 }
830
831 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800832 * Delete a previously added flow.
833 *
834 * @param flowId the Flow ID of the flow to delete.
835 * @return true on success, otherwise false.
836 */
837 @Override
838 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700839 /*
840 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700841 if (flowId.value() == measurementFlowId) {
842 modifiedMeasurementFlowTime = System.nanoTime();
843 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700844 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700845
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800846 IFlowPath flowObj = null;
847 //
848 // We just mark the entries for deletion,
849 // and let the switches remove each individual entry after
850 // it has been removed from the switches.
851 //
852 try {
853 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
854 != null) {
855 log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath",
856 flowId.toString());
857 } else {
858 log.debug("Deleting FlowPath with FlowId {}: FlowPath not found",
859 flowId.toString());
860 }
861 } catch (Exception e) {
862 // TODO: handle exceptions
863 conn.endTx(Transaction.ROLLBACK);
864 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
865 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700866 if (flowObj == null) {
867 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800868 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700869 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800870
871 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000872 // Find and mark for deletion all Flow Entries,
873 // and the Flow itself.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800874 //
Pavlin Radoslavov710e2a72013-04-08 02:31:05 +0000875 flowObj.setUserState("FE_USER_DELETE");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800876 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
877 boolean empty = true; // TODO: an ugly hack
878 for (IFlowEntry flowEntryObj : flowEntries) {
879 empty = false;
880 // flowObj.removeFlowEntry(flowEntryObj);
881 // conn.utils().removeFlowEntry(conn, flowEntryObj);
882 flowEntryObj.setUserState("FE_USER_DELETE");
883 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
884 }
885 // Remove from the database empty flows
886 if (empty)
887 conn.utils().removeFlowPath(conn, flowObj);
888 conn.endTx(Transaction.COMMIT);
889
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800890 return true;
891 }
892
893 /**
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000894 * Clear the state for all previously added flows.
895 *
896 * @return true on success, otherwise false.
897 */
898 @Override
899 public boolean clearAllFlows() {
Pavlin Radoslavov759772f2013-05-20 20:50:00 +0000900 List<FlowId> allFlowIds = new LinkedList<FlowId>();
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000901
Pavlin Radoslavov759772f2013-05-20 20:50:00 +0000902 // Get all Flow IDs
903 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
904 for (IFlowPath flowPathObj : allFlowPaths) {
905 if (flowPathObj == null)
906 continue;
907 String flowIdStr = flowPathObj.getFlowId();
908 if (flowIdStr == null)
909 continue;
910 FlowId flowId = new FlowId(flowIdStr);
911 allFlowIds.add(flowId);
912 }
913
914 // Clear all flows one-by-one
915 for (FlowId flowId : allFlowIds) {
916 clearFlow(flowId);
Pavlin Radoslavovbaea9242013-05-08 00:20:09 +0000917 }
918
919 return true;
920 }
921
922 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700923 * Clear the state for a previously added flow.
924 *
925 * @param flowId the Flow ID of the flow to clear.
926 * @return true on success, otherwise false.
927 */
928 @Override
929 public boolean clearFlow(FlowId flowId) {
930 IFlowPath flowObj = null;
931 try {
932 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
933 != null) {
934 log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath",
935 flowId.toString());
936 } else {
937 log.debug("Clearing FlowPath with FlowId {}: FlowPath not found",
938 flowId.toString());
939 }
940 } catch (Exception e) {
941 // TODO: handle exceptions
942 conn.endTx(Transaction.ROLLBACK);
943 log.error(":clearFlow FlowId:{} failed", flowId.toString());
944 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700945 if (flowObj == null) {
946 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700947 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700948 }
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700949
950 //
951 // Remove all Flow Entries
952 //
953 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
954 for (IFlowEntry flowEntryObj : flowEntries) {
955 flowObj.removeFlowEntry(flowEntryObj);
956 conn.utils().removeFlowEntry(conn, flowEntryObj);
957 }
958 // Remove the Flow itself
959 conn.utils().removeFlowPath(conn, flowObj);
960 conn.endTx(Transaction.COMMIT);
961
962 return true;
963 }
964
965 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800966 * Get a previously added flow.
967 *
968 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800969 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800970 */
971 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800972 public FlowPath getFlow(FlowId flowId) {
973 IFlowPath flowObj = null;
974 try {
975 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
976 != null) {
977 log.debug("Get FlowPath with FlowId {}: found existing FlowPath",
978 flowId.toString());
979 } else {
980 log.debug("Get FlowPath with FlowId {}: FlowPath not found",
981 flowId.toString());
982 }
983 } catch (Exception e) {
984 // TODO: handle exceptions
985 conn.endTx(Transaction.ROLLBACK);
986 log.error(":getFlow FlowId:{} failed", flowId.toString());
987 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700988 if (flowObj == null) {
989 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800990 return null; // Flow not found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700991 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800992
993 //
994 // Extract the Flow state
995 //
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800996 FlowPath flowPath = extractFlowPath(flowObj);
997 conn.endTx(Transaction.COMMIT);
998
999 return flowPath;
1000 }
1001
1002 /**
1003 * Get all previously added flows by a specific installer for a given
1004 * data path endpoints.
1005 *
1006 * @param installerId the Caller ID of the installer of the flow to get.
1007 * @param dataPathEndpoints the data path endpoints of the flow to get.
1008 * @return the Flow Paths if found, otherwise null.
1009 */
1010 @Override
1011 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
1012 DataPathEndpoints dataPathEndpoints) {
1013 //
1014 // TODO: The implementation below is not optimal:
1015 // We fetch all flows, and then return only the subset that match
1016 // the query conditions.
1017 // We should use the appropriate Titan/Gremlin query to filter-out
1018 // the flows as appropriate.
1019 //
1020 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001021 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001022
1023 if (allFlows == null) {
1024 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001025 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001026 }
1027
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001028 for (FlowPath flow : allFlows) {
1029 //
1030 // TODO: String-based comparison is sub-optimal.
1031 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -08001032 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001033 //
1034 if (! flow.installerId().toString().equals(installerId.toString()))
1035 continue;
1036 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
1037 continue;
1038 }
1039 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
1040 continue;
1041 }
1042 flowPaths.add(flow);
1043 }
1044
1045 if (flowPaths.isEmpty()) {
1046 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001047 } else {
1048 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints);
1049 }
1050
1051 return flowPaths;
1052 }
1053
1054 /**
1055 * Get all installed flows by all installers for given data path endpoints.
1056 *
1057 * @param dataPathEndpoints the data path endpoints of the flows to get.
1058 * @return the Flow Paths if found, otherwise null.
1059 */
1060 @Override
1061 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
1062 //
1063 // TODO: The implementation below is not optimal:
1064 // We fetch all flows, and then return only the subset that match
1065 // the query conditions.
1066 // We should use the appropriate Titan/Gremlin query to filter-out
1067 // the flows as appropriate.
1068 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001069 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1070 ArrayList<FlowPath> allFlows = getAllFlows();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001071
1072 if (allFlows == null) {
1073 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001074 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001075 }
1076
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001077 for (FlowPath flow : allFlows) {
1078 //
1079 // TODO: String-based comparison is sub-optimal.
1080 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -08001081 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001082 //
1083 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
1084 continue;
1085 }
1086 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
1087 continue;
1088 }
1089 flowPaths.add(flow);
1090 }
1091
1092 if (flowPaths.isEmpty()) {
1093 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001094 } else {
1095 log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
1096 }
1097
1098 return flowPaths;
1099 }
1100
1101 /**
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001102 * Get summary of all installed flows by all installers in a given range
1103 *
1104 * @param flowId the data path endpoints of the flows to get.
1105 * @param maxFlows: the maximum number of flows to be returned
1106 * @return the Flow Paths if found, otherwise null.
1107 */
1108 @Override
Jonathan Hart01f2d272013-04-04 20:03:46 -07001109 public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) {
Jonathan Hartf5315fb2013-04-05 11:41:56 -07001110
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001111 // TODO: The implementation below is not optimal:
1112 // We fetch all flows, and then return only the subset that match
1113 // the query conditions.
1114 // We should use the appropriate Titan/Gremlin query to filter-out
1115 // the flows as appropriate.
1116 //
Jonathan Hart01f2d272013-04-04 20:03:46 -07001117 //ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001118
Jonathan Hart01f2d272013-04-04 20:03:46 -07001119 ArrayList<IFlowPath> flowPathsWithoutFlowEntries = getAllFlowsWithoutFlowEntries();
1120
Jonathan Hartf5315fb2013-04-05 11:41:56 -07001121 Collections.sort(flowPathsWithoutFlowEntries,
1122 new Comparator<IFlowPath>(){
1123 @Override
1124 public int compare(IFlowPath first, IFlowPath second) {
1125 // TODO Auto-generated method stub
1126 long result = new FlowId(first.getFlowId()).value()
1127 - new FlowId(second.getFlowId()).value();
1128 if (result > 0) return 1;
1129 else if (result < 0) return -1;
1130 else return 0;
1131 }
1132 }
1133 );
1134
Jonathan Hart01f2d272013-04-04 20:03:46 -07001135 return flowPathsWithoutFlowEntries;
1136
1137 /*
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001138 ArrayList<FlowPath> allFlows = getAllFlows();
Jonathan Hart01f2d272013-04-04 20:03:46 -07001139
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001140 if (allFlows == null) {
1141 log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001142 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001143 }
1144
Umesh Krishnaswamy244b4ae2013-03-29 12:05:15 -07001145 Collections.sort(allFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001146
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001147 for (FlowPath flow : allFlows) {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001148 flow.setFlowEntryMatch(null);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001149
Pavlin Radoslavov96b43422013-04-04 19:14:56 -07001150 // start from desired flowId
1151 if (flow.flowId().value() < flowId.value()) {
1152 continue;
1153 }
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001154
1155 // Summarize by making null flow entry fields that are not relevant to report
1156 for (FlowEntry flowEntry : flow.dataPath().flowEntries()) {
1157 flowEntry.setFlowEntryActions(null);
1158 flowEntry.setFlowEntryMatch(null);
1159 }
1160
1161 flowPaths.add(flow);
1162 if (maxFlows != 0 && flowPaths.size() >= maxFlows) {
1163 break;
1164 }
1165 }
1166
1167 if (flowPaths.isEmpty()) {
1168 log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001169 } else {
1170 log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows);
1171 }
1172
1173 return flowPaths;
Jonathan Hart01f2d272013-04-04 20:03:46 -07001174 */
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001175 }
1176
1177 /**
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001178 * Get all installed flows by all installers.
1179 *
1180 * @return the Flow Paths if found, otherwise null.
1181 */
1182 @Override
1183 public ArrayList<FlowPath> getAllFlows() {
1184 Iterable<IFlowPath> flowPathsObj = null;
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001185 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001186
1187 try {
1188 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
1189 log.debug("Get all FlowPaths: found FlowPaths");
1190 } else {
1191 log.debug("Get all FlowPaths: no FlowPaths found");
1192 }
1193 } catch (Exception e) {
1194 // TODO: handle exceptions
1195 conn.endTx(Transaction.ROLLBACK);
1196 log.error(":getAllFlowPaths failed");
1197 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001198 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1199 conn.endTx(Transaction.COMMIT);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001200 return flowPaths; // No Flows found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001201 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001202
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001203 for (IFlowPath flowObj : flowPathsObj) {
1204 //
1205 // Extract the Flow state
1206 //
1207 FlowPath flowPath = extractFlowPath(flowObj);
Pavlin Radoslavov3f2af732013-03-29 15:29:35 -07001208 if (flowPath != null)
1209 flowPaths.add(flowPath);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001210 }
1211
1212 conn.endTx(Transaction.COMMIT);
1213
1214 return flowPaths;
1215 }
Jonathan Hart01f2d272013-04-04 20:03:46 -07001216
1217 public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(){
1218 Iterable<IFlowPath> flowPathsObj = null;
1219 ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>();
1220 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1221
Jonathan Harte6e91872013-04-13 11:10:32 -07001222 conn.endTx(Transaction.COMMIT);
1223
Jonathan Hart01f2d272013-04-04 20:03:46 -07001224 try {
1225 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
1226 log.debug("Get all FlowPaths: found FlowPaths");
1227 } else {
1228 log.debug("Get all FlowPaths: no FlowPaths found");
1229 }
1230 } catch (Exception e) {
1231 // TODO: handle exceptions
1232 conn.endTx(Transaction.ROLLBACK);
1233 log.error(":getAllFlowPaths failed");
1234 }
1235 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1236 return new ArrayList<IFlowPath>(); // No Flows found
1237 }
1238
1239 for (IFlowPath flowObj : flowPathsObj){
1240 flowPathsObjArray.add(flowObj);
1241 }
1242 /*
1243 for (IFlowPath flowObj : flowPathsObj) {
1244 //
1245 // Extract the Flow state
1246 //
1247 FlowPath flowPath = extractFlowPath(flowObj);
1248 if (flowPath != null)
1249 flowPaths.add(flowPath);
1250 }
1251 */
1252
1253 //conn.endTx(Transaction.COMMIT);
1254
1255 return flowPathsObjArray;
1256 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001257
1258 /**
1259 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
1260 *
1261 * @param flowObj the object to extract the Flow Path State from.
1262 * @return the extracted Flow Path State.
1263 */
1264 private FlowPath extractFlowPath(IFlowPath flowObj) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001265 //
1266 // Extract the Flow state
1267 //
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001268 String flowIdStr = flowObj.getFlowId();
1269 String installerIdStr = flowObj.getInstallerId();
1270 String srcSwitchStr = flowObj.getSrcSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001271 Short srcPortShort = flowObj.getSrcPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001272 String dstSwitchStr = flowObj.getDstSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001273 Short dstPortShort = flowObj.getDstPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001274
1275 if ((flowIdStr == null) ||
1276 (installerIdStr == null) ||
1277 (srcSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001278 (srcPortShort == null) ||
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001279 (dstSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001280 (dstPortShort == null)) {
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001281 // TODO: A work-around, becauuse of some bogus database objects
1282 return null;
1283 }
1284
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001285 FlowPath flowPath = new FlowPath();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001286 flowPath.setFlowId(new FlowId(flowIdStr));
1287 flowPath.setInstallerId(new CallerId(installerIdStr));
1288 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001289 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001290 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001291 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001292 //
1293 // Extract the match conditions common for all Flow Entries
1294 //
1295 {
1296 FlowEntryMatch match = new FlowEntryMatch();
1297 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1298 if (matchEthernetFrameType != null)
1299 match.enableEthernetFrameType(matchEthernetFrameType);
1300 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1301 if (matchSrcIPv4Net != null)
1302 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1303 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1304 if (matchDstIPv4Net != null)
1305 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1306 String matchSrcMac = flowObj.getMatchSrcMac();
1307 if (matchSrcMac != null)
1308 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1309 String matchDstMac = flowObj.getMatchDstMac();
1310 if (matchDstMac != null)
1311 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1312 flowPath.setFlowEntryMatch(match);
1313 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001314
1315 //
1316 // Extract all Flow Entries
1317 //
1318 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1319 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001320 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
1321 if (flowEntry == null)
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001322 continue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001323 flowPath.dataPath().flowEntries().add(flowEntry);
1324 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001325
1326 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001327 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001328
1329 /**
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001330 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
1331 *
1332 * @param flowEntryObj the object to extract the Flow Entry State from.
1333 * @return the extracted Flow Entry State.
1334 */
1335 private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
1336 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1337 String switchDpidStr = flowEntryObj.getSwitchDpid();
1338 String userState = flowEntryObj.getUserState();
1339 String switchState = flowEntryObj.getSwitchState();
1340
1341 if ((flowEntryIdStr == null) ||
1342 (switchDpidStr == null) ||
1343 (userState == null) ||
1344 (switchState == null)) {
1345 // TODO: A work-around, becauuse of some bogus database objects
1346 return null;
1347 }
1348
1349 FlowEntry flowEntry = new FlowEntry();
1350 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
1351 flowEntry.setDpid(new Dpid(switchDpidStr));
1352
1353 //
1354 // Extract the match conditions
1355 //
1356 FlowEntryMatch match = new FlowEntryMatch();
1357 Short matchInPort = flowEntryObj.getMatchInPort();
1358 if (matchInPort != null)
1359 match.enableInPort(new Port(matchInPort));
1360 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1361 if (matchEthernetFrameType != null)
1362 match.enableEthernetFrameType(matchEthernetFrameType);
1363 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1364 if (matchSrcIPv4Net != null)
1365 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1366 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1367 if (matchDstIPv4Net != null)
1368 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1369 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1370 if (matchSrcMac != null)
1371 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1372 String matchDstMac = flowEntryObj.getMatchDstMac();
1373 if (matchDstMac != null)
1374 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1375 flowEntry.setFlowEntryMatch(match);
1376
1377 //
1378 // Extract the actions
1379 //
1380 ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>();
1381 Short actionOutputPort = flowEntryObj.getActionOutput();
1382 if (actionOutputPort != null) {
1383 FlowEntryAction action = new FlowEntryAction();
1384 action.setActionOutput(new Port(actionOutputPort));
1385 actions.add(action);
1386 }
1387 flowEntry.setFlowEntryActions(actions);
1388 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
1389 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1390 //
1391 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
1392 // and FlowEntryErrorState.
1393 //
1394 return flowEntry;
1395 }
1396
1397 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001398 * Add and maintain a shortest-path flow.
1399 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001400 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001401 *
1402 * @param flowPath the Flow Path with the endpoints and the match
1403 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001404 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001405 */
1406 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001407 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001408 //
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +00001409 // Don't do the shortest path computation here.
1410 // Instead, let the Flow reconciliation thread take care of it.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001411 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001412
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +00001413 // We need the DataPath to populate the Network MAP
1414 DataPath dataPath = new DataPath();
1415 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1416 dataPath.setDstPort(flowPath.dataPath().dstPort());
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001417
1418 //
1419 // Prepare the computed Flow Path
1420 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001421 FlowPath computedFlowPath = new FlowPath();
1422 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1423 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1424 computedFlowPath.setDataPath(dataPath);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001425 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001426
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001427 FlowId flowId = new FlowId();
Pavlin Radoslavov8b4b0592013-04-10 04:33:33 +00001428 String dataPathSummaryStr = dataPath.dataPathSummary();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001429 if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr))
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001430 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001431
1432 // TODO: Mark the flow for maintenance purpose
1433
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001434 return (computedFlowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001435 }
1436
1437 /**
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001438 * Reconcile a flow.
1439 *
1440 * @param flowObj the flow that needs to be reconciliated.
1441 * @param newDataPath the new data path to use.
1442 * @return true on success, otherwise false.
1443 */
1444 public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) {
1445 Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches();
1446
1447 //
1448 // Set the incoming port matching and the outgoing port output
1449 // actions for each flow entry.
1450 //
1451 for (FlowEntry flowEntry : newDataPath.flowEntries()) {
1452 // Set the incoming port matching
1453 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
1454 flowEntry.setFlowEntryMatch(flowEntryMatch);
1455 flowEntryMatch.enableInPort(flowEntry.inPort());
1456
1457 // Set the outgoing port output action
1458 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1459 if (flowEntryActions == null) {
1460 flowEntryActions = new ArrayList<FlowEntryAction>();
1461 flowEntry.setFlowEntryActions(flowEntryActions);
1462 }
1463 FlowEntryAction flowEntryAction = new FlowEntryAction();
1464 flowEntryAction.setActionOutput(flowEntry.outPort());
1465 flowEntryActions.add(flowEntryAction);
1466 }
1467
1468 //
1469 // Remove the old Flow Entries, and add the new Flow Entries
1470 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001471 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1472 LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>();
1473 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001474 flowEntryObj.setUserState("FE_USER_DELETE");
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -07001475 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001476 }
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001477 for (FlowEntry flowEntry : newDataPath.flowEntries()) {
Pavlin Radoslavov6fb76d12013-04-09 22:52:25 -07001478 addFlowEntry(flowObj, flowEntry);
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001479 }
1480
1481 //
1482 // Set the Data Path Summary
1483 //
1484 String dataPathSummaryStr = newDataPath.dataPathSummary();
1485 flowObj.setDataPathSummary(dataPathSummaryStr);
1486
1487 return true;
1488 }
1489
1490 /**
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001491 * Reconcile all flows in a set.
1492 *
1493 * @param flowObjSet the set of flows that need to be reconciliated.
1494 */
1495 public void reconcileFlows(Iterable<IFlowPath> flowObjSet) {
1496 if (! flowObjSet.iterator().hasNext())
1497 return;
Pavlin Radoslavov0eeb15d2013-04-05 10:23:51 -07001498 // TODO: Not implemented/used yet.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001499 }
1500
1501 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001502 * Install a Flow Entry on a switch.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001503 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001504 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001505 * @param flowObj the flow path object for the flow entry to install.
1506 * @param flowEntryObj the flow entry object to install.
1507 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001508 */
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001509 public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj,
1510 IFlowEntry flowEntryObj) {
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001511 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1512 if (flowEntryIdStr == null)
1513 return false;
1514 FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr);
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001515 String userState = flowEntryObj.getUserState();
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001516 if (userState == null)
1517 return false;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001518
1519 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001520 // Create the Open Flow Flow Modification Entry to push
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001521 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001522 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1523 .getMessage(OFType.FLOW_MOD);
1524 long cookie = flowEntryId.value();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001525
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001526 short flowModCommand = OFFlowMod.OFPFC_ADD;
1527 if (userState.equals("FE_USER_ADD")) {
1528 flowModCommand = OFFlowMod.OFPFC_ADD;
1529 } else if (userState.equals("FE_USER_MODIFY")) {
1530 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1531 } else if (userState.equals("FE_USER_DELETE")) {
1532 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1533 } else {
1534 // Unknown user state. Ignore the entry
1535 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1536 flowEntryId.toString(), userState);
1537 return false;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001538 }
1539
1540 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001541 // Fetch the match conditions.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001542 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001543 // NOTE: The Flow matching conditions common for all Flow Entries are
1544 // used ONLY if a Flow Entry does NOT have the corresponding matching
1545 // condition set.
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001546 //
1547 OFMatch match = new OFMatch();
1548 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001549
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001550 // Match the Incoming Port
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001551 Short matchInPort = flowEntryObj.getMatchInPort();
1552 if (matchInPort != null) {
1553 match.setInputPort(matchInPort);
1554 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1555 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001556
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001557 // Match the Ethernet Frame Type
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001558 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1559 if (matchEthernetFrameType == null)
1560 matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1561 if (matchEthernetFrameType != null) {
1562 match.setDataLayerType(matchEthernetFrameType);
1563 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1564 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001565
1566 // Match the Source IPv4 Network prefix
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001567 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1568 if (matchSrcIPv4Net == null)
1569 matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1570 if (matchSrcIPv4Net != null) {
1571 match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC);
1572 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001573
1574 // Natch the Destination IPv4 Network prefix
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001575 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1576 if (matchDstIPv4Net == null)
1577 matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1578 if (matchDstIPv4Net != null) {
1579 match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST);
1580 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001581
1582 // Match the Source MAC address
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001583 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1584 if (matchSrcMac == null)
1585 matchSrcMac = flowObj.getMatchSrcMac();
1586 if (matchSrcMac != null) {
1587 match.setDataLayerSource(matchSrcMac);
1588 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1589 }
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001590
1591 // Match the Destination MAC address
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001592 String matchDstMac = flowEntryObj.getMatchDstMac();
1593 if (matchDstMac == null)
1594 matchDstMac = flowObj.getMatchDstMac();
1595 if (matchDstMac != null) {
1596 match.setDataLayerDestination(matchDstMac);
1597 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1598 }
1599
1600 //
1601 // Fetch the actions
1602 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001603 // TODO: For now we support only the "OUTPUT" actions.
1604 //
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001605 List<OFAction> actions = new ArrayList<OFAction>();
1606 Short actionOutputPort = flowEntryObj.getActionOutput();
1607 if (actionOutputPort != null) {
1608 OFActionOutput action = new OFActionOutput();
1609 // XXX: The max length is hard-coded for now
1610 action.setMaxLength((short)0xffff);
1611 action.setPort(actionOutputPort);
1612 actions.add(action);
1613 }
1614
1615 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1616 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1617 .setPriority(PRIORITY_DEFAULT)
1618 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1619 .setCookie(cookie)
1620 .setCommand(flowModCommand)
1621 .setMatch(match)
1622 .setActions(actions)
1623 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1624 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1625 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1626 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1627 if (actionOutputPort != null)
1628 fm.setOutPort(actionOutputPort);
1629 }
1630
1631 //
1632 // TODO: Set the following flag
1633 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1634 // See method ForwardingBase::pushRoute()
1635 //
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001636
1637 //
1638 // Write the message to the switch
1639 //
Pavlin Radoslavov20d35a22013-04-05 10:16:15 -07001640 log.debug("MEASUREMENT: Installing flow entry " + userState +
1641 " into switch DPID: " +
1642 mySwitch.getStringId() +
1643 " flowEntryId: " + flowEntryId.toString() +
1644 " srcMac: " + matchSrcMac + " dstMac: " + matchDstMac +
1645 " inPort: " + matchInPort + " outPort: " + actionOutputPort
1646 );
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001647 try {
1648 messageDamper.write(mySwitch, fm, null);
1649 mySwitch.flush();
1650 //
1651 // TODO: We should use the OpenFlow Barrier mechanism
1652 // to check for errors, and update the SwitchState
1653 // for a flow entry after the Barrier message is
1654 // is received.
1655 //
1656 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
1657 } catch (IOException e) {
1658 log.error("Failure writing flow mod from network map", e);
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001659 return false;
Pavlin Radoslavovec8e2e62013-04-04 18:18:29 -07001660 }
1661
1662 return true;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001663 }
1664
1665 /**
1666 * Install a Flow Entry on a switch.
1667 *
1668 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001669 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001670 * @param flowEntry the flow entry to install.
1671 * @return true on success, otherwise false.
1672 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001673 public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1674 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001675 //
1676 // Create the OpenFlow Flow Modification Entry to push
1677 //
1678 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1679 .getMessage(OFType.FLOW_MOD);
1680 long cookie = flowEntry.flowEntryId().value();
1681
1682 short flowModCommand = OFFlowMod.OFPFC_ADD;
1683 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
1684 flowModCommand = OFFlowMod.OFPFC_ADD;
1685 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
1686 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1687 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
1688 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1689 } else {
1690 // Unknown user state. Ignore the entry
1691 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1692 flowEntry.flowEntryId().toString(),
1693 flowEntry.flowEntryUserState());
1694 return false;
1695 }
1696
1697 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001698 // Fetch the match conditions.
1699 //
1700 // NOTE: The Flow matching conditions common for all Flow Entries are
1701 // used ONLY if a Flow Entry does NOT have the corresponding matching
1702 // condition set.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001703 //
1704 OFMatch match = new OFMatch();
1705 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001706 FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch();
1707 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
1708
1709 // Match the Incoming Port
1710 Port matchInPort = flowEntryMatch.inPort();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001711 if (matchInPort != null) {
1712 match.setInputPort(matchInPort.value());
1713 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1714 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001715
1716 // Match the Ethernet Frame Type
1717 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
1718 if ((matchEthernetFrameType == null) && (flowPathMatch != null)) {
1719 matchEthernetFrameType = flowPathMatch.ethernetFrameType();
1720 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001721 if (matchEthernetFrameType != null) {
1722 match.setDataLayerType(matchEthernetFrameType);
1723 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1724 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001725
1726 // Match the Source IPv4 Network prefix
1727 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
1728 if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) {
1729 matchSrcIPv4Net = flowPathMatch.srcIPv4Net();
1730 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001731 if (matchSrcIPv4Net != null) {
1732 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
1733 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001734
1735 // Natch the Destination IPv4 Network prefix
1736 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
1737 if ((matchDstIPv4Net == null) && (flowPathMatch != null)) {
1738 matchDstIPv4Net = flowPathMatch.dstIPv4Net();
1739 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001740 if (matchDstIPv4Net != null) {
1741 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
1742 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001743
1744 // Match the Source MAC address
1745 MACAddress matchSrcMac = flowEntryMatch.srcMac();
1746 if ((matchSrcMac == null) && (flowPathMatch != null)) {
1747 matchSrcMac = flowPathMatch.srcMac();
1748 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001749 if (matchSrcMac != null) {
1750 match.setDataLayerSource(matchSrcMac.toString());
1751 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1752 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001753
1754 // Match the Destination MAC address
1755 MACAddress matchDstMac = flowEntryMatch.dstMac();
1756 if ((matchDstMac == null) && (flowPathMatch != null)) {
1757 matchDstMac = flowPathMatch.dstMac();
1758 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001759 if (matchDstMac != null) {
1760 match.setDataLayerDestination(matchDstMac.toString());
1761 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1762 }
1763
1764 //
1765 // Fetch the actions
1766 //
1767 // TODO: For now we support only the "OUTPUT" actions.
1768 //
1769 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1770 List<OFAction> actions = new ArrayList<OFAction>();
1771 ArrayList<FlowEntryAction> flowEntryActions =
1772 flowEntry.flowEntryActions();
1773 for (FlowEntryAction flowEntryAction : flowEntryActions) {
1774 FlowEntryAction.ActionOutput actionOutput =
1775 flowEntryAction.actionOutput();
1776 if (actionOutput != null) {
1777 short actionOutputPort = actionOutput.port().value();
1778 OFActionOutput action = new OFActionOutput();
1779 // XXX: The max length is hard-coded for now
1780 action.setMaxLength((short)0xffff);
1781 action.setPort(actionOutputPort);
1782 actions.add(action);
1783 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1784 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1785 fm.setOutPort(actionOutputPort);
1786 }
1787 }
1788 }
1789
1790 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1791 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1792 .setPriority(PRIORITY_DEFAULT)
1793 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1794 .setCookie(cookie)
1795 .setCommand(flowModCommand)
1796 .setMatch(match)
1797 .setActions(actions)
1798 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1799
1800 //
1801 // TODO: Set the following flag
1802 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1803 // See method ForwardingBase::pushRoute()
1804 //
1805
1806 //
1807 // Write the message to the switch
1808 //
1809 try {
1810 messageDamper.write(mySwitch, fm, null);
1811 mySwitch.flush();
Pavlin Radoslavov44a3dcd2013-04-04 18:42:56 -07001812 //
1813 // TODO: We should use the OpenFlow Barrier mechanism
1814 // to check for errors, and update the SwitchState
1815 // for a flow entry after the Barrier message is
1816 // is received.
1817 //
1818 // TODO: The FlowEntry Object in Titan should be set
1819 // to FE_SWITCH_UPDATED.
1820 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001821 } catch (IOException e) {
1822 log.error("Failure writing flow mod from network map", e);
1823 return false;
1824 }
1825 return true;
1826 }
1827
1828 /**
1829 * Remove a Flow Entry from a switch.
1830 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001831 * @param mySwitch the switch to remove the Flow Entry from.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001832 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001833 * @param flowEntry the flow entry to remove.
1834 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001835 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001836 public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1837 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001838 //
1839 // The installFlowEntry() method implements both installation
1840 // and removal of flow entries.
1841 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001842 return (installFlowEntry(mySwitch, flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001843 }
1844
1845 /**
1846 * Install a Flow Entry on a remote controller.
1847 *
1848 * TODO: We need it now: Jono
1849 * - For now it will make a REST call to the remote controller.
1850 * - Internally, it needs to know the name of the remote controller.
1851 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001852 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001853 * @param flowEntry the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001854 * @return true on success, otherwise false.
1855 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001856 public boolean installRemoteFlowEntry(FlowPath flowPath,
1857 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001858 // TODO: We need it now: Jono
1859 // - For now it will make a REST call to the remote controller.
1860 // - Internally, it needs to know the name of the remote controller.
1861 return true;
1862 }
1863
1864 /**
1865 * Remove a flow entry on a remote controller.
1866 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001867 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001868 * @param flowEntry the flow entry to remove.
1869 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001870 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001871 public boolean removeRemoteFlowEntry(FlowPath flowPath,
1872 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001873 //
1874 // The installRemoteFlowEntry() method implements both installation
1875 // and removal of flow entries.
1876 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001877 return (installRemoteFlowEntry(flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001878 }
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001879
1880 /**
1881 * Store a path flow for measurement purpose.
1882 *
1883 * NOTE: The Flow Path argument does NOT contain flow entries.
1884 * The Shortest Path is computed, and the corresponding Flow Entries
1885 * are stored in the Flow Path.
1886 *
1887 * @param flowPath the Flow Path with the endpoints and the match
1888 * conditions to store.
1889 * @return the stored shortest-path flow on success, otherwise null.
1890 */
1891 @Override
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00001892 public synchronized FlowPath measurementStorePathFlow(FlowPath flowPath) {
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001893 //
1894 // Prepare the Shortest Path computation if the first Flow Path
1895 //
1896 if (measurementStoredPaths.isEmpty())
Pavlin Radoslavov9556b142013-05-20 21:49:04 +00001897 measurementShortestPathTopo = topoRouteService.prepareShortestPathTopo();
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001898
1899 //
1900 // Compute the Shortest Path
1901 //
1902 DataPath dataPath =
Pavlin Radoslavov9556b142013-05-20 21:49:04 +00001903 topoRouteService.getTopoShortestPath(measurementShortestPathTopo,
1904 flowPath.dataPath().srcPort(),
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001905 flowPath.dataPath().dstPort());
1906 if (dataPath == null) {
1907 // We need the DataPath to populate the Network MAP
1908 dataPath = new DataPath();
1909 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1910 dataPath.setDstPort(flowPath.dataPath().dstPort());
1911 }
1912
1913 //
1914 // Set the incoming port matching and the outgoing port output
1915 // actions for each flow entry.
1916 //
1917 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1918 // Set the incoming port matching
1919 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
1920 flowEntry.setFlowEntryMatch(flowEntryMatch);
1921 flowEntryMatch.enableInPort(flowEntry.inPort());
1922
1923 // Set the outgoing port output action
1924 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1925 if (flowEntryActions == null) {
1926 flowEntryActions = new ArrayList<FlowEntryAction>();
1927 flowEntry.setFlowEntryActions(flowEntryActions);
1928 }
1929 FlowEntryAction flowEntryAction = new FlowEntryAction();
1930 flowEntryAction.setActionOutput(flowEntry.outPort());
1931 flowEntryActions.add(flowEntryAction);
1932 }
1933
1934 //
1935 // Prepare the computed Flow Path
1936 //
1937 FlowPath computedFlowPath = new FlowPath();
1938 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1939 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1940 computedFlowPath.setDataPath(dataPath);
1941 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
1942
1943 //
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00001944 // Add the computed Flow Path to the internal storage
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001945 //
1946 measurementStoredPaths.add(computedFlowPath);
1947
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00001948 log.debug("Measurement storing path {}",
1949 computedFlowPath.flowId().toString());
1950
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001951 return (computedFlowPath);
1952 }
1953
1954 /**
1955 * Install path flows for measurement purpose.
1956 *
1957 * @param numThreads the number of threads to use to install the path
1958 * flows.
1959 * @return true on success, otherwise false.
1960 */
1961 @Override
1962 public boolean measurementInstallPaths(Integer numThreads) {
1963 List<Thread> threads = new LinkedList<Thread>();
1964
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00001965 // Create a copy of the Flow Paths to install
1966 final ConcurrentLinkedQueue<FlowPath> measurementProcessingPaths =
1967 new ConcurrentLinkedQueue<FlowPath>(measurementStoredPaths);
1968
1969 log.debug("Measurement Installing {} flows",
1970 measurementProcessingPaths.size());
Pavlin Radoslavove0938f32013-05-07 23:17:22 +00001971
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001972 //
1973 // Create the threads to install the Flow Paths
1974 //
1975 for (int i = 0; i < numThreads; i++) {
1976 Thread thread = new Thread(new Runnable() {
1977 @Override
1978 public void run() {
1979 while (true) {
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00001980 FlowPath flowPath = measurementProcessingPaths.poll();
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07001981 if (flowPath == null)
1982 return;
1983 // Install the Flow Path
1984 FlowId flowId = new FlowId();
1985 String dataPathSummaryStr =
1986 flowPath.dataPath().dataPathSummary();
1987 addFlow(flowPath, flowId, dataPathSummaryStr);
1988 }
1989 }}, "Measurement Add Flow Path");
1990 threads.add(thread);
1991 }
1992
1993 //
1994 // Start processing
1995 //
1996 measurementEndTimeProcessingPaths = 0;
1997 measurementStartTimeProcessingPaths = System.nanoTime();
1998 for (Thread thread : threads) {
1999 thread.start();
2000 }
2001
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00002002 // Want for all threads to complete
2003 for (Thread thread : threads) {
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07002004 try {
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00002005 thread.join();
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07002006 } catch (InterruptedException e) {
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00002007 log.debug("Exception waiting for a thread to install a Flow Path: ", e);
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07002008 }
2009 }
2010
Pavlin Radoslavov759772f2013-05-20 20:50:00 +00002011 // Record the end of processing
2012 measurementEndTimeProcessingPaths = System.nanoTime();
2013
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07002014 return true;
2015 }
2016
2017 /**
2018 * Get the measurement time that took to install the path flows.
2019 *
2020 * @return the measurement time (in nanoseconds) it took to install
2021 * the path flows.
2022 */
2023 @Override
2024 public Long measurementGetInstallPathsTimeNsec() {
2025 return new Long(measurementEndTimeProcessingPaths -
2026 measurementStartTimeProcessingPaths);
2027 }
2028
2029 /**
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07002030 * Clear the path flows stored for measurement purpose.
2031 *
2032 * @return true on success, otherwise false.
2033 */
2034 @Override
2035 public boolean measurementClearAllPaths() {
2036 measurementStoredPaths.clear();
Pavlin Radoslavov9556b142013-05-20 21:49:04 +00002037 topoRouteService.dropShortestPathTopo(measurementShortestPathTopo);
Pavlin Radoslavove0938f32013-05-07 23:17:22 +00002038 measurementStartTimeProcessingPaths = 0;
2039 measurementEndTimeProcessingPaths = 0;
Pavlin Radoslavov4ef543e2013-05-07 13:36:57 -07002040
2041 return true;
2042 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08002043}