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 | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 10 | import java.util.SortedMap; |
| 11 | import java.util.TreeMap; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 12 | import java.util.concurrent.BlockingQueue; |
| 13 | import java.util.concurrent.LinkedBlockingQueue; |
| 14 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 16 | import net.onrc.onos.datagrid.IDatagridService; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 17 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 19 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.util.DataPath; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.util.EventEntry; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 23 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 24 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 27 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 28 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.util.FlowId; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.util.FlowPath; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
| 33 | |
| 34 | import com.esotericsoftware.kryo2.Kryo; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 35 | |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | /** |
Yuta HIGUCHI | e1038fb | 2013-10-30 15:35:18 -0700 | [diff] [blame] | 40 | * Class for FlowPath Maintenance. |
| 41 | * This class listens for FlowEvents to: |
| 42 | * - Maintain a local cache of the Network Topology. |
| 43 | * - Detect FlowPaths impacted by Topology change. |
| 44 | * - Recompute impacted FlowPath using cached Topology. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 45 | */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 46 | class FlowEventHandler extends Thread implements IFlowEventHandlerService { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 47 | /** The logger. */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 48 | private final static Logger log = LoggerFactory.getLogger(FlowEventHandler.class); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 49 | |
| 50 | private FlowManager flowManager; // The Flow Manager to use |
| 51 | private IDatagridService datagridService; // The Datagrid Service to use |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 52 | private Topology topology; // The network topology |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 53 | private KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 54 | |
| 55 | // The queue with Flow Path and Topology Element updates |
| 56 | private BlockingQueue<EventEntry<?>> networkEvents = |
| 57 | new LinkedBlockingQueue<EventEntry<?>>(); |
| 58 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 59 | // The pending Topology, FlowPath, and FlowEntry events |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 60 | private List<EventEntry<TopologyElement>> topologyEvents = |
| 61 | new LinkedList<EventEntry<TopologyElement>>(); |
| 62 | private List<EventEntry<FlowPath>> flowPathEvents = |
| 63 | new LinkedList<EventEntry<FlowPath>>(); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 64 | private List<EventEntry<FlowEntry>> flowEntryEvents = |
| 65 | new LinkedList<EventEntry<FlowEntry>>(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 66 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 67 | // All internally computed Flow Paths |
| 68 | private Map<Long, FlowPath> allFlowPaths = new HashMap<Long, FlowPath>(); |
| 69 | |
| 70 | // The Flow Entries received as notifications with unmatched Flow Paths |
| 71 | private Map<Long, FlowEntry> unmatchedFlowEntryAdd = |
| 72 | new HashMap<Long, FlowEntry>(); |
| 73 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 74 | // |
| 75 | // Transient state for processing the Flow Paths: |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 76 | // - The Flow Paths that should be recomputed |
| 77 | // - The Flow Paths with modified Flow Entries |
| 78 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 79 | private Map<Long, FlowPath> shouldRecomputeFlowPaths = |
| 80 | new HashMap<Long, FlowPath>(); |
| 81 | private Map<Long, FlowPath> modifiedFlowPaths = |
| 82 | new HashMap<Long, FlowPath>(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 83 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Constructor for a given Flow Manager and Datagrid Service. |
| 86 | * |
| 87 | * @param flowManager the Flow Manager to use. |
| 88 | * @param datagridService the Datagrid Service to use. |
| 89 | */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 90 | FlowEventHandler(FlowManager flowManager, |
| 91 | IDatagridService datagridService) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 92 | this.flowManager = flowManager; |
| 93 | this.datagridService = datagridService; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 94 | this.topology = new Topology(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 98 | * Get the network topology. |
| 99 | * |
| 100 | * @return the network topology. |
| 101 | */ |
| 102 | protected Topology getTopology() { return this.topology; } |
| 103 | |
| 104 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 105 | * Startup processing. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 106 | */ |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 107 | private void startup() { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 108 | // |
| 109 | // Obtain the initial Topology state |
| 110 | // |
| 111 | Collection<TopologyElement> topologyElements = |
| 112 | datagridService.getAllTopologyElements(); |
| 113 | for (TopologyElement topologyElement : topologyElements) { |
| 114 | EventEntry<TopologyElement> eventEntry = |
| 115 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 116 | topologyEvents.add(eventEntry); |
| 117 | } |
| 118 | // |
| 119 | // Obtain the initial Flow Path state |
| 120 | // |
| 121 | Collection<FlowPath> flowPaths = datagridService.getAllFlows(); |
| 122 | for (FlowPath flowPath : flowPaths) { |
| 123 | EventEntry<FlowPath> eventEntry = |
| 124 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 125 | flowPathEvents.add(eventEntry); |
| 126 | } |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 127 | // |
| 128 | // Obtain the initial FlowEntry state |
| 129 | // |
| 130 | Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries(); |
| 131 | for (FlowEntry flowEntry : flowEntries) { |
| 132 | EventEntry<FlowEntry> eventEntry = |
| 133 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 134 | flowEntryEvents.add(eventEntry); |
| 135 | } |
| 136 | |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 137 | // Process the initial events (if any) |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 138 | synchronized (allFlowPaths) { |
| 139 | processEvents(); |
| 140 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Run the thread. |
| 145 | */ |
| 146 | @Override |
| 147 | public void run() { |
| 148 | startup(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 149 | |
| 150 | // |
| 151 | // The main loop |
| 152 | // |
| 153 | Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>(); |
| 154 | try { |
| 155 | while (true) { |
| 156 | EventEntry<?> eventEntry = networkEvents.take(); |
| 157 | collection.add(eventEntry); |
| 158 | networkEvents.drainTo(collection); |
| 159 | |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 160 | // |
| 161 | // Demultiplex all events: |
| 162 | // - EventEntry<TopologyElement> |
| 163 | // - EventEntry<FlowPath> |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 164 | // - EventEntry<FlowEntry> |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 165 | // |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 166 | for (EventEntry<?> event : collection) { |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 167 | // Topology event |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 168 | if (event.eventData() instanceof TopologyElement) { |
| 169 | EventEntry<TopologyElement> topologyEventEntry = |
| 170 | (EventEntry<TopologyElement>)event; |
| 171 | topologyEvents.add(topologyEventEntry); |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 172 | continue; |
| 173 | } |
| 174 | |
| 175 | // FlowPath event |
| 176 | if (event.eventData() instanceof FlowPath) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 177 | EventEntry<FlowPath> flowPathEventEntry = |
| 178 | (EventEntry<FlowPath>)event; |
| 179 | flowPathEvents.add(flowPathEventEntry); |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // FlowEntry event |
| 184 | if (event.eventData() instanceof FlowEntry) { |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 185 | EventEntry<FlowEntry> flowEntryEventEntry = |
| 186 | (EventEntry<FlowEntry>)event; |
| 187 | flowEntryEvents.add(flowEntryEventEntry); |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 188 | continue; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | collection.clear(); |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 192 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 193 | // Process the events (if any) |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 194 | synchronized (allFlowPaths) { |
| 195 | processEvents(); |
| 196 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 197 | } |
| 198 | } catch (Exception exception) { |
| 199 | log.debug("Exception processing Network Events: ", exception); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Process the events (if any) |
| 205 | */ |
| 206 | private void processEvents() { |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 207 | Collection<FlowEntry> modifiedFlowEntries; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 208 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 209 | if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() && |
| 210 | flowEntryEvents.isEmpty()) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 211 | return; // Nothing to do |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 212 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 213 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 214 | processFlowPathEvents(); |
| 215 | processTopologyEvents(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 216 | processFlowEntryEvents(); |
| 217 | |
| 218 | // Recompute all affected Flow Paths and keep only the modified |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 219 | for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 220 | if (recomputeFlowPath(flowPath)) |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 221 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 224 | // Extract the modified Flow Entries |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 225 | modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 226 | |
| 227 | // Assign missing Flow Entry IDs |
| 228 | assignFlowEntryId(modifiedFlowEntries); |
| 229 | |
| 230 | // |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 231 | // Push the modified state to the Flow Manager |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 232 | // |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 233 | flowManager.pushModifiedFlowState(modifiedFlowPaths.values(), |
| 234 | modifiedFlowEntries); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 235 | |
| 236 | // |
| 237 | // Remove Flow Entries that were deleted |
| 238 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 239 | for (FlowPath flowPath : modifiedFlowPaths.values()) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 240 | flowPath.dataPath().removeDeletedFlowEntries(); |
| 241 | |
| 242 | // Cleanup |
| 243 | topologyEvents.clear(); |
| 244 | flowPathEvents.clear(); |
| 245 | flowEntryEvents.clear(); |
| 246 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 247 | shouldRecomputeFlowPaths.clear(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 248 | modifiedFlowPaths.clear(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Extract the modified Flow Entries. |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 253 | * |
| 254 | * @param modifiedFlowPaths the Flow Paths to process. |
| 255 | * @return a collection with the modified Flow Entries. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 256 | */ |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 257 | private Collection<FlowEntry> extractModifiedFlowEntries( |
| 258 | Collection<FlowPath> modifiedFlowPaths) { |
| 259 | List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 260 | |
| 261 | // Extract only the modified Flow Entries |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 262 | for (FlowPath flowPath : modifiedFlowPaths) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 263 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 264 | if (flowEntry.flowEntrySwitchState() == |
| 265 | FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) { |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 266 | modifiedFlowEntries.add(flowEntry); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } |
| 270 | return modifiedFlowEntries; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Assign the Flow Entry ID as needed. |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 275 | * |
| 276 | * @param modifiedFlowEnries the collection of Flow Entries that need |
| 277 | * Flow Entry ID assigned. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 278 | */ |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 279 | private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 280 | if (modifiedFlowEntries.isEmpty()) |
| 281 | return; |
| 282 | |
| 283 | Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches(); |
| 284 | |
| 285 | // |
| 286 | // Assign the Flow Entry ID only for Flow Entries for my switches |
| 287 | // |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 288 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 289 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 290 | if (mySwitch == null) |
| 291 | continue; |
| 292 | if (! flowEntry.isValidFlowEntryId()) { |
| 293 | long id = flowManager.getNextFlowEntryId(); |
| 294 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Process the Flow Path events. |
| 301 | */ |
| 302 | private void processFlowPathEvents() { |
| 303 | // |
| 304 | // Process all Flow Path events and update the appropriate state |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 305 | // |
| 306 | for (EventEntry<FlowPath> eventEntry : flowPathEvents) { |
| 307 | FlowPath flowPath = eventEntry.eventData(); |
| 308 | |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 309 | log.debug("Flow Event: {} {}", eventEntry.eventType(), |
| 310 | flowPath.toString()); |
| 311 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 312 | switch (eventEntry.eventType()) { |
| 313 | case ENTRY_ADD: { |
| 314 | // |
| 315 | // Add a new Flow Path |
| 316 | // |
| 317 | if (allFlowPaths.get(flowPath.flowId().value()) != null) { |
| 318 | // |
| 319 | // TODO: What to do if the Flow Path already exists? |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 320 | // Fow now, we just re-add it. |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 321 | // |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | switch (flowPath.flowPathType()) { |
| 325 | case FP_TYPE_SHORTEST_PATH: |
| 326 | // |
| 327 | // Reset the Data Path, in case it was set already, because |
| 328 | // we are going to recompute it anyway. |
| 329 | // |
| 330 | flowPath.flowEntries().clear(); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 331 | shouldRecomputeFlowPaths.put(flowPath.flowId().value(), |
| 332 | flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 333 | break; |
| 334 | case FP_TYPE_EXPLICIT_PATH: |
| 335 | // |
| 336 | // Mark all Flow Entries for installation in the switches. |
| 337 | // |
| 338 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 339 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 340 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 341 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 342 | break; |
| 343 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 344 | allFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 345 | |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | case ENTRY_REMOVE: { |
| 350 | // |
| 351 | // Remove an existing Flow Path. |
| 352 | // |
| 353 | // Find the Flow Path, and mark the Flow and its Flow Entries |
| 354 | // for deletion. |
| 355 | // |
| 356 | FlowPath existingFlowPath = |
| 357 | allFlowPaths.get(flowPath.flowId().value()); |
| 358 | if (existingFlowPath == null) |
| 359 | continue; // Nothing to do |
| 360 | |
| 361 | existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE); |
| 362 | for (FlowEntry flowEntry : existingFlowPath.flowEntries()) { |
| 363 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 364 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 365 | } |
| 366 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 367 | // Remove the Flow Path from the internal state |
| 368 | Long key = existingFlowPath.flowId().value(); |
| 369 | allFlowPaths.remove(key); |
| 370 | shouldRecomputeFlowPaths.remove(key); |
| 371 | modifiedFlowPaths.put(key, existingFlowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 372 | |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 377 | } |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 378 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 379 | /** |
| 380 | * Process the Topology events. |
| 381 | */ |
| 382 | private void processTopologyEvents() { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 383 | // |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 384 | // Process all Topology events and update the appropriate state |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 385 | // |
| 386 | boolean isTopologyModified = false; |
| 387 | for (EventEntry<TopologyElement> eventEntry : topologyEvents) { |
| 388 | TopologyElement topologyElement = eventEntry.eventData(); |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 389 | |
| 390 | log.debug("Topology Event: {} {}", eventEntry.eventType(), |
| 391 | topologyElement.toString()); |
| 392 | |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 393 | switch (eventEntry.eventType()) { |
| 394 | case ENTRY_ADD: |
Yuta HIGUCHI | b32f77f | 2013-10-30 15:36:32 -0700 | [diff] [blame] | 395 | isTopologyModified |= topology.addTopologyElement(topologyElement); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 396 | break; |
| 397 | case ENTRY_REMOVE: |
Yuta HIGUCHI | b32f77f | 2013-10-30 15:36:32 -0700 | [diff] [blame] | 398 | isTopologyModified |= topology.removeTopologyElement(topologyElement); |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | } |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 402 | if (isTopologyModified) { |
| 403 | // TODO: For now, if the topology changes, we recompute all Flows |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 404 | shouldRecomputeFlowPaths.putAll(allFlowPaths); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 405 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 406 | } |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 407 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Process the Flow Entry events. |
| 410 | */ |
| 411 | private void processFlowEntryEvents() { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 412 | FlowPath flowPath; |
| 413 | FlowEntry updatedFlowEntry; |
| 414 | |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 415 | // |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 416 | // Update Flow Entries with previously unmatched Flow Entry updates |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 417 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 418 | if (! unmatchedFlowEntryAdd.isEmpty()) { |
| 419 | Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>(); |
| 420 | for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) { |
| 421 | flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
| 422 | if (flowPath == null) |
| 423 | continue; |
| 424 | updatedFlowEntry = updateFlowEntryAdd(flowPath, flowEntry); |
| 425 | if (updatedFlowEntry == null) { |
| 426 | remainingUpdates.put(flowEntry.flowEntryId().value(), |
| 427 | flowEntry); |
| 428 | continue; |
| 429 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 430 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 431 | } |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 432 | unmatchedFlowEntryAdd = remainingUpdates; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 436 | // Process all Flow Entry events and update the appropriate state |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 437 | // |
| 438 | for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) { |
| 439 | FlowEntry flowEntry = eventEntry.eventData(); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 440 | |
| 441 | log.debug("Flow Entry Event: {} {}", eventEntry.eventType(), |
| 442 | flowEntry.toString()); |
| 443 | |
| 444 | if ((! flowEntry.isValidFlowId()) || |
| 445 | (! flowEntry.isValidFlowEntryId())) { |
| 446 | continue; |
| 447 | } |
| 448 | |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 449 | switch (eventEntry.eventType()) { |
| 450 | case ENTRY_ADD: |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 451 | flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
| 452 | if (flowPath == null) { |
| 453 | // Flow Path not found: keep the entry for later matching |
| 454 | unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(), |
| 455 | flowEntry); |
| 456 | break; |
| 457 | } |
| 458 | updatedFlowEntry = updateFlowEntryAdd(flowPath, flowEntry); |
| 459 | if (updatedFlowEntry == null) { |
| 460 | // Flow Entry not found: keep the entry for later matching |
| 461 | unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(), |
| 462 | flowEntry); |
| 463 | break; |
| 464 | } |
| 465 | // Add the updated entry to the list of updated Flow Entries |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 466 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 467 | break; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 468 | |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 469 | case ENTRY_REMOVE: |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 470 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 471 | if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 472 | continue; // Removed previously unmatched entry |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 473 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 474 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 475 | flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
| 476 | if (flowPath == null) { |
| 477 | // Flow Path not found: ignore the update |
| 478 | break; |
| 479 | } |
| 480 | updatedFlowEntry = updateFlowEntryRemove(flowPath, flowEntry); |
| 481 | if (updatedFlowEntry == null) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 482 | // Flow Entry not found: ignore it |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 483 | break; |
| 484 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 485 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /** |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 492 | * Update a Flow Entry because of an external ENTRY_ADD event. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 493 | * |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 494 | * @param flowPath the FlowPath for the Flow Entry to update. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 495 | * @param flowEntry the FlowEntry with the new state. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 496 | * @return the updated Flow Entry if found, otherwise null. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 497 | */ |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 498 | private FlowEntry updateFlowEntryAdd(FlowPath flowPath, |
| 499 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 500 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 501 | // Iterate over all Flow Entries and find a match. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 502 | // |
| 503 | for (FlowEntry localFlowEntry : flowPath.flowEntries()) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 504 | if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry, |
| 505 | flowEntry)) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 506 | continue; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 507 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 508 | |
| 509 | // |
| 510 | // Local Flow Entry match found |
| 511 | // |
| 512 | if (localFlowEntry.isValidFlowEntryId()) { |
| 513 | if (localFlowEntry.flowEntryId().value() != |
| 514 | flowEntry.flowEntryId().value()) { |
| 515 | // |
| 516 | // Find a local Flow Entry, but the Flow Entry ID doesn't |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 517 | // match. Keep looking. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 518 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 519 | continue; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 520 | } |
| 521 | } else { |
| 522 | // Update the Flow Entry ID |
| 523 | FlowEntryId flowEntryId = |
| 524 | new FlowEntryId(flowEntry.flowEntryId().value()); |
| 525 | localFlowEntry.setFlowEntryId(flowEntryId); |
| 526 | } |
| 527 | |
| 528 | // |
| 529 | // Update the local Flow Entry. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 530 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 531 | localFlowEntry.setFlowEntryUserState(flowEntry.flowEntryUserState()); |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 532 | localFlowEntry.setFlowEntrySwitchState(flowEntry.flowEntrySwitchState()); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 533 | return localFlowEntry; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 536 | return null; // Entry not found |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Update a Flow Entry because of an external ENTRY_REMOVE event. |
| 541 | * |
| 542 | * @param flowPath the FlowPath for the Flow Entry to update. |
| 543 | * @param flowEntry the FlowEntry with the new state. |
| 544 | * @return the updated Flow Entry if found, otherwise null. |
| 545 | */ |
| 546 | private FlowEntry updateFlowEntryRemove(FlowPath flowPath, |
| 547 | FlowEntry flowEntry) { |
| 548 | // |
| 549 | // Iterate over all Flow Entries and find a match based on |
| 550 | // the Flow Entry ID. |
| 551 | // |
| 552 | for (FlowEntry localFlowEntry : flowPath.flowEntries()) { |
| 553 | if (! localFlowEntry.isValidFlowEntryId()) |
| 554 | continue; |
| 555 | if (localFlowEntry.flowEntryId().value() != |
| 556 | flowEntry.flowEntryId().value()) { |
| 557 | continue; |
| 558 | } |
| 559 | // |
| 560 | // Update the local Flow Entry. |
| 561 | // |
| 562 | localFlowEntry.setFlowEntryUserState(flowEntry.flowEntryUserState()); |
| 563 | localFlowEntry.setFlowEntrySwitchState(flowEntry.flowEntrySwitchState()); |
| 564 | return localFlowEntry; |
| 565 | } |
| 566 | |
| 567 | return null; // Entry not found |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 571 | * Recompute a Flow Path. |
| 572 | * |
| 573 | * @param flowPath the Flow Path to recompute. |
| 574 | * @return true if the recomputed Flow Path has changed, otherwise false. |
| 575 | */ |
| 576 | private boolean recomputeFlowPath(FlowPath flowPath) { |
| 577 | boolean hasChanged = false; |
| 578 | |
| 579 | // |
| 580 | // Test whether the Flow Path needs to be recomputed |
| 581 | // |
| 582 | switch (flowPath.flowPathType()) { |
Yuta HIGUCHI | e1038fb | 2013-10-30 15:35:18 -0700 | [diff] [blame] | 583 | case FP_TYPE_UNKNOWN: |
| 584 | return false; // Can't recompute on Unknown FlowType |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 585 | case FP_TYPE_SHORTEST_PATH: |
| 586 | break; |
| 587 | case FP_TYPE_EXPLICIT_PATH: |
| 588 | return false; // An explicit path never changes |
| 589 | } |
| 590 | |
| 591 | DataPath oldDataPath = flowPath.dataPath(); |
| 592 | |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 593 | // Compute the new path |
| 594 | DataPath newDataPath = TopologyManager.computeNetworkPath(topology, |
| 595 | flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 596 | if (newDataPath == null) { |
| 597 | // We need the DataPath to compare the paths |
| 598 | newDataPath = new DataPath(); |
| 599 | } |
| 600 | newDataPath.applyFlowPathFlags(flowPath.flowPathFlags()); |
| 601 | |
| 602 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 603 | // Test whether the new path is same |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 604 | // |
| 605 | if (oldDataPath.flowEntries().size() != |
| 606 | newDataPath.flowEntries().size()) { |
| 607 | hasChanged = true; |
| 608 | } else { |
| 609 | Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator(); |
| 610 | Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator(); |
| 611 | while (oldIter.hasNext() && newIter.hasNext()) { |
| 612 | FlowEntry oldFlowEntry = oldIter.next(); |
| 613 | FlowEntry newFlowEntry = newIter.next(); |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 614 | if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry, |
| 615 | newFlowEntry)) { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 616 | hasChanged = true; |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | if (! hasChanged) |
| 622 | return hasChanged; |
| 623 | |
| 624 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 625 | // Merge the changes in the path: |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 626 | // - If a Flow Entry for a switch is in the old data path, but not |
| 627 | // in the new data path, then mark it for deletion. |
| 628 | // - If a Flow Entry for a switch is in the new data path, but not |
| 629 | // in the old data path, then mark it for addition. |
| 630 | // - If a Flow Entry for a switch is in both the old and the new |
| 631 | // data path, but it has changed, e.g., the incoming and/or outgoing |
| 632 | // port(s), then mark the old Flow Entry for deletion, and mark |
| 633 | // the new Flow Entry for addition. |
| 634 | // - If a Flow Entry for a switch is in both the old and the new |
| 635 | // data path, and it hasn't changed, then just keep it. |
| 636 | // |
| 637 | // NOTE: We use the Switch DPID of each entry to match the entries |
| 638 | // |
| 639 | Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>(); |
| 640 | Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>(); |
| 641 | ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>(); |
| 642 | List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>(); |
| 643 | |
| 644 | // Prepare maps with the Flow Entries, so they are fast to lookup |
| 645 | for (FlowEntry flowEntry : oldDataPath.flowEntries()) |
| 646 | oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry); |
| 647 | for (FlowEntry flowEntry : newDataPath.flowEntries()) |
| 648 | newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry); |
| 649 | |
| 650 | // |
| 651 | // Find the old Flow Entries that should be deleted |
| 652 | // |
| 653 | for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) { |
| 654 | FlowEntry newFlowEntry = |
| 655 | newFlowEntriesMap.get(oldFlowEntry.dpid().value()); |
| 656 | if (newFlowEntry == null) { |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 657 | // The old Flow Entry should be deleted: not on the path |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 658 | oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 659 | oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 660 | deletedFlowEntries.add(oldFlowEntry); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // |
| 665 | // Find the new Flow Entries that should be added or updated |
| 666 | // |
| 667 | int idx = 0; |
| 668 | for (FlowEntry newFlowEntry : newDataPath.flowEntries()) { |
| 669 | FlowEntry oldFlowEntry = |
| 670 | oldFlowEntriesMap.get(newFlowEntry.dpid().value()); |
| 671 | |
| 672 | if ((oldFlowEntry != null) && |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 673 | TopologyManager.isSameFlowEntryDataPath(oldFlowEntry, |
| 674 | newFlowEntry)) { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 675 | // |
| 676 | // Both Flow Entries are same |
| 677 | // |
| 678 | finalFlowEntries.add(oldFlowEntry); |
| 679 | idx++; |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | if (oldFlowEntry != null) { |
| 684 | // |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 685 | // The old Flow Entry should be deleted: path diverges |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 686 | // |
| 687 | oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 688 | oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 689 | deletedFlowEntries.add(oldFlowEntry); |
| 690 | } |
| 691 | |
| 692 | // |
| 693 | // Add the new Flow Entry |
| 694 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 695 | // |
| 696 | // NOTE: Assign only the Flow ID. |
| 697 | // The Flow Entry ID is assigned later only for the Flow Entries |
| 698 | // this instance is responsible for. |
| 699 | // |
| 700 | newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 701 | |
Pavlin Radoslavov | a4df952 | 2013-12-03 11:03:04 -0800 | [diff] [blame] | 702 | // |
Pavlin Radoslavov | 6fde217 | 2013-12-10 11:23:18 -0800 | [diff] [blame] | 703 | // Copy the Flow timeouts |
| 704 | // |
| 705 | newFlowEntry.setIdleTimeout(flowPath.idleTimeout()); |
| 706 | newFlowEntry.setHardTimeout(flowPath.hardTimeout()); |
| 707 | |
| 708 | // |
Pavlin Radoslavov | a4df952 | 2013-12-03 11:03:04 -0800 | [diff] [blame] | 709 | // Allocate the FlowEntryMatch by copying the default one |
| 710 | // from the FlowPath (if set). |
| 711 | // |
| 712 | FlowEntryMatch flowEntryMatch = null; |
| 713 | if (flowPath.flowEntryMatch() != null) |
| 714 | flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch()); |
| 715 | else |
| 716 | flowEntryMatch = new FlowEntryMatch(); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 717 | newFlowEntry.setFlowEntryMatch(flowEntryMatch); |
Pavlin Radoslavov | a4df952 | 2013-12-03 11:03:04 -0800 | [diff] [blame] | 718 | |
| 719 | // Set the incoming port matching |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 720 | flowEntryMatch.enableInPort(newFlowEntry.inPort()); |
| 721 | |
| 722 | // |
| 723 | // Set the actions: |
| 724 | // If the first Flow Entry, copy the Flow Path actions to it. |
| 725 | // |
| 726 | FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions(); |
| 727 | if ((idx == 0) && (flowPath.flowEntryActions() != null)) { |
| 728 | FlowEntryActions flowActions = |
| 729 | new FlowEntryActions(flowPath.flowEntryActions()); |
| 730 | for (FlowEntryAction action : flowActions.actions()) |
| 731 | flowEntryActions.addAction(action); |
| 732 | } |
| 733 | idx++; |
| 734 | |
| 735 | // |
| 736 | // Add the outgoing port output action |
| 737 | // |
| 738 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 739 | flowEntryAction.setActionOutput(newFlowEntry.outPort()); |
| 740 | flowEntryActions.addAction(flowEntryAction); |
| 741 | |
| 742 | // |
| 743 | // Set the state of the new Flow Entry |
| 744 | // |
| 745 | newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
| 746 | newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 747 | finalFlowEntries.add(newFlowEntry); |
| 748 | } |
| 749 | |
| 750 | // |
| 751 | // Replace the old Flow Entries with the new Flow Entries. |
| 752 | // Note that the Flow Entries that will be deleted are added at |
| 753 | // the end. |
| 754 | // |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 755 | finalFlowEntries.addAll(deletedFlowEntries); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 756 | flowPath.dataPath().setFlowEntries(finalFlowEntries); |
| 757 | |
| 758 | return hasChanged; |
| 759 | } |
| 760 | |
| 761 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 762 | * Receive a notification that a Flow is added. |
| 763 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 764 | * @param flowPath the Flow that is added. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 765 | */ |
| 766 | @Override |
| 767 | public void notificationRecvFlowAdded(FlowPath flowPath) { |
| 768 | EventEntry<FlowPath> eventEntry = |
| 769 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 770 | networkEvents.add(eventEntry); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Receive a notification that a Flow is removed. |
| 775 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 776 | * @param flowPath the Flow that is removed. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 777 | */ |
| 778 | @Override |
| 779 | public void notificationRecvFlowRemoved(FlowPath flowPath) { |
| 780 | EventEntry<FlowPath> eventEntry = |
| 781 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath); |
| 782 | networkEvents.add(eventEntry); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Receive a notification that a Flow is updated. |
| 787 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 788 | * @param flowPath the Flow that is updated. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 789 | */ |
| 790 | @Override |
| 791 | public void notificationRecvFlowUpdated(FlowPath flowPath) { |
| 792 | // NOTE: The ADD and UPDATE events are processed in same way |
| 793 | EventEntry<FlowPath> eventEntry = |
| 794 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 795 | networkEvents.add(eventEntry); |
| 796 | } |
| 797 | |
| 798 | /** |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 799 | * Receive a notification that a FlowEntry is added. |
| 800 | * |
| 801 | * @param flowEntry the FlowEntry that is added. |
| 802 | */ |
| 803 | @Override |
| 804 | public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) { |
| 805 | EventEntry<FlowEntry> eventEntry = |
| 806 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 807 | networkEvents.add(eventEntry); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Receive a notification that a FlowEntry is removed. |
| 812 | * |
| 813 | * @param flowEntry the FlowEntry that is removed. |
| 814 | */ |
| 815 | @Override |
| 816 | public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) { |
| 817 | EventEntry<FlowEntry> eventEntry = |
| 818 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry); |
| 819 | networkEvents.add(eventEntry); |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Receive a notification that a FlowEntry is updated. |
| 824 | * |
| 825 | * @param flowEntry the FlowEntry that is updated. |
| 826 | */ |
| 827 | @Override |
| 828 | public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) { |
| 829 | // NOTE: The ADD and UPDATE events are processed in same way |
| 830 | EventEntry<FlowEntry> eventEntry = |
| 831 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 832 | networkEvents.add(eventEntry); |
| 833 | } |
| 834 | |
| 835 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 836 | * Receive a notification that a Topology Element is added. |
| 837 | * |
| 838 | * @param topologyElement the Topology Element that is added. |
| 839 | */ |
| 840 | @Override |
| 841 | public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) { |
| 842 | EventEntry<TopologyElement> eventEntry = |
| 843 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 844 | networkEvents.add(eventEntry); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Receive a notification that a Topology Element is removed. |
| 849 | * |
| 850 | * @param topologyElement the Topology Element that is removed. |
| 851 | */ |
| 852 | @Override |
| 853 | public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) { |
| 854 | EventEntry<TopologyElement> eventEntry = |
| 855 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement); |
| 856 | networkEvents.add(eventEntry); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Receive a notification that a Topology Element is updated. |
| 861 | * |
| 862 | * @param topologyElement the Topology Element that is updated. |
| 863 | */ |
| 864 | @Override |
| 865 | public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) { |
| 866 | // NOTE: The ADD and UPDATE events are processed in same way |
| 867 | EventEntry<TopologyElement> eventEntry = |
| 868 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 869 | networkEvents.add(eventEntry); |
| 870 | } |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 871 | |
| 872 | /** |
| 873 | * Get a sorted copy of all Flow Paths. |
| 874 | * |
| 875 | * @return a sorted copy of all Flow Paths. |
| 876 | */ |
| 877 | synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() { |
| 878 | SortedMap<Long, FlowPath> sortedFlowPaths = |
| 879 | new TreeMap<Long, FlowPath>(); |
| 880 | |
| 881 | // |
| 882 | // TODO: For now we use serialization/deserialization to create |
| 883 | // a copy of each Flow Path. In the future, we should use proper |
| 884 | // copy constructors. |
| 885 | // |
| 886 | Kryo kryo = kryoFactory.newKryo(); |
| 887 | synchronized (allFlowPaths) { |
| 888 | for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) { |
| 889 | FlowPath origFlowPath = entry.getValue(); |
| 890 | FlowPath copyFlowPath = kryo.copy(origFlowPath); |
| 891 | sortedFlowPaths.put(entry.getKey(), copyFlowPath); |
| 892 | } |
| 893 | } |
| 894 | kryoFactory.deleteKryo(kryo); |
| 895 | |
| 896 | return sortedFlowPaths; |
| 897 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 898 | } |