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