blob: afadaa40970ae05fe1150505f22ed96823c81b53 [file] [log] [blame]
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001package net.onrc.onos.ofcontroller.flowmanager;
2
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07003import java.util.ArrayList;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07004import java.util.Collection;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07005import java.util.HashMap;
6import java.util.Iterator;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07007import java.util.LinkedList;
8import java.util.List;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07009import java.util.Map;
Pavlin Radoslavov53219802013-12-06 11:02:04 -080010import java.util.SortedMap;
11import java.util.TreeMap;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070012import java.util.concurrent.BlockingQueue;
13import java.util.concurrent.LinkedBlockingQueue;
14
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080015import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070016import net.onrc.onos.datagrid.IDatagridService;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070017import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070018import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -070019import net.onrc.onos.ofcontroller.topology.TopologyManager;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070020import net.onrc.onos.ofcontroller.util.DataPath;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070021import net.onrc.onos.ofcontroller.util.EventEntry;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070022import net.onrc.onos.ofcontroller.util.FlowEntry;
23import net.onrc.onos.ofcontroller.util.FlowEntryAction;
24import net.onrc.onos.ofcontroller.util.FlowEntryActions;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -070025import net.onrc.onos.ofcontroller.util.FlowEntryId;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070026import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
27import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
28import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -070029import net.onrc.onos.ofcontroller.util.FlowId;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070030import net.onrc.onos.ofcontroller.util.FlowPath;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070031import net.onrc.onos.ofcontroller.util.FlowPathUserState;
Pavlin Radoslavov53219802013-12-06 11:02:04 -080032import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
33
34import com.esotericsoftware.kryo2.Kryo;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070035
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39/**
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -070040 * Class for FlowPath Maintenance.
41 * This class listens for FlowEvents to:
42 * - Maintain a local cache of the Network Topology.
43 * - Detect FlowPaths impacted by Topology change.
44 * - Recompute impacted FlowPath using cached Topology.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070045 */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070046class FlowEventHandler extends Thread implements IFlowEventHandlerService {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070047 /** The logger. */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070048 private final static Logger log = LoggerFactory.getLogger(FlowEventHandler.class);
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070049
50 private FlowManager flowManager; // The Flow Manager to use
51 private IDatagridService datagridService; // The Datagrid Service to use
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070052 private Topology topology; // The network topology
Pavlin Radoslavov53219802013-12-06 11:02:04 -080053 private KryoFactory kryoFactory = new KryoFactory();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070054
55 // The queue with Flow Path and Topology Element updates
56 private BlockingQueue<EventEntry<?>> networkEvents =
57 new LinkedBlockingQueue<EventEntry<?>>();
58
Pavlin Radoslavovb7506842013-10-29 17:46:54 -070059 // The pending Topology, FlowPath, and FlowEntry events
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070060 private List<EventEntry<TopologyElement>> topologyEvents =
61 new LinkedList<EventEntry<TopologyElement>>();
62 private List<EventEntry<FlowPath>> flowPathEvents =
63 new LinkedList<EventEntry<FlowPath>>();
Pavlin Radoslavovb7506842013-10-29 17:46:54 -070064 private List<EventEntry<FlowEntry>> flowEntryEvents =
65 new LinkedList<EventEntry<FlowEntry>>();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070066
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080067 // All internally computed Flow Paths
68 private Map<Long, FlowPath> allFlowPaths = new HashMap<Long, FlowPath>();
69
70 // The Flow Entries received as notifications with unmatched Flow Paths
71 private Map<Long, FlowEntry> unmatchedFlowEntryAdd =
72 new HashMap<Long, FlowEntry>();
73
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080074 //
75 // Transient state for processing the Flow Paths:
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080076 // - The Flow Paths that should be recomputed
77 // - The Flow Paths with modified Flow Entries
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080078 // - The Flow Paths that we should check if installed in all switches
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080079 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080080 private Map<Long, FlowPath> shouldRecomputeFlowPaths =
81 new HashMap<Long, FlowPath>();
82 private Map<Long, FlowPath> modifiedFlowPaths =
83 new HashMap<Long, FlowPath>();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080084 private Map<Long, FlowPath> checkIfInstalledFlowPaths =
85 new HashMap<Long, FlowPath>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080086
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070087 /**
88 * Constructor for a given Flow Manager and Datagrid Service.
89 *
90 * @param flowManager the Flow Manager to use.
91 * @param datagridService the Datagrid Service to use.
92 */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070093 FlowEventHandler(FlowManager flowManager,
94 IDatagridService datagridService) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070095 this.flowManager = flowManager;
96 this.datagridService = datagridService;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070097 this.topology = new Topology();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070098 }
99
100 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800101 * Get the network topology.
102 *
103 * @return the network topology.
104 */
105 protected Topology getTopology() { return this.topology; }
106
107 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800108 * Startup processing.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700109 */
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800110 private void startup() {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700111 //
112 // Obtain the initial Topology state
113 //
114 Collection<TopologyElement> topologyElements =
115 datagridService.getAllTopologyElements();
116 for (TopologyElement topologyElement : topologyElements) {
117 EventEntry<TopologyElement> eventEntry =
118 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
119 topologyEvents.add(eventEntry);
120 }
121 //
122 // Obtain the initial Flow Path state
123 //
124 Collection<FlowPath> flowPaths = datagridService.getAllFlows();
125 for (FlowPath flowPath : flowPaths) {
126 EventEntry<FlowPath> eventEntry =
127 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
128 flowPathEvents.add(eventEntry);
129 }
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700130 //
131 // Obtain the initial FlowEntry state
132 //
133 Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries();
134 for (FlowEntry flowEntry : flowEntries) {
135 EventEntry<FlowEntry> eventEntry =
136 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
137 flowEntryEvents.add(eventEntry);
138 }
139
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800140 // Process the initial events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800141 synchronized (allFlowPaths) {
142 processEvents();
143 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800144 }
145
146 /**
147 * Run the thread.
148 */
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800149 @Override
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800150 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800151 this.setName("FlowEventHandler " + this.getId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800152 startup();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700153
154 //
155 // The main loop
156 //
157 Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>();
158 try {
159 while (true) {
160 EventEntry<?> eventEntry = networkEvents.take();
161 collection.add(eventEntry);
162 networkEvents.drainTo(collection);
163
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700164 //
165 // Demultiplex all events:
166 // - EventEntry<TopologyElement>
167 // - EventEntry<FlowPath>
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700168 // - EventEntry<FlowEntry>
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700169 //
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700170 for (EventEntry<?> event : collection) {
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800171 // Topology event
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700172 if (event.eventData() instanceof TopologyElement) {
173 EventEntry<TopologyElement> topologyEventEntry =
174 (EventEntry<TopologyElement>)event;
175 topologyEvents.add(topologyEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800176 continue;
177 }
178
179 // FlowPath event
180 if (event.eventData() instanceof FlowPath) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700181 EventEntry<FlowPath> flowPathEventEntry =
182 (EventEntry<FlowPath>)event;
183 flowPathEvents.add(flowPathEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800184 continue;
185 }
186
187 // FlowEntry event
188 if (event.eventData() instanceof FlowEntry) {
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700189 EventEntry<FlowEntry> flowEntryEventEntry =
190 (EventEntry<FlowEntry>)event;
191 flowEntryEvents.add(flowEntryEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800192 continue;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700193 }
194 }
195 collection.clear();
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700196
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700197 // Process the events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800198 synchronized (allFlowPaths) {
199 processEvents();
200 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700201 }
202 } catch (Exception exception) {
203 log.debug("Exception processing Network Events: ", exception);
204 }
205 }
206
207 /**
208 * Process the events (if any)
209 */
210 private void processEvents() {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800211 Collection<FlowEntry> modifiedFlowEntries;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700212
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700213 if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() &&
214 flowEntryEvents.isEmpty()) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700215 return; // Nothing to do
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700216 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700217
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800218 processFlowPathEvents();
219 processTopologyEvents();
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800220 processUnmatchedFlowEntryAdd();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800221 processFlowEntryEvents();
222
223 // Recompute all affected Flow Paths and keep only the modified
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800224 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800225 if (recomputeFlowPath(flowPath))
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800226 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800227 }
228
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800229 // Extract the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800230 modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800231
232 // Assign missing Flow Entry IDs
233 assignFlowEntryId(modifiedFlowEntries);
234
235 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800236 // Push the modified state to the Flow Manager
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800237 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800238 flowManager.pushModifiedFlowState(modifiedFlowPaths.values(),
239 modifiedFlowEntries);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800240
241 //
242 // Remove Flow Entries that were deleted
243 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800244 for (FlowPath flowPath : modifiedFlowPaths.values())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800245 flowPath.dataPath().removeDeletedFlowEntries();
246
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800247 //
248 // Check if Flow Paths have been installed into all switches,
249 // and generate the appropriate events.
250 //
251 checkInstalledFlowPaths(checkIfInstalledFlowPaths.values());
252
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800253 // Cleanup
254 topologyEvents.clear();
255 flowPathEvents.clear();
256 flowEntryEvents.clear();
257 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800258 shouldRecomputeFlowPaths.clear();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800259 modifiedFlowPaths.clear();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800260 checkIfInstalledFlowPaths.clear();
261 }
262
263 /**
264 * Check if Flow Paths have been installed into all switches,
265 * and generate the appropriate events.
266 *
267 * @param flowPaths the flowPaths to process.
268 */
269 private void checkInstalledFlowPaths(Collection<FlowPath> flowPaths) {
270 List<FlowPath> installedFlowPaths = new LinkedList<FlowPath>();
271
272 Kryo kryo = kryoFactory.newKryo();
273
274 for (FlowPath flowPath : flowPaths) {
275 boolean isInstalled = true;
276
277 //
278 // Check whether all Flow Entries have been installed
279 //
280 for (FlowEntry flowEntry : flowPath.flowEntries()) {
281 if (flowEntry.flowEntrySwitchState() !=
282 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
283 isInstalled = false;
284 break;
285 }
286 }
287
288 if (isInstalled) {
289 // Create a copy and add it to the list
290 FlowPath copyFlowPath = kryo.copy(flowPath);
291 installedFlowPaths.add(copyFlowPath);
292 }
293 }
294 kryoFactory.deleteKryo(kryo);
295
296 // Generate an event for the installed Flow Path.
297 flowManager.notificationFlowPathsInstalled(installedFlowPaths);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800298 }
299
300 /**
301 * Extract the modified Flow Entries.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800302 *
303 * @param modifiedFlowPaths the Flow Paths to process.
304 * @return a collection with the modified Flow Entries.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800305 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800306 private Collection<FlowEntry> extractModifiedFlowEntries(
307 Collection<FlowPath> modifiedFlowPaths) {
308 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800309
310 // Extract only the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800311 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800312 for (FlowEntry flowEntry : flowPath.flowEntries()) {
313 if (flowEntry.flowEntrySwitchState() ==
314 FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800315 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800316 }
317 }
318 }
319 return modifiedFlowEntries;
320 }
321
322 /**
323 * Assign the Flow Entry ID as needed.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800324 *
325 * @param modifiedFlowEnries the collection of Flow Entries that need
326 * Flow Entry ID assigned.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800327 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800328 private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800329 if (modifiedFlowEntries.isEmpty())
330 return;
331
332 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
333
334 //
335 // Assign the Flow Entry ID only for Flow Entries for my switches
336 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800337 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800338 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
339 if (mySwitch == null)
340 continue;
341 if (! flowEntry.isValidFlowEntryId()) {
342 long id = flowManager.getNextFlowEntryId();
343 flowEntry.setFlowEntryId(new FlowEntryId(id));
344 }
345 }
346 }
347
348 /**
349 * Process the Flow Path events.
350 */
351 private void processFlowPathEvents() {
352 //
353 // Process all Flow Path events and update the appropriate state
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700354 //
355 for (EventEntry<FlowPath> eventEntry : flowPathEvents) {
356 FlowPath flowPath = eventEntry.eventData();
357
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800358 log.debug("Flow Event: {} {}", eventEntry.eventType(),
359 flowPath.toString());
360
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700361 switch (eventEntry.eventType()) {
362 case ENTRY_ADD: {
363 //
364 // Add a new Flow Path
365 //
366 if (allFlowPaths.get(flowPath.flowId().value()) != null) {
367 //
368 // TODO: What to do if the Flow Path already exists?
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800369 // Fow now, we just re-add it.
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700370 //
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700371 }
372
373 switch (flowPath.flowPathType()) {
374 case FP_TYPE_SHORTEST_PATH:
375 //
376 // Reset the Data Path, in case it was set already, because
377 // we are going to recompute it anyway.
378 //
379 flowPath.flowEntries().clear();
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800380 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
381 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700382 break;
383 case FP_TYPE_EXPLICIT_PATH:
384 //
385 // Mark all Flow Entries for installation in the switches.
386 //
387 for (FlowEntry flowEntry : flowPath.flowEntries()) {
388 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
389 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800390 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700391 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800392 case FP_TYPE_UNKNOWN:
393 log.error("FlowPath event with unknown type");
394 break;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700395 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800396 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700397
398 break;
399 }
400
401 case ENTRY_REMOVE: {
402 //
403 // Remove an existing Flow Path.
404 //
405 // Find the Flow Path, and mark the Flow and its Flow Entries
406 // for deletion.
407 //
408 FlowPath existingFlowPath =
409 allFlowPaths.get(flowPath.flowId().value());
410 if (existingFlowPath == null)
411 continue; // Nothing to do
412
413 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
414 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
415 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
416 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
417 }
418
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800419 // Remove the Flow Path from the internal state
420 Long key = existingFlowPath.flowId().value();
421 allFlowPaths.remove(key);
422 shouldRecomputeFlowPaths.remove(key);
423 modifiedFlowPaths.put(key, existingFlowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700424
425 break;
426 }
427 }
428 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800429 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700430
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800431 /**
432 * Process the Topology events.
433 */
434 private void processTopologyEvents() {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700435 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800436 // Process all Topology events and update the appropriate state
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700437 //
438 boolean isTopologyModified = false;
439 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
440 TopologyElement topologyElement = eventEntry.eventData();
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800441
442 log.debug("Topology Event: {} {}", eventEntry.eventType(),
443 topologyElement.toString());
444
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700445 switch (eventEntry.eventType()) {
446 case ENTRY_ADD:
Yuta HIGUCHIb32f77f2013-10-30 15:36:32 -0700447 isTopologyModified |= topology.addTopologyElement(topologyElement);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700448 break;
449 case ENTRY_REMOVE:
Yuta HIGUCHIb32f77f2013-10-30 15:36:32 -0700450 isTopologyModified |= topology.removeTopologyElement(topologyElement);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700451 break;
452 }
453 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700454 if (isTopologyModified) {
455 // TODO: For now, if the topology changes, we recompute all Flows
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800456 shouldRecomputeFlowPaths.putAll(allFlowPaths);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700457 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800458 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700459
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800460 /**
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800461 * Process previously received Flow Entries with unmatched Flow Paths.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800462 */
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800463 private void processUnmatchedFlowEntryAdd() {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800464 FlowPath flowPath;
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800465 FlowEntry localFlowEntry;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800466
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700467 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800468 // Update Flow Entries with previously unmatched Flow Entry updates
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700469 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800470 if (! unmatchedFlowEntryAdd.isEmpty()) {
471 Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>();
472 for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) {
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800473 // log.debug("Processing Unmatched Flow Entry: {}",
474 // flowEntry.toString());
475
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800476 flowPath = allFlowPaths.get(flowEntry.flowId().value());
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800477 if (flowPath == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800478 remainingUpdates.put(flowEntry.flowEntryId().value(),
479 flowEntry);
480 continue;
481 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800482 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
483 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800484 remainingUpdates.put(flowEntry.flowEntryId().value(),
485 flowEntry);
486 continue;
487 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800488 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
489 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
490 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700491 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800492 unmatchedFlowEntryAdd = remainingUpdates;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700493 }
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800494 }
495
496 /**
497 * Process the Flow Entry events.
498 */
499 private void processFlowEntryEvents() {
500 FlowPath flowPath;
501 FlowEntry localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700502
503 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800504 // Process all Flow Entry events and update the appropriate state
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700505 //
506 for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) {
507 FlowEntry flowEntry = eventEntry.eventData();
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800508
509 log.debug("Flow Entry Event: {} {}", eventEntry.eventType(),
510 flowEntry.toString());
511
512 if ((! flowEntry.isValidFlowId()) ||
513 (! flowEntry.isValidFlowEntryId())) {
514 continue;
515 }
516
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700517 switch (eventEntry.eventType()) {
518 case ENTRY_ADD:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800519 flowPath = allFlowPaths.get(flowEntry.flowId().value());
520 if (flowPath == null) {
521 // Flow Path not found: keep the entry for later matching
522 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
523 flowEntry);
524 break;
525 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800526 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
527 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800528 // Flow Entry not found: keep the entry for later matching
529 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
530 flowEntry);
531 break;
532 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800533 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
534 // Add the updated Flow Path to the list of updated paths
535 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
536 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700537 break;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800538
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700539 case ENTRY_REMOVE:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800540 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
541 if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800542 continue; // Removed previously unmatched entry
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800543 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800544
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800545 flowPath = allFlowPaths.get(flowEntry.flowId().value());
546 if (flowPath == null) {
547 // Flow Path not found: ignore the update
548 break;
549 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800550 localFlowEntry = findFlowEntryRemove(flowPath, flowEntry);
551 if (localFlowEntry == null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800552 // Flow Entry not found: ignore it
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800553 break;
554 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800555 if (updateFlowEntryRemove(flowPath, localFlowEntry,
556 flowEntry)) {
557 // Add the updated Flow Path to the list of updated paths
558 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
559 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700560 break;
561 }
562 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700563 }
564
565 /**
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800566 * Find a Flow Entry that should be updated because of an external
567 * ENTRY_ADD event.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700568 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800569 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800570 * @param newFlowEntry the FlowEntry with the new state.
571 * @return the Flow Entry that should be updated if found, otherwise null.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700572 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800573 private FlowEntry findFlowEntryAdd(FlowPath flowPath,
574 FlowEntry newFlowEntry) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700575 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800576 // Iterate over all Flow Entries and find a match.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700577 //
578 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800579 if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry,
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800580 newFlowEntry)) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700581 continue;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800582 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700583
584 //
585 // Local Flow Entry match found
586 //
587 if (localFlowEntry.isValidFlowEntryId()) {
588 if (localFlowEntry.flowEntryId().value() !=
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800589 newFlowEntry.flowEntryId().value()) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700590 //
591 // Find a local Flow Entry, but the Flow Entry ID doesn't
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800592 // match. Keep looking.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700593 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800594 continue;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700595 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700596 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800597 return localFlowEntry;
598 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700599
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800600 return null; // Entry not found
601 }
602
603 /**
604 * Update a Flow Entry because of an external ENTRY_ADD event.
605 *
606 * @param flowPath the FlowPath for the Flow Entry to update.
607 * @param localFlowEntry the local Flow Entry to update.
608 * @param newFlowEntry the FlowEntry with the new state.
609 * @return true if the local Flow Entry was updated, otherwise false.
610 */
611 private boolean updateFlowEntryAdd(FlowPath flowPath,
612 FlowEntry localFlowEntry,
613 FlowEntry newFlowEntry) {
614 boolean updated = false;
615
616 if (localFlowEntry.flowEntryUserState() ==
617 FlowEntryUserState.FE_USER_DELETE) {
618 // Don't add-back a Flow Entry that is already deleted
619 return false;
620 }
621
622 if (! localFlowEntry.isValidFlowEntryId()) {
623 // Update the Flow Entry ID
624 FlowEntryId flowEntryId =
625 new FlowEntryId(newFlowEntry.flowEntryId().value());
626 localFlowEntry.setFlowEntryId(flowEntryId);
627 updated = true;
628 }
629
630 //
631 // Update the local Flow Entry, and keep state to check
632 // if the Flow Path has been installed.
633 //
634 if (localFlowEntry.flowEntryUserState() !=
635 newFlowEntry.flowEntryUserState()) {
636 localFlowEntry.setFlowEntryUserState(
637 newFlowEntry.flowEntryUserState());
638 updated = true;
639 }
640 if (localFlowEntry.flowEntrySwitchState() !=
641 newFlowEntry.flowEntrySwitchState()) {
642 localFlowEntry.setFlowEntrySwitchState(
643 newFlowEntry.flowEntrySwitchState());
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800644 checkIfInstalledFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800645 updated = true;
646 }
647
648 return updated;
649 }
650
651 /**
652 * Find a Flow Entry that should be updated because of an external
653 * ENTRY_REMOVE event.
654 *
655 * @param flowPath the FlowPath for the Flow Entry to update.
656 * @param newFlowEntry the FlowEntry with the new state.
657 * @return the Flow Entry that should be updated if found, otherwise null.
658 */
659 private FlowEntry findFlowEntryRemove(FlowPath flowPath,
660 FlowEntry newFlowEntry) {
661 //
662 // Iterate over all Flow Entries and find a match based on
663 // the Flow Entry ID.
664 //
665 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
666 if (! localFlowEntry.isValidFlowEntryId())
667 continue;
668 if (localFlowEntry.flowEntryId().value() !=
669 newFlowEntry.flowEntryId().value()) {
670 continue;
671 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800672 return localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700673 }
674
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800675 return null; // Entry not found
676 }
677
678 /**
679 * Update a Flow Entry because of an external ENTRY_REMOVE event.
680 *
681 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800682 * @param localFlowEntry the local Flow Entry to update.
683 * @param newFlowEntry the FlowEntry with the new state.
684 * @return true if the local Flow Entry was updated, otherwise false.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800685 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800686 private boolean updateFlowEntryRemove(FlowPath flowPath,
687 FlowEntry localFlowEntry,
688 FlowEntry newFlowEntry) {
689 boolean updated = false;
690
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800691 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800692 // Update the local Flow Entry.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800693 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800694 if (localFlowEntry.flowEntryUserState() !=
695 newFlowEntry.flowEntryUserState()) {
696 localFlowEntry.setFlowEntryUserState(
697 newFlowEntry.flowEntryUserState());
698 updated = true;
699 }
700 if (localFlowEntry.flowEntrySwitchState() !=
701 newFlowEntry.flowEntrySwitchState()) {
702 localFlowEntry.setFlowEntrySwitchState(
703 newFlowEntry.flowEntrySwitchState());
704 updated = true;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800705 }
706
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800707 return updated;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700708 }
709
710 /**
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700711 * Recompute a Flow Path.
712 *
713 * @param flowPath the Flow Path to recompute.
714 * @return true if the recomputed Flow Path has changed, otherwise false.
715 */
716 private boolean recomputeFlowPath(FlowPath flowPath) {
717 boolean hasChanged = false;
718
719 //
720 // Test whether the Flow Path needs to be recomputed
721 //
722 switch (flowPath.flowPathType()) {
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -0700723 case FP_TYPE_UNKNOWN:
724 return false; // Can't recompute on Unknown FlowType
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700725 case FP_TYPE_SHORTEST_PATH:
726 break;
727 case FP_TYPE_EXPLICIT_PATH:
728 return false; // An explicit path never changes
729 }
730
731 DataPath oldDataPath = flowPath.dataPath();
732
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700733 // Compute the new path
734 DataPath newDataPath = TopologyManager.computeNetworkPath(topology,
735 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700736 if (newDataPath == null) {
737 // We need the DataPath to compare the paths
738 newDataPath = new DataPath();
739 }
740 newDataPath.applyFlowPathFlags(flowPath.flowPathFlags());
741
742 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700743 // Test whether the new path is same
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700744 //
745 if (oldDataPath.flowEntries().size() !=
746 newDataPath.flowEntries().size()) {
747 hasChanged = true;
748 } else {
749 Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator();
750 Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator();
751 while (oldIter.hasNext() && newIter.hasNext()) {
752 FlowEntry oldFlowEntry = oldIter.next();
753 FlowEntry newFlowEntry = newIter.next();
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700754 if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
755 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700756 hasChanged = true;
757 break;
758 }
759 }
760 }
761 if (! hasChanged)
762 return hasChanged;
763
764 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700765 // Merge the changes in the path:
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700766 // - If a Flow Entry for a switch is in the old data path, but not
767 // in the new data path, then mark it for deletion.
768 // - If a Flow Entry for a switch is in the new data path, but not
769 // in the old data path, then mark it for addition.
770 // - If a Flow Entry for a switch is in both the old and the new
771 // data path, but it has changed, e.g., the incoming and/or outgoing
772 // port(s), then mark the old Flow Entry for deletion, and mark
773 // the new Flow Entry for addition.
774 // - If a Flow Entry for a switch is in both the old and the new
775 // data path, and it hasn't changed, then just keep it.
776 //
777 // NOTE: We use the Switch DPID of each entry to match the entries
778 //
779 Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>();
780 Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>();
781 ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>();
782 List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>();
783
784 // Prepare maps with the Flow Entries, so they are fast to lookup
785 for (FlowEntry flowEntry : oldDataPath.flowEntries())
786 oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
787 for (FlowEntry flowEntry : newDataPath.flowEntries())
788 newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
789
790 //
791 // Find the old Flow Entries that should be deleted
792 //
793 for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) {
794 FlowEntry newFlowEntry =
795 newFlowEntriesMap.get(oldFlowEntry.dpid().value());
796 if (newFlowEntry == null) {
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800797 // The old Flow Entry should be deleted: not on the path
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700798 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
799 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
800 deletedFlowEntries.add(oldFlowEntry);
801 }
802 }
803
804 //
805 // Find the new Flow Entries that should be added or updated
806 //
807 int idx = 0;
808 for (FlowEntry newFlowEntry : newDataPath.flowEntries()) {
809 FlowEntry oldFlowEntry =
810 oldFlowEntriesMap.get(newFlowEntry.dpid().value());
811
812 if ((oldFlowEntry != null) &&
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700813 TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
814 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700815 //
816 // Both Flow Entries are same
817 //
818 finalFlowEntries.add(oldFlowEntry);
819 idx++;
820 continue;
821 }
822
823 if (oldFlowEntry != null) {
824 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800825 // The old Flow Entry should be deleted: path diverges
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700826 //
827 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
828 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
829 deletedFlowEntries.add(oldFlowEntry);
830 }
831
832 //
833 // Add the new Flow Entry
834 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700835 //
836 // NOTE: Assign only the Flow ID.
837 // The Flow Entry ID is assigned later only for the Flow Entries
838 // this instance is responsible for.
839 //
840 newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700841
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800842 //
Pavlin Radoslavov6fde2172013-12-10 11:23:18 -0800843 // Copy the Flow timeouts
844 //
845 newFlowEntry.setIdleTimeout(flowPath.idleTimeout());
846 newFlowEntry.setHardTimeout(flowPath.hardTimeout());
847
848 //
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800849 // Allocate the FlowEntryMatch by copying the default one
850 // from the FlowPath (if set).
851 //
852 FlowEntryMatch flowEntryMatch = null;
853 if (flowPath.flowEntryMatch() != null)
854 flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch());
855 else
856 flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700857 newFlowEntry.setFlowEntryMatch(flowEntryMatch);
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800858
859 // Set the incoming port matching
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700860 flowEntryMatch.enableInPort(newFlowEntry.inPort());
861
862 //
863 // Set the actions:
864 // If the first Flow Entry, copy the Flow Path actions to it.
865 //
866 FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions();
867 if ((idx == 0) && (flowPath.flowEntryActions() != null)) {
868 FlowEntryActions flowActions =
869 new FlowEntryActions(flowPath.flowEntryActions());
870 for (FlowEntryAction action : flowActions.actions())
871 flowEntryActions.addAction(action);
872 }
873 idx++;
874
875 //
876 // Add the outgoing port output action
877 //
878 FlowEntryAction flowEntryAction = new FlowEntryAction();
879 flowEntryAction.setActionOutput(newFlowEntry.outPort());
880 flowEntryActions.addAction(flowEntryAction);
881
882 //
883 // Set the state of the new Flow Entry
884 //
885 newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
886 newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
887 finalFlowEntries.add(newFlowEntry);
888 }
889
890 //
891 // Replace the old Flow Entries with the new Flow Entries.
892 // Note that the Flow Entries that will be deleted are added at
893 // the end.
894 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800895 finalFlowEntries.addAll(deletedFlowEntries);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700896 flowPath.dataPath().setFlowEntries(finalFlowEntries);
897
898 return hasChanged;
899 }
900
901 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700902 * Receive a notification that a Flow is added.
903 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700904 * @param flowPath the Flow that is added.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700905 */
906 @Override
907 public void notificationRecvFlowAdded(FlowPath flowPath) {
908 EventEntry<FlowPath> eventEntry =
909 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
910 networkEvents.add(eventEntry);
911 }
912
913 /**
914 * Receive a notification that a Flow is removed.
915 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700916 * @param flowPath the Flow that is removed.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700917 */
918 @Override
919 public void notificationRecvFlowRemoved(FlowPath flowPath) {
920 EventEntry<FlowPath> eventEntry =
921 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath);
922 networkEvents.add(eventEntry);
923 }
924
925 /**
926 * Receive a notification that a Flow is updated.
927 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700928 * @param flowPath the Flow that is updated.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700929 */
930 @Override
931 public void notificationRecvFlowUpdated(FlowPath flowPath) {
932 // NOTE: The ADD and UPDATE events are processed in same way
933 EventEntry<FlowPath> eventEntry =
934 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
935 networkEvents.add(eventEntry);
936 }
937
938 /**
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700939 * Receive a notification that a FlowEntry is added.
940 *
941 * @param flowEntry the FlowEntry that is added.
942 */
943 @Override
944 public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) {
945 EventEntry<FlowEntry> eventEntry =
946 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
947 networkEvents.add(eventEntry);
948 }
949
950 /**
951 * Receive a notification that a FlowEntry is removed.
952 *
953 * @param flowEntry the FlowEntry that is removed.
954 */
955 @Override
956 public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) {
957 EventEntry<FlowEntry> eventEntry =
958 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry);
959 networkEvents.add(eventEntry);
960 }
961
962 /**
963 * Receive a notification that a FlowEntry is updated.
964 *
965 * @param flowEntry the FlowEntry that is updated.
966 */
967 @Override
968 public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) {
969 // NOTE: The ADD and UPDATE events are processed in same way
970 EventEntry<FlowEntry> eventEntry =
971 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
972 networkEvents.add(eventEntry);
973 }
974
975 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700976 * Receive a notification that a Topology Element is added.
977 *
978 * @param topologyElement the Topology Element that is added.
979 */
980 @Override
981 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
982 EventEntry<TopologyElement> eventEntry =
983 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
984 networkEvents.add(eventEntry);
985 }
986
987 /**
988 * Receive a notification that a Topology Element is removed.
989 *
990 * @param topologyElement the Topology Element that is removed.
991 */
992 @Override
993 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
994 EventEntry<TopologyElement> eventEntry =
995 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement);
996 networkEvents.add(eventEntry);
997 }
998
999 /**
1000 * Receive a notification that a Topology Element is updated.
1001 *
1002 * @param topologyElement the Topology Element that is updated.
1003 */
1004 @Override
1005 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
1006 // NOTE: The ADD and UPDATE events are processed in same way
1007 EventEntry<TopologyElement> eventEntry =
1008 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1009 networkEvents.add(eventEntry);
1010 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001011
1012 /**
1013 * Get a sorted copy of all Flow Paths.
1014 *
1015 * @return a sorted copy of all Flow Paths.
1016 */
1017 synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() {
1018 SortedMap<Long, FlowPath> sortedFlowPaths =
1019 new TreeMap<Long, FlowPath>();
1020
1021 //
1022 // TODO: For now we use serialization/deserialization to create
1023 // a copy of each Flow Path. In the future, we should use proper
1024 // copy constructors.
1025 //
1026 Kryo kryo = kryoFactory.newKryo();
1027 synchronized (allFlowPaths) {
1028 for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) {
1029 FlowPath origFlowPath = entry.getValue();
1030 FlowPath copyFlowPath = kryo.copy(origFlowPath);
1031 sortedFlowPaths.put(entry.getKey(), copyFlowPath);
1032 }
1033 }
1034 kryoFactory.deleteKryo(kryo);
1035
1036 return sortedFlowPaths;
1037 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001038}