blob: 49ec46a110298ea644ebcb0ae9051c58d12eb3ac [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
78 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080079 private Map<Long, FlowPath> shouldRecomputeFlowPaths =
80 new HashMap<Long, FlowPath>();
81 private Map<Long, FlowPath> modifiedFlowPaths =
82 new HashMap<Long, FlowPath>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080083
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070084 /**
85 * Constructor for a given Flow Manager and Datagrid Service.
86 *
87 * @param flowManager the Flow Manager to use.
88 * @param datagridService the Datagrid Service to use.
89 */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070090 FlowEventHandler(FlowManager flowManager,
91 IDatagridService datagridService) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070092 this.flowManager = flowManager;
93 this.datagridService = datagridService;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070094 this.topology = new Topology();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070095 }
96
97 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -080098 * Get the network topology.
99 *
100 * @return the network topology.
101 */
102 protected Topology getTopology() { return this.topology; }
103
104 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800105 * Startup processing.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700106 */
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800107 private void startup() {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700108 //
109 // Obtain the initial Topology state
110 //
111 Collection<TopologyElement> topologyElements =
112 datagridService.getAllTopologyElements();
113 for (TopologyElement topologyElement : topologyElements) {
114 EventEntry<TopologyElement> eventEntry =
115 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
116 topologyEvents.add(eventEntry);
117 }
118 //
119 // Obtain the initial Flow Path state
120 //
121 Collection<FlowPath> flowPaths = datagridService.getAllFlows();
122 for (FlowPath flowPath : flowPaths) {
123 EventEntry<FlowPath> eventEntry =
124 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
125 flowPathEvents.add(eventEntry);
126 }
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700127 //
128 // Obtain the initial FlowEntry state
129 //
130 Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries();
131 for (FlowEntry flowEntry : flowEntries) {
132 EventEntry<FlowEntry> eventEntry =
133 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
134 flowEntryEvents.add(eventEntry);
135 }
136
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800137 // Process the initial events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800138 synchronized (allFlowPaths) {
139 processEvents();
140 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800141 }
142
143 /**
144 * Run the thread.
145 */
146 @Override
147 public void run() {
148 startup();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700149
150 //
151 // The main loop
152 //
153 Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>();
154 try {
155 while (true) {
156 EventEntry<?> eventEntry = networkEvents.take();
157 collection.add(eventEntry);
158 networkEvents.drainTo(collection);
159
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700160 //
161 // Demultiplex all events:
162 // - EventEntry<TopologyElement>
163 // - EventEntry<FlowPath>
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700164 // - EventEntry<FlowEntry>
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700165 //
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700166 for (EventEntry<?> event : collection) {
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800167 // Topology event
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700168 if (event.eventData() instanceof TopologyElement) {
169 EventEntry<TopologyElement> topologyEventEntry =
170 (EventEntry<TopologyElement>)event;
171 topologyEvents.add(topologyEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800172 continue;
173 }
174
175 // FlowPath event
176 if (event.eventData() instanceof FlowPath) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700177 EventEntry<FlowPath> flowPathEventEntry =
178 (EventEntry<FlowPath>)event;
179 flowPathEvents.add(flowPathEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800180 continue;
181 }
182
183 // FlowEntry event
184 if (event.eventData() instanceof FlowEntry) {
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700185 EventEntry<FlowEntry> flowEntryEventEntry =
186 (EventEntry<FlowEntry>)event;
187 flowEntryEvents.add(flowEntryEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800188 continue;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700189 }
190 }
191 collection.clear();
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700192
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700193 // Process the events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800194 synchronized (allFlowPaths) {
195 processEvents();
196 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700197 }
198 } catch (Exception exception) {
199 log.debug("Exception processing Network Events: ", exception);
200 }
201 }
202
203 /**
204 * Process the events (if any)
205 */
206 private void processEvents() {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800207 Collection<FlowEntry> modifiedFlowEntries;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700208
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700209 if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() &&
210 flowEntryEvents.isEmpty()) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700211 return; // Nothing to do
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700212 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700213
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800214 processFlowPathEvents();
215 processTopologyEvents();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800216 processFlowEntryEvents();
217
218 // Recompute all affected Flow Paths and keep only the modified
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800219 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800220 if (recomputeFlowPath(flowPath))
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800221 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800222 }
223
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800224 // Extract the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800225 modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800226
227 // Assign missing Flow Entry IDs
228 assignFlowEntryId(modifiedFlowEntries);
229
230 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800231 // Push the modified state to the Flow Manager
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800232 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800233 flowManager.pushModifiedFlowState(modifiedFlowPaths.values(),
234 modifiedFlowEntries);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800235
236 //
237 // Remove Flow Entries that were deleted
238 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800239 for (FlowPath flowPath : modifiedFlowPaths.values())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800240 flowPath.dataPath().removeDeletedFlowEntries();
241
242 // Cleanup
243 topologyEvents.clear();
244 flowPathEvents.clear();
245 flowEntryEvents.clear();
246 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800247 shouldRecomputeFlowPaths.clear();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800248 modifiedFlowPaths.clear();
249 }
250
251 /**
252 * Extract the modified Flow Entries.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800253 *
254 * @param modifiedFlowPaths the Flow Paths to process.
255 * @return a collection with the modified Flow Entries.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800256 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800257 private Collection<FlowEntry> extractModifiedFlowEntries(
258 Collection<FlowPath> modifiedFlowPaths) {
259 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800260
261 // Extract only the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800262 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800263 for (FlowEntry flowEntry : flowPath.flowEntries()) {
264 if (flowEntry.flowEntrySwitchState() ==
265 FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800266 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800267 }
268 }
269 }
270 return modifiedFlowEntries;
271 }
272
273 /**
274 * Assign the Flow Entry ID as needed.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800275 *
276 * @param modifiedFlowEnries the collection of Flow Entries that need
277 * Flow Entry ID assigned.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800278 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800279 private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800280 if (modifiedFlowEntries.isEmpty())
281 return;
282
283 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
284
285 //
286 // Assign the Flow Entry ID only for Flow Entries for my switches
287 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800288 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800289 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
290 if (mySwitch == null)
291 continue;
292 if (! flowEntry.isValidFlowEntryId()) {
293 long id = flowManager.getNextFlowEntryId();
294 flowEntry.setFlowEntryId(new FlowEntryId(id));
295 }
296 }
297 }
298
299 /**
300 * Process the Flow Path events.
301 */
302 private void processFlowPathEvents() {
303 //
304 // Process all Flow Path events and update the appropriate state
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700305 //
306 for (EventEntry<FlowPath> eventEntry : flowPathEvents) {
307 FlowPath flowPath = eventEntry.eventData();
308
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800309 log.debug("Flow Event: {} {}", eventEntry.eventType(),
310 flowPath.toString());
311
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700312 switch (eventEntry.eventType()) {
313 case ENTRY_ADD: {
314 //
315 // Add a new Flow Path
316 //
317 if (allFlowPaths.get(flowPath.flowId().value()) != null) {
318 //
319 // TODO: What to do if the Flow Path already exists?
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800320 // Fow now, we just re-add it.
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700321 //
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700322 }
323
324 switch (flowPath.flowPathType()) {
325 case FP_TYPE_SHORTEST_PATH:
326 //
327 // Reset the Data Path, in case it was set already, because
328 // we are going to recompute it anyway.
329 //
330 flowPath.flowEntries().clear();
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800331 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
332 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700333 break;
334 case FP_TYPE_EXPLICIT_PATH:
335 //
336 // Mark all Flow Entries for installation in the switches.
337 //
338 for (FlowEntry flowEntry : flowPath.flowEntries()) {
339 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
340 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800341 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700342 break;
343 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800344 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700345
346 break;
347 }
348
349 case ENTRY_REMOVE: {
350 //
351 // Remove an existing Flow Path.
352 //
353 // Find the Flow Path, and mark the Flow and its Flow Entries
354 // for deletion.
355 //
356 FlowPath existingFlowPath =
357 allFlowPaths.get(flowPath.flowId().value());
358 if (existingFlowPath == null)
359 continue; // Nothing to do
360
361 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
362 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
363 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
364 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
365 }
366
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800367 // Remove the Flow Path from the internal state
368 Long key = existingFlowPath.flowId().value();
369 allFlowPaths.remove(key);
370 shouldRecomputeFlowPaths.remove(key);
371 modifiedFlowPaths.put(key, existingFlowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700372
373 break;
374 }
375 }
376 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800377 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700378
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800379 /**
380 * Process the Topology events.
381 */
382 private void processTopologyEvents() {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700383 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800384 // Process all Topology events and update the appropriate state
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700385 //
386 boolean isTopologyModified = false;
387 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
388 TopologyElement topologyElement = eventEntry.eventData();
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800389
390 log.debug("Topology Event: {} {}", eventEntry.eventType(),
391 topologyElement.toString());
392
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700393 switch (eventEntry.eventType()) {
394 case ENTRY_ADD:
Yuta HIGUCHIb32f77f2013-10-30 15:36:32 -0700395 isTopologyModified |= topology.addTopologyElement(topologyElement);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700396 break;
397 case ENTRY_REMOVE:
Yuta HIGUCHIb32f77f2013-10-30 15:36:32 -0700398 isTopologyModified |= topology.removeTopologyElement(topologyElement);
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700399 break;
400 }
401 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700402 if (isTopologyModified) {
403 // TODO: For now, if the topology changes, we recompute all Flows
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800404 shouldRecomputeFlowPaths.putAll(allFlowPaths);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700405 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800406 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700407
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800408 /**
409 * Process the Flow Entry events.
410 */
411 private void processFlowEntryEvents() {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800412 FlowPath flowPath;
413 FlowEntry updatedFlowEntry;
414
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700415 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800416 // Update Flow Entries with previously unmatched Flow Entry updates
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700417 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800418 if (! unmatchedFlowEntryAdd.isEmpty()) {
419 Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>();
420 for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) {
421 flowPath = allFlowPaths.get(flowEntry.flowId().value());
422 if (flowPath == null)
423 continue;
424 updatedFlowEntry = updateFlowEntryAdd(flowPath, flowEntry);
425 if (updatedFlowEntry == null) {
426 remainingUpdates.put(flowEntry.flowEntryId().value(),
427 flowEntry);
428 continue;
429 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800430 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700431 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800432 unmatchedFlowEntryAdd = remainingUpdates;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700433 }
434
435 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800436 // Process all Flow Entry events and update the appropriate state
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700437 //
438 for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) {
439 FlowEntry flowEntry = eventEntry.eventData();
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800440
441 log.debug("Flow Entry Event: {} {}", eventEntry.eventType(),
442 flowEntry.toString());
443
444 if ((! flowEntry.isValidFlowId()) ||
445 (! flowEntry.isValidFlowEntryId())) {
446 continue;
447 }
448
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700449 switch (eventEntry.eventType()) {
450 case ENTRY_ADD:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800451 flowPath = allFlowPaths.get(flowEntry.flowId().value());
452 if (flowPath == null) {
453 // Flow Path not found: keep the entry for later matching
454 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
455 flowEntry);
456 break;
457 }
458 updatedFlowEntry = updateFlowEntryAdd(flowPath, flowEntry);
459 if (updatedFlowEntry == null) {
460 // Flow Entry not found: keep the entry for later matching
461 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
462 flowEntry);
463 break;
464 }
465 // Add the updated entry to the list of updated Flow Entries
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800466 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700467 break;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800468
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700469 case ENTRY_REMOVE:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800470 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
471 if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800472 continue; // Removed previously unmatched entry
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800473 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800474
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800475 flowPath = allFlowPaths.get(flowEntry.flowId().value());
476 if (flowPath == null) {
477 // Flow Path not found: ignore the update
478 break;
479 }
480 updatedFlowEntry = updateFlowEntryRemove(flowPath, flowEntry);
481 if (updatedFlowEntry == null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800482 // Flow Entry not found: ignore it
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800483 break;
484 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800485 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700486 break;
487 }
488 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700489 }
490
491 /**
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800492 * Update a Flow Entry because of an external ENTRY_ADD event.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700493 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800494 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700495 * @param flowEntry the FlowEntry with the new state.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800496 * @return the updated Flow Entry if found, otherwise null.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700497 */
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800498 private FlowEntry updateFlowEntryAdd(FlowPath flowPath,
499 FlowEntry flowEntry) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700500 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800501 // Iterate over all Flow Entries and find a match.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700502 //
503 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800504 if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry,
505 flowEntry)) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700506 continue;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800507 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700508
509 //
510 // Local Flow Entry match found
511 //
512 if (localFlowEntry.isValidFlowEntryId()) {
513 if (localFlowEntry.flowEntryId().value() !=
514 flowEntry.flowEntryId().value()) {
515 //
516 // Find a local Flow Entry, but the Flow Entry ID doesn't
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800517 // match. Keep looking.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700518 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800519 continue;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700520 }
521 } else {
522 // Update the Flow Entry ID
523 FlowEntryId flowEntryId =
524 new FlowEntryId(flowEntry.flowEntryId().value());
525 localFlowEntry.setFlowEntryId(flowEntryId);
526 }
527
528 //
529 // Update the local Flow Entry.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700530 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800531 localFlowEntry.setFlowEntryUserState(flowEntry.flowEntryUserState());
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700532 localFlowEntry.setFlowEntrySwitchState(flowEntry.flowEntrySwitchState());
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800533 return localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700534 }
535
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800536 return null; // Entry not found
537 }
538
539 /**
540 * Update a Flow Entry because of an external ENTRY_REMOVE event.
541 *
542 * @param flowPath the FlowPath for the Flow Entry to update.
543 * @param flowEntry the FlowEntry with the new state.
544 * @return the updated Flow Entry if found, otherwise null.
545 */
546 private FlowEntry updateFlowEntryRemove(FlowPath flowPath,
547 FlowEntry flowEntry) {
548 //
549 // Iterate over all Flow Entries and find a match based on
550 // the Flow Entry ID.
551 //
552 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
553 if (! localFlowEntry.isValidFlowEntryId())
554 continue;
555 if (localFlowEntry.flowEntryId().value() !=
556 flowEntry.flowEntryId().value()) {
557 continue;
558 }
559 //
560 // Update the local Flow Entry.
561 //
562 localFlowEntry.setFlowEntryUserState(flowEntry.flowEntryUserState());
563 localFlowEntry.setFlowEntrySwitchState(flowEntry.flowEntrySwitchState());
564 return localFlowEntry;
565 }
566
567 return null; // Entry not found
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700568 }
569
570 /**
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700571 * Recompute a Flow Path.
572 *
573 * @param flowPath the Flow Path to recompute.
574 * @return true if the recomputed Flow Path has changed, otherwise false.
575 */
576 private boolean recomputeFlowPath(FlowPath flowPath) {
577 boolean hasChanged = false;
578
579 //
580 // Test whether the Flow Path needs to be recomputed
581 //
582 switch (flowPath.flowPathType()) {
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -0700583 case FP_TYPE_UNKNOWN:
584 return false; // Can't recompute on Unknown FlowType
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700585 case FP_TYPE_SHORTEST_PATH:
586 break;
587 case FP_TYPE_EXPLICIT_PATH:
588 return false; // An explicit path never changes
589 }
590
591 DataPath oldDataPath = flowPath.dataPath();
592
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700593 // Compute the new path
594 DataPath newDataPath = TopologyManager.computeNetworkPath(topology,
595 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700596 if (newDataPath == null) {
597 // We need the DataPath to compare the paths
598 newDataPath = new DataPath();
599 }
600 newDataPath.applyFlowPathFlags(flowPath.flowPathFlags());
601
602 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700603 // Test whether the new path is same
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700604 //
605 if (oldDataPath.flowEntries().size() !=
606 newDataPath.flowEntries().size()) {
607 hasChanged = true;
608 } else {
609 Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator();
610 Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator();
611 while (oldIter.hasNext() && newIter.hasNext()) {
612 FlowEntry oldFlowEntry = oldIter.next();
613 FlowEntry newFlowEntry = newIter.next();
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700614 if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
615 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700616 hasChanged = true;
617 break;
618 }
619 }
620 }
621 if (! hasChanged)
622 return hasChanged;
623
624 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700625 // Merge the changes in the path:
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700626 // - If a Flow Entry for a switch is in the old data path, but not
627 // in the new data path, then mark it for deletion.
628 // - If a Flow Entry for a switch is in the new data path, but not
629 // in the old data path, then mark it for addition.
630 // - If a Flow Entry for a switch is in both the old and the new
631 // data path, but it has changed, e.g., the incoming and/or outgoing
632 // port(s), then mark the old Flow Entry for deletion, and mark
633 // the new Flow Entry for addition.
634 // - If a Flow Entry for a switch is in both the old and the new
635 // data path, and it hasn't changed, then just keep it.
636 //
637 // NOTE: We use the Switch DPID of each entry to match the entries
638 //
639 Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>();
640 Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>();
641 ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>();
642 List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>();
643
644 // Prepare maps with the Flow Entries, so they are fast to lookup
645 for (FlowEntry flowEntry : oldDataPath.flowEntries())
646 oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
647 for (FlowEntry flowEntry : newDataPath.flowEntries())
648 newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
649
650 //
651 // Find the old Flow Entries that should be deleted
652 //
653 for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) {
654 FlowEntry newFlowEntry =
655 newFlowEntriesMap.get(oldFlowEntry.dpid().value());
656 if (newFlowEntry == null) {
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800657 // The old Flow Entry should be deleted: not on the path
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700658 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
659 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
660 deletedFlowEntries.add(oldFlowEntry);
661 }
662 }
663
664 //
665 // Find the new Flow Entries that should be added or updated
666 //
667 int idx = 0;
668 for (FlowEntry newFlowEntry : newDataPath.flowEntries()) {
669 FlowEntry oldFlowEntry =
670 oldFlowEntriesMap.get(newFlowEntry.dpid().value());
671
672 if ((oldFlowEntry != null) &&
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700673 TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
674 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700675 //
676 // Both Flow Entries are same
677 //
678 finalFlowEntries.add(oldFlowEntry);
679 idx++;
680 continue;
681 }
682
683 if (oldFlowEntry != null) {
684 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800685 // The old Flow Entry should be deleted: path diverges
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700686 //
687 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
688 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
689 deletedFlowEntries.add(oldFlowEntry);
690 }
691
692 //
693 // Add the new Flow Entry
694 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700695 //
696 // NOTE: Assign only the Flow ID.
697 // The Flow Entry ID is assigned later only for the Flow Entries
698 // this instance is responsible for.
699 //
700 newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700701
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800702 //
Pavlin Radoslavov6fde2172013-12-10 11:23:18 -0800703 // Copy the Flow timeouts
704 //
705 newFlowEntry.setIdleTimeout(flowPath.idleTimeout());
706 newFlowEntry.setHardTimeout(flowPath.hardTimeout());
707
708 //
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800709 // Allocate the FlowEntryMatch by copying the default one
710 // from the FlowPath (if set).
711 //
712 FlowEntryMatch flowEntryMatch = null;
713 if (flowPath.flowEntryMatch() != null)
714 flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch());
715 else
716 flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700717 newFlowEntry.setFlowEntryMatch(flowEntryMatch);
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800718
719 // Set the incoming port matching
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700720 flowEntryMatch.enableInPort(newFlowEntry.inPort());
721
722 //
723 // Set the actions:
724 // If the first Flow Entry, copy the Flow Path actions to it.
725 //
726 FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions();
727 if ((idx == 0) && (flowPath.flowEntryActions() != null)) {
728 FlowEntryActions flowActions =
729 new FlowEntryActions(flowPath.flowEntryActions());
730 for (FlowEntryAction action : flowActions.actions())
731 flowEntryActions.addAction(action);
732 }
733 idx++;
734
735 //
736 // Add the outgoing port output action
737 //
738 FlowEntryAction flowEntryAction = new FlowEntryAction();
739 flowEntryAction.setActionOutput(newFlowEntry.outPort());
740 flowEntryActions.addAction(flowEntryAction);
741
742 //
743 // Set the state of the new Flow Entry
744 //
745 newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
746 newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
747 finalFlowEntries.add(newFlowEntry);
748 }
749
750 //
751 // Replace the old Flow Entries with the new Flow Entries.
752 // Note that the Flow Entries that will be deleted are added at
753 // the end.
754 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800755 finalFlowEntries.addAll(deletedFlowEntries);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700756 flowPath.dataPath().setFlowEntries(finalFlowEntries);
757
758 return hasChanged;
759 }
760
761 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700762 * Receive a notification that a Flow is added.
763 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700764 * @param flowPath the Flow that is added.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700765 */
766 @Override
767 public void notificationRecvFlowAdded(FlowPath flowPath) {
768 EventEntry<FlowPath> eventEntry =
769 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
770 networkEvents.add(eventEntry);
771 }
772
773 /**
774 * Receive a notification that a Flow is removed.
775 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700776 * @param flowPath the Flow that is removed.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700777 */
778 @Override
779 public void notificationRecvFlowRemoved(FlowPath flowPath) {
780 EventEntry<FlowPath> eventEntry =
781 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath);
782 networkEvents.add(eventEntry);
783 }
784
785 /**
786 * Receive a notification that a Flow is updated.
787 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700788 * @param flowPath the Flow that is updated.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700789 */
790 @Override
791 public void notificationRecvFlowUpdated(FlowPath flowPath) {
792 // NOTE: The ADD and UPDATE events are processed in same way
793 EventEntry<FlowPath> eventEntry =
794 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
795 networkEvents.add(eventEntry);
796 }
797
798 /**
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700799 * Receive a notification that a FlowEntry is added.
800 *
801 * @param flowEntry the FlowEntry that is added.
802 */
803 @Override
804 public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) {
805 EventEntry<FlowEntry> eventEntry =
806 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
807 networkEvents.add(eventEntry);
808 }
809
810 /**
811 * Receive a notification that a FlowEntry is removed.
812 *
813 * @param flowEntry the FlowEntry that is removed.
814 */
815 @Override
816 public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) {
817 EventEntry<FlowEntry> eventEntry =
818 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry);
819 networkEvents.add(eventEntry);
820 }
821
822 /**
823 * Receive a notification that a FlowEntry is updated.
824 *
825 * @param flowEntry the FlowEntry that is updated.
826 */
827 @Override
828 public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) {
829 // NOTE: The ADD and UPDATE events are processed in same way
830 EventEntry<FlowEntry> eventEntry =
831 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
832 networkEvents.add(eventEntry);
833 }
834
835 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700836 * Receive a notification that a Topology Element is added.
837 *
838 * @param topologyElement the Topology Element that is added.
839 */
840 @Override
841 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
842 EventEntry<TopologyElement> eventEntry =
843 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
844 networkEvents.add(eventEntry);
845 }
846
847 /**
848 * Receive a notification that a Topology Element is removed.
849 *
850 * @param topologyElement the Topology Element that is removed.
851 */
852 @Override
853 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
854 EventEntry<TopologyElement> eventEntry =
855 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement);
856 networkEvents.add(eventEntry);
857 }
858
859 /**
860 * Receive a notification that a Topology Element is updated.
861 *
862 * @param topologyElement the Topology Element that is updated.
863 */
864 @Override
865 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
866 // NOTE: The ADD and UPDATE events are processed in same way
867 EventEntry<TopologyElement> eventEntry =
868 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
869 networkEvents.add(eventEntry);
870 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800871
872 /**
873 * Get a sorted copy of all Flow Paths.
874 *
875 * @return a sorted copy of all Flow Paths.
876 */
877 synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() {
878 SortedMap<Long, FlowPath> sortedFlowPaths =
879 new TreeMap<Long, FlowPath>();
880
881 //
882 // TODO: For now we use serialization/deserialization to create
883 // a copy of each Flow Path. In the future, we should use proper
884 // copy constructors.
885 //
886 Kryo kryo = kryoFactory.newKryo();
887 synchronized (allFlowPaths) {
888 for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) {
889 FlowPath origFlowPath = entry.getValue();
890 FlowPath copyFlowPath = kryo.copy(origFlowPath);
891 sortedFlowPaths.put(entry.getKey(), copyFlowPath);
892 }
893 }
894 kryoFactory.deleteKryo(kryo);
895
896 return sortedFlowPaths;
897 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700898}