Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 2 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 4 | import java.util.Collection; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
| 6 | import java.util.Iterator; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 7 | import java.util.LinkedList; |
| 8 | import java.util.List; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 9 | import java.util.Map; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 10 | import java.util.concurrent.BlockingQueue; |
| 11 | import java.util.concurrent.LinkedBlockingQueue; |
| 12 | |
| 13 | import net.onrc.onos.datagrid.IDatagridService; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 14 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 15 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 16 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 17 | import net.onrc.onos.ofcontroller.util.DataPath; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.util.EventEntry; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 19 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 20 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 21 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 24 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 25 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.util.FlowId; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.util.FlowPath; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 29 | |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | /** |
Yuta HIGUCHI | e1038fb | 2013-10-30 15:35:18 -0700 | [diff] [blame] | 34 | * Class for FlowPath Maintenance. |
| 35 | * This class listens for FlowEvents to: |
| 36 | * - Maintain a local cache of the Network Topology. |
| 37 | * - Detect FlowPaths impacted by Topology change. |
| 38 | * - Recompute impacted FlowPath using cached Topology. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 39 | */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 40 | class FlowEventHandler extends Thread implements IFlowEventHandlerService { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 41 | /** The logger. */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 42 | private final static Logger log = LoggerFactory.getLogger(FlowEventHandler.class); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 43 | |
| 44 | private FlowManager flowManager; // The Flow Manager to use |
| 45 | private IDatagridService datagridService; // The Datagrid Service to use |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 46 | private Topology topology; // The network topology |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 47 | private Map<Long, FlowPath> allFlowPaths = new HashMap<Long, FlowPath>(); |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 48 | private List<FlowEntry> unmatchedFlowEntryUpdates = |
| 49 | new LinkedList<FlowEntry>(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 50 | |
| 51 | // The queue with Flow Path and Topology Element updates |
| 52 | private BlockingQueue<EventEntry<?>> networkEvents = |
| 53 | new LinkedBlockingQueue<EventEntry<?>>(); |
| 54 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 55 | // The pending Topology, FlowPath, and FlowEntry events |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 56 | private List<EventEntry<TopologyElement>> topologyEvents = |
| 57 | new LinkedList<EventEntry<TopologyElement>>(); |
| 58 | private List<EventEntry<FlowPath>> flowPathEvents = |
| 59 | new LinkedList<EventEntry<FlowPath>>(); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 60 | private List<EventEntry<FlowEntry>> flowEntryEvents = |
| 61 | new LinkedList<EventEntry<FlowEntry>>(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Constructor for a given Flow Manager and Datagrid Service. |
| 65 | * |
| 66 | * @param flowManager the Flow Manager to use. |
| 67 | * @param datagridService the Datagrid Service to use. |
| 68 | */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 69 | FlowEventHandler(FlowManager flowManager, |
| 70 | IDatagridService datagridService) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 71 | this.flowManager = flowManager; |
| 72 | this.datagridService = datagridService; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 73 | this.topology = new Topology(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame^] | 77 | * Get the network topology. |
| 78 | * |
| 79 | * @return the network topology. |
| 80 | */ |
| 81 | protected Topology getTopology() { return this.topology; } |
| 82 | |
| 83 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 84 | * Run the thread. |
| 85 | */ |
| 86 | @Override |
| 87 | public void run() { |
| 88 | // |
| 89 | // Obtain the initial Topology state |
| 90 | // |
| 91 | Collection<TopologyElement> topologyElements = |
| 92 | datagridService.getAllTopologyElements(); |
| 93 | for (TopologyElement topologyElement : topologyElements) { |
| 94 | EventEntry<TopologyElement> eventEntry = |
| 95 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 96 | topologyEvents.add(eventEntry); |
| 97 | } |
| 98 | // |
| 99 | // Obtain the initial Flow Path state |
| 100 | // |
| 101 | Collection<FlowPath> flowPaths = datagridService.getAllFlows(); |
| 102 | for (FlowPath flowPath : flowPaths) { |
| 103 | EventEntry<FlowPath> eventEntry = |
| 104 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 105 | flowPathEvents.add(eventEntry); |
| 106 | } |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 107 | // |
| 108 | // Obtain the initial FlowEntry state |
| 109 | // |
| 110 | Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries(); |
| 111 | for (FlowEntry flowEntry : flowEntries) { |
| 112 | EventEntry<FlowEntry> eventEntry = |
| 113 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 114 | flowEntryEvents.add(eventEntry); |
| 115 | } |
| 116 | |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 117 | // Process the initial events (if any) |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 118 | processEvents(); |
| 119 | |
| 120 | // |
| 121 | // The main loop |
| 122 | // |
| 123 | Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>(); |
| 124 | try { |
| 125 | while (true) { |
| 126 | EventEntry<?> eventEntry = networkEvents.take(); |
| 127 | collection.add(eventEntry); |
| 128 | networkEvents.drainTo(collection); |
| 129 | |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 130 | // |
| 131 | // Demultiplex all events: |
| 132 | // - EventEntry<TopologyElement> |
| 133 | // - EventEntry<FlowPath> |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 134 | // - EventEntry<FlowEntry> |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 135 | // |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 136 | for (EventEntry<?> event : collection) { |
| 137 | if (event.eventData() instanceof TopologyElement) { |
| 138 | EventEntry<TopologyElement> topologyEventEntry = |
| 139 | (EventEntry<TopologyElement>)event; |
| 140 | topologyEvents.add(topologyEventEntry); |
| 141 | } else if (event.eventData() instanceof FlowPath) { |
| 142 | EventEntry<FlowPath> flowPathEventEntry = |
| 143 | (EventEntry<FlowPath>)event; |
| 144 | flowPathEvents.add(flowPathEventEntry); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 145 | } else if (event.eventData() instanceof FlowEntry) { |
| 146 | EventEntry<FlowEntry> flowEntryEventEntry = |
| 147 | (EventEntry<FlowEntry>)event; |
| 148 | flowEntryEvents.add(flowEntryEventEntry); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | collection.clear(); |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 152 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 153 | // Process the events (if any) |
| 154 | processEvents(); |
| 155 | } |
| 156 | } catch (Exception exception) { |
| 157 | log.debug("Exception processing Network Events: ", exception); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Process the events (if any) |
| 163 | */ |
| 164 | private void processEvents() { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 165 | List<FlowPath> newFlowPaths = new LinkedList<FlowPath>(); |
| 166 | List<FlowPath> recomputeFlowPaths = new LinkedList<FlowPath>(); |
| 167 | List<FlowPath> modifiedFlowPaths = new LinkedList<FlowPath>(); |
| 168 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 169 | if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() && |
| 170 | flowEntryEvents.isEmpty()) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 171 | return; // Nothing to do |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 172 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 173 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 174 | // |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 175 | // Process the Flow Path events |
| 176 | // |
| 177 | for (EventEntry<FlowPath> eventEntry : flowPathEvents) { |
| 178 | FlowPath flowPath = eventEntry.eventData(); |
| 179 | |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 180 | log.debug("Flow Event: {} {}", eventEntry.eventType(), |
| 181 | flowPath.toString()); |
| 182 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 183 | switch (eventEntry.eventType()) { |
| 184 | case ENTRY_ADD: { |
| 185 | // |
| 186 | // Add a new Flow Path |
| 187 | // |
| 188 | if (allFlowPaths.get(flowPath.flowId().value()) != null) { |
| 189 | // |
| 190 | // TODO: What to do if the Flow Path already exists? |
| 191 | // Remove and then re-add it, or merge the info? |
| 192 | // For now, we don't have to do anything. |
| 193 | // |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | switch (flowPath.flowPathType()) { |
| 198 | case FP_TYPE_SHORTEST_PATH: |
| 199 | // |
| 200 | // Reset the Data Path, in case it was set already, because |
| 201 | // we are going to recompute it anyway. |
| 202 | // |
| 203 | flowPath.flowEntries().clear(); |
| 204 | recomputeFlowPaths.add(flowPath); |
| 205 | break; |
| 206 | case FP_TYPE_EXPLICIT_PATH: |
| 207 | // |
| 208 | // Mark all Flow Entries for installation in the switches. |
| 209 | // |
| 210 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 211 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 212 | } |
| 213 | modifiedFlowPaths.add(flowPath); |
| 214 | break; |
| 215 | } |
| 216 | newFlowPaths.add(flowPath); |
| 217 | |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | case ENTRY_REMOVE: { |
| 222 | // |
| 223 | // Remove an existing Flow Path. |
| 224 | // |
| 225 | // Find the Flow Path, and mark the Flow and its Flow Entries |
| 226 | // for deletion. |
| 227 | // |
| 228 | FlowPath existingFlowPath = |
| 229 | allFlowPaths.get(flowPath.flowId().value()); |
| 230 | if (existingFlowPath == null) |
| 231 | continue; // Nothing to do |
| 232 | |
| 233 | existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE); |
| 234 | for (FlowEntry flowEntry : existingFlowPath.flowEntries()) { |
| 235 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 236 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 237 | } |
| 238 | |
Yuta HIGUCHI | 2b5d071 | 2013-10-30 15:48:13 -0700 | [diff] [blame] | 239 | allFlowPaths.remove(existingFlowPath.flowId().value()); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 240 | modifiedFlowPaths.add(existingFlowPath); |
| 241 | |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // |
| 248 | // Process the topology events |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 249 | // |
| 250 | boolean isTopologyModified = false; |
| 251 | for (EventEntry<TopologyElement> eventEntry : topologyEvents) { |
| 252 | TopologyElement topologyElement = eventEntry.eventData(); |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 253 | |
| 254 | log.debug("Topology Event: {} {}", eventEntry.eventType(), |
| 255 | topologyElement.toString()); |
| 256 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 257 | switch (eventEntry.eventType()) { |
| 258 | case ENTRY_ADD: |
Yuta HIGUCHI | b32f77f | 2013-10-30 15:36:32 -0700 | [diff] [blame] | 259 | isTopologyModified |= topology.addTopologyElement(topologyElement); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 260 | break; |
| 261 | case ENTRY_REMOVE: |
Yuta HIGUCHI | b32f77f | 2013-10-30 15:36:32 -0700 | [diff] [blame] | 262 | isTopologyModified |= topology.removeTopologyElement(topologyElement); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | } |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 266 | if (isTopologyModified) { |
| 267 | // TODO: For now, if the topology changes, we recompute all Flows |
| 268 | recomputeFlowPaths.addAll(allFlowPaths.values()); |
| 269 | } |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 270 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 271 | // Add all new Flows |
| 272 | for (FlowPath flowPath : newFlowPaths) { |
| 273 | allFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 274 | } |
| 275 | |
| 276 | // Recompute all affected Flow Paths and keep only the modified |
| 277 | for (FlowPath flowPath : recomputeFlowPaths) { |
| 278 | if (recomputeFlowPath(flowPath)) |
| 279 | modifiedFlowPaths.add(flowPath); |
| 280 | } |
| 281 | |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 282 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 283 | // Process previously unmatched Flow Entry updates |
| 284 | // |
| 285 | if ((! flowPathEvents.isEmpty()) && (! unmatchedFlowEntryUpdates.isEmpty())) { |
| 286 | List<FlowEntry> remainingUpdates = new LinkedList<FlowEntry>(); |
| 287 | for (FlowEntry flowEntry : unmatchedFlowEntryUpdates) { |
| 288 | if (! updateFlowEntry(flowEntry)) |
| 289 | remainingUpdates.add(flowEntry); |
| 290 | } |
| 291 | unmatchedFlowEntryUpdates = remainingUpdates; |
| 292 | } |
| 293 | |
| 294 | // |
| 295 | // Process the Flow Entry events |
| 296 | // |
| 297 | for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) { |
| 298 | FlowEntry flowEntry = eventEntry.eventData(); |
| 299 | switch (eventEntry.eventType()) { |
| 300 | case ENTRY_ADD: |
| 301 | // |
| 302 | // Find the corresponding Flow Entry and update it. |
| 303 | // If not found, then keep it in a local cache for |
| 304 | // later matching. |
| 305 | // |
| 306 | if (! updateFlowEntry(flowEntry)) |
| 307 | unmatchedFlowEntryUpdates.add(flowEntry); |
| 308 | break; |
| 309 | case ENTRY_REMOVE: |
| 310 | // |
| 311 | // NOTE: For now we remove the Flow Entries based on |
| 312 | // local decisions, so no need to remove them because of an |
| 313 | // external event. |
| 314 | // |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 320 | // Push the Flow Entries that have been modified |
| 321 | // |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 322 | flowManager.pushModifiedFlowEntries(modifiedFlowPaths); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 323 | |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 324 | // Cleanup |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 325 | topologyEvents.clear(); |
| 326 | flowPathEvents.clear(); |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 327 | flowEntryEvents.clear(); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Update a Flow Entry because of an external event. |
| 332 | * |
| 333 | * @param flowEntry the FlowEntry with the new state. |
| 334 | * @return true if the Flow Entry was found and updated, otherwise false. |
| 335 | */ |
| 336 | private boolean updateFlowEntry(FlowEntry flowEntry) { |
| 337 | if ((! flowEntry.isValidFlowId()) || |
| 338 | (! flowEntry.isValidFlowEntryId())) { |
| 339 | // |
| 340 | // Ignore events for Flow Entries with invalid Flow ID or |
| 341 | // Flow Entry ID. |
| 342 | // This shouldn't happen. |
| 343 | // |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | FlowPath flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
| 348 | if (flowPath == null) |
| 349 | return false; |
| 350 | |
| 351 | // |
| 352 | // Iterate over all Flow Entries and find a match based on the DPID |
| 353 | // |
| 354 | for (FlowEntry localFlowEntry : flowPath.flowEntries()) { |
| 355 | if (localFlowEntry.dpid().value() != flowEntry.dpid().value()) |
| 356 | continue; |
| 357 | // |
| 358 | // TODO: We might want to check the FlowEntryMatch and |
| 359 | // FlowEntryActions to double-check it is the same Flow Entry |
| 360 | // |
| 361 | |
| 362 | // |
| 363 | // Local Flow Entry match found |
| 364 | // |
| 365 | if (localFlowEntry.isValidFlowEntryId()) { |
| 366 | if (localFlowEntry.flowEntryId().value() != |
| 367 | flowEntry.flowEntryId().value()) { |
| 368 | // |
| 369 | // Find a local Flow Entry, but the Flow Entry ID doesn't |
| 370 | // match. Ignore the event. |
| 371 | // |
| 372 | return true; |
| 373 | } |
| 374 | } else { |
| 375 | // Update the Flow Entry ID |
| 376 | FlowEntryId flowEntryId = |
| 377 | new FlowEntryId(flowEntry.flowEntryId().value()); |
| 378 | localFlowEntry.setFlowEntryId(flowEntryId); |
| 379 | } |
| 380 | |
| 381 | // |
| 382 | // Update the local Flow Entry. |
| 383 | // For now we update only the Flow Entry Switch State |
| 384 | // |
| 385 | localFlowEntry.setFlowEntrySwitchState(flowEntry.flowEntrySwitchState()); |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | return false; // Entry not found |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /** |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 393 | * Recompute a Flow Path. |
| 394 | * |
| 395 | * @param flowPath the Flow Path to recompute. |
| 396 | * @return true if the recomputed Flow Path has changed, otherwise false. |
| 397 | */ |
| 398 | private boolean recomputeFlowPath(FlowPath flowPath) { |
| 399 | boolean hasChanged = false; |
| 400 | |
| 401 | // |
| 402 | // Test whether the Flow Path needs to be recomputed |
| 403 | // |
| 404 | switch (flowPath.flowPathType()) { |
Yuta HIGUCHI | e1038fb | 2013-10-30 15:35:18 -0700 | [diff] [blame] | 405 | case FP_TYPE_UNKNOWN: |
| 406 | return false; // Can't recompute on Unknown FlowType |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 407 | case FP_TYPE_SHORTEST_PATH: |
| 408 | break; |
| 409 | case FP_TYPE_EXPLICIT_PATH: |
| 410 | return false; // An explicit path never changes |
| 411 | } |
| 412 | |
| 413 | DataPath oldDataPath = flowPath.dataPath(); |
| 414 | |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 415 | // Compute the new path |
| 416 | DataPath newDataPath = TopologyManager.computeNetworkPath(topology, |
| 417 | flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 418 | if (newDataPath == null) { |
| 419 | // We need the DataPath to compare the paths |
| 420 | newDataPath = new DataPath(); |
| 421 | } |
| 422 | newDataPath.applyFlowPathFlags(flowPath.flowPathFlags()); |
| 423 | |
| 424 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 425 | // Test whether the new path is same |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 426 | // |
| 427 | if (oldDataPath.flowEntries().size() != |
| 428 | newDataPath.flowEntries().size()) { |
| 429 | hasChanged = true; |
| 430 | } else { |
| 431 | Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator(); |
| 432 | Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator(); |
| 433 | while (oldIter.hasNext() && newIter.hasNext()) { |
| 434 | FlowEntry oldFlowEntry = oldIter.next(); |
| 435 | FlowEntry newFlowEntry = newIter.next(); |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 436 | if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry, |
| 437 | newFlowEntry)) { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 438 | hasChanged = true; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | if (! hasChanged) |
| 444 | return hasChanged; |
| 445 | |
| 446 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 447 | // Merge the changes in the path: |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 448 | // - If a Flow Entry for a switch is in the old data path, but not |
| 449 | // in the new data path, then mark it for deletion. |
| 450 | // - If a Flow Entry for a switch is in the new data path, but not |
| 451 | // in the old data path, then mark it for addition. |
| 452 | // - If a Flow Entry for a switch is in both the old and the new |
| 453 | // data path, but it has changed, e.g., the incoming and/or outgoing |
| 454 | // port(s), then mark the old Flow Entry for deletion, and mark |
| 455 | // the new Flow Entry for addition. |
| 456 | // - If a Flow Entry for a switch is in both the old and the new |
| 457 | // data path, and it hasn't changed, then just keep it. |
| 458 | // |
| 459 | // NOTE: We use the Switch DPID of each entry to match the entries |
| 460 | // |
| 461 | Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>(); |
| 462 | Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>(); |
| 463 | ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>(); |
| 464 | List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>(); |
| 465 | |
| 466 | // Prepare maps with the Flow Entries, so they are fast to lookup |
| 467 | for (FlowEntry flowEntry : oldDataPath.flowEntries()) |
| 468 | oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry); |
| 469 | for (FlowEntry flowEntry : newDataPath.flowEntries()) |
| 470 | newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry); |
| 471 | |
| 472 | // |
| 473 | // Find the old Flow Entries that should be deleted |
| 474 | // |
| 475 | for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) { |
| 476 | FlowEntry newFlowEntry = |
| 477 | newFlowEntriesMap.get(oldFlowEntry.dpid().value()); |
| 478 | if (newFlowEntry == null) { |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 479 | // The old Flow Entry should be deleted: not on the path |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 480 | oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 481 | oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 482 | deletedFlowEntries.add(oldFlowEntry); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // |
| 487 | // Find the new Flow Entries that should be added or updated |
| 488 | // |
| 489 | int idx = 0; |
| 490 | for (FlowEntry newFlowEntry : newDataPath.flowEntries()) { |
| 491 | FlowEntry oldFlowEntry = |
| 492 | oldFlowEntriesMap.get(newFlowEntry.dpid().value()); |
| 493 | |
| 494 | if ((oldFlowEntry != null) && |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 495 | TopologyManager.isSameFlowEntryDataPath(oldFlowEntry, |
| 496 | newFlowEntry)) { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 497 | // |
| 498 | // Both Flow Entries are same |
| 499 | // |
| 500 | finalFlowEntries.add(oldFlowEntry); |
| 501 | idx++; |
| 502 | continue; |
| 503 | } |
| 504 | |
| 505 | if (oldFlowEntry != null) { |
| 506 | // |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 507 | // The old Flow Entry should be deleted: path diverges |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 508 | // |
| 509 | oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 510 | oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 511 | deletedFlowEntries.add(oldFlowEntry); |
| 512 | } |
| 513 | |
| 514 | // |
| 515 | // Add the new Flow Entry |
| 516 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 517 | // |
| 518 | // NOTE: Assign only the Flow ID. |
| 519 | // The Flow Entry ID is assigned later only for the Flow Entries |
| 520 | // this instance is responsible for. |
| 521 | // |
| 522 | newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 523 | |
| 524 | // Set the incoming port matching |
| 525 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 526 | newFlowEntry.setFlowEntryMatch(flowEntryMatch); |
| 527 | flowEntryMatch.enableInPort(newFlowEntry.inPort()); |
| 528 | |
| 529 | // |
| 530 | // Set the actions: |
| 531 | // If the first Flow Entry, copy the Flow Path actions to it. |
| 532 | // |
| 533 | FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions(); |
| 534 | if ((idx == 0) && (flowPath.flowEntryActions() != null)) { |
| 535 | FlowEntryActions flowActions = |
| 536 | new FlowEntryActions(flowPath.flowEntryActions()); |
| 537 | for (FlowEntryAction action : flowActions.actions()) |
| 538 | flowEntryActions.addAction(action); |
| 539 | } |
| 540 | idx++; |
| 541 | |
| 542 | // |
| 543 | // Add the outgoing port output action |
| 544 | // |
| 545 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 546 | flowEntryAction.setActionOutput(newFlowEntry.outPort()); |
| 547 | flowEntryActions.addAction(flowEntryAction); |
| 548 | |
| 549 | // |
| 550 | // Set the state of the new Flow Entry |
| 551 | // |
| 552 | newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
| 553 | newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 554 | finalFlowEntries.add(newFlowEntry); |
| 555 | } |
| 556 | |
| 557 | // |
| 558 | // Replace the old Flow Entries with the new Flow Entries. |
| 559 | // Note that the Flow Entries that will be deleted are added at |
| 560 | // the end. |
| 561 | // |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 562 | finalFlowEntries.addAll(deletedFlowEntries); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 563 | flowPath.dataPath().setFlowEntries(finalFlowEntries); |
| 564 | |
| 565 | return hasChanged; |
| 566 | } |
| 567 | |
| 568 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 569 | * Receive a notification that a Flow is added. |
| 570 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 571 | * @param flowPath the Flow that is added. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 572 | */ |
| 573 | @Override |
| 574 | public void notificationRecvFlowAdded(FlowPath flowPath) { |
| 575 | EventEntry<FlowPath> eventEntry = |
| 576 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 577 | networkEvents.add(eventEntry); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Receive a notification that a Flow is removed. |
| 582 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 583 | * @param flowPath the Flow that is removed. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 584 | */ |
| 585 | @Override |
| 586 | public void notificationRecvFlowRemoved(FlowPath flowPath) { |
| 587 | EventEntry<FlowPath> eventEntry = |
| 588 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath); |
| 589 | networkEvents.add(eventEntry); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Receive a notification that a Flow is updated. |
| 594 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 595 | * @param flowPath the Flow that is updated. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 596 | */ |
| 597 | @Override |
| 598 | public void notificationRecvFlowUpdated(FlowPath flowPath) { |
| 599 | // NOTE: The ADD and UPDATE events are processed in same way |
| 600 | EventEntry<FlowPath> eventEntry = |
| 601 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 602 | networkEvents.add(eventEntry); |
| 603 | } |
| 604 | |
| 605 | /** |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 606 | * Receive a notification that a FlowEntry is added. |
| 607 | * |
| 608 | * @param flowEntry the FlowEntry that is added. |
| 609 | */ |
| 610 | @Override |
| 611 | public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) { |
| 612 | EventEntry<FlowEntry> eventEntry = |
| 613 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 614 | networkEvents.add(eventEntry); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Receive a notification that a FlowEntry is removed. |
| 619 | * |
| 620 | * @param flowEntry the FlowEntry that is removed. |
| 621 | */ |
| 622 | @Override |
| 623 | public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) { |
| 624 | EventEntry<FlowEntry> eventEntry = |
| 625 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry); |
| 626 | networkEvents.add(eventEntry); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Receive a notification that a FlowEntry is updated. |
| 631 | * |
| 632 | * @param flowEntry the FlowEntry that is updated. |
| 633 | */ |
| 634 | @Override |
| 635 | public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) { |
| 636 | // NOTE: The ADD and UPDATE events are processed in same way |
| 637 | EventEntry<FlowEntry> eventEntry = |
| 638 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 639 | networkEvents.add(eventEntry); |
| 640 | } |
| 641 | |
| 642 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 643 | * Receive a notification that a Topology Element is added. |
| 644 | * |
| 645 | * @param topologyElement the Topology Element that is added. |
| 646 | */ |
| 647 | @Override |
| 648 | public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) { |
| 649 | EventEntry<TopologyElement> eventEntry = |
| 650 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 651 | networkEvents.add(eventEntry); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Receive a notification that a Topology Element is removed. |
| 656 | * |
| 657 | * @param topologyElement the Topology Element that is removed. |
| 658 | */ |
| 659 | @Override |
| 660 | public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) { |
| 661 | EventEntry<TopologyElement> eventEntry = |
| 662 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement); |
| 663 | networkEvents.add(eventEntry); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Receive a notification that a Topology Element is updated. |
| 668 | * |
| 669 | * @param topologyElement the Topology Element that is updated. |
| 670 | */ |
| 671 | @Override |
| 672 | public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) { |
| 673 | // NOTE: The ADD and UPDATE events are processed in same way |
| 674 | EventEntry<TopologyElement> eventEntry = |
| 675 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 676 | networkEvents.add(eventEntry); |
| 677 | } |
| 678 | } |