blob: 20d1ff6596497f7d9f17da364af7ce6a820b2617 [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 Radoslavovcc757162014-01-10 16:26:38 -080016import net.floodlightcontroller.core.IOFSwitchListener;
17
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070018import net.onrc.onos.datagrid.IDatagridService;
Naoki Shiota0abe38d2014-01-07 15:31:22 -080019import net.onrc.onos.graph.GraphDBOperation;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070020import net.onrc.onos.ofcontroller.topology.Topology;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070021import net.onrc.onos.ofcontroller.topology.TopologyElement;
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -070022import net.onrc.onos.ofcontroller.topology.TopologyManager;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070023import net.onrc.onos.ofcontroller.util.DataPath;
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -080024import net.onrc.onos.ofcontroller.util.Dpid;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070025import net.onrc.onos.ofcontroller.util.EventEntry;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070026import net.onrc.onos.ofcontroller.util.FlowEntry;
27import net.onrc.onos.ofcontroller.util.FlowEntryAction;
28import net.onrc.onos.ofcontroller.util.FlowEntryActions;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -070029import net.onrc.onos.ofcontroller.util.FlowEntryId;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070030import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
31import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState;
32import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -070033import net.onrc.onos.ofcontroller.util.FlowId;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070034import net.onrc.onos.ofcontroller.util.FlowPath;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -070035import net.onrc.onos.ofcontroller.util.FlowPathUserState;
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -080036import net.onrc.onos.ofcontroller.util.Pair;
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -080037import net.onrc.onos.ofcontroller.util.Port;
Pavlin Radoslavov53219802013-12-06 11:02:04 -080038import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
39
40import com.esotericsoftware.kryo2.Kryo;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070041
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45/**
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -070046 * Class for FlowPath Maintenance.
47 * This class listens for FlowEvents to:
48 * - Maintain a local cache of the Network Topology.
49 * - Detect FlowPaths impacted by Topology change.
50 * - Recompute impacted FlowPath using cached Topology.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070051 */
Pavlin Radoslavovcc757162014-01-10 16:26:38 -080052class FlowEventHandler extends Thread implements IFlowEventHandlerService,
53 IOFSwitchListener {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -080054
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -080055 private boolean enableOnrc2014MeasurementsFlows = true;
56 private boolean enableOnrc2014MeasurementsTopology = true;
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -080057
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070058 /** The logger. */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -070059 private final static Logger log = LoggerFactory.getLogger(FlowEventHandler.class);
Pavlin Radoslavovcc757162014-01-10 16:26:38 -080060
Naoki Shiota0abe38d2014-01-07 15:31:22 -080061 private GraphDBOperation dbHandler;
Naoki Shiotaf74d5f32014-01-09 21:29:38 -080062
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070063 private FlowManager flowManager; // The Flow Manager to use
64 private IDatagridService datagridService; // The Datagrid Service to use
Pavlin Radoslavova23e5412013-10-27 19:56:40 -070065 private Topology topology; // The network topology
Pavlin Radoslavov53219802013-12-06 11:02:04 -080066 private KryoFactory kryoFactory = new KryoFactory();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070067
68 // The queue with Flow Path and Topology Element updates
69 private BlockingQueue<EventEntry<?>> networkEvents =
70 new LinkedBlockingQueue<EventEntry<?>>();
71
Pavlin Radoslavovb7506842013-10-29 17:46:54 -070072 // The pending Topology, FlowPath, and FlowEntry events
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070073 private List<EventEntry<TopologyElement>> topologyEvents =
74 new LinkedList<EventEntry<TopologyElement>>();
75 private List<EventEntry<FlowPath>> flowPathEvents =
76 new LinkedList<EventEntry<FlowPath>>();
Pavlin Radoslavovb7506842013-10-29 17:46:54 -070077 private List<EventEntry<FlowEntry>> flowEntryEvents =
78 new LinkedList<EventEntry<FlowEntry>>();
Pavlin Radoslavov2194d112014-01-10 13:36:00 -080079 private List<EventEntry<Pair<FlowId, Dpid>>> flowIdEvents =
80 new LinkedList<EventEntry<Pair<FlowId, Dpid>>>();
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -080081 private List<EventEntry<Pair<FlowEntryId, Dpid>>> flowEntryIdEvents =
82 new LinkedList<EventEntry<Pair<FlowEntryId, Dpid>>>();
Pavlin Radoslavovcc757162014-01-10 16:26:38 -080083 private List<EventEntry<Dpid>> switchDpidEvents =
84 new LinkedList<EventEntry<Dpid>>();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -070085
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080086 // All internally computed Flow Paths
87 private Map<Long, FlowPath> allFlowPaths = new HashMap<Long, FlowPath>();
88
89 // The Flow Entries received as notifications with unmatched Flow Paths
90 private Map<Long, FlowEntry> unmatchedFlowEntryAdd =
91 new HashMap<Long, FlowEntry>();
92
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080093 //
94 // Transient state for processing the Flow Paths:
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080095 // - The Flow Paths that should be recomputed
96 // - The Flow Paths with modified Flow Entries
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -080097 // - The Flow Paths that we should check if installed in all switches
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -080098 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -080099 private Map<Long, FlowPath> shouldRecomputeFlowPaths =
100 new HashMap<Long, FlowPath>();
101 private Map<Long, FlowPath> modifiedFlowPaths =
102 new HashMap<Long, FlowPath>();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800103 private Map<Long, FlowPath> checkIfInstalledFlowPaths =
104 new HashMap<Long, FlowPath>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800105
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700106 /**
107 * Constructor for a given Flow Manager and Datagrid Service.
108 *
109 * @param flowManager the Flow Manager to use.
110 * @param datagridService the Datagrid Service to use.
111 */
Pavlin Radoslavov9a859022013-10-30 10:08:24 -0700112 FlowEventHandler(FlowManager flowManager,
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800113 IDatagridService datagridService) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700114 this.flowManager = flowManager;
115 this.datagridService = datagridService;
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700116 this.topology = new Topology();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700117 }
118
119 /**
Pavlin Radoslavoved0f4a82013-11-04 16:38:36 -0800120 * Get the network topology.
121 *
122 * @return the network topology.
123 */
124 protected Topology getTopology() { return this.topology; }
125
126 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800127 * Startup processing.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700128 */
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800129 private void startup() {
Pavlin Radoslavov8252fee2014-01-07 17:24:29 -0800130 this.dbHandler = new GraphDBOperation("");
131
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700132 //
133 // Obtain the initial Topology state
134 //
135 Collection<TopologyElement> topologyElements =
136 datagridService.getAllTopologyElements();
137 for (TopologyElement topologyElement : topologyElements) {
138 EventEntry<TopologyElement> eventEntry =
139 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
140 topologyEvents.add(eventEntry);
141 }
142 //
143 // Obtain the initial Flow Path state
144 //
145 Collection<FlowPath> flowPaths = datagridService.getAllFlows();
146 for (FlowPath flowPath : flowPaths) {
147 EventEntry<FlowPath> eventEntry =
148 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
149 flowPathEvents.add(eventEntry);
150 }
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700151 //
152 // Obtain the initial FlowEntry state
153 //
154 Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries();
155 for (FlowEntry flowEntry : flowEntries) {
156 EventEntry<FlowEntry> eventEntry =
157 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
158 flowEntryEvents.add(eventEntry);
159 }
160
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800161 //
162 // Obtain the initial FlowId state
163 //
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800164 Collection<Pair<FlowId, Dpid>> flowIds =
165 datagridService.getAllFlowIds();
166 for (Pair<FlowId, Dpid> pair : flowIds) {
167 EventEntry<Pair<FlowId, Dpid>> eventEntry =
168 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, pair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800169 flowIdEvents.add(eventEntry);
170 }
171
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -0800172 //
173 // Obtain the initial FlowEntryId state
174 //
175 Collection<Pair<FlowEntryId, Dpid>> flowEntryIds =
176 datagridService.getAllFlowEntryIds();
177 for (Pair<FlowEntryId, Dpid> pair : flowEntryIds) {
178 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
179 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, pair);
180 flowEntryIdEvents.add(eventEntry);
181 }
182
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800183 // Process the initial events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800184 synchronized (allFlowPaths) {
185 processEvents();
186 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800187 }
188
189 /**
190 * Run the thread.
191 */
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800192 @Override
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800193 public void run() {
Yuta HIGUCHI61509a42013-12-17 10:41:04 -0800194 this.setName("FlowEventHandler " + this.getId());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800195 startup();
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700196
197 //
198 // The main loop
199 //
200 Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>();
201 try {
202 while (true) {
203 EventEntry<?> eventEntry = networkEvents.take();
204 collection.add(eventEntry);
205 networkEvents.drainTo(collection);
206
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700207 //
208 // Demultiplex all events:
209 // - EventEntry<TopologyElement>
210 // - EventEntry<FlowPath>
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700211 // - EventEntry<FlowEntry>
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800212 // - EventEntry<Pair<FlowId, Dpid>>
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -0800213 // - EventEntry<Pair<FlowEntryId, Dpid>>
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700214 //
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700215 for (EventEntry<?> event : collection) {
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800216 // Topology event
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700217 if (event.eventData() instanceof TopologyElement) {
218 EventEntry<TopologyElement> topologyEventEntry =
219 (EventEntry<TopologyElement>)event;
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800220
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700221 topologyEvents.add(topologyEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800222 continue;
223 }
224
225 // FlowPath event
226 if (event.eventData() instanceof FlowPath) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700227 EventEntry<FlowPath> flowPathEventEntry =
228 (EventEntry<FlowPath>)event;
229 flowPathEvents.add(flowPathEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800230 continue;
231 }
232
233 // FlowEntry event
234 if (event.eventData() instanceof FlowEntry) {
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700235 EventEntry<FlowEntry> flowEntryEventEntry =
236 (EventEntry<FlowEntry>)event;
237 flowEntryEvents.add(flowEntryEventEntry);
Pavlin Radoslavovc8038a82013-12-02 17:43:20 -0800238 continue;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700239 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800240
241 // FlowId event
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800242 if (event.eventData() instanceof Pair) {
243 EventEntry<Pair<FlowId, Dpid>> flowIdEventEntry =
244 (EventEntry<Pair<FlowId, Dpid>>)event;
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800245 flowIdEvents.add(flowIdEventEntry);
246 continue;
247 }
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800248
249 // Switch Dpid event
250 if (event.eventData() instanceof Dpid) {
251 EventEntry<Dpid> switchDpidEventEntry =
252 (EventEntry<Dpid>)event;
253 switchDpidEvents.add(switchDpidEventEntry);
254 continue;
255 }
256
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -0800257 // FlowEntryId event
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800258 // TODO: Fix the code below if we need again to handle
259 // the FlowEntryId events
260 /*
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -0800261 if (event.eventData() instanceof Pair) {
262 EventEntry<Pair<FlowEntryId, Dpid>> flowEntryIdEventEntry =
263 (EventEntry<Pair<FlowEntryId, Dpid>>)event;
264 flowEntryIdEvents.add(flowEntryIdEventEntry);
265 continue;
266 }
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800267 */
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700268 }
269 collection.clear();
Pavlin Radoslavoved4c7a92013-10-26 21:36:21 -0700270
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700271 // Process the events (if any)
Pavlin Radoslavov53219802013-12-06 11:02:04 -0800272 synchronized (allFlowPaths) {
273 processEvents();
274 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700275 }
276 } catch (Exception exception) {
277 log.debug("Exception processing Network Events: ", exception);
278 }
279 }
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800280
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700281 /**
282 * Process the events (if any)
283 */
284 private void processEvents() {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800285 Collection<FlowEntry> modifiedFlowEntries;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700286
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800287 if (enableOnrc2014MeasurementsFlows) {
288
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800289 PerformanceMonitor.start("EventHandler.ProcessAllEvents");
290
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800291 if (topologyEvents.isEmpty() && flowIdEvents.isEmpty() &&
292 switchDpidEvents.isEmpty()) {
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800293 return; // Nothing to do
294 }
295
296 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
297
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800298 // Process the Switch Dpid events
Pavlin Radoslavovcfcd2722014-01-12 22:32:36 -0800299 PerformanceMonitor.start("EventHandler.SwitchDpid");
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800300 processSwitchDpidEvents();
Pavlin Radoslavovcfcd2722014-01-12 22:32:36 -0800301 PerformanceMonitor.stop("EventHandler.SwitchDpid");
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800302
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800303 // Process the Flow ID events
Pavlin Radoslavovcfcd2722014-01-12 22:32:36 -0800304 PerformanceMonitor.start("EventHandler.FlowIdEvents");
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800305 processFlowIdEvents(mySwitches);
Pavlin Radoslavovcfcd2722014-01-12 22:32:36 -0800306 PerformanceMonitor.stop("EventHandler.FlowIdEvents");
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800307
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800308 // Fetch the topology
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800309 PerformanceMonitor.start("EventHandler.ReadTopology");
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800310 processTopologyEvents();
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800311 PerformanceMonitor.stop("EventHandler.ReadTopology");
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800312
313 // Recompute all affected Flow Paths and keep only the modified
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800314 PerformanceMonitor.start("EventHandler.RecomputeFlows");
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800315 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
316 if (recomputeFlowPath(flowPath))
317 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
318 }
319
320 // Assign the Flow Entry ID as needed
321 for (FlowPath flowPath : modifiedFlowPaths.values()) {
322 for (FlowEntry flowEntry : flowPath.flowEntries()) {
323 if (! flowEntry.isValidFlowEntryId()) {
324 long id = flowManager.getNextFlowEntryId();
325 flowEntry.setFlowEntryId(new FlowEntryId(id));
326 }
327 }
328 }
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800329 PerformanceMonitor.stop("EventHandler.RecomputeFlows");
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800330
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800331 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800332 // Push the modified state to the database
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800333 //
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800334 PerformanceMonitor.start("EventHandler.WriteFlowsToDb");
Pavlin Radoslavov85d81c02014-01-10 17:22:22 -0800335 for (FlowPath flowPath : modifiedFlowPaths.values()) {
336 //
337 // Delete the Flow Path from the Network Map
338 //
339 if (flowPath.flowPathUserState() ==
340 FlowPathUserState.FP_USER_DELETE) {
341 log.debug("Deleting Flow Path From Database: {}", flowPath);
Pavlin Radoslavov6c3e6802014-01-10 19:27:34 -0800342 // TODO: For now the deleting of a Flow Path is blocking
Pavlin Radoslavov85d81c02014-01-10 17:22:22 -0800343 ParallelFlowDatabaseOperation.deleteFlow(dbHandler,
Pavlin Radoslavov6c3e6802014-01-10 19:27:34 -0800344 flowPath.flowId());
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800345 //
346 // NOTE: For now the sending of the notifications
347 // is outside of this loop, so the performance measurements
348 // are more accurate.
349 //
350 /*
Pavlin Radoslavov6c3e6802014-01-10 19:27:34 -0800351 // Send the notifications for the deleted Flow Entries
352 for (FlowEntry flowEntry : flowPath.flowEntries()) {
353 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
354 }
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800355 */
Pavlin Radoslavov6c3e6802014-01-10 19:27:34 -0800356
Pavlin Radoslavov85d81c02014-01-10 17:22:22 -0800357 continue;
358 }
359
360 log.debug("Pushing Flow Path To Database: {}", flowPath);
361 //
362 // Write the Flow Path to the Network Map
363 //
364 ParallelFlowDatabaseOperation.addFlow(dbHandler, flowPath,
365 datagridService);
366 }
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800367 PerformanceMonitor.stop("EventHandler.WriteFlowsToDb");
368
369 //
370 // Send the notifications for the deleted Flow Entries
371 // NOTE: This code was pulled outside of the above loop,
372 // so the performance measurements are more accurate.
373 //
374 PerformanceMonitor.start("EventHandler.NotificationSend.FlowEntryRemoved");
375 for (FlowPath flowPath : modifiedFlowPaths.values()) {
376 if (flowPath.flowPathUserState() ==
377 FlowPathUserState.FP_USER_DELETE) {
378 for (FlowEntry flowEntry : flowPath.flowEntries()) {
379 datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId());
380 }
381 }
382 }
Pavlin Radoslavovdbd09cc2014-01-12 18:13:35 -0800383 PerformanceMonitor.stop("EventHandler.NotificationSend.FlowEntryRemoved");
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800384
385 // Cleanup
386 topologyEvents.clear();
387 flowIdEvents.clear();
Pavlin Radoslavov2d6e5f12014-01-10 16:34:05 -0800388 switchDpidEvents.clear();
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800389 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800390 // NOTE: Keep a cache with my Flow Paths
391 // allFlowPaths.clear();
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800392 shouldRecomputeFlowPaths.clear();
393 modifiedFlowPaths.clear();
394
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800395 PerformanceMonitor.stop("EventHandler.ProcessAllEvents");
Pavlin Radoslavov8bd6d112014-01-12 20:12:37 -0800396
397
Pavlin Radoslavovcfcd2722014-01-12 22:32:36 -0800398 PerformanceMonitor.report("EventHandler.SwitchDpid");
399 PerformanceMonitor.report("EventHandler.FlowIdEvents");
Pavlin Radoslavov8bd6d112014-01-12 20:12:37 -0800400 PerformanceMonitor.report("EventHandler.ReadTopology");
401 PerformanceMonitor.report("EventHandler.RecomputeFlows");
402 PerformanceMonitor.report("EventHandler.WriteFlowsToDb");
403 PerformanceMonitor.report("EventHandler.NotificationSend.FlowEntryRemoved");
404 PerformanceMonitor.report("EventHandler.ProcessAllEvents");
Pavlin Radoslavove2497672014-01-12 18:03:35 -0800405
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800406 return;
407 }
408
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700409 if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() &&
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800410 flowEntryEvents.isEmpty()) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700411 return; // Nothing to do
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700412 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700413
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800414 processFlowPathEvents();
415 processTopologyEvents();
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800416 processUnmatchedFlowEntryAdd();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800417 processFlowEntryEvents();
418
419 // Recompute all affected Flow Paths and keep only the modified
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800420 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800421 if (recomputeFlowPath(flowPath))
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800422 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800423 }
424
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800425 // Extract the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800426 modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800427
428 // Assign missing Flow Entry IDs
429 assignFlowEntryId(modifiedFlowEntries);
430
431 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800432 // Push the modified state to the Flow Manager
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800433 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800434 flowManager.pushModifiedFlowState(modifiedFlowPaths.values(),
435 modifiedFlowEntries);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800436
437 //
438 // Remove Flow Entries that were deleted
439 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800440 for (FlowPath flowPath : modifiedFlowPaths.values())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800441 flowPath.dataPath().removeDeletedFlowEntries();
442
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800443 //
444 // Check if Flow Paths have been installed into all switches,
445 // and generate the appropriate events.
446 //
447 checkInstalledFlowPaths(checkIfInstalledFlowPaths.values());
448
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800449 // Cleanup
450 topologyEvents.clear();
451 flowPathEvents.clear();
452 flowEntryEvents.clear();
453 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800454 shouldRecomputeFlowPaths.clear();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800455 modifiedFlowPaths.clear();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800456 checkIfInstalledFlowPaths.clear();
457 }
458
459 /**
460 * Check if Flow Paths have been installed into all switches,
461 * and generate the appropriate events.
462 *
463 * @param flowPaths the flowPaths to process.
464 */
465 private void checkInstalledFlowPaths(Collection<FlowPath> flowPaths) {
466 List<FlowPath> installedFlowPaths = new LinkedList<FlowPath>();
467
468 Kryo kryo = kryoFactory.newKryo();
469
470 for (FlowPath flowPath : flowPaths) {
471 boolean isInstalled = true;
472
473 //
474 // Check whether all Flow Entries have been installed
475 //
476 for (FlowEntry flowEntry : flowPath.flowEntries()) {
477 if (flowEntry.flowEntrySwitchState() !=
478 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
479 isInstalled = false;
480 break;
481 }
482 }
483
484 if (isInstalled) {
485 // Create a copy and add it to the list
486 FlowPath copyFlowPath = kryo.copy(flowPath);
487 installedFlowPaths.add(copyFlowPath);
488 }
489 }
490 kryoFactory.deleteKryo(kryo);
491
492 // Generate an event for the installed Flow Path.
493 flowManager.notificationFlowPathsInstalled(installedFlowPaths);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800494 }
495
496 /**
497 * Extract the modified Flow Entries.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800498 *
499 * @param modifiedFlowPaths the Flow Paths to process.
500 * @return a collection with the modified Flow Entries.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800501 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800502 private Collection<FlowEntry> extractModifiedFlowEntries(
503 Collection<FlowPath> modifiedFlowPaths) {
504 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800505
506 // Extract only the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800507 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800508 for (FlowEntry flowEntry : flowPath.flowEntries()) {
509 if (flowEntry.flowEntrySwitchState() ==
510 FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800511 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800512 }
513 }
514 }
515 return modifiedFlowEntries;
516 }
517
518 /**
519 * Assign the Flow Entry ID as needed.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800520 *
521 * @param modifiedFlowEnries the collection of Flow Entries that need
522 * Flow Entry ID assigned.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800523 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800524 private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800525 if (modifiedFlowEntries.isEmpty())
526 return;
527
528 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
529
530 //
531 // Assign the Flow Entry ID only for Flow Entries for my switches
532 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800533 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800534 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
535 if (mySwitch == null)
536 continue;
537 if (! flowEntry.isValidFlowEntryId()) {
538 long id = flowManager.getNextFlowEntryId();
539 flowEntry.setFlowEntryId(new FlowEntryId(id));
540 }
541 }
542 }
543
544 /**
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800545 * Fix a flow fetched from the database.
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800546 *
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800547 * @param flowPath the Flow to fix.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800548 */
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800549 private void fixFlowFromDatabase(FlowPath flowPath) {
550 //
551 // TODO: Bug workaround / fix :
552 // method FlowDatabaseOperation.extractFlowEntry() doesn't
553 // fetch the inPort and outPort, hence we assign them here.
554 //
555 // Assign the inPort and outPort for the Flow Entries
556 for (FlowEntry flowEntry : flowPath.flowEntries()) {
557 // Set the inPort
558 do {
559 if (flowEntry.inPort() != null)
560 break;
561 if (flowEntry.flowEntryMatch() == null)
562 break;
563 Port inPort = new Port(flowEntry.flowEntryMatch().inPort().value());
564 flowEntry.setInPort(inPort);
565 } while (false);
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800566
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800567 // Set the outPort
568 do {
569 if (flowEntry.outPort() != null)
570 break;
571 for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) {
572 if (fa.actionOutput() != null) {
573 Port outPort = new Port(fa.actionOutput().port().value());
574 flowEntry.setOutPort(outPort);
575 break;
576 }
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800577 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800578 } while (false);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800579 }
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800580 }
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800581
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800582 /**
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800583 * Process the Switch Dpid events.
584 */
585 private void processSwitchDpidEvents() {
586 Map<Long, Dpid> addedSwitches = new HashMap<Long, Dpid>();
587 Map<Long, Dpid> removedSwitches = new HashMap<Long, Dpid>();
588
589 //
590 // Process all Switch Dpid events and update the appropriate state
591 //
592 for (EventEntry<Dpid> eventEntry : switchDpidEvents) {
593 Dpid dpid = eventEntry.eventData();
594
595 log.debug("SwitchDpid Event: {} {}", eventEntry.eventType(), dpid);
596
597 // Compute the final set of added and removed switches
598 switch (eventEntry.eventType()) {
599 case ENTRY_ADD:
600 addedSwitches.put(dpid.value(), dpid);
601 removedSwitches.remove(dpid.value());
602 break;
603 case ENTRY_REMOVE:
604 addedSwitches.remove(dpid.value());
605 removedSwitches.put(dpid.value(), dpid);
606 break;
607 }
608 }
609
610 //
611 // Remove the Flows from the local cache if the removed
612 // switch is the Source Switch.
613 //
614 // TODO: This search can be expensive for a large number of flows
615 // and should be optmized.
616 //
617 List<FlowId> deleteFlowIds = new LinkedList<FlowId>();
618 for (Dpid switchDpid : removedSwitches.values()) {
619 for (FlowPath flowPath : allFlowPaths.values()) {
620 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
621 if (srcDpid.value() == switchDpid.value())
622 deleteFlowIds.add(flowPath.flowId());
623 }
624 }
625 //
626 // Remove the Flows from the local cache
627 //
628 for (FlowId flowId : deleteFlowIds)
629 allFlowPaths.remove(flowId.value());
630
631 // Get the Flows for the added switches
632 Collection<FlowPath> flowPaths =
633 ParallelFlowDatabaseOperation.getFlowsForSwitches(dbHandler,
634 addedSwitches.values());
635 for (FlowPath flowPath : flowPaths) {
636 allFlowPaths.put(flowPath.flowId().value(), flowPath);
637 }
638 }
639
640 /**
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800641 * Process the Flow ID events.
642 *
643 * @param mySwitches the collection of my switches.
644 */
645 private void processFlowIdEvents(Map<Long, IOFSwitch> mySwitches) {
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800646 List<FlowId> shouldFetchMyFlowIds = new LinkedList<FlowId>();
647
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800648 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800649 // Process all Flow Id events and update the appropriate state
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800650 //
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800651 for (EventEntry<Pair<FlowId, Dpid>> eventEntry : flowIdEvents) {
652 Pair<FlowId, Dpid> pair = eventEntry.eventData();
653 FlowId flowId = pair.first;
654 Dpid dpid = pair.second;
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800655
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800656 log.debug("Flow ID Event: {} {} {}", eventEntry.eventType(),
657 flowId, dpid);
658
659 //
660 // Ignore Flows if the Source Switch is not controlled by this
661 // instance.
662 //
663 if (mySwitches.get(dpid.value()) == null)
664 continue;
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800665
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800666 switch (eventEntry.eventType()) {
667 case ENTRY_ADD: {
668 //
669 // Add a new Flow Path
670 //
671 if (allFlowPaths.get(flowId.value()) != null) {
672 //
673 // TODO: What to do if the Flow Path already exists?
674 // Fow now, we just re-add it.
675 //
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800676 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800677 shouldFetchMyFlowIds.add(flowId);
678
679 break;
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800680 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800681
682 case ENTRY_REMOVE: {
683 //
684 // Remove an existing Flow Path.
685 //
686 // Find the Flow Path, and mark the Flow and its Flow Entries
687 // for deletion.
688 //
689 FlowPath existingFlowPath =
690 allFlowPaths.get(flowId.value());
691 if (existingFlowPath == null)
692 continue; // Nothing to do
693
694 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
695 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
696 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
697 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
698 }
699
700 // Remove the Flow Path from the internal state
701 Long key = existingFlowPath.flowId().value();
702 allFlowPaths.remove(key);
703 shouldRecomputeFlowPaths.remove(key);
704 modifiedFlowPaths.put(key, existingFlowPath);
705
706 break;
707 }
708 }
709 }
710
711 // Get my Flows
712 Collection<FlowPath> myFlows =
Pavlin Radoslavov6602b6a2014-01-10 16:43:29 -0800713 ParallelFlowDatabaseOperation.getFlows(dbHandler,
714 shouldFetchMyFlowIds);
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800715
716 for (FlowPath flowPath : myFlows) {
717 fixFlowFromDatabase(flowPath);
718
719 switch (flowPath.flowPathType()) {
720 case FP_TYPE_SHORTEST_PATH:
721 //
722 // Reset the Data Path, in case it was set already, because
723 // we are going to recompute it anyway.
724 //
725 flowPath.flowEntries().clear();
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800726 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
727 flowPath);
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800728 break;
729 case FP_TYPE_EXPLICIT_PATH:
730 //
731 // Mark all Flow Entries for installation in the switches.
732 //
733 for (FlowEntry flowEntry : flowPath.flowEntries()) {
734 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
735 }
736 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
737 break;
738 case FP_TYPE_UNKNOWN:
739 log.error("FlowPath event with unknown type");
740 break;
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800741 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800742 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800743 }
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800744 }
745
746 /**
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800747 * Process the Flow Entry ID events.
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800748 *
749 * @param mySwitches the collection of my switches.
750 * @return a collection of modified Flow Entries this instance needs
751 * to push to its own switches.
752 */
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800753 private Collection<FlowEntry> processFlowEntryIdEvents(Map<Long, IOFSwitch> mySwitches) {
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800754 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
755
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800756 //
757 // Process all Flow ID events and update the appropriate state
758 //
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800759 for (EventEntry<Pair<FlowEntryId, Dpid>> eventEntry : flowEntryIdEvents) {
760 Pair<FlowEntryId, Dpid> pair = eventEntry.eventData();
761 FlowEntryId flowEntryId = pair.first;
762 Dpid dpid = pair.second;
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800763
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800764 log.debug("Flow Entry ID Event: {} {} {}", eventEntry.eventType(),
765 flowEntryId, dpid);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800766
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800767 if (mySwitches.get(dpid.value()) == null)
768 continue;
769
770 // Fetch the Flow Entry
771 FlowEntry flowEntry = FlowDatabaseOperation.getFlowEntry(dbHandler,
772 flowEntryId);
773 if (flowEntry == null) {
774 log.debug("Flow Entry ID {} : Flow Entry not found!",
775 flowEntryId);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800776 continue;
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800777 }
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800778 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800779 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800780
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800781 return modifiedFlowEntries;
782 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800783
784 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800785 * Process the Flow Path events.
786 */
787 private void processFlowPathEvents() {
788 //
789 // Process all Flow Path events and update the appropriate state
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700790 //
791 for (EventEntry<FlowPath> eventEntry : flowPathEvents) {
792 FlowPath flowPath = eventEntry.eventData();
793
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800794 log.debug("Flow Event: {} {}", eventEntry.eventType(), flowPath);
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800795
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700796 switch (eventEntry.eventType()) {
797 case ENTRY_ADD: {
798 //
799 // Add a new Flow Path
800 //
801 if (allFlowPaths.get(flowPath.flowId().value()) != null) {
802 //
803 // TODO: What to do if the Flow Path already exists?
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800804 // Fow now, we just re-add it.
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700805 //
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700806 }
807
808 switch (flowPath.flowPathType()) {
809 case FP_TYPE_SHORTEST_PATH:
810 //
811 // Reset the Data Path, in case it was set already, because
812 // we are going to recompute it anyway.
813 //
814 flowPath.flowEntries().clear();
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800815 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
816 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700817 break;
818 case FP_TYPE_EXPLICIT_PATH:
819 //
820 // Mark all Flow Entries for installation in the switches.
821 //
822 for (FlowEntry flowEntry : flowPath.flowEntries()) {
823 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
824 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800825 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700826 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800827 case FP_TYPE_UNKNOWN:
828 log.error("FlowPath event with unknown type");
829 break;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700830 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800831 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700832
833 break;
834 }
835
836 case ENTRY_REMOVE: {
837 //
838 // Remove an existing Flow Path.
839 //
840 // Find the Flow Path, and mark the Flow and its Flow Entries
841 // for deletion.
842 //
843 FlowPath existingFlowPath =
844 allFlowPaths.get(flowPath.flowId().value());
845 if (existingFlowPath == null)
846 continue; // Nothing to do
847
848 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
849 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
850 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
851 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
852 }
853
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800854 // Remove the Flow Path from the internal state
855 Long key = existingFlowPath.flowId().value();
856 allFlowPaths.remove(key);
857 shouldRecomputeFlowPaths.remove(key);
858 modifiedFlowPaths.put(key, existingFlowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700859
860 break;
861 }
862 }
863 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800864 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700865
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800866 /**
867 * Process the Topology events.
868 */
869 private void processTopologyEvents() {
Pavlin Radoslavov1b454902014-01-09 16:28:27 -0800870 boolean isTopologyModified = false;
871
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800872 if (enableOnrc2014MeasurementsTopology) {
873 if (topologyEvents.isEmpty())
874 return;
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800875
Pavlin Radoslavov2a8b9de2014-01-09 15:58:32 -0800876 // TODO: Code for debugging purpose only
877 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
878 TopologyElement topologyElement = eventEntry.eventData();
879 log.debug("Topology Event: {} {}", eventEntry.eventType(),
880 topologyElement.toString());
881 }
882
Pavlin Radoslavov1b454902014-01-09 16:28:27 -0800883 log.debug("[BEFORE] {}", topology.toString());
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800884 topology.readFromDatabase(dbHandler);
885 log.debug("[AFTER] {}", topology.toString());
886 shouldRecomputeFlowPaths.putAll(allFlowPaths);
887 return;
888 }
889
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700890 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800891 // Process all Topology events and update the appropriate state
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700892 //
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800893 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
894 TopologyElement topologyElement = eventEntry.eventData();
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800895
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800896 log.debug("Topology Event: {} {}", eventEntry.eventType(),
897 topologyElement.toString());
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800898
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800899 switch (eventEntry.eventType()) {
900 case ENTRY_ADD:
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800901 isTopologyModified |= topology.addTopologyElement(topologyElement);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800902 break;
903 case ENTRY_REMOVE:
Naoki Shiota9f6fc212014-01-09 21:38:08 -0800904 isTopologyModified |= topology.removeTopologyElement(topologyElement);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800905 break;
906 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700907 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700908 if (isTopologyModified) {
909 // TODO: For now, if the topology changes, we recompute all Flows
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800910 shouldRecomputeFlowPaths.putAll(allFlowPaths);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700911 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800912 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700913
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800914 /**
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800915 * Process previously received Flow Entries with unmatched Flow Paths.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800916 */
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800917 private void processUnmatchedFlowEntryAdd() {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800918 FlowPath flowPath;
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800919 FlowEntry localFlowEntry;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800920
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700921 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800922 // Update Flow Entries with previously unmatched Flow Entry updates
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700923 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800924 if (! unmatchedFlowEntryAdd.isEmpty()) {
925 Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>();
926 for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) {
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800927 // log.debug("Processing Unmatched Flow Entry: {}",
928 // flowEntry.toString());
929
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800930 flowPath = allFlowPaths.get(flowEntry.flowId().value());
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800931 if (flowPath == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800932 remainingUpdates.put(flowEntry.flowEntryId().value(),
933 flowEntry);
934 continue;
935 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800936 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
937 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800938 remainingUpdates.put(flowEntry.flowEntryId().value(),
939 flowEntry);
940 continue;
941 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800942 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
943 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
944 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700945 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800946 unmatchedFlowEntryAdd = remainingUpdates;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700947 }
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800948 }
949
950 /**
951 * Process the Flow Entry events.
952 */
953 private void processFlowEntryEvents() {
954 FlowPath flowPath;
955 FlowEntry localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700956
957 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800958 // Process all Flow Entry events and update the appropriate state
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700959 //
960 for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) {
961 FlowEntry flowEntry = eventEntry.eventData();
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800962
963 log.debug("Flow Entry Event: {} {}", eventEntry.eventType(),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800964 flowEntry);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800965
966 if ((! flowEntry.isValidFlowId()) ||
967 (! flowEntry.isValidFlowEntryId())) {
968 continue;
969 }
970
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700971 switch (eventEntry.eventType()) {
972 case ENTRY_ADD:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800973 flowPath = allFlowPaths.get(flowEntry.flowId().value());
974 if (flowPath == null) {
975 // Flow Path not found: keep the entry for later matching
976 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
977 flowEntry);
978 break;
979 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800980 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
981 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800982 // Flow Entry not found: keep the entry for later matching
983 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
984 flowEntry);
985 break;
986 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800987 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
988 // Add the updated Flow Path to the list of updated paths
989 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
990 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700991 break;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800992
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700993 case ENTRY_REMOVE:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800994 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
995 if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800996 continue; // Removed previously unmatched entry
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800997 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800998
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800999 flowPath = allFlowPaths.get(flowEntry.flowId().value());
1000 if (flowPath == null) {
1001 // Flow Path not found: ignore the update
1002 break;
1003 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001004 localFlowEntry = findFlowEntryRemove(flowPath, flowEntry);
1005 if (localFlowEntry == null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -08001006 // Flow Entry not found: ignore it
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001007 break;
1008 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001009 if (updateFlowEntryRemove(flowPath, localFlowEntry,
1010 flowEntry)) {
1011 // Add the updated Flow Path to the list of updated paths
1012 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
1013 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001014 break;
1015 }
1016 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001017 }
1018
1019 /**
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001020 * Find a Flow Entry that should be updated because of an external
1021 * ENTRY_ADD event.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001022 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001023 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001024 * @param newFlowEntry the FlowEntry with the new state.
1025 * @return the Flow Entry that should be updated if found, otherwise null.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001026 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001027 private FlowEntry findFlowEntryAdd(FlowPath flowPath,
1028 FlowEntry newFlowEntry) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001029 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001030 // Iterate over all Flow Entries and find a match.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001031 //
1032 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001033 if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry,
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001034 newFlowEntry)) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001035 continue;
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001036 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001037
1038 //
1039 // Local Flow Entry match found
1040 //
1041 if (localFlowEntry.isValidFlowEntryId()) {
1042 if (localFlowEntry.flowEntryId().value() !=
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001043 newFlowEntry.flowEntryId().value()) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001044 //
1045 // Find a local Flow Entry, but the Flow Entry ID doesn't
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001046 // match. Keep looking.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001047 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001048 continue;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001049 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001050 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001051 return localFlowEntry;
1052 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001053
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001054 return null; // Entry not found
1055 }
1056
1057 /**
1058 * Update a Flow Entry because of an external ENTRY_ADD event.
1059 *
1060 * @param flowPath the FlowPath for the Flow Entry to update.
1061 * @param localFlowEntry the local Flow Entry to update.
1062 * @param newFlowEntry the FlowEntry with the new state.
1063 * @return true if the local Flow Entry was updated, otherwise false.
1064 */
1065 private boolean updateFlowEntryAdd(FlowPath flowPath,
1066 FlowEntry localFlowEntry,
1067 FlowEntry newFlowEntry) {
1068 boolean updated = false;
1069
1070 if (localFlowEntry.flowEntryUserState() ==
1071 FlowEntryUserState.FE_USER_DELETE) {
1072 // Don't add-back a Flow Entry that is already deleted
1073 return false;
1074 }
1075
1076 if (! localFlowEntry.isValidFlowEntryId()) {
1077 // Update the Flow Entry ID
1078 FlowEntryId flowEntryId =
1079 new FlowEntryId(newFlowEntry.flowEntryId().value());
1080 localFlowEntry.setFlowEntryId(flowEntryId);
1081 updated = true;
1082 }
1083
1084 //
1085 // Update the local Flow Entry, and keep state to check
1086 // if the Flow Path has been installed.
1087 //
1088 if (localFlowEntry.flowEntryUserState() !=
1089 newFlowEntry.flowEntryUserState()) {
1090 localFlowEntry.setFlowEntryUserState(
1091 newFlowEntry.flowEntryUserState());
1092 updated = true;
1093 }
1094 if (localFlowEntry.flowEntrySwitchState() !=
1095 newFlowEntry.flowEntrySwitchState()) {
1096 localFlowEntry.setFlowEntrySwitchState(
1097 newFlowEntry.flowEntrySwitchState());
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -08001098 checkIfInstalledFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001099 updated = true;
1100 }
1101
1102 return updated;
1103 }
1104
1105 /**
1106 * Find a Flow Entry that should be updated because of an external
1107 * ENTRY_REMOVE event.
1108 *
1109 * @param flowPath the FlowPath for the Flow Entry to update.
1110 * @param newFlowEntry the FlowEntry with the new state.
1111 * @return the Flow Entry that should be updated if found, otherwise null.
1112 */
1113 private FlowEntry findFlowEntryRemove(FlowPath flowPath,
1114 FlowEntry newFlowEntry) {
1115 //
1116 // Iterate over all Flow Entries and find a match based on
1117 // the Flow Entry ID.
1118 //
1119 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
1120 if (! localFlowEntry.isValidFlowEntryId())
1121 continue;
1122 if (localFlowEntry.flowEntryId().value() !=
1123 newFlowEntry.flowEntryId().value()) {
1124 continue;
1125 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001126 return localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001127 }
1128
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001129 return null; // Entry not found
1130 }
1131
1132 /**
1133 * Update a Flow Entry because of an external ENTRY_REMOVE event.
1134 *
1135 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001136 * @param localFlowEntry the local Flow Entry to update.
1137 * @param newFlowEntry the FlowEntry with the new state.
1138 * @return true if the local Flow Entry was updated, otherwise false.
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001139 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001140 private boolean updateFlowEntryRemove(FlowPath flowPath,
1141 FlowEntry localFlowEntry,
1142 FlowEntry newFlowEntry) {
1143 boolean updated = false;
1144
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001145 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001146 // Update the local Flow Entry.
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001147 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001148 if (localFlowEntry.flowEntryUserState() !=
1149 newFlowEntry.flowEntryUserState()) {
1150 localFlowEntry.setFlowEntryUserState(
1151 newFlowEntry.flowEntryUserState());
1152 updated = true;
1153 }
1154 if (localFlowEntry.flowEntrySwitchState() !=
1155 newFlowEntry.flowEntrySwitchState()) {
1156 localFlowEntry.setFlowEntrySwitchState(
1157 newFlowEntry.flowEntrySwitchState());
1158 updated = true;
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001159 }
1160
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001161 return updated;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001162 }
1163
1164 /**
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001165 * Recompute a Flow Path.
1166 *
1167 * @param flowPath the Flow Path to recompute.
1168 * @return true if the recomputed Flow Path has changed, otherwise false.
1169 */
1170 private boolean recomputeFlowPath(FlowPath flowPath) {
1171 boolean hasChanged = false;
1172
Pavlin Radoslavov7bf837a2014-01-09 14:22:05 -08001173 if (enableOnrc2014MeasurementsFlows) {
1174 // Cleanup the deleted Flow Entries from the earlier iteration
1175 flowPath.dataPath().removeDeletedFlowEntries();
Pavlin Radoslavov737aa522014-01-09 15:35:00 -08001176
1177 //
1178 // TODO: Fake it that the Flow Entries have been already pushed
1179 // into the switches, so we don't push them again.
1180 //
1181 for (FlowEntry flowEntry : flowPath.flowEntries()) {
1182 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
1183 }
Pavlin Radoslavov7bf837a2014-01-09 14:22:05 -08001184 }
1185
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001186 //
1187 // Test whether the Flow Path needs to be recomputed
1188 //
1189 switch (flowPath.flowPathType()) {
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -07001190 case FP_TYPE_UNKNOWN:
1191 return false; // Can't recompute on Unknown FlowType
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001192 case FP_TYPE_SHORTEST_PATH:
1193 break;
1194 case FP_TYPE_EXPLICIT_PATH:
1195 return false; // An explicit path never changes
1196 }
1197
1198 DataPath oldDataPath = flowPath.dataPath();
1199
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001200 // Compute the new path
Naoki Shiota9f6fc212014-01-09 21:38:08 -08001201 DataPath newDataPath = TopologyManager.computeNetworkPath(topology,
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001202 flowPath);
Naoki Shiotaf74d5f32014-01-09 21:29:38 -08001203
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001204 if (newDataPath == null) {
1205 // We need the DataPath to compare the paths
1206 newDataPath = new DataPath();
1207 }
1208 newDataPath.applyFlowPathFlags(flowPath.flowPathFlags());
1209
1210 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001211 // Test whether the new path is same
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001212 //
1213 if (oldDataPath.flowEntries().size() !=
1214 newDataPath.flowEntries().size()) {
1215 hasChanged = true;
1216 } else {
1217 Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator();
1218 Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator();
1219 while (oldIter.hasNext() && newIter.hasNext()) {
1220 FlowEntry oldFlowEntry = oldIter.next();
1221 FlowEntry newFlowEntry = newIter.next();
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001222 if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
1223 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001224 hasChanged = true;
1225 break;
1226 }
1227 }
1228 }
1229 if (! hasChanged)
1230 return hasChanged;
1231
1232 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001233 // Merge the changes in the path:
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001234 // - If a Flow Entry for a switch is in the old data path, but not
1235 // in the new data path, then mark it for deletion.
1236 // - If a Flow Entry for a switch is in the new data path, but not
1237 // in the old data path, then mark it for addition.
1238 // - If a Flow Entry for a switch is in both the old and the new
1239 // data path, but it has changed, e.g., the incoming and/or outgoing
1240 // port(s), then mark the old Flow Entry for deletion, and mark
1241 // the new Flow Entry for addition.
1242 // - If a Flow Entry for a switch is in both the old and the new
1243 // data path, and it hasn't changed, then just keep it.
1244 //
1245 // NOTE: We use the Switch DPID of each entry to match the entries
1246 //
1247 Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>();
1248 Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>();
1249 ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>();
1250 List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>();
1251
1252 // Prepare maps with the Flow Entries, so they are fast to lookup
1253 for (FlowEntry flowEntry : oldDataPath.flowEntries())
1254 oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
1255 for (FlowEntry flowEntry : newDataPath.flowEntries())
1256 newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
1257
1258 //
1259 // Find the old Flow Entries that should be deleted
1260 //
1261 for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) {
1262 FlowEntry newFlowEntry =
1263 newFlowEntriesMap.get(oldFlowEntry.dpid().value());
1264 if (newFlowEntry == null) {
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001265 // The old Flow Entry should be deleted: not on the path
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001266 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
1267 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1268 deletedFlowEntries.add(oldFlowEntry);
1269 }
1270 }
1271
1272 //
1273 // Find the new Flow Entries that should be added or updated
1274 //
1275 int idx = 0;
1276 for (FlowEntry newFlowEntry : newDataPath.flowEntries()) {
1277 FlowEntry oldFlowEntry =
1278 oldFlowEntriesMap.get(newFlowEntry.dpid().value());
1279
1280 if ((oldFlowEntry != null) &&
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001281 TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
1282 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001283 //
1284 // Both Flow Entries are same
1285 //
1286 finalFlowEntries.add(oldFlowEntry);
1287 idx++;
1288 continue;
1289 }
1290
1291 if (oldFlowEntry != null) {
1292 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001293 // The old Flow Entry should be deleted: path diverges
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001294 //
1295 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
1296 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1297 deletedFlowEntries.add(oldFlowEntry);
1298 }
1299
1300 //
1301 // Add the new Flow Entry
1302 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001303 //
1304 // NOTE: Assign only the Flow ID.
1305 // The Flow Entry ID is assigned later only for the Flow Entries
1306 // this instance is responsible for.
1307 //
1308 newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001309
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001310 //
Pavlin Radoslavov6fde2172013-12-10 11:23:18 -08001311 // Copy the Flow timeouts
1312 //
1313 newFlowEntry.setIdleTimeout(flowPath.idleTimeout());
1314 newFlowEntry.setHardTimeout(flowPath.hardTimeout());
1315
1316 //
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001317 // Allocate the FlowEntryMatch by copying the default one
1318 // from the FlowPath (if set).
1319 //
1320 FlowEntryMatch flowEntryMatch = null;
1321 if (flowPath.flowEntryMatch() != null)
1322 flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch());
1323 else
1324 flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001325 newFlowEntry.setFlowEntryMatch(flowEntryMatch);
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001326
1327 // Set the incoming port matching
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001328 flowEntryMatch.enableInPort(newFlowEntry.inPort());
1329
1330 //
1331 // Set the actions:
1332 // If the first Flow Entry, copy the Flow Path actions to it.
1333 //
1334 FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions();
1335 if ((idx == 0) && (flowPath.flowEntryActions() != null)) {
1336 FlowEntryActions flowActions =
1337 new FlowEntryActions(flowPath.flowEntryActions());
1338 for (FlowEntryAction action : flowActions.actions())
1339 flowEntryActions.addAction(action);
1340 }
1341 idx++;
1342
1343 //
1344 // Add the outgoing port output action
1345 //
1346 FlowEntryAction flowEntryAction = new FlowEntryAction();
1347 flowEntryAction.setActionOutput(newFlowEntry.outPort());
1348 flowEntryActions.addAction(flowEntryAction);
1349
1350 //
1351 // Set the state of the new Flow Entry
1352 //
1353 newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
1354 newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1355 finalFlowEntries.add(newFlowEntry);
1356 }
1357
1358 //
1359 // Replace the old Flow Entries with the new Flow Entries.
1360 // Note that the Flow Entries that will be deleted are added at
1361 // the end.
1362 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001363 finalFlowEntries.addAll(deletedFlowEntries);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001364 flowPath.dataPath().setFlowEntries(finalFlowEntries);
1365
1366 return hasChanged;
1367 }
1368
1369 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001370 * Receive a notification that a Flow is added.
1371 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001372 * @param flowPath the Flow that is added.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001373 */
1374 @Override
1375 public void notificationRecvFlowAdded(FlowPath flowPath) {
1376 EventEntry<FlowPath> eventEntry =
1377 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
1378 networkEvents.add(eventEntry);
1379 }
1380
1381 /**
1382 * Receive a notification that a Flow is removed.
1383 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001384 * @param flowPath the Flow that is removed.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001385 */
1386 @Override
1387 public void notificationRecvFlowRemoved(FlowPath flowPath) {
1388 EventEntry<FlowPath> eventEntry =
1389 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath);
1390 networkEvents.add(eventEntry);
1391 }
1392
1393 /**
1394 * Receive a notification that a Flow is updated.
1395 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001396 * @param flowPath the Flow that is updated.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001397 */
1398 @Override
1399 public void notificationRecvFlowUpdated(FlowPath flowPath) {
1400 // NOTE: The ADD and UPDATE events are processed in same way
1401 EventEntry<FlowPath> eventEntry =
1402 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
1403 networkEvents.add(eventEntry);
1404 }
1405
1406 /**
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001407 * Receive a notification that a FlowEntry is added.
1408 *
1409 * @param flowEntry the FlowEntry that is added.
1410 */
1411 @Override
1412 public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) {
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001413 if (enableOnrc2014MeasurementsFlows) {
Pavlin Radoslavov9fbdc992014-01-12 20:56:58 -08001414 String tag = "EventHandler.AddFlowEntryToSwitch." + flowEntry.flowEntryId();
1415 PerformanceMonitor.start(tag);
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001416 Collection entries = new ArrayList();
1417 entries.add(flowEntry);
1418 flowManager.pushModifiedFlowEntriesToSwitches(entries);
Pavlin Radoslavov9fbdc992014-01-12 20:56:58 -08001419 PerformanceMonitor.stop(tag);
1420 PerformanceMonitor.report(tag);
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001421 return;
1422 }
1423
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001424 EventEntry<FlowEntry> eventEntry =
1425 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
1426 networkEvents.add(eventEntry);
1427 }
1428
1429 /**
1430 * Receive a notification that a FlowEntry is removed.
1431 *
1432 * @param flowEntry the FlowEntry that is removed.
1433 */
1434 @Override
1435 public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) {
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001436 if (enableOnrc2014MeasurementsFlows) {
Pavlin Radoslavov9fbdc992014-01-12 20:56:58 -08001437 String tag = "EventHandler.RemoveFlowEntryFromSwitch." + flowEntry.flowEntryId();
1438 PerformanceMonitor.start(tag);
Pavlin Radoslavove4d2a432014-01-10 12:01:08 -08001439 //
1440 // NOTE: Must update the state to DELETE, because
1441 // the notification contains the original state.
1442 //
1443 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
1444
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001445 Collection entries = new ArrayList();
1446 entries.add(flowEntry);
1447 flowManager.pushModifiedFlowEntriesToSwitches(entries);
Pavlin Radoslavov9fbdc992014-01-12 20:56:58 -08001448 PerformanceMonitor.stop(tag);
1449 PerformanceMonitor.report(tag);
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001450 return;
1451 }
1452
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001453 EventEntry<FlowEntry> eventEntry =
1454 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry);
1455 networkEvents.add(eventEntry);
1456 }
1457
1458 /**
1459 * Receive a notification that a FlowEntry is updated.
1460 *
1461 * @param flowEntry the FlowEntry that is updated.
1462 */
1463 @Override
1464 public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) {
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001465 if (enableOnrc2014MeasurementsFlows) {
1466 Collection entries = new ArrayList();
1467 entries.add(flowEntry);
1468 flowManager.pushModifiedFlowEntriesToSwitches(entries);
1469 return;
1470 }
1471
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001472 // NOTE: The ADD and UPDATE events are processed in same way
1473 EventEntry<FlowEntry> eventEntry =
1474 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
1475 networkEvents.add(eventEntry);
1476 }
1477
1478 /**
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001479 * Receive a notification that a FlowId is added.
1480 *
1481 * @param flowId the FlowId that is added.
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001482 * @param dpid the Source Switch Dpid for the corresponding Flow.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001483 */
1484 @Override
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001485 public void notificationRecvFlowIdAdded(FlowId flowId, Dpid dpid) {
1486 Pair flowIdPair = new Pair(flowId, dpid);
1487
1488 EventEntry<Pair<FlowId, Dpid>> eventEntry =
1489 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowIdPair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001490 networkEvents.add(eventEntry);
1491 }
1492
1493 /**
1494 * Receive a notification that a FlowId is removed.
1495 *
1496 * @param flowId the FlowId that is removed.
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001497 * @param dpid the Source Switch Dpid for the corresponding Flow.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001498 */
1499 @Override
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001500 public void notificationRecvFlowIdRemoved(FlowId flowId, Dpid dpid) {
1501 Pair flowIdPair = new Pair(flowId, dpid);
1502
1503 EventEntry<Pair<FlowId, Dpid>> eventEntry =
1504 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_REMOVE, flowIdPair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001505 networkEvents.add(eventEntry);
1506 }
1507
1508 /**
1509 * Receive a notification that a FlowId is updated.
1510 *
1511 * @param flowId the FlowId that is updated.
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001512 * @param dpid the Source Switch Dpid for the corresponding Flow.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001513 */
1514 @Override
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001515 public void notificationRecvFlowIdUpdated(FlowId flowId, Dpid dpid) {
1516 Pair flowIdPair = new Pair(flowId, dpid);
1517
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001518 // NOTE: The ADD and UPDATE events are processed in same way
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001519 EventEntry<Pair<FlowId, Dpid>> eventEntry =
1520 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowIdPair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001521 networkEvents.add(eventEntry);
1522 }
1523
1524 /**
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001525 * Receive a notification that a FlowEntryId is added.
1526 *
1527 * @param flowEntryId the FlowEntryId that is added.
1528 * @param dpid the Switch Dpid for the corresponding Flow Entry.
1529 */
1530 @Override
1531 public void notificationRecvFlowEntryIdAdded(FlowEntryId flowEntryId,
1532 Dpid dpid) {
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001533 Pair flowEntryIdPair = new Pair(flowEntryId, dpid);
1534
1535 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
1536 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowEntryIdPair);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001537 networkEvents.add(eventEntry);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001538 }
1539
1540 /**
1541 * Receive a notification that a FlowEntryId is removed.
1542 *
1543 * @param flowEntryId the FlowEntryId that is removed.
1544 * @param dpid the Switch Dpid for the corresponding Flow Entry.
1545 */
1546 @Override
1547 public void notificationRecvFlowEntryIdRemoved(FlowEntryId flowEntryId,
1548 Dpid dpid) {
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001549 Pair flowEntryIdPair = new Pair(flowEntryId, dpid);
1550
1551 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
1552 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_REMOVE, flowEntryIdPair);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001553 networkEvents.add(eventEntry);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001554 }
1555
1556 /**
1557 * Receive a notification that a FlowEntryId is updated.
1558 *
1559 * @param flowEntryId the FlowEntryId that is updated.
1560 * @param dpid the Switch Dpid for the corresponding Flow Entry.
1561 */
1562 @Override
1563 public void notificationRecvFlowEntryIdUpdated(FlowEntryId flowEntryId,
1564 Dpid dpid) {
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001565 Pair flowEntryIdPair = new Pair(flowEntryId, dpid);
1566
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001567 // NOTE: The ADD and UPDATE events are processed in same way
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001568 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
1569 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowEntryIdPair);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001570 networkEvents.add(eventEntry);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001571 }
1572
1573 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001574 * Receive a notification that a Topology Element is added.
1575 *
1576 * @param topologyElement the Topology Element that is added.
1577 */
1578 @Override
1579 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
1580 EventEntry<TopologyElement> eventEntry =
1581 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1582 networkEvents.add(eventEntry);
1583 }
1584
1585 /**
1586 * Receive a notification that a Topology Element is removed.
1587 *
1588 * @param topologyElement the Topology Element that is removed.
1589 */
1590 @Override
1591 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
1592 EventEntry<TopologyElement> eventEntry =
1593 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement);
1594 networkEvents.add(eventEntry);
1595 }
1596
1597 /**
1598 * Receive a notification that a Topology Element is updated.
1599 *
1600 * @param topologyElement the Topology Element that is updated.
1601 */
1602 @Override
1603 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
1604 // NOTE: The ADD and UPDATE events are processed in same way
1605 EventEntry<TopologyElement> eventEntry =
1606 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1607 networkEvents.add(eventEntry);
1608 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001609
1610 /**
Pavlin Radoslavovcc757162014-01-10 16:26:38 -08001611 * Receive a notification that a switch is added to this instance.
1612 *
1613 * @param sw the switch that is added.
1614 */
1615 @Override
1616 public void addedSwitch(IOFSwitch sw) {
1617 Dpid dpid = new Dpid(sw.getId());
1618 EventEntry<Dpid> eventEntry =
1619 new EventEntry<Dpid>(EventEntry.Type.ENTRY_ADD, dpid);
1620 networkEvents.add(eventEntry);
1621 }
1622
1623 /**
1624 * Receive a notification that a switch is removed from this instance.
1625 *
1626 * @param sw the switch that is removed.
1627 */
1628 @Override
1629 public void removedSwitch(IOFSwitch sw) {
1630 Dpid dpid = new Dpid(sw.getId());
1631 EventEntry<Dpid> eventEntry =
1632 new EventEntry<Dpid>(EventEntry.Type.ENTRY_REMOVE, dpid);
1633 networkEvents.add(eventEntry);
1634 }
1635
1636 /**
1637 * Receive a notification that the ports on a switch have changed.
1638 */
1639 @Override
1640 public void switchPortChanged(Long switchId) {
1641 // Nothing to do
1642 }
1643
1644 /**
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001645 * Get a sorted copy of all Flow Paths.
1646 *
1647 * @return a sorted copy of all Flow Paths.
1648 */
1649 synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() {
1650 SortedMap<Long, FlowPath> sortedFlowPaths =
1651 new TreeMap<Long, FlowPath>();
1652
1653 //
1654 // TODO: For now we use serialization/deserialization to create
1655 // a copy of each Flow Path. In the future, we should use proper
1656 // copy constructors.
1657 //
1658 Kryo kryo = kryoFactory.newKryo();
1659 synchronized (allFlowPaths) {
1660 for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) {
1661 FlowPath origFlowPath = entry.getValue();
1662 FlowPath copyFlowPath = kryo.copy(origFlowPath);
1663 sortedFlowPaths.put(entry.getKey(), copyFlowPath);
1664 }
1665 }
1666 kryoFactory.deleteKryo(kryo);
1667
1668 return sortedFlowPaths;
1669 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001670}