blob: 8b1f7c0cff488fd207c015650f605d105c6536c3 [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
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800358 log.debug("Flow Event: {} {}", eventEntry.eventType(), flowPath);
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800359
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700360 switch (eventEntry.eventType()) {
361 case ENTRY_ADD: {
362 //
363 // Add a new Flow Path
364 //
365 if (allFlowPaths.get(flowPath.flowId().value()) != null) {
366 //
367 // TODO: What to do if the Flow Path already exists?
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800368 // Fow now, we just re-add it.
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700369 //
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700370 }
371
372 switch (flowPath.flowPathType()) {
373 case FP_TYPE_SHORTEST_PATH:
374 //
375 // Reset the Data Path, in case it was set already, because
376 // we are going to recompute it anyway.
377 //
378 flowPath.flowEntries().clear();
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800379 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
380 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700381 break;
382 case FP_TYPE_EXPLICIT_PATH:
383 //
384 // Mark all Flow Entries for installation in the switches.
385 //
386 for (FlowEntry flowEntry : flowPath.flowEntries()) {
387 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
388 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800389 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700390 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800391 case FP_TYPE_UNKNOWN:
392 log.error("FlowPath event with unknown type");
393 break;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700394 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800395 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700396
397 break;
398 }
399
400 case ENTRY_REMOVE: {
401 //
402 // Remove an existing Flow Path.
403 //
404 // Find the Flow Path, and mark the Flow and its Flow Entries
405 // for deletion.
406 //
407 FlowPath existingFlowPath =
408 allFlowPaths.get(flowPath.flowId().value());
409 if (existingFlowPath == null)
410 continue; // Nothing to do
411
412 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
413 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
414 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
415 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
416 }
417
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800418 // Remove the Flow Path from the internal state
419 Long key = existingFlowPath.flowId().value();
420 allFlowPaths.remove(key);
421 shouldRecomputeFlowPaths.remove(key);
422 modifiedFlowPaths.put(key, existingFlowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700423
424 break;
425 }
426 }
427 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800428 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700429
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800430 /**
431 * Process the Topology events.
432 */
433 private void processTopologyEvents() {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700434 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800435 // Process all Topology events and update the appropriate state
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700436 //
437 boolean isTopologyModified = false;
438 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
439 TopologyElement topologyElement = eventEntry.eventData();
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800440
441 log.debug("Topology Event: {} {}", eventEntry.eventType(),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800442 topologyElement);
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800443
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700444 switch (eventEntry.eventType()) {
445 case ENTRY_ADD:
Yuta HIGUCHIb32f77f2013-10-30 15:36:32 -0700446 isTopologyModified |= topology.addTopologyElement(topologyElement);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700447 break;
448 case ENTRY_REMOVE:
Yuta HIGUCHIb32f77f2013-10-30 15:36:32 -0700449 isTopologyModified |= topology.removeTopologyElement(topologyElement);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700450 break;
451 }
452 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700453 if (isTopologyModified) {
454 // TODO: For now, if the topology changes, we recompute all Flows
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800455 shouldRecomputeFlowPaths.putAll(allFlowPaths);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700456 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800457 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700458
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800459 /**
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800460 * Process previously received Flow Entries with unmatched Flow Paths.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800461 */
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800462 private void processUnmatchedFlowEntryAdd() {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800463 FlowPath flowPath;
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800464 FlowEntry localFlowEntry;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800465
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700466 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800467 // Update Flow Entries with previously unmatched Flow Entry updates
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700468 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800469 if (! unmatchedFlowEntryAdd.isEmpty()) {
470 Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>();
471 for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) {
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800472 // log.debug("Processing Unmatched Flow Entry: {}",
473 // flowEntry.toString());
474
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800475 flowPath = allFlowPaths.get(flowEntry.flowId().value());
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800476 if (flowPath == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800477 remainingUpdates.put(flowEntry.flowEntryId().value(),
478 flowEntry);
479 continue;
480 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800481 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
482 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800483 remainingUpdates.put(flowEntry.flowEntryId().value(),
484 flowEntry);
485 continue;
486 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800487 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
488 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
489 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700490 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800491 unmatchedFlowEntryAdd = remainingUpdates;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700492 }
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800493 }
494
495 /**
496 * Process the Flow Entry events.
497 */
498 private void processFlowEntryEvents() {
499 FlowPath flowPath;
500 FlowEntry localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700501
502 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800503 // Process all Flow Entry events and update the appropriate state
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700504 //
505 for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) {
506 FlowEntry flowEntry = eventEntry.eventData();
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800507
508 log.debug("Flow Entry Event: {} {}", eventEntry.eventType(),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800509 flowEntry);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800510
511 if ((! flowEntry.isValidFlowId()) ||
512 (! flowEntry.isValidFlowEntryId())) {
513 continue;
514 }
515
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700516 switch (eventEntry.eventType()) {
517 case ENTRY_ADD:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800518 flowPath = allFlowPaths.get(flowEntry.flowId().value());
519 if (flowPath == null) {
520 // Flow Path not found: keep the entry for later matching
521 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
522 flowEntry);
523 break;
524 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800525 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
526 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800527 // Flow Entry not found: keep the entry for later matching
528 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
529 flowEntry);
530 break;
531 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800532 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
533 // Add the updated Flow Path to the list of updated paths
534 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
535 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700536 break;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800537
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700538 case ENTRY_REMOVE:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800539 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
540 if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800541 continue; // Removed previously unmatched entry
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800542 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800543
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800544 flowPath = allFlowPaths.get(flowEntry.flowId().value());
545 if (flowPath == null) {
546 // Flow Path not found: ignore the update
547 break;
548 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800549 localFlowEntry = findFlowEntryRemove(flowPath, flowEntry);
550 if (localFlowEntry == null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800551 // Flow Entry not found: ignore it
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800552 break;
553 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800554 if (updateFlowEntryRemove(flowPath, localFlowEntry,
555 flowEntry)) {
556 // Add the updated Flow Path to the list of updated paths
557 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
558 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700559 break;
560 }
561 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700562 }
563
564 /**
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800565 * Find a Flow Entry that should be updated because of an external
566 * ENTRY_ADD event.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700567 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800568 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800569 * @param newFlowEntry the FlowEntry with the new state.
570 * @return the Flow Entry that should be updated if found, otherwise null.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700571 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800572 private FlowEntry findFlowEntryAdd(FlowPath flowPath,
573 FlowEntry newFlowEntry) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700574 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800575 // Iterate over all Flow Entries and find a match.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700576 //
577 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800578 if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry,
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800579 newFlowEntry)) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700580 continue;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800581 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700582
583 //
584 // Local Flow Entry match found
585 //
586 if (localFlowEntry.isValidFlowEntryId()) {
587 if (localFlowEntry.flowEntryId().value() !=
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800588 newFlowEntry.flowEntryId().value()) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700589 //
590 // Find a local Flow Entry, but the Flow Entry ID doesn't
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800591 // match. Keep looking.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700592 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800593 continue;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700594 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700595 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800596 return localFlowEntry;
597 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700598
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800599 return null; // Entry not found
600 }
601
602 /**
603 * Update a Flow Entry because of an external ENTRY_ADD event.
604 *
605 * @param flowPath the FlowPath for the Flow Entry to update.
606 * @param localFlowEntry the local Flow Entry to update.
607 * @param newFlowEntry the FlowEntry with the new state.
608 * @return true if the local Flow Entry was updated, otherwise false.
609 */
610 private boolean updateFlowEntryAdd(FlowPath flowPath,
611 FlowEntry localFlowEntry,
612 FlowEntry newFlowEntry) {
613 boolean updated = false;
614
615 if (localFlowEntry.flowEntryUserState() ==
616 FlowEntryUserState.FE_USER_DELETE) {
617 // Don't add-back a Flow Entry that is already deleted
618 return false;
619 }
620
621 if (! localFlowEntry.isValidFlowEntryId()) {
622 // Update the Flow Entry ID
623 FlowEntryId flowEntryId =
624 new FlowEntryId(newFlowEntry.flowEntryId().value());
625 localFlowEntry.setFlowEntryId(flowEntryId);
626 updated = true;
627 }
628
629 //
630 // Update the local Flow Entry, and keep state to check
631 // if the Flow Path has been installed.
632 //
633 if (localFlowEntry.flowEntryUserState() !=
634 newFlowEntry.flowEntryUserState()) {
635 localFlowEntry.setFlowEntryUserState(
636 newFlowEntry.flowEntryUserState());
637 updated = true;
638 }
639 if (localFlowEntry.flowEntrySwitchState() !=
640 newFlowEntry.flowEntrySwitchState()) {
641 localFlowEntry.setFlowEntrySwitchState(
642 newFlowEntry.flowEntrySwitchState());
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800643 checkIfInstalledFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800644 updated = true;
645 }
646
647 return updated;
648 }
649
650 /**
651 * Find a Flow Entry that should be updated because of an external
652 * ENTRY_REMOVE event.
653 *
654 * @param flowPath the FlowPath for the Flow Entry to update.
655 * @param newFlowEntry the FlowEntry with the new state.
656 * @return the Flow Entry that should be updated if found, otherwise null.
657 */
658 private FlowEntry findFlowEntryRemove(FlowPath flowPath,
659 FlowEntry newFlowEntry) {
660 //
661 // Iterate over all Flow Entries and find a match based on
662 // the Flow Entry ID.
663 //
664 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
665 if (! localFlowEntry.isValidFlowEntryId())
666 continue;
667 if (localFlowEntry.flowEntryId().value() !=
668 newFlowEntry.flowEntryId().value()) {
669 continue;
670 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800671 return localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700672 }
673
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800674 return null; // Entry not found
675 }
676
677 /**
678 * Update a Flow Entry because of an external ENTRY_REMOVE event.
679 *
680 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800681 * @param localFlowEntry the local Flow Entry to update.
682 * @param newFlowEntry the FlowEntry with the new state.
683 * @return true if the local Flow Entry was updated, otherwise false.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800684 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800685 private boolean updateFlowEntryRemove(FlowPath flowPath,
686 FlowEntry localFlowEntry,
687 FlowEntry newFlowEntry) {
688 boolean updated = false;
689
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800690 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800691 // Update the local Flow Entry.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800692 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800693 if (localFlowEntry.flowEntryUserState() !=
694 newFlowEntry.flowEntryUserState()) {
695 localFlowEntry.setFlowEntryUserState(
696 newFlowEntry.flowEntryUserState());
697 updated = true;
698 }
699 if (localFlowEntry.flowEntrySwitchState() !=
700 newFlowEntry.flowEntrySwitchState()) {
701 localFlowEntry.setFlowEntrySwitchState(
702 newFlowEntry.flowEntrySwitchState());
703 updated = true;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800704 }
705
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800706 return updated;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700707 }
708
709 /**
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700710 * Recompute a Flow Path.
711 *
712 * @param flowPath the Flow Path to recompute.
713 * @return true if the recomputed Flow Path has changed, otherwise false.
714 */
715 private boolean recomputeFlowPath(FlowPath flowPath) {
716 boolean hasChanged = false;
717
718 //
719 // Test whether the Flow Path needs to be recomputed
720 //
721 switch (flowPath.flowPathType()) {
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -0700722 case FP_TYPE_UNKNOWN:
723 return false; // Can't recompute on Unknown FlowType
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700724 case FP_TYPE_SHORTEST_PATH:
725 break;
726 case FP_TYPE_EXPLICIT_PATH:
727 return false; // An explicit path never changes
728 }
729
730 DataPath oldDataPath = flowPath.dataPath();
731
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700732 // Compute the new path
733 DataPath newDataPath = TopologyManager.computeNetworkPath(topology,
734 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700735 if (newDataPath == null) {
736 // We need the DataPath to compare the paths
737 newDataPath = new DataPath();
738 }
739 newDataPath.applyFlowPathFlags(flowPath.flowPathFlags());
740
741 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700742 // Test whether the new path is same
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700743 //
744 if (oldDataPath.flowEntries().size() !=
745 newDataPath.flowEntries().size()) {
746 hasChanged = true;
747 } else {
748 Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator();
749 Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator();
750 while (oldIter.hasNext() && newIter.hasNext()) {
751 FlowEntry oldFlowEntry = oldIter.next();
752 FlowEntry newFlowEntry = newIter.next();
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700753 if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
754 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700755 hasChanged = true;
756 break;
757 }
758 }
759 }
760 if (! hasChanged)
761 return hasChanged;
762
763 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700764 // Merge the changes in the path:
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700765 // - If a Flow Entry for a switch is in the old data path, but not
766 // in the new data path, then mark it for deletion.
767 // - If a Flow Entry for a switch is in the new data path, but not
768 // in the old data path, then mark it for addition.
769 // - If a Flow Entry for a switch is in both the old and the new
770 // data path, but it has changed, e.g., the incoming and/or outgoing
771 // port(s), then mark the old Flow Entry for deletion, and mark
772 // the new Flow Entry for addition.
773 // - If a Flow Entry for a switch is in both the old and the new
774 // data path, and it hasn't changed, then just keep it.
775 //
776 // NOTE: We use the Switch DPID of each entry to match the entries
777 //
778 Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>();
779 Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>();
780 ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>();
781 List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>();
782
783 // Prepare maps with the Flow Entries, so they are fast to lookup
784 for (FlowEntry flowEntry : oldDataPath.flowEntries())
785 oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
786 for (FlowEntry flowEntry : newDataPath.flowEntries())
787 newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
788
789 //
790 // Find the old Flow Entries that should be deleted
791 //
792 for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) {
793 FlowEntry newFlowEntry =
794 newFlowEntriesMap.get(oldFlowEntry.dpid().value());
795 if (newFlowEntry == null) {
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800796 // The old Flow Entry should be deleted: not on the path
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700797 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
798 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
799 deletedFlowEntries.add(oldFlowEntry);
800 }
801 }
802
803 //
804 // Find the new Flow Entries that should be added or updated
805 //
806 int idx = 0;
807 for (FlowEntry newFlowEntry : newDataPath.flowEntries()) {
808 FlowEntry oldFlowEntry =
809 oldFlowEntriesMap.get(newFlowEntry.dpid().value());
810
811 if ((oldFlowEntry != null) &&
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700812 TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
813 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700814 //
815 // Both Flow Entries are same
816 //
817 finalFlowEntries.add(oldFlowEntry);
818 idx++;
819 continue;
820 }
821
822 if (oldFlowEntry != null) {
823 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800824 // The old Flow Entry should be deleted: path diverges
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700825 //
826 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
827 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
828 deletedFlowEntries.add(oldFlowEntry);
829 }
830
831 //
832 // Add the new Flow Entry
833 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700834 //
835 // NOTE: Assign only the Flow ID.
836 // The Flow Entry ID is assigned later only for the Flow Entries
837 // this instance is responsible for.
838 //
839 newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700840
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800841 //
Pavlin Radoslavov6fde2172013-12-10 11:23:18 -0800842 // Copy the Flow timeouts
843 //
844 newFlowEntry.setIdleTimeout(flowPath.idleTimeout());
845 newFlowEntry.setHardTimeout(flowPath.hardTimeout());
846
847 //
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800848 // Allocate the FlowEntryMatch by copying the default one
849 // from the FlowPath (if set).
850 //
851 FlowEntryMatch flowEntryMatch = null;
852 if (flowPath.flowEntryMatch() != null)
853 flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch());
854 else
855 flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700856 newFlowEntry.setFlowEntryMatch(flowEntryMatch);
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800857
858 // Set the incoming port matching
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700859 flowEntryMatch.enableInPort(newFlowEntry.inPort());
860
861 //
862 // Set the actions:
863 // If the first Flow Entry, copy the Flow Path actions to it.
864 //
865 FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions();
866 if ((idx == 0) && (flowPath.flowEntryActions() != null)) {
867 FlowEntryActions flowActions =
868 new FlowEntryActions(flowPath.flowEntryActions());
869 for (FlowEntryAction action : flowActions.actions())
870 flowEntryActions.addAction(action);
871 }
872 idx++;
873
874 //
875 // Add the outgoing port output action
876 //
877 FlowEntryAction flowEntryAction = new FlowEntryAction();
878 flowEntryAction.setActionOutput(newFlowEntry.outPort());
879 flowEntryActions.addAction(flowEntryAction);
880
881 //
882 // Set the state of the new Flow Entry
883 //
884 newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
885 newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
886 finalFlowEntries.add(newFlowEntry);
887 }
888
889 //
890 // Replace the old Flow Entries with the new Flow Entries.
891 // Note that the Flow Entries that will be deleted are added at
892 // the end.
893 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800894 finalFlowEntries.addAll(deletedFlowEntries);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700895 flowPath.dataPath().setFlowEntries(finalFlowEntries);
896
897 return hasChanged;
898 }
899
900 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700901 * Receive a notification that a Flow is added.
902 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700903 * @param flowPath the Flow that is added.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700904 */
905 @Override
906 public void notificationRecvFlowAdded(FlowPath flowPath) {
907 EventEntry<FlowPath> eventEntry =
908 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
909 networkEvents.add(eventEntry);
910 }
911
912 /**
913 * Receive a notification that a Flow is removed.
914 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700915 * @param flowPath the Flow that is removed.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700916 */
917 @Override
918 public void notificationRecvFlowRemoved(FlowPath flowPath) {
919 EventEntry<FlowPath> eventEntry =
920 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath);
921 networkEvents.add(eventEntry);
922 }
923
924 /**
925 * Receive a notification that a Flow is updated.
926 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700927 * @param flowPath the Flow that is updated.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700928 */
929 @Override
930 public void notificationRecvFlowUpdated(FlowPath flowPath) {
931 // NOTE: The ADD and UPDATE events are processed in same way
932 EventEntry<FlowPath> eventEntry =
933 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
934 networkEvents.add(eventEntry);
935 }
936
937 /**
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700938 * Receive a notification that a FlowEntry is added.
939 *
940 * @param flowEntry the FlowEntry that is added.
941 */
942 @Override
943 public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) {
944 EventEntry<FlowEntry> eventEntry =
945 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
946 networkEvents.add(eventEntry);
947 }
948
949 /**
950 * Receive a notification that a FlowEntry is removed.
951 *
952 * @param flowEntry the FlowEntry that is removed.
953 */
954 @Override
955 public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) {
956 EventEntry<FlowEntry> eventEntry =
957 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry);
958 networkEvents.add(eventEntry);
959 }
960
961 /**
962 * Receive a notification that a FlowEntry is updated.
963 *
964 * @param flowEntry the FlowEntry that is updated.
965 */
966 @Override
967 public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) {
968 // NOTE: The ADD and UPDATE events are processed in same way
969 EventEntry<FlowEntry> eventEntry =
970 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
971 networkEvents.add(eventEntry);
972 }
973
974 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700975 * Receive a notification that a Topology Element is added.
976 *
977 * @param topologyElement the Topology Element that is added.
978 */
979 @Override
980 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
981 EventEntry<TopologyElement> eventEntry =
982 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
983 networkEvents.add(eventEntry);
984 }
985
986 /**
987 * Receive a notification that a Topology Element is removed.
988 *
989 * @param topologyElement the Topology Element that is removed.
990 */
991 @Override
992 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
993 EventEntry<TopologyElement> eventEntry =
994 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement);
995 networkEvents.add(eventEntry);
996 }
997
998 /**
999 * Receive a notification that a Topology Element is updated.
1000 *
1001 * @param topologyElement the Topology Element that is updated.
1002 */
1003 @Override
1004 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
1005 // NOTE: The ADD and UPDATE events are processed in same way
1006 EventEntry<TopologyElement> eventEntry =
1007 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1008 networkEvents.add(eventEntry);
1009 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001010
1011 /**
1012 * Get a sorted copy of all Flow Paths.
1013 *
1014 * @return a sorted copy of all Flow Paths.
1015 */
1016 synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() {
1017 SortedMap<Long, FlowPath> sortedFlowPaths =
1018 new TreeMap<Long, FlowPath>();
1019
1020 //
1021 // TODO: For now we use serialization/deserialization to create
1022 // a copy of each Flow Path. In the future, we should use proper
1023 // copy constructors.
1024 //
1025 Kryo kryo = kryoFactory.newKryo();
1026 synchronized (allFlowPaths) {
1027 for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) {
1028 FlowPath origFlowPath = entry.getValue();
1029 FlowPath copyFlowPath = kryo.copy(origFlowPath);
1030 sortedFlowPaths.put(entry.getKey(), copyFlowPath);
1031 }
1032 }
1033 kryoFactory.deleteKryo(kryo);
1034
1035 return sortedFlowPaths;
1036 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001037}