blob: 9d37a63d9d9084639437283c397358b89e78a2a4 [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;
Naoki Shiota0abe38d2014-01-07 15:31:22 -080017import net.onrc.onos.graph.GraphDBOperation;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070018import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070019import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -070020import net.onrc.onos.ofcontroller.topology.TopologyManager;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070021import net.onrc.onos.ofcontroller.util.DataPath;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070022import net.onrc.onos.ofcontroller.util.EventEntry;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070023import net.onrc.onos.ofcontroller.util.FlowEntry;
24import net.onrc.onos.ofcontroller.util.FlowEntryAction;
25import net.onrc.onos.ofcontroller.util.FlowEntryActions;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -070026import net.onrc.onos.ofcontroller.util.FlowEntryId;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070027import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
28import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
29import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -070030import net.onrc.onos.ofcontroller.util.FlowId;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070031import net.onrc.onos.ofcontroller.util.FlowPath;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070032import net.onrc.onos.ofcontroller.util.FlowPathUserState;
Pavlin Radoslavov53219802013-12-06 11:02:04 -080033import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
34
35import com.esotericsoftware.kryo2.Kryo;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070036
37import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40/**
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -070041 * Class for FlowPath Maintenance.
42 * This class listens for FlowEvents to:
43 * - Maintain a local cache of the Network Topology.
44 * - Detect FlowPaths impacted by Topology change.
45 * - Recompute impacted FlowPath using cached Topology.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070046 */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070047class FlowEventHandler extends Thread implements IFlowEventHandlerService {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -080048
49 private boolean enableOnrc2014MeasurementsFlows = false;
50 private boolean enableOnrc2014MeasurementsTopology = false;
51
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070052 /** The logger. */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070053 private final static Logger log = LoggerFactory.getLogger(FlowEventHandler.class);
Naoki Shiota0abe38d2014-01-07 15:31:22 -080054
Naoki Shiota0abe38d2014-01-07 15:31:22 -080055 private GraphDBOperation dbHandler;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070056 private FlowManager flowManager; // The Flow Manager to use
57 private IDatagridService datagridService; // The Datagrid Service to use
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070058 private Topology topology; // The network topology
Pavlin Radoslavov53219802013-12-06 11:02:04 -080059 private KryoFactory kryoFactory = new KryoFactory();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070060
61 // The queue with Flow Path and Topology Element updates
62 private BlockingQueue<EventEntry<?>> networkEvents =
63 new LinkedBlockingQueue<EventEntry<?>>();
64
Pavlin Radoslavovb7506842013-10-29 17:46:54 -070065 // The pending Topology, FlowPath, and FlowEntry events
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070066 private List<EventEntry<TopologyElement>> topologyEvents =
67 new LinkedList<EventEntry<TopologyElement>>();
68 private List<EventEntry<FlowPath>> flowPathEvents =
69 new LinkedList<EventEntry<FlowPath>>();
Pavlin Radoslavovb7506842013-10-29 17:46:54 -070070 private List<EventEntry<FlowEntry>> flowEntryEvents =
71 new LinkedList<EventEntry<FlowEntry>>();
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -080072 private List<EventEntry<FlowId>> flowIdEvents =
73 new LinkedList<EventEntry<FlowId>>();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070074
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080075 // All internally computed Flow Paths
76 private Map<Long, FlowPath> allFlowPaths = new HashMap<Long, FlowPath>();
77
78 // The Flow Entries received as notifications with unmatched Flow Paths
79 private Map<Long, FlowEntry> unmatchedFlowEntryAdd =
80 new HashMap<Long, FlowEntry>();
81
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080082 //
83 // Transient state for processing the Flow Paths:
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080084 // - The Flow Paths that should be recomputed
85 // - The Flow Paths with modified Flow Entries
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080086 // - The Flow Paths that we should check if installed in all switches
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080087 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080088 private Map<Long, FlowPath> shouldRecomputeFlowPaths =
89 new HashMap<Long, FlowPath>();
90 private Map<Long, FlowPath> modifiedFlowPaths =
91 new HashMap<Long, FlowPath>();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080092 private Map<Long, FlowPath> checkIfInstalledFlowPaths =
93 new HashMap<Long, FlowPath>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080094
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070095 /**
96 * Constructor for a given Flow Manager and Datagrid Service.
97 *
98 * @param flowManager the Flow Manager to use.
99 * @param datagridService the Datagrid Service to use.
100 */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700101 FlowEventHandler(FlowManager flowManager,
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800102 IDatagridService datagridService) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700103 this.flowManager = flowManager;
104 this.datagridService = datagridService;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700105 this.topology = new Topology();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700106 }
107
108 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800109 * Get the network topology.
110 *
111 * @return the network topology.
112 */
113 protected Topology getTopology() { return this.topology; }
114
115 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800116 * Startup processing.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700117 */
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800118 private void startup() {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800119 this.dbHandler = new GraphDBOperation("");
120
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700121 //
122 // Obtain the initial Topology state
123 //
124 Collection<TopologyElement> topologyElements =
125 datagridService.getAllTopologyElements();
126 for (TopologyElement topologyElement : topologyElements) {
127 EventEntry<TopologyElement> eventEntry =
128 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
129 topologyEvents.add(eventEntry);
130 }
131 //
132 // Obtain the initial Flow Path state
133 //
134 Collection<FlowPath> flowPaths = datagridService.getAllFlows();
135 for (FlowPath flowPath : flowPaths) {
136 EventEntry<FlowPath> eventEntry =
137 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
138 flowPathEvents.add(eventEntry);
139 }
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700140 //
141 // Obtain the initial FlowEntry state
142 //
143 Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries();
144 for (FlowEntry flowEntry : flowEntries) {
145 EventEntry<FlowEntry> eventEntry =
146 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
147 flowEntryEvents.add(eventEntry);
148 }
149
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800150 //
151 // Obtain the initial FlowId state
152 //
153 Collection<FlowId> flowIds = datagridService.getAllFlowIds();
154 for (FlowId flowId : flowIds) {
155 EventEntry<FlowId> eventEntry =
156 new EventEntry<FlowId>(EventEntry.Type.ENTRY_ADD, flowId);
157 flowIdEvents.add(eventEntry);
158 }
159
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800160 // Process the initial events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800161 synchronized (allFlowPaths) {
162 processEvents();
163 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800164 }
165
166 /**
167 * Run the thread.
168 */
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800169 @Override
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800170 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800171 this.setName("FlowEventHandler " + this.getId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800172 startup();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700173
174 //
175 // The main loop
176 //
177 Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>();
178 try {
179 while (true) {
180 EventEntry<?> eventEntry = networkEvents.take();
181 collection.add(eventEntry);
182 networkEvents.drainTo(collection);
183
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700184 //
185 // Demultiplex all events:
186 // - EventEntry<TopologyElement>
187 // - EventEntry<FlowPath>
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700188 // - EventEntry<FlowEntry>
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800189 // - EventEntry<FlowId>
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700190 //
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700191 for (EventEntry<?> event : collection) {
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800192 // Topology event
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700193 if (event.eventData() instanceof TopologyElement) {
194 EventEntry<TopologyElement> topologyEventEntry =
195 (EventEntry<TopologyElement>)event;
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800196
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700197 topologyEvents.add(topologyEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800198 continue;
199 }
200
201 // FlowPath event
202 if (event.eventData() instanceof FlowPath) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700203 EventEntry<FlowPath> flowPathEventEntry =
204 (EventEntry<FlowPath>)event;
205 flowPathEvents.add(flowPathEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800206 continue;
207 }
208
209 // FlowEntry event
210 if (event.eventData() instanceof FlowEntry) {
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700211 EventEntry<FlowEntry> flowEntryEventEntry =
212 (EventEntry<FlowEntry>)event;
213 flowEntryEvents.add(flowEntryEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800214 continue;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700215 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800216
217 // FlowId event
218 if (event.eventData() instanceof FlowId) {
219 EventEntry<FlowId> flowIdEventEntry =
220 (EventEntry<FlowId>)event;
221 flowIdEvents.add(flowIdEventEntry);
222 continue;
223 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700224 }
225 collection.clear();
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700226
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700227 // Process the events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800228 synchronized (allFlowPaths) {
229 processEvents();
230 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700231 }
232 } catch (Exception exception) {
233 log.debug("Exception processing Network Events: ", exception);
234 }
235 }
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800236
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700237 /**
238 * Process the events (if any)
239 */
240 private void processEvents() {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800241 Collection<FlowEntry> modifiedFlowEntries;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700242
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800243 if (enableOnrc2014MeasurementsFlows) {
244
245 if (topologyEvents.isEmpty() && flowIdEvents.isEmpty()) {
246 return; // Nothing to do
247 }
248
249 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
250
251 // Fetch and prepare my flows
252 prepareMyFlows(mySwitches);
253
254 // Fetch the topology
255 processTopologyEvents();
256
257 // Recompute all affected Flow Paths and keep only the modified
258 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
259 if (recomputeFlowPath(flowPath))
260 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
261 }
262
263 // Assign the Flow Entry ID as needed
264 for (FlowPath flowPath : modifiedFlowPaths.values()) {
265 for (FlowEntry flowEntry : flowPath.flowEntries()) {
266 if (! flowEntry.isValidFlowEntryId()) {
267 long id = flowManager.getNextFlowEntryId();
268 flowEntry.setFlowEntryId(new FlowEntryId(id));
269 }
270 }
271 }
272
273 // Extract my modified Flow Entries
274 modifiedFlowEntries = processFlowIdEvents(mySwitches);
275
276 //
277 // Push the modified state to the Flow Manager
278 //
279 flowManager.pushModifiedFlowState(modifiedFlowPaths.values(),
280 modifiedFlowEntries);
281
282 // Cleanup
283 topologyEvents.clear();
284 flowIdEvents.clear();
285 //
286 allFlowPaths.clear();
287 shouldRecomputeFlowPaths.clear();
288 modifiedFlowPaths.clear();
289
290 return;
291 }
292
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700293 if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() &&
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800294 flowEntryEvents.isEmpty()) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700295 return; // Nothing to do
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700296 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700297
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800298 processFlowPathEvents();
299 processTopologyEvents();
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800300 processUnmatchedFlowEntryAdd();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800301 processFlowEntryEvents();
302
303 // Recompute all affected Flow Paths and keep only the modified
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800304 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800305 if (recomputeFlowPath(flowPath))
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800306 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800307 }
308
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800309 // Extract the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800310 modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800311
312 // Assign missing Flow Entry IDs
313 assignFlowEntryId(modifiedFlowEntries);
314
315 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800316 // Push the modified state to the Flow Manager
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800317 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800318 flowManager.pushModifiedFlowState(modifiedFlowPaths.values(),
319 modifiedFlowEntries);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800320
321 //
322 // Remove Flow Entries that were deleted
323 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800324 for (FlowPath flowPath : modifiedFlowPaths.values())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800325 flowPath.dataPath().removeDeletedFlowEntries();
326
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800327 //
328 // Check if Flow Paths have been installed into all switches,
329 // and generate the appropriate events.
330 //
331 checkInstalledFlowPaths(checkIfInstalledFlowPaths.values());
332
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800333 // Cleanup
334 topologyEvents.clear();
335 flowPathEvents.clear();
336 flowEntryEvents.clear();
337 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800338 shouldRecomputeFlowPaths.clear();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800339 modifiedFlowPaths.clear();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800340 checkIfInstalledFlowPaths.clear();
341 }
342
343 /**
344 * Check if Flow Paths have been installed into all switches,
345 * and generate the appropriate events.
346 *
347 * @param flowPaths the flowPaths to process.
348 */
349 private void checkInstalledFlowPaths(Collection<FlowPath> flowPaths) {
350 List<FlowPath> installedFlowPaths = new LinkedList<FlowPath>();
351
352 Kryo kryo = kryoFactory.newKryo();
353
354 for (FlowPath flowPath : flowPaths) {
355 boolean isInstalled = true;
356
357 //
358 // Check whether all Flow Entries have been installed
359 //
360 for (FlowEntry flowEntry : flowPath.flowEntries()) {
361 if (flowEntry.flowEntrySwitchState() !=
362 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
363 isInstalled = false;
364 break;
365 }
366 }
367
368 if (isInstalled) {
369 // Create a copy and add it to the list
370 FlowPath copyFlowPath = kryo.copy(flowPath);
371 installedFlowPaths.add(copyFlowPath);
372 }
373 }
374 kryoFactory.deleteKryo(kryo);
375
376 // Generate an event for the installed Flow Path.
377 flowManager.notificationFlowPathsInstalled(installedFlowPaths);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800378 }
379
380 /**
381 * Extract the modified Flow Entries.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800382 *
383 * @param modifiedFlowPaths the Flow Paths to process.
384 * @return a collection with the modified Flow Entries.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800385 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800386 private Collection<FlowEntry> extractModifiedFlowEntries(
387 Collection<FlowPath> modifiedFlowPaths) {
388 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800389
390 // Extract only the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800391 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800392 for (FlowEntry flowEntry : flowPath.flowEntries()) {
393 if (flowEntry.flowEntrySwitchState() ==
394 FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800395 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800396 }
397 }
398 }
399 return modifiedFlowEntries;
400 }
401
402 /**
403 * Assign the Flow Entry ID as needed.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800404 *
405 * @param modifiedFlowEnries the collection of Flow Entries that need
406 * Flow Entry ID assigned.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800407 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800408 private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800409 if (modifiedFlowEntries.isEmpty())
410 return;
411
412 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
413
414 //
415 // Assign the Flow Entry ID only for Flow Entries for my switches
416 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800417 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800418 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
419 if (mySwitch == null)
420 continue;
421 if (! flowEntry.isValidFlowEntryId()) {
422 long id = flowManager.getNextFlowEntryId();
423 flowEntry.setFlowEntryId(new FlowEntryId(id));
424 }
425 }
426 }
427
428 /**
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800429 * Prepare my flows.
430 *
431 * @param mySwitches the collection of my switches.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800432 */
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800433 private void prepareMyFlows(Map<Long, IOFSwitch> mySwitches) {
434 ArrayList<FlowPath> myFlows = flowManager.getAllMyFlows(mySwitches);
435 for (FlowPath flowPath : myFlows) {
436 allFlowPaths.put(flowPath.flowId().value(), flowPath);
437 }
438 }
439
440 /**
441 * Process the Flow ID events.
442 *
443 * @param mySwitches the collection of my switches.
444 * @return a collection of modified Flow Entries this instance needs
445 * to push to its own switches.
446 */
447 private Collection<FlowEntry> processFlowIdEvents(Map<Long, IOFSwitch> mySwitches) {
448 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
449
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800450 //
451 // Process all Flow ID events and update the appropriate state
452 //
453 for (EventEntry<FlowId> eventEntry : flowIdEvents) {
454 FlowId flowId = eventEntry.eventData();
455
456 log.debug("Flow ID Event: {} {}", eventEntry.eventType(), flowId);
457
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800458 //
459 // Lookup the Flow ID in the Flows that were read from the
460 // database in in a previous step. If not found, read from
461 // the database.
462 //
463 FlowPath flowPath = allFlowPaths.get(flowId.value());
464 if (flowPath == null)
465 flowPath = flowManager.getFlow(flowId);
466 if (flowPath == null) {
467 log.debug("Flow ID {} : Flow not found!", flowId);
468 continue;
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800469 }
470
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800471 // Collect only my flow entries that are not updated.
472 for (FlowEntry flowEntry : flowPath.flowEntries()) {
473 if (flowEntry.flowEntrySwitchState() !=
474 FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) {
475 continue;
476 }
477 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
478 if (mySwitch == null)
479 continue;
480 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800481 }
482 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800483
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800484 return modifiedFlowEntries;
485 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800486
487 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800488 * Process the Flow Path events.
489 */
490 private void processFlowPathEvents() {
491 //
492 // Process all Flow Path events and update the appropriate state
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700493 //
494 for (EventEntry<FlowPath> eventEntry : flowPathEvents) {
495 FlowPath flowPath = eventEntry.eventData();
496
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800497 log.debug("Flow Event: {} {}", eventEntry.eventType(), flowPath);
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800498
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700499 switch (eventEntry.eventType()) {
500 case ENTRY_ADD: {
501 //
502 // Add a new Flow Path
503 //
504 if (allFlowPaths.get(flowPath.flowId().value()) != null) {
505 //
506 // TODO: What to do if the Flow Path already exists?
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800507 // Fow now, we just re-add it.
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700508 //
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700509 }
510
511 switch (flowPath.flowPathType()) {
512 case FP_TYPE_SHORTEST_PATH:
513 //
514 // Reset the Data Path, in case it was set already, because
515 // we are going to recompute it anyway.
516 //
517 flowPath.flowEntries().clear();
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800518 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
519 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700520 break;
521 case FP_TYPE_EXPLICIT_PATH:
522 //
523 // Mark all Flow Entries for installation in the switches.
524 //
525 for (FlowEntry flowEntry : flowPath.flowEntries()) {
526 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
527 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800528 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700529 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800530 case FP_TYPE_UNKNOWN:
531 log.error("FlowPath event with unknown type");
532 break;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700533 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800534 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700535
536 break;
537 }
538
539 case ENTRY_REMOVE: {
540 //
541 // Remove an existing Flow Path.
542 //
543 // Find the Flow Path, and mark the Flow and its Flow Entries
544 // for deletion.
545 //
546 FlowPath existingFlowPath =
547 allFlowPaths.get(flowPath.flowId().value());
548 if (existingFlowPath == null)
549 continue; // Nothing to do
550
551 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
552 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
553 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
554 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
555 }
556
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800557 // Remove the Flow Path from the internal state
558 Long key = existingFlowPath.flowId().value();
559 allFlowPaths.remove(key);
560 shouldRecomputeFlowPaths.remove(key);
561 modifiedFlowPaths.put(key, existingFlowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700562
563 break;
564 }
565 }
566 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800567 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700568
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800569 /**
570 * Process the Topology events.
571 */
572 private void processTopologyEvents() {
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800573 if (enableOnrc2014MeasurementsTopology) {
574 if (topologyEvents.isEmpty())
575 return;
576 log.debug("[BEFORE] {}", topology.toString());
577 topology.readFromDatabase(dbHandler);
578 log.debug("[AFTER] {}", topology.toString());
579 shouldRecomputeFlowPaths.putAll(allFlowPaths);
580 return;
581 }
582
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700583 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800584 // Process all Topology events and update the appropriate state
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700585 //
586 boolean isTopologyModified = false;
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800587 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
588 TopologyElement topologyElement = eventEntry.eventData();
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800589
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800590 log.debug("Topology Event: {} {}", eventEntry.eventType(),
591 topologyElement.toString());
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800592
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800593 switch (eventEntry.eventType()) {
594 case ENTRY_ADD:
595 isTopologyModified |= topology.addTopologyElement(topologyElement);
596 break;
597 case ENTRY_REMOVE:
598 isTopologyModified |= topology.removeTopologyElement(topologyElement);
599 break;
600 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700601 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700602 if (isTopologyModified) {
603 // TODO: For now, if the topology changes, we recompute all Flows
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800604 shouldRecomputeFlowPaths.putAll(allFlowPaths);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700605 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800606 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700607
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800608 /**
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800609 * Process previously received Flow Entries with unmatched Flow Paths.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800610 */
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800611 private void processUnmatchedFlowEntryAdd() {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800612 FlowPath flowPath;
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800613 FlowEntry localFlowEntry;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800614
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700615 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800616 // Update Flow Entries with previously unmatched Flow Entry updates
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700617 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800618 if (! unmatchedFlowEntryAdd.isEmpty()) {
619 Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>();
620 for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) {
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800621 // log.debug("Processing Unmatched Flow Entry: {}",
622 // flowEntry.toString());
623
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800624 flowPath = allFlowPaths.get(flowEntry.flowId().value());
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800625 if (flowPath == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800626 remainingUpdates.put(flowEntry.flowEntryId().value(),
627 flowEntry);
628 continue;
629 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800630 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
631 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800632 remainingUpdates.put(flowEntry.flowEntryId().value(),
633 flowEntry);
634 continue;
635 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800636 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
637 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
638 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700639 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800640 unmatchedFlowEntryAdd = remainingUpdates;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700641 }
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800642 }
643
644 /**
645 * Process the Flow Entry events.
646 */
647 private void processFlowEntryEvents() {
648 FlowPath flowPath;
649 FlowEntry localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700650
651 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800652 // Process all Flow Entry events and update the appropriate state
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700653 //
654 for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) {
655 FlowEntry flowEntry = eventEntry.eventData();
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800656
657 log.debug("Flow Entry Event: {} {}", eventEntry.eventType(),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800658 flowEntry);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800659
660 if ((! flowEntry.isValidFlowId()) ||
661 (! flowEntry.isValidFlowEntryId())) {
662 continue;
663 }
664
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700665 switch (eventEntry.eventType()) {
666 case ENTRY_ADD:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800667 flowPath = allFlowPaths.get(flowEntry.flowId().value());
668 if (flowPath == null) {
669 // Flow Path not found: keep the entry for later matching
670 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
671 flowEntry);
672 break;
673 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800674 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
675 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800676 // Flow Entry not found: keep the entry for later matching
677 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
678 flowEntry);
679 break;
680 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800681 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
682 // Add the updated Flow Path to the list of updated paths
683 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
684 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700685 break;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800686
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700687 case ENTRY_REMOVE:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800688 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
689 if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800690 continue; // Removed previously unmatched entry
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800691 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800692
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800693 flowPath = allFlowPaths.get(flowEntry.flowId().value());
694 if (flowPath == null) {
695 // Flow Path not found: ignore the update
696 break;
697 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800698 localFlowEntry = findFlowEntryRemove(flowPath, flowEntry);
699 if (localFlowEntry == null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800700 // Flow Entry not found: ignore it
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800701 break;
702 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800703 if (updateFlowEntryRemove(flowPath, localFlowEntry,
704 flowEntry)) {
705 // Add the updated Flow Path to the list of updated paths
706 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
707 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700708 break;
709 }
710 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700711 }
712
713 /**
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800714 * Find a Flow Entry that should be updated because of an external
715 * ENTRY_ADD event.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700716 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800717 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800718 * @param newFlowEntry the FlowEntry with the new state.
719 * @return the Flow Entry that should be updated if found, otherwise null.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700720 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800721 private FlowEntry findFlowEntryAdd(FlowPath flowPath,
722 FlowEntry newFlowEntry) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700723 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800724 // Iterate over all Flow Entries and find a match.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700725 //
726 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800727 if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry,
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800728 newFlowEntry)) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700729 continue;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800730 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700731
732 //
733 // Local Flow Entry match found
734 //
735 if (localFlowEntry.isValidFlowEntryId()) {
736 if (localFlowEntry.flowEntryId().value() !=
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800737 newFlowEntry.flowEntryId().value()) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700738 //
739 // Find a local Flow Entry, but the Flow Entry ID doesn't
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800740 // match. Keep looking.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700741 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800742 continue;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700743 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700744 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800745 return localFlowEntry;
746 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700747
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800748 return null; // Entry not found
749 }
750
751 /**
752 * Update a Flow Entry because of an external ENTRY_ADD event.
753 *
754 * @param flowPath the FlowPath for the Flow Entry to update.
755 * @param localFlowEntry the local Flow Entry to update.
756 * @param newFlowEntry the FlowEntry with the new state.
757 * @return true if the local Flow Entry was updated, otherwise false.
758 */
759 private boolean updateFlowEntryAdd(FlowPath flowPath,
760 FlowEntry localFlowEntry,
761 FlowEntry newFlowEntry) {
762 boolean updated = false;
763
764 if (localFlowEntry.flowEntryUserState() ==
765 FlowEntryUserState.FE_USER_DELETE) {
766 // Don't add-back a Flow Entry that is already deleted
767 return false;
768 }
769
770 if (! localFlowEntry.isValidFlowEntryId()) {
771 // Update the Flow Entry ID
772 FlowEntryId flowEntryId =
773 new FlowEntryId(newFlowEntry.flowEntryId().value());
774 localFlowEntry.setFlowEntryId(flowEntryId);
775 updated = true;
776 }
777
778 //
779 // Update the local Flow Entry, and keep state to check
780 // if the Flow Path has been installed.
781 //
782 if (localFlowEntry.flowEntryUserState() !=
783 newFlowEntry.flowEntryUserState()) {
784 localFlowEntry.setFlowEntryUserState(
785 newFlowEntry.flowEntryUserState());
786 updated = true;
787 }
788 if (localFlowEntry.flowEntrySwitchState() !=
789 newFlowEntry.flowEntrySwitchState()) {
790 localFlowEntry.setFlowEntrySwitchState(
791 newFlowEntry.flowEntrySwitchState());
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800792 checkIfInstalledFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800793 updated = true;
794 }
795
796 return updated;
797 }
798
799 /**
800 * Find a Flow Entry that should be updated because of an external
801 * ENTRY_REMOVE event.
802 *
803 * @param flowPath the FlowPath for the Flow Entry to update.
804 * @param newFlowEntry the FlowEntry with the new state.
805 * @return the Flow Entry that should be updated if found, otherwise null.
806 */
807 private FlowEntry findFlowEntryRemove(FlowPath flowPath,
808 FlowEntry newFlowEntry) {
809 //
810 // Iterate over all Flow Entries and find a match based on
811 // the Flow Entry ID.
812 //
813 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
814 if (! localFlowEntry.isValidFlowEntryId())
815 continue;
816 if (localFlowEntry.flowEntryId().value() !=
817 newFlowEntry.flowEntryId().value()) {
818 continue;
819 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800820 return localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700821 }
822
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800823 return null; // Entry not found
824 }
825
826 /**
827 * Update a Flow Entry because of an external ENTRY_REMOVE event.
828 *
829 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800830 * @param localFlowEntry the local Flow Entry to update.
831 * @param newFlowEntry the FlowEntry with the new state.
832 * @return true if the local Flow Entry was updated, otherwise false.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800833 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800834 private boolean updateFlowEntryRemove(FlowPath flowPath,
835 FlowEntry localFlowEntry,
836 FlowEntry newFlowEntry) {
837 boolean updated = false;
838
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800839 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800840 // Update the local Flow Entry.
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800841 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800842 if (localFlowEntry.flowEntryUserState() !=
843 newFlowEntry.flowEntryUserState()) {
844 localFlowEntry.setFlowEntryUserState(
845 newFlowEntry.flowEntryUserState());
846 updated = true;
847 }
848 if (localFlowEntry.flowEntrySwitchState() !=
849 newFlowEntry.flowEntrySwitchState()) {
850 localFlowEntry.setFlowEntrySwitchState(
851 newFlowEntry.flowEntrySwitchState());
852 updated = true;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800853 }
854
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800855 return updated;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700856 }
857
858 /**
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700859 * Recompute a Flow Path.
860 *
861 * @param flowPath the Flow Path to recompute.
862 * @return true if the recomputed Flow Path has changed, otherwise false.
863 */
864 private boolean recomputeFlowPath(FlowPath flowPath) {
865 boolean hasChanged = false;
866
867 //
868 // Test whether the Flow Path needs to be recomputed
869 //
870 switch (flowPath.flowPathType()) {
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -0700871 case FP_TYPE_UNKNOWN:
872 return false; // Can't recompute on Unknown FlowType
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700873 case FP_TYPE_SHORTEST_PATH:
874 break;
875 case FP_TYPE_EXPLICIT_PATH:
876 return false; // An explicit path never changes
877 }
878
879 DataPath oldDataPath = flowPath.dataPath();
880
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700881 // Compute the new path
882 DataPath newDataPath = TopologyManager.computeNetworkPath(topology,
883 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700884 if (newDataPath == null) {
885 // We need the DataPath to compare the paths
886 newDataPath = new DataPath();
887 }
888 newDataPath.applyFlowPathFlags(flowPath.flowPathFlags());
889
890 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700891 // Test whether the new path is same
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700892 //
893 if (oldDataPath.flowEntries().size() !=
894 newDataPath.flowEntries().size()) {
895 hasChanged = true;
896 } else {
897 Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator();
898 Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator();
899 while (oldIter.hasNext() && newIter.hasNext()) {
900 FlowEntry oldFlowEntry = oldIter.next();
901 FlowEntry newFlowEntry = newIter.next();
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700902 if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
903 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700904 hasChanged = true;
905 break;
906 }
907 }
908 }
909 if (! hasChanged)
910 return hasChanged;
911
912 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700913 // Merge the changes in the path:
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700914 // - If a Flow Entry for a switch is in the old data path, but not
915 // in the new data path, then mark it for deletion.
916 // - If a Flow Entry for a switch is in the new data path, but not
917 // in the old data path, then mark it for addition.
918 // - If a Flow Entry for a switch is in both the old and the new
919 // data path, but it has changed, e.g., the incoming and/or outgoing
920 // port(s), then mark the old Flow Entry for deletion, and mark
921 // the new Flow Entry for addition.
922 // - If a Flow Entry for a switch is in both the old and the new
923 // data path, and it hasn't changed, then just keep it.
924 //
925 // NOTE: We use the Switch DPID of each entry to match the entries
926 //
927 Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>();
928 Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>();
929 ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>();
930 List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>();
931
932 // Prepare maps with the Flow Entries, so they are fast to lookup
933 for (FlowEntry flowEntry : oldDataPath.flowEntries())
934 oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
935 for (FlowEntry flowEntry : newDataPath.flowEntries())
936 newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
937
938 //
939 // Find the old Flow Entries that should be deleted
940 //
941 for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) {
942 FlowEntry newFlowEntry =
943 newFlowEntriesMap.get(oldFlowEntry.dpid().value());
944 if (newFlowEntry == null) {
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800945 // The old Flow Entry should be deleted: not on the path
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700946 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
947 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
948 deletedFlowEntries.add(oldFlowEntry);
949 }
950 }
951
952 //
953 // Find the new Flow Entries that should be added or updated
954 //
955 int idx = 0;
956 for (FlowEntry newFlowEntry : newDataPath.flowEntries()) {
957 FlowEntry oldFlowEntry =
958 oldFlowEntriesMap.get(newFlowEntry.dpid().value());
959
960 if ((oldFlowEntry != null) &&
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700961 TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
962 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700963 //
964 // Both Flow Entries are same
965 //
966 finalFlowEntries.add(oldFlowEntry);
967 idx++;
968 continue;
969 }
970
971 if (oldFlowEntry != null) {
972 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800973 // The old Flow Entry should be deleted: path diverges
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700974 //
975 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
976 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
977 deletedFlowEntries.add(oldFlowEntry);
978 }
979
980 //
981 // Add the new Flow Entry
982 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700983 //
984 // NOTE: Assign only the Flow ID.
985 // The Flow Entry ID is assigned later only for the Flow Entries
986 // this instance is responsible for.
987 //
988 newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700989
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800990 //
Pavlin Radoslavov6fde2172013-12-10 11:23:18 -0800991 // Copy the Flow timeouts
992 //
993 newFlowEntry.setIdleTimeout(flowPath.idleTimeout());
994 newFlowEntry.setHardTimeout(flowPath.hardTimeout());
995
996 //
Pavlin Radoslavova4df9522013-12-03 11:03:04 -0800997 // Allocate the FlowEntryMatch by copying the default one
998 // from the FlowPath (if set).
999 //
1000 FlowEntryMatch flowEntryMatch = null;
1001 if (flowPath.flowEntryMatch() != null)
1002 flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch());
1003 else
1004 flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001005 newFlowEntry.setFlowEntryMatch(flowEntryMatch);
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001006
1007 // Set the incoming port matching
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001008 flowEntryMatch.enableInPort(newFlowEntry.inPort());
1009
1010 //
1011 // Set the actions:
1012 // If the first Flow Entry, copy the Flow Path actions to it.
1013 //
1014 FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions();
1015 if ((idx == 0) && (flowPath.flowEntryActions() != null)) {
1016 FlowEntryActions flowActions =
1017 new FlowEntryActions(flowPath.flowEntryActions());
1018 for (FlowEntryAction action : flowActions.actions())
1019 flowEntryActions.addAction(action);
1020 }
1021 idx++;
1022
1023 //
1024 // Add the outgoing port output action
1025 //
1026 FlowEntryAction flowEntryAction = new FlowEntryAction();
1027 flowEntryAction.setActionOutput(newFlowEntry.outPort());
1028 flowEntryActions.addAction(flowEntryAction);
1029
1030 //
1031 // Set the state of the new Flow Entry
1032 //
1033 newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
1034 newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1035 finalFlowEntries.add(newFlowEntry);
1036 }
1037
1038 //
1039 // Replace the old Flow Entries with the new Flow Entries.
1040 // Note that the Flow Entries that will be deleted are added at
1041 // the end.
1042 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001043 finalFlowEntries.addAll(deletedFlowEntries);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001044 flowPath.dataPath().setFlowEntries(finalFlowEntries);
1045
1046 return hasChanged;
1047 }
1048
1049 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001050 * Receive a notification that a Flow is added.
1051 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001052 * @param flowPath the Flow that is added.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001053 */
1054 @Override
1055 public void notificationRecvFlowAdded(FlowPath flowPath) {
1056 EventEntry<FlowPath> eventEntry =
1057 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
1058 networkEvents.add(eventEntry);
1059 }
1060
1061 /**
1062 * Receive a notification that a Flow is removed.
1063 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001064 * @param flowPath the Flow that is removed.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001065 */
1066 @Override
1067 public void notificationRecvFlowRemoved(FlowPath flowPath) {
1068 EventEntry<FlowPath> eventEntry =
1069 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath);
1070 networkEvents.add(eventEntry);
1071 }
1072
1073 /**
1074 * Receive a notification that a Flow is updated.
1075 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001076 * @param flowPath the Flow that is updated.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001077 */
1078 @Override
1079 public void notificationRecvFlowUpdated(FlowPath flowPath) {
1080 // NOTE: The ADD and UPDATE events are processed in same way
1081 EventEntry<FlowPath> eventEntry =
1082 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
1083 networkEvents.add(eventEntry);
1084 }
1085
1086 /**
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001087 * Receive a notification that a FlowEntry is added.
1088 *
1089 * @param flowEntry the FlowEntry that is added.
1090 */
1091 @Override
1092 public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) {
1093 EventEntry<FlowEntry> eventEntry =
1094 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
1095 networkEvents.add(eventEntry);
1096 }
1097
1098 /**
1099 * Receive a notification that a FlowEntry is removed.
1100 *
1101 * @param flowEntry the FlowEntry that is removed.
1102 */
1103 @Override
1104 public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) {
1105 EventEntry<FlowEntry> eventEntry =
1106 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry);
1107 networkEvents.add(eventEntry);
1108 }
1109
1110 /**
1111 * Receive a notification that a FlowEntry is updated.
1112 *
1113 * @param flowEntry the FlowEntry that is updated.
1114 */
1115 @Override
1116 public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) {
1117 // NOTE: The ADD and UPDATE events are processed in same way
1118 EventEntry<FlowEntry> eventEntry =
1119 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
1120 networkEvents.add(eventEntry);
1121 }
1122
1123 /**
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001124 * Receive a notification that a FlowId is added.
1125 *
1126 * @param flowId the FlowId that is added.
1127 */
1128 @Override
1129 public void notificationRecvFlowIdAdded(FlowId flowId) {
1130 EventEntry<FlowId> eventEntry =
1131 new EventEntry<FlowId>(EventEntry.Type.ENTRY_ADD, flowId);
1132 networkEvents.add(eventEntry);
1133 }
1134
1135 /**
1136 * Receive a notification that a FlowId is removed.
1137 *
1138 * @param flowId the FlowId that is removed.
1139 */
1140 @Override
1141 public void notificationRecvFlowIdRemoved(FlowId flowId) {
1142 EventEntry<FlowId> eventEntry =
1143 new EventEntry<FlowId>(EventEntry.Type.ENTRY_REMOVE, flowId);
1144 networkEvents.add(eventEntry);
1145 }
1146
1147 /**
1148 * Receive a notification that a FlowId is updated.
1149 *
1150 * @param flowId the FlowId that is updated.
1151 */
1152 @Override
1153 public void notificationRecvFlowIdUpdated(FlowId flowId) {
1154 // NOTE: The ADD and UPDATE events are processed in same way
1155 EventEntry<FlowId> eventEntry =
1156 new EventEntry<FlowId>(EventEntry.Type.ENTRY_ADD, flowId);
1157 networkEvents.add(eventEntry);
1158 }
1159
1160 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001161 * Receive a notification that a Topology Element is added.
1162 *
1163 * @param topologyElement the Topology Element that is added.
1164 */
1165 @Override
1166 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
1167 EventEntry<TopologyElement> eventEntry =
1168 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1169 networkEvents.add(eventEntry);
1170 }
1171
1172 /**
1173 * Receive a notification that a Topology Element is removed.
1174 *
1175 * @param topologyElement the Topology Element that is removed.
1176 */
1177 @Override
1178 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
1179 EventEntry<TopologyElement> eventEntry =
1180 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement);
1181 networkEvents.add(eventEntry);
1182 }
1183
1184 /**
1185 * Receive a notification that a Topology Element is updated.
1186 *
1187 * @param topologyElement the Topology Element that is updated.
1188 */
1189 @Override
1190 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
1191 // NOTE: The ADD and UPDATE events are processed in same way
1192 EventEntry<TopologyElement> eventEntry =
1193 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1194 networkEvents.add(eventEntry);
1195 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001196
1197 /**
1198 * Get a sorted copy of all Flow Paths.
1199 *
1200 * @return a sorted copy of all Flow Paths.
1201 */
1202 synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() {
1203 SortedMap<Long, FlowPath> sortedFlowPaths =
1204 new TreeMap<Long, FlowPath>();
1205
1206 //
1207 // TODO: For now we use serialization/deserialization to create
1208 // a copy of each Flow Path. In the future, we should use proper
1209 // copy constructors.
1210 //
1211 Kryo kryo = kryoFactory.newKryo();
1212 synchronized (allFlowPaths) {
1213 for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) {
1214 FlowPath origFlowPath = entry.getValue();
1215 FlowPath copyFlowPath = kryo.copy(origFlowPath);
1216 sortedFlowPaths.put(entry.getKey(), copyFlowPath);
1217 }
1218 }
1219 kryoFactory.deleteKryo(kryo);
1220
1221 return sortedFlowPaths;
1222 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001223}