blob: 7118e4185c28f3d69ab0b8df8978ba12f46a380b [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 Radoslavovcc757162014-01-10 16:26:38 -0800289 if (topologyEvents.isEmpty() && flowIdEvents.isEmpty() &&
290 switchDpidEvents.isEmpty()) {
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800291 return; // Nothing to do
292 }
293
294 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
295
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800296 // Process the Switch Dpid events
297 processSwitchDpidEvents();
298
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800299 // Process the Flow ID events
300 processFlowIdEvents(mySwitches);
301
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800302 // Fetch the topology
303 processTopologyEvents();
304
305 // Recompute all affected Flow Paths and keep only the modified
306 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
307 if (recomputeFlowPath(flowPath))
308 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
309 }
310
311 // Assign the Flow Entry ID as needed
312 for (FlowPath flowPath : modifiedFlowPaths.values()) {
313 for (FlowEntry flowEntry : flowPath.flowEntries()) {
314 if (! flowEntry.isValidFlowEntryId()) {
315 long id = flowManager.getNextFlowEntryId();
316 flowEntry.setFlowEntryId(new FlowEntryId(id));
317 }
318 }
319 }
320
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800321 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800322 // Push the modified state to the database
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800323 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800324 flowManager.writeModifiedFlowPathsToDatabase(modifiedFlowPaths.values());
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800325
326 // Cleanup
327 topologyEvents.clear();
328 flowIdEvents.clear();
Pavlin Radoslavov2d6e5f12014-01-10 16:34:05 -0800329 switchDpidEvents.clear();
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800330 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800331 // NOTE: Keep a cache with my Flow Paths
332 // allFlowPaths.clear();
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800333 shouldRecomputeFlowPaths.clear();
334 modifiedFlowPaths.clear();
335
336 return;
337 }
338
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700339 if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() &&
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800340 flowEntryEvents.isEmpty()) {
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700341 return; // Nothing to do
Pavlin Radoslavovb7506842013-10-29 17:46:54 -0700342 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -0700343
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800344 processFlowPathEvents();
345 processTopologyEvents();
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800346 processUnmatchedFlowEntryAdd();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800347 processFlowEntryEvents();
348
349 // Recompute all affected Flow Paths and keep only the modified
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800350 for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800351 if (recomputeFlowPath(flowPath))
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800352 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800353 }
354
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800355 // Extract the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800356 modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values());
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800357
358 // Assign missing Flow Entry IDs
359 assignFlowEntryId(modifiedFlowEntries);
360
361 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800362 // Push the modified state to the Flow Manager
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800363 //
Pavlin Radoslavova0c16362013-12-04 13:18:08 -0800364 flowManager.pushModifiedFlowState(modifiedFlowPaths.values(),
365 modifiedFlowEntries);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800366
367 //
368 // Remove Flow Entries that were deleted
369 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800370 for (FlowPath flowPath : modifiedFlowPaths.values())
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800371 flowPath.dataPath().removeDeletedFlowEntries();
372
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800373 //
374 // Check if Flow Paths have been installed into all switches,
375 // and generate the appropriate events.
376 //
377 checkInstalledFlowPaths(checkIfInstalledFlowPaths.values());
378
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800379 // Cleanup
380 topologyEvents.clear();
381 flowPathEvents.clear();
382 flowEntryEvents.clear();
383 //
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800384 shouldRecomputeFlowPaths.clear();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800385 modifiedFlowPaths.clear();
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800386 checkIfInstalledFlowPaths.clear();
387 }
388
389 /**
390 * Check if Flow Paths have been installed into all switches,
391 * and generate the appropriate events.
392 *
393 * @param flowPaths the flowPaths to process.
394 */
395 private void checkInstalledFlowPaths(Collection<FlowPath> flowPaths) {
396 List<FlowPath> installedFlowPaths = new LinkedList<FlowPath>();
397
398 Kryo kryo = kryoFactory.newKryo();
399
400 for (FlowPath flowPath : flowPaths) {
401 boolean isInstalled = true;
402
403 //
404 // Check whether all Flow Entries have been installed
405 //
406 for (FlowEntry flowEntry : flowPath.flowEntries()) {
407 if (flowEntry.flowEntrySwitchState() !=
408 FlowEntrySwitchState.FE_SWITCH_UPDATED) {
409 isInstalled = false;
410 break;
411 }
412 }
413
414 if (isInstalled) {
415 // Create a copy and add it to the list
416 FlowPath copyFlowPath = kryo.copy(flowPath);
417 installedFlowPaths.add(copyFlowPath);
418 }
419 }
420 kryoFactory.deleteKryo(kryo);
421
422 // Generate an event for the installed Flow Path.
423 flowManager.notificationFlowPathsInstalled(installedFlowPaths);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800424 }
425
426 /**
427 * Extract the modified Flow Entries.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800428 *
429 * @param modifiedFlowPaths the Flow Paths to process.
430 * @return a collection with the modified Flow Entries.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800431 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800432 private Collection<FlowEntry> extractModifiedFlowEntries(
433 Collection<FlowPath> modifiedFlowPaths) {
434 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800435
436 // Extract only the modified Flow Entries
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800437 for (FlowPath flowPath : modifiedFlowPaths) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800438 for (FlowEntry flowEntry : flowPath.flowEntries()) {
439 if (flowEntry.flowEntrySwitchState() ==
440 FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) {
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800441 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800442 }
443 }
444 }
445 return modifiedFlowEntries;
446 }
447
448 /**
449 * Assign the Flow Entry ID as needed.
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800450 *
451 * @param modifiedFlowEnries the collection of Flow Entries that need
452 * Flow Entry ID assigned.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800453 */
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800454 private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800455 if (modifiedFlowEntries.isEmpty())
456 return;
457
458 Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches();
459
460 //
461 // Assign the Flow Entry ID only for Flow Entries for my switches
462 //
Pavlin Radoslavovafc4aa92013-12-04 12:44:23 -0800463 for (FlowEntry flowEntry : modifiedFlowEntries) {
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800464 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
465 if (mySwitch == null)
466 continue;
467 if (! flowEntry.isValidFlowEntryId()) {
468 long id = flowManager.getNextFlowEntryId();
469 flowEntry.setFlowEntryId(new FlowEntryId(id));
470 }
471 }
472 }
473
474 /**
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800475 * Fix a flow fetched from the database.
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800476 *
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800477 * @param flowPath the Flow to fix.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800478 */
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800479 private void fixFlowFromDatabase(FlowPath flowPath) {
480 //
481 // TODO: Bug workaround / fix :
482 // method FlowDatabaseOperation.extractFlowEntry() doesn't
483 // fetch the inPort and outPort, hence we assign them here.
484 //
485 // Assign the inPort and outPort for the Flow Entries
486 for (FlowEntry flowEntry : flowPath.flowEntries()) {
487 // Set the inPort
488 do {
489 if (flowEntry.inPort() != null)
490 break;
491 if (flowEntry.flowEntryMatch() == null)
492 break;
493 Port inPort = new Port(flowEntry.flowEntryMatch().inPort().value());
494 flowEntry.setInPort(inPort);
495 } while (false);
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800496
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800497 // Set the outPort
498 do {
499 if (flowEntry.outPort() != null)
500 break;
501 for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) {
502 if (fa.actionOutput() != null) {
503 Port outPort = new Port(fa.actionOutput().port().value());
504 flowEntry.setOutPort(outPort);
505 break;
506 }
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800507 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800508 } while (false);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800509 }
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800510 }
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800511
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800512 /**
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800513 * Process the Switch Dpid events.
514 */
515 private void processSwitchDpidEvents() {
516 Map<Long, Dpid> addedSwitches = new HashMap<Long, Dpid>();
517 Map<Long, Dpid> removedSwitches = new HashMap<Long, Dpid>();
518
519 //
520 // Process all Switch Dpid events and update the appropriate state
521 //
522 for (EventEntry<Dpid> eventEntry : switchDpidEvents) {
523 Dpid dpid = eventEntry.eventData();
524
525 log.debug("SwitchDpid Event: {} {}", eventEntry.eventType(), dpid);
526
527 // Compute the final set of added and removed switches
528 switch (eventEntry.eventType()) {
529 case ENTRY_ADD:
530 addedSwitches.put(dpid.value(), dpid);
531 removedSwitches.remove(dpid.value());
532 break;
533 case ENTRY_REMOVE:
534 addedSwitches.remove(dpid.value());
535 removedSwitches.put(dpid.value(), dpid);
536 break;
537 }
538 }
539
540 //
541 // Remove the Flows from the local cache if the removed
542 // switch is the Source Switch.
543 //
544 // TODO: This search can be expensive for a large number of flows
545 // and should be optmized.
546 //
547 List<FlowId> deleteFlowIds = new LinkedList<FlowId>();
548 for (Dpid switchDpid : removedSwitches.values()) {
549 for (FlowPath flowPath : allFlowPaths.values()) {
550 Dpid srcDpid = flowPath.dataPath().srcPort().dpid();
551 if (srcDpid.value() == switchDpid.value())
552 deleteFlowIds.add(flowPath.flowId());
553 }
554 }
555 //
556 // Remove the Flows from the local cache
557 //
558 for (FlowId flowId : deleteFlowIds)
559 allFlowPaths.remove(flowId.value());
560
561 // Get the Flows for the added switches
562 Collection<FlowPath> flowPaths =
563 ParallelFlowDatabaseOperation.getFlowsForSwitches(dbHandler,
564 addedSwitches.values());
565 for (FlowPath flowPath : flowPaths) {
566 allFlowPaths.put(flowPath.flowId().value(), flowPath);
567 }
568 }
569
570 /**
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800571 * Process the Flow ID events.
572 *
573 * @param mySwitches the collection of my switches.
574 */
575 private void processFlowIdEvents(Map<Long, IOFSwitch> mySwitches) {
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800576 List<FlowId> shouldFetchMyFlowIds = new LinkedList<FlowId>();
577
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800578 //
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800579 // Process all Flow Id events and update the appropriate state
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800580 //
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800581 for (EventEntry<Pair<FlowId, Dpid>> eventEntry : flowIdEvents) {
582 Pair<FlowId, Dpid> pair = eventEntry.eventData();
583 FlowId flowId = pair.first;
584 Dpid dpid = pair.second;
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800585
Pavlin Radoslavov2194d112014-01-10 13:36:00 -0800586 log.debug("Flow ID Event: {} {} {}", eventEntry.eventType(),
587 flowId, dpid);
588
589 //
590 // Ignore Flows if the Source Switch is not controlled by this
591 // instance.
592 //
593 if (mySwitches.get(dpid.value()) == null)
594 continue;
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800595
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800596 switch (eventEntry.eventType()) {
597 case ENTRY_ADD: {
598 //
599 // Add a new Flow Path
600 //
601 if (allFlowPaths.get(flowId.value()) != null) {
602 //
603 // TODO: What to do if the Flow Path already exists?
604 // Fow now, we just re-add it.
605 //
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800606 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800607 shouldFetchMyFlowIds.add(flowId);
608
609 break;
Pavlin Radoslavov3a7cc902014-01-09 02:32:08 -0800610 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800611
612 case ENTRY_REMOVE: {
613 //
614 // Remove an existing Flow Path.
615 //
616 // Find the Flow Path, and mark the Flow and its Flow Entries
617 // for deletion.
618 //
619 FlowPath existingFlowPath =
620 allFlowPaths.get(flowId.value());
621 if (existingFlowPath == null)
622 continue; // Nothing to do
623
624 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
625 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
626 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
627 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
628 }
629
630 // Remove the Flow Path from the internal state
631 Long key = existingFlowPath.flowId().value();
632 allFlowPaths.remove(key);
633 shouldRecomputeFlowPaths.remove(key);
634 modifiedFlowPaths.put(key, existingFlowPath);
635
636 break;
637 }
638 }
639 }
640
641 // Get my Flows
642 Collection<FlowPath> myFlows =
643 FlowDatabaseOperation.getFlows(dbHandler, shouldFetchMyFlowIds);
644
645 for (FlowPath flowPath : myFlows) {
646 fixFlowFromDatabase(flowPath);
647
648 switch (flowPath.flowPathType()) {
649 case FP_TYPE_SHORTEST_PATH:
650 //
651 // Reset the Data Path, in case it was set already, because
652 // we are going to recompute it anyway.
653 //
654 flowPath.flowEntries().clear();
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800655 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
656 flowPath);
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800657 break;
658 case FP_TYPE_EXPLICIT_PATH:
659 //
660 // Mark all Flow Entries for installation in the switches.
661 //
662 for (FlowEntry flowEntry : flowPath.flowEntries()) {
663 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
664 }
665 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
666 break;
667 case FP_TYPE_UNKNOWN:
668 log.error("FlowPath event with unknown type");
669 break;
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800670 }
Pavlin Radoslavov417398f2014-01-10 13:04:33 -0800671 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovdb1f91c2014-01-08 16:04:17 -0800672 }
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800673 }
674
675 /**
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800676 * Process the Flow Entry ID events.
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800677 *
678 * @param mySwitches the collection of my switches.
679 * @return a collection of modified Flow Entries this instance needs
680 * to push to its own switches.
681 */
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800682 private Collection<FlowEntry> processFlowEntryIdEvents(Map<Long, IOFSwitch> mySwitches) {
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800683 List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>();
684
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800685 //
686 // Process all Flow ID events and update the appropriate state
687 //
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800688 for (EventEntry<Pair<FlowEntryId, Dpid>> eventEntry : flowEntryIdEvents) {
689 Pair<FlowEntryId, Dpid> pair = eventEntry.eventData();
690 FlowEntryId flowEntryId = pair.first;
691 Dpid dpid = pair.second;
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800692
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800693 log.debug("Flow Entry ID Event: {} {} {}", eventEntry.eventType(),
694 flowEntryId, dpid);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800695
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800696 if (mySwitches.get(dpid.value()) == null)
697 continue;
698
699 // Fetch the Flow Entry
700 FlowEntry flowEntry = FlowDatabaseOperation.getFlowEntry(dbHandler,
701 flowEntryId);
702 if (flowEntry == null) {
703 log.debug("Flow Entry ID {} : Flow Entry not found!",
704 flowEntryId);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800705 continue;
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800706 }
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800707 modifiedFlowEntries.add(flowEntry);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800708 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800709
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800710 return modifiedFlowEntries;
711 }
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -0800712
713 /**
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800714 * Process the Flow Path events.
715 */
716 private void processFlowPathEvents() {
717 //
718 // Process all Flow Path events and update the appropriate state
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700719 //
720 for (EventEntry<FlowPath> eventEntry : flowPathEvents) {
721 FlowPath flowPath = eventEntry.eventData();
722
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800723 log.debug("Flow Event: {} {}", eventEntry.eventType(), flowPath);
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800724
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700725 switch (eventEntry.eventType()) {
726 case ENTRY_ADD: {
727 //
728 // Add a new Flow Path
729 //
730 if (allFlowPaths.get(flowPath.flowId().value()) != null) {
731 //
732 // TODO: What to do if the Flow Path already exists?
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800733 // Fow now, we just re-add it.
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700734 //
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700735 }
736
737 switch (flowPath.flowPathType()) {
738 case FP_TYPE_SHORTEST_PATH:
739 //
740 // Reset the Data Path, in case it was set already, because
741 // we are going to recompute it anyway.
742 //
743 flowPath.flowEntries().clear();
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800744 shouldRecomputeFlowPaths.put(flowPath.flowId().value(),
745 flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700746 break;
747 case FP_TYPE_EXPLICIT_PATH:
748 //
749 // Mark all Flow Entries for installation in the switches.
750 //
751 for (FlowEntry flowEntry : flowPath.flowEntries()) {
752 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
753 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800754 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700755 break;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800756 case FP_TYPE_UNKNOWN:
757 log.error("FlowPath event with unknown type");
758 break;
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700759 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800760 allFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700761
762 break;
763 }
764
765 case ENTRY_REMOVE: {
766 //
767 // Remove an existing Flow Path.
768 //
769 // Find the Flow Path, and mark the Flow and its Flow Entries
770 // for deletion.
771 //
772 FlowPath existingFlowPath =
773 allFlowPaths.get(flowPath.flowId().value());
774 if (existingFlowPath == null)
775 continue; // Nothing to do
776
777 existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE);
778 for (FlowEntry flowEntry : existingFlowPath.flowEntries()) {
779 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
780 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
781 }
782
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800783 // Remove the Flow Path from the internal state
784 Long key = existingFlowPath.flowId().value();
785 allFlowPaths.remove(key);
786 shouldRecomputeFlowPaths.remove(key);
787 modifiedFlowPaths.put(key, existingFlowPath);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700788
789 break;
790 }
791 }
792 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800793 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700794
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800795 /**
796 * Process the Topology events.
797 */
798 private void processTopologyEvents() {
Pavlin Radoslavov1b454902014-01-09 16:28:27 -0800799 boolean isTopologyModified = false;
800
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800801 if (enableOnrc2014MeasurementsTopology) {
802 if (topologyEvents.isEmpty())
803 return;
Pavlin Radoslavov52119fa2014-01-09 13:37:52 -0800804
Pavlin Radoslavov2a8b9de2014-01-09 15:58:32 -0800805 // TODO: Code for debugging purpose only
806 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
807 TopologyElement topologyElement = eventEntry.eventData();
808 log.debug("Topology Event: {} {}", eventEntry.eventType(),
809 topologyElement.toString());
810 }
811
Pavlin Radoslavov1b454902014-01-09 16:28:27 -0800812 log.debug("[BEFORE] {}", topology.toString());
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800813 topology.readFromDatabase(dbHandler);
814 log.debug("[AFTER] {}", topology.toString());
815 shouldRecomputeFlowPaths.putAll(allFlowPaths);
816 return;
817 }
818
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700819 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800820 // Process all Topology events and update the appropriate state
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700821 //
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800822 for (EventEntry<TopologyElement> eventEntry : topologyEvents) {
823 TopologyElement topologyElement = eventEntry.eventData();
Naoki Shiota0abe38d2014-01-07 15:31:22 -0800824
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800825 log.debug("Topology Event: {} {}", eventEntry.eventType(),
826 topologyElement.toString());
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -0800827
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800828 switch (eventEntry.eventType()) {
829 case ENTRY_ADD:
Pavlin Radoslavovcc757162014-01-10 16:26:38 -0800830 isTopologyModified |= topology.addTopologyElement(topologyElement);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800831 break;
832 case ENTRY_REMOVE:
Naoki Shiota9f6fc212014-01-09 21:38:08 -0800833 isTopologyModified |= topology.removeTopologyElement(topologyElement);
Pavlin Radoslavov80bc3cf2014-01-08 11:17:30 -0800834 break;
835 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700836 }
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700837 if (isTopologyModified) {
838 // TODO: For now, if the topology changes, we recompute all Flows
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800839 shouldRecomputeFlowPaths.putAll(allFlowPaths);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -0700840 }
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800841 }
Pavlin Radoslavova23e5412013-10-27 19:56:40 -0700842
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800843 /**
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800844 * Process previously received Flow Entries with unmatched Flow Paths.
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800845 */
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800846 private void processUnmatchedFlowEntryAdd() {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800847 FlowPath flowPath;
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800848 FlowEntry localFlowEntry;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800849
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -0700850 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800851 // Update Flow Entries with previously unmatched Flow Entry updates
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700852 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800853 if (! unmatchedFlowEntryAdd.isEmpty()) {
854 Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>();
855 for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) {
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800856 // log.debug("Processing Unmatched Flow Entry: {}",
857 // flowEntry.toString());
858
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800859 flowPath = allFlowPaths.get(flowEntry.flowId().value());
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800860 if (flowPath == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800861 remainingUpdates.put(flowEntry.flowEntryId().value(),
862 flowEntry);
863 continue;
864 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800865 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
866 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800867 remainingUpdates.put(flowEntry.flowEntryId().value(),
868 flowEntry);
869 continue;
870 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800871 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
872 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
873 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700874 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800875 unmatchedFlowEntryAdd = remainingUpdates;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700876 }
Pavlin Radoslavov9bb40552013-12-18 21:08:30 -0800877 }
878
879 /**
880 * Process the Flow Entry events.
881 */
882 private void processFlowEntryEvents() {
883 FlowPath flowPath;
884 FlowEntry localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700885
886 //
Pavlin Radoslavov9f33edb2013-11-06 18:24:37 -0800887 // Process all Flow Entry events and update the appropriate state
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700888 //
889 for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) {
890 FlowEntry flowEntry = eventEntry.eventData();
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800891
892 log.debug("Flow Entry Event: {} {}", eventEntry.eventType(),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800893 flowEntry);
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800894
895 if ((! flowEntry.isValidFlowId()) ||
896 (! flowEntry.isValidFlowEntryId())) {
897 continue;
898 }
899
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700900 switch (eventEntry.eventType()) {
901 case ENTRY_ADD:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800902 flowPath = allFlowPaths.get(flowEntry.flowId().value());
903 if (flowPath == null) {
904 // Flow Path not found: keep the entry for later matching
905 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
906 flowEntry);
907 break;
908 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800909 localFlowEntry = findFlowEntryAdd(flowPath, flowEntry);
910 if (localFlowEntry == null) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800911 // Flow Entry not found: keep the entry for later matching
912 unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(),
913 flowEntry);
914 break;
915 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800916 if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) {
917 // Add the updated Flow Path to the list of updated paths
918 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
919 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700920 break;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800921
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700922 case ENTRY_REMOVE:
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800923 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
924 if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800925 continue; // Removed previously unmatched entry
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800926 }
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800927
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800928 flowPath = allFlowPaths.get(flowEntry.flowId().value());
929 if (flowPath == null) {
930 // Flow Path not found: ignore the update
931 break;
932 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800933 localFlowEntry = findFlowEntryRemove(flowPath, flowEntry);
934 if (localFlowEntry == null) {
Pavlin Radoslavov2fca8d12013-12-04 09:39:06 -0800935 // Flow Entry not found: ignore it
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800936 break;
937 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800938 if (updateFlowEntryRemove(flowPath, localFlowEntry,
939 flowEntry)) {
940 // Add the updated Flow Path to the list of updated paths
941 modifiedFlowPaths.put(flowPath.flowId().value(), flowPath);
942 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700943 break;
944 }
945 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700946 }
947
948 /**
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800949 * Find a Flow Entry that should be updated because of an external
950 * ENTRY_ADD event.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700951 *
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800952 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800953 * @param newFlowEntry the FlowEntry with the new state.
954 * @return the Flow Entry that should be updated if found, otherwise null.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700955 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800956 private FlowEntry findFlowEntryAdd(FlowPath flowPath,
957 FlowEntry newFlowEntry) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700958 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800959 // Iterate over all Flow Entries and find a match.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700960 //
961 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800962 if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry,
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800963 newFlowEntry)) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700964 continue;
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800965 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700966
967 //
968 // Local Flow Entry match found
969 //
970 if (localFlowEntry.isValidFlowEntryId()) {
971 if (localFlowEntry.flowEntryId().value() !=
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800972 newFlowEntry.flowEntryId().value()) {
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700973 //
974 // Find a local Flow Entry, but the Flow Entry ID doesn't
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800975 // match. Keep looking.
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700976 //
Pavlin Radoslavov63117172013-11-07 02:18:37 -0800977 continue;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700978 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700979 }
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800980 return localFlowEntry;
981 }
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -0700982
Pavlin Radoslavov237fde72013-12-17 22:21:06 -0800983 return null; // Entry not found
984 }
985
986 /**
987 * Update a Flow Entry because of an external ENTRY_ADD event.
988 *
989 * @param flowPath the FlowPath for the Flow Entry to update.
990 * @param localFlowEntry the local Flow Entry to update.
991 * @param newFlowEntry the FlowEntry with the new state.
992 * @return true if the local Flow Entry was updated, otherwise false.
993 */
994 private boolean updateFlowEntryAdd(FlowPath flowPath,
995 FlowEntry localFlowEntry,
996 FlowEntry newFlowEntry) {
997 boolean updated = false;
998
999 if (localFlowEntry.flowEntryUserState() ==
1000 FlowEntryUserState.FE_USER_DELETE) {
1001 // Don't add-back a Flow Entry that is already deleted
1002 return false;
1003 }
1004
1005 if (! localFlowEntry.isValidFlowEntryId()) {
1006 // Update the Flow Entry ID
1007 FlowEntryId flowEntryId =
1008 new FlowEntryId(newFlowEntry.flowEntryId().value());
1009 localFlowEntry.setFlowEntryId(flowEntryId);
1010 updated = true;
1011 }
1012
1013 //
1014 // Update the local Flow Entry, and keep state to check
1015 // if the Flow Path has been installed.
1016 //
1017 if (localFlowEntry.flowEntryUserState() !=
1018 newFlowEntry.flowEntryUserState()) {
1019 localFlowEntry.setFlowEntryUserState(
1020 newFlowEntry.flowEntryUserState());
1021 updated = true;
1022 }
1023 if (localFlowEntry.flowEntrySwitchState() !=
1024 newFlowEntry.flowEntrySwitchState()) {
1025 localFlowEntry.setFlowEntrySwitchState(
1026 newFlowEntry.flowEntrySwitchState());
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -08001027 checkIfInstalledFlowPaths.put(flowPath.flowId().value(), flowPath);
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001028 updated = true;
1029 }
1030
1031 return updated;
1032 }
1033
1034 /**
1035 * Find a Flow Entry that should be updated because of an external
1036 * ENTRY_REMOVE event.
1037 *
1038 * @param flowPath the FlowPath for the Flow Entry to update.
1039 * @param newFlowEntry the FlowEntry with the new state.
1040 * @return the Flow Entry that should be updated if found, otherwise null.
1041 */
1042 private FlowEntry findFlowEntryRemove(FlowPath flowPath,
1043 FlowEntry newFlowEntry) {
1044 //
1045 // Iterate over all Flow Entries and find a match based on
1046 // the Flow Entry ID.
1047 //
1048 for (FlowEntry localFlowEntry : flowPath.flowEntries()) {
1049 if (! localFlowEntry.isValidFlowEntryId())
1050 continue;
1051 if (localFlowEntry.flowEntryId().value() !=
1052 newFlowEntry.flowEntryId().value()) {
1053 continue;
1054 }
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001055 return localFlowEntry;
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001056 }
1057
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001058 return null; // Entry not found
1059 }
1060
1061 /**
1062 * Update a Flow Entry because of an external ENTRY_REMOVE event.
1063 *
1064 * @param flowPath the FlowPath for the Flow Entry to update.
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001065 * @param localFlowEntry the local Flow Entry to update.
1066 * @param newFlowEntry the FlowEntry with the new state.
1067 * @return true if the local Flow Entry was updated, otherwise false.
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001068 */
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001069 private boolean updateFlowEntryRemove(FlowPath flowPath,
1070 FlowEntry localFlowEntry,
1071 FlowEntry newFlowEntry) {
1072 boolean updated = false;
1073
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001074 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001075 // Update the local Flow Entry.
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001076 //
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001077 if (localFlowEntry.flowEntryUserState() !=
1078 newFlowEntry.flowEntryUserState()) {
1079 localFlowEntry.setFlowEntryUserState(
1080 newFlowEntry.flowEntryUserState());
1081 updated = true;
1082 }
1083 if (localFlowEntry.flowEntrySwitchState() !=
1084 newFlowEntry.flowEntrySwitchState()) {
1085 localFlowEntry.setFlowEntrySwitchState(
1086 newFlowEntry.flowEntrySwitchState());
1087 updated = true;
Pavlin Radoslavov63117172013-11-07 02:18:37 -08001088 }
1089
Pavlin Radoslavov237fde72013-12-17 22:21:06 -08001090 return updated;
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001091 }
1092
1093 /**
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001094 * Recompute a Flow Path.
1095 *
1096 * @param flowPath the Flow Path to recompute.
1097 * @return true if the recomputed Flow Path has changed, otherwise false.
1098 */
1099 private boolean recomputeFlowPath(FlowPath flowPath) {
1100 boolean hasChanged = false;
1101
Pavlin Radoslavov7bf837a2014-01-09 14:22:05 -08001102 if (enableOnrc2014MeasurementsFlows) {
1103 // Cleanup the deleted Flow Entries from the earlier iteration
1104 flowPath.dataPath().removeDeletedFlowEntries();
Pavlin Radoslavov737aa522014-01-09 15:35:00 -08001105
1106 //
1107 // TODO: Fake it that the Flow Entries have been already pushed
1108 // into the switches, so we don't push them again.
1109 //
1110 for (FlowEntry flowEntry : flowPath.flowEntries()) {
1111 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED);
1112 }
Pavlin Radoslavov7bf837a2014-01-09 14:22:05 -08001113 }
1114
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001115 //
1116 // Test whether the Flow Path needs to be recomputed
1117 //
1118 switch (flowPath.flowPathType()) {
Yuta HIGUCHIe1038fb2013-10-30 15:35:18 -07001119 case FP_TYPE_UNKNOWN:
1120 return false; // Can't recompute on Unknown FlowType
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001121 case FP_TYPE_SHORTEST_PATH:
1122 break;
1123 case FP_TYPE_EXPLICIT_PATH:
1124 return false; // An explicit path never changes
1125 }
1126
1127 DataPath oldDataPath = flowPath.dataPath();
1128
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001129 // Compute the new path
Naoki Shiota9f6fc212014-01-09 21:38:08 -08001130 DataPath newDataPath = TopologyManager.computeNetworkPath(topology,
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001131 flowPath);
Naoki Shiotaf74d5f32014-01-09 21:29:38 -08001132
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001133 if (newDataPath == null) {
1134 // We need the DataPath to compare the paths
1135 newDataPath = new DataPath();
1136 }
1137 newDataPath.applyFlowPathFlags(flowPath.flowPathFlags());
1138
1139 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001140 // Test whether the new path is same
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001141 //
1142 if (oldDataPath.flowEntries().size() !=
1143 newDataPath.flowEntries().size()) {
1144 hasChanged = true;
1145 } else {
1146 Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator();
1147 Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator();
1148 while (oldIter.hasNext() && newIter.hasNext()) {
1149 FlowEntry oldFlowEntry = oldIter.next();
1150 FlowEntry newFlowEntry = newIter.next();
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001151 if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
1152 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001153 hasChanged = true;
1154 break;
1155 }
1156 }
1157 }
1158 if (! hasChanged)
1159 return hasChanged;
1160
1161 //
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001162 // Merge the changes in the path:
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001163 // - If a Flow Entry for a switch is in the old data path, but not
1164 // in the new data path, then mark it for deletion.
1165 // - If a Flow Entry for a switch is in the new data path, but not
1166 // in the old data path, then mark it for addition.
1167 // - If a Flow Entry for a switch is in both the old and the new
1168 // data path, but it has changed, e.g., the incoming and/or outgoing
1169 // port(s), then mark the old Flow Entry for deletion, and mark
1170 // the new Flow Entry for addition.
1171 // - If a Flow Entry for a switch is in both the old and the new
1172 // data path, and it hasn't changed, then just keep it.
1173 //
1174 // NOTE: We use the Switch DPID of each entry to match the entries
1175 //
1176 Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>();
1177 Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>();
1178 ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>();
1179 List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>();
1180
1181 // Prepare maps with the Flow Entries, so they are fast to lookup
1182 for (FlowEntry flowEntry : oldDataPath.flowEntries())
1183 oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
1184 for (FlowEntry flowEntry : newDataPath.flowEntries())
1185 newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry);
1186
1187 //
1188 // Find the old Flow Entries that should be deleted
1189 //
1190 for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) {
1191 FlowEntry newFlowEntry =
1192 newFlowEntriesMap.get(oldFlowEntry.dpid().value());
1193 if (newFlowEntry == null) {
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001194 // The old Flow Entry should be deleted: not on the path
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001195 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
1196 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1197 deletedFlowEntries.add(oldFlowEntry);
1198 }
1199 }
1200
1201 //
1202 // Find the new Flow Entries that should be added or updated
1203 //
1204 int idx = 0;
1205 for (FlowEntry newFlowEntry : newDataPath.flowEntries()) {
1206 FlowEntry oldFlowEntry =
1207 oldFlowEntriesMap.get(newFlowEntry.dpid().value());
1208
1209 if ((oldFlowEntry != null) &&
Pavlin Radoslavov3ecd41e2013-10-29 14:29:30 -07001210 TopologyManager.isSameFlowEntryDataPath(oldFlowEntry,
1211 newFlowEntry)) {
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001212 //
1213 // Both Flow Entries are same
1214 //
1215 finalFlowEntries.add(oldFlowEntry);
1216 idx++;
1217 continue;
1218 }
1219
1220 if (oldFlowEntry != null) {
1221 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001222 // The old Flow Entry should be deleted: path diverges
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001223 //
1224 oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
1225 oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1226 deletedFlowEntries.add(oldFlowEntry);
1227 }
1228
1229 //
1230 // Add the new Flow Entry
1231 //
Pavlin Radoslavov1c24f222013-10-30 13:56:46 -07001232 //
1233 // NOTE: Assign only the Flow ID.
1234 // The Flow Entry ID is assigned later only for the Flow Entries
1235 // this instance is responsible for.
1236 //
1237 newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value()));
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001238
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001239 //
Pavlin Radoslavov6fde2172013-12-10 11:23:18 -08001240 // Copy the Flow timeouts
1241 //
1242 newFlowEntry.setIdleTimeout(flowPath.idleTimeout());
1243 newFlowEntry.setHardTimeout(flowPath.hardTimeout());
1244
1245 //
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001246 // Allocate the FlowEntryMatch by copying the default one
1247 // from the FlowPath (if set).
1248 //
1249 FlowEntryMatch flowEntryMatch = null;
1250 if (flowPath.flowEntryMatch() != null)
1251 flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch());
1252 else
1253 flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001254 newFlowEntry.setFlowEntryMatch(flowEntryMatch);
Pavlin Radoslavova4df9522013-12-03 11:03:04 -08001255
1256 // Set the incoming port matching
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001257 flowEntryMatch.enableInPort(newFlowEntry.inPort());
1258
1259 //
1260 // Set the actions:
1261 // If the first Flow Entry, copy the Flow Path actions to it.
1262 //
1263 FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions();
1264 if ((idx == 0) && (flowPath.flowEntryActions() != null)) {
1265 FlowEntryActions flowActions =
1266 new FlowEntryActions(flowPath.flowEntryActions());
1267 for (FlowEntryAction action : flowActions.actions())
1268 flowEntryActions.addAction(action);
1269 }
1270 idx++;
1271
1272 //
1273 // Add the outgoing port output action
1274 //
1275 FlowEntryAction flowEntryAction = new FlowEntryAction();
1276 flowEntryAction.setActionOutput(newFlowEntry.outPort());
1277 flowEntryActions.addAction(flowEntryAction);
1278
1279 //
1280 // Set the state of the new Flow Entry
1281 //
1282 newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
1283 newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED);
1284 finalFlowEntries.add(newFlowEntry);
1285 }
1286
1287 //
1288 // Replace the old Flow Entries with the new Flow Entries.
1289 // Note that the Flow Entries that will be deleted are added at
1290 // the end.
1291 //
Pavlin Radoslavovfb94edc2013-11-04 16:29:40 -08001292 finalFlowEntries.addAll(deletedFlowEntries);
Pavlin Radoslavovfb06a9e2013-10-28 23:56:15 -07001293 flowPath.dataPath().setFlowEntries(finalFlowEntries);
1294
1295 return hasChanged;
1296 }
1297
1298 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001299 * Receive a notification that a Flow is added.
1300 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001301 * @param flowPath the Flow that is added.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001302 */
1303 @Override
1304 public void notificationRecvFlowAdded(FlowPath flowPath) {
1305 EventEntry<FlowPath> eventEntry =
1306 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
1307 networkEvents.add(eventEntry);
1308 }
1309
1310 /**
1311 * Receive a notification that a Flow is removed.
1312 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001313 * @param flowPath the Flow that is removed.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001314 */
1315 @Override
1316 public void notificationRecvFlowRemoved(FlowPath flowPath) {
1317 EventEntry<FlowPath> eventEntry =
1318 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath);
1319 networkEvents.add(eventEntry);
1320 }
1321
1322 /**
1323 * Receive a notification that a Flow is updated.
1324 *
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001325 * @param flowPath the Flow that is updated.
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001326 */
1327 @Override
1328 public void notificationRecvFlowUpdated(FlowPath flowPath) {
1329 // NOTE: The ADD and UPDATE events are processed in same way
1330 EventEntry<FlowPath> eventEntry =
1331 new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath);
1332 networkEvents.add(eventEntry);
1333 }
1334
1335 /**
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001336 * Receive a notification that a FlowEntry is added.
1337 *
1338 * @param flowEntry the FlowEntry that is added.
1339 */
1340 @Override
1341 public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) {
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001342 if (enableOnrc2014MeasurementsFlows) {
1343 Collection entries = new ArrayList();
1344 entries.add(flowEntry);
1345 flowManager.pushModifiedFlowEntriesToSwitches(entries);
1346 return;
1347 }
1348
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001349 EventEntry<FlowEntry> eventEntry =
1350 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
1351 networkEvents.add(eventEntry);
1352 }
1353
1354 /**
1355 * Receive a notification that a FlowEntry is removed.
1356 *
1357 * @param flowEntry the FlowEntry that is removed.
1358 */
1359 @Override
1360 public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) {
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001361 if (enableOnrc2014MeasurementsFlows) {
Pavlin Radoslavove4d2a432014-01-10 12:01:08 -08001362 //
1363 // NOTE: Must update the state to DELETE, because
1364 // the notification contains the original state.
1365 //
1366 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
1367
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001368 Collection entries = new ArrayList();
1369 entries.add(flowEntry);
1370 flowManager.pushModifiedFlowEntriesToSwitches(entries);
1371 return;
1372 }
1373
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001374 EventEntry<FlowEntry> eventEntry =
1375 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry);
1376 networkEvents.add(eventEntry);
1377 }
1378
1379 /**
1380 * Receive a notification that a FlowEntry is updated.
1381 *
1382 * @param flowEntry the FlowEntry that is updated.
1383 */
1384 @Override
1385 public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) {
Pavlin Radoslavov7847db72014-01-10 11:35:21 -08001386 if (enableOnrc2014MeasurementsFlows) {
1387 Collection entries = new ArrayList();
1388 entries.add(flowEntry);
1389 flowManager.pushModifiedFlowEntriesToSwitches(entries);
1390 return;
1391 }
1392
Pavlin Radoslavovb7506842013-10-29 17:46:54 -07001393 // NOTE: The ADD and UPDATE events are processed in same way
1394 EventEntry<FlowEntry> eventEntry =
1395 new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry);
1396 networkEvents.add(eventEntry);
1397 }
1398
1399 /**
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001400 * Receive a notification that a FlowId is added.
1401 *
1402 * @param flowId the FlowId that is added.
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001403 * @param dpid the Source Switch Dpid for the corresponding Flow.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001404 */
1405 @Override
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001406 public void notificationRecvFlowIdAdded(FlowId flowId, Dpid dpid) {
1407 Pair flowIdPair = new Pair(flowId, dpid);
1408
1409 EventEntry<Pair<FlowId, Dpid>> eventEntry =
1410 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowIdPair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001411 networkEvents.add(eventEntry);
1412 }
1413
1414 /**
1415 * Receive a notification that a FlowId is removed.
1416 *
1417 * @param flowId the FlowId that is removed.
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001418 * @param dpid the Source Switch Dpid for the corresponding Flow.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001419 */
1420 @Override
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001421 public void notificationRecvFlowIdRemoved(FlowId flowId, Dpid dpid) {
1422 Pair flowIdPair = new Pair(flowId, dpid);
1423
1424 EventEntry<Pair<FlowId, Dpid>> eventEntry =
1425 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_REMOVE, flowIdPair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001426 networkEvents.add(eventEntry);
1427 }
1428
1429 /**
1430 * Receive a notification that a FlowId is updated.
1431 *
1432 * @param flowId the FlowId that is updated.
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001433 * @param dpid the Source Switch Dpid for the corresponding Flow.
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001434 */
1435 @Override
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001436 public void notificationRecvFlowIdUpdated(FlowId flowId, Dpid dpid) {
1437 Pair flowIdPair = new Pair(flowId, dpid);
1438
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001439 // NOTE: The ADD and UPDATE events are processed in same way
Pavlin Radoslavov2194d112014-01-10 13:36:00 -08001440 EventEntry<Pair<FlowId, Dpid>> eventEntry =
1441 new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowIdPair);
Pavlin Radoslavov2004fa02014-01-07 14:46:42 -08001442 networkEvents.add(eventEntry);
1443 }
1444
1445 /**
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001446 * Receive a notification that a FlowEntryId is added.
1447 *
1448 * @param flowEntryId the FlowEntryId that is added.
1449 * @param dpid the Switch Dpid for the corresponding Flow Entry.
1450 */
1451 @Override
1452 public void notificationRecvFlowEntryIdAdded(FlowEntryId flowEntryId,
1453 Dpid dpid) {
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001454 Pair flowEntryIdPair = new Pair(flowEntryId, dpid);
1455
1456 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
1457 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowEntryIdPair);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001458 networkEvents.add(eventEntry);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001459 }
1460
1461 /**
1462 * Receive a notification that a FlowEntryId is removed.
1463 *
1464 * @param flowEntryId the FlowEntryId that is removed.
1465 * @param dpid the Switch Dpid for the corresponding Flow Entry.
1466 */
1467 @Override
1468 public void notificationRecvFlowEntryIdRemoved(FlowEntryId flowEntryId,
1469 Dpid dpid) {
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001470 Pair flowEntryIdPair = new Pair(flowEntryId, dpid);
1471
1472 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
1473 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_REMOVE, flowEntryIdPair);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001474 networkEvents.add(eventEntry);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001475 }
1476
1477 /**
1478 * Receive a notification that a FlowEntryId is updated.
1479 *
1480 * @param flowEntryId the FlowEntryId that is updated.
1481 * @param dpid the Switch Dpid for the corresponding Flow Entry.
1482 */
1483 @Override
1484 public void notificationRecvFlowEntryIdUpdated(FlowEntryId flowEntryId,
1485 Dpid dpid) {
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001486 Pair flowEntryIdPair = new Pair(flowEntryId, dpid);
1487
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001488 // NOTE: The ADD and UPDATE events are processed in same way
Pavlin Radoslavova9c0c3b2014-01-09 10:54:45 -08001489 EventEntry<Pair<FlowEntryId, Dpid>> eventEntry =
1490 new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowEntryIdPair);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001491 networkEvents.add(eventEntry);
Pavlin Radoslavov909da3c2014-01-09 04:04:33 -08001492 }
1493
1494 /**
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001495 * Receive a notification that a Topology Element is added.
1496 *
1497 * @param topologyElement the Topology Element that is added.
1498 */
1499 @Override
1500 public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) {
1501 EventEntry<TopologyElement> eventEntry =
1502 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1503 networkEvents.add(eventEntry);
1504 }
1505
1506 /**
1507 * Receive a notification that a Topology Element is removed.
1508 *
1509 * @param topologyElement the Topology Element that is removed.
1510 */
1511 @Override
1512 public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) {
1513 EventEntry<TopologyElement> eventEntry =
1514 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement);
1515 networkEvents.add(eventEntry);
1516 }
1517
1518 /**
1519 * Receive a notification that a Topology Element is updated.
1520 *
1521 * @param topologyElement the Topology Element that is updated.
1522 */
1523 @Override
1524 public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) {
1525 // NOTE: The ADD and UPDATE events are processed in same way
1526 EventEntry<TopologyElement> eventEntry =
1527 new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement);
1528 networkEvents.add(eventEntry);
1529 }
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001530
1531 /**
Pavlin Radoslavovcc757162014-01-10 16:26:38 -08001532 * Receive a notification that a switch is added to this instance.
1533 *
1534 * @param sw the switch that is added.
1535 */
1536 @Override
1537 public void addedSwitch(IOFSwitch sw) {
1538 Dpid dpid = new Dpid(sw.getId());
1539 EventEntry<Dpid> eventEntry =
1540 new EventEntry<Dpid>(EventEntry.Type.ENTRY_ADD, dpid);
1541 networkEvents.add(eventEntry);
1542 }
1543
1544 /**
1545 * Receive a notification that a switch is removed from this instance.
1546 *
1547 * @param sw the switch that is removed.
1548 */
1549 @Override
1550 public void removedSwitch(IOFSwitch sw) {
1551 Dpid dpid = new Dpid(sw.getId());
1552 EventEntry<Dpid> eventEntry =
1553 new EventEntry<Dpid>(EventEntry.Type.ENTRY_REMOVE, dpid);
1554 networkEvents.add(eventEntry);
1555 }
1556
1557 /**
1558 * Receive a notification that the ports on a switch have changed.
1559 */
1560 @Override
1561 public void switchPortChanged(Long switchId) {
1562 // Nothing to do
1563 }
1564
1565 /**
Pavlin Radoslavov53219802013-12-06 11:02:04 -08001566 * Get a sorted copy of all Flow Paths.
1567 *
1568 * @return a sorted copy of all Flow Paths.
1569 */
1570 synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() {
1571 SortedMap<Long, FlowPath> sortedFlowPaths =
1572 new TreeMap<Long, FlowPath>();
1573
1574 //
1575 // TODO: For now we use serialization/deserialization to create
1576 // a copy of each Flow Path. In the future, we should use proper
1577 // copy constructors.
1578 //
1579 Kryo kryo = kryoFactory.newKryo();
1580 synchronized (allFlowPaths) {
1581 for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) {
1582 FlowPath origFlowPath = entry.getValue();
1583 FlowPath copyFlowPath = kryo.copy(origFlowPath);
1584 sortedFlowPaths.put(entry.getKey(), copyFlowPath);
1585 }
1586 }
1587 kryoFactory.deleteKryo(kryo);
1588
1589 return sortedFlowPaths;
1590 }
Pavlin Radoslavov6b79f2b2013-10-26 21:31:10 -07001591}