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 | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.core.IOFSwitchListener; |
| 17 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 18 | import net.onrc.onos.datagrid.IDatagridService; |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 19 | import net.onrc.onos.graph.GraphDBOperation; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.util.DataPath; |
Pavlin Radoslavov | 3a7cc90 | 2014-01-09 02:32:08 -0800 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.util.Dpid; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.util.EventEntry; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 27 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 28 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 31 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 32 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 33 | import net.onrc.onos.ofcontroller.util.FlowId; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 34 | import net.onrc.onos.ofcontroller.util.FlowPath; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 35 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.util.Pair; |
Pavlin Radoslavov | 3a7cc90 | 2014-01-09 02:32:08 -0800 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.util.Port; |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
| 39 | |
| 40 | import com.esotericsoftware.kryo2.Kryo; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 41 | |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | /** |
Yuta HIGUCHI | e1038fb | 2013-10-30 15:35:18 -0700 | [diff] [blame] | 46 | * Class for FlowPath Maintenance. |
| 47 | * This class listens for FlowEvents to: |
| 48 | * - Maintain a local cache of the Network Topology. |
| 49 | * - Detect FlowPaths impacted by Topology change. |
| 50 | * - Recompute impacted FlowPath using cached Topology. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 51 | */ |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 52 | class FlowEventHandler extends Thread implements IFlowEventHandlerService, |
| 53 | IOFSwitchListener { |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 54 | |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 55 | private boolean enableOnrc2014MeasurementsFlows = true; |
| 56 | private boolean enableOnrc2014MeasurementsTopology = true; |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 57 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 58 | /** The logger. */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 59 | private final static Logger log = LoggerFactory.getLogger(FlowEventHandler.class); |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 60 | |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 61 | private GraphDBOperation dbHandler; |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 62 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 63 | private FlowManager flowManager; // The Flow Manager to use |
| 64 | private IDatagridService datagridService; // The Datagrid Service to use |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 65 | private Topology topology; // The network topology |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 66 | private KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 67 | |
| 68 | // The queue with Flow Path and Topology Element updates |
| 69 | private BlockingQueue<EventEntry<?>> networkEvents = |
| 70 | new LinkedBlockingQueue<EventEntry<?>>(); |
| 71 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 72 | // The pending Topology, FlowPath, and FlowEntry events |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 73 | private List<EventEntry<TopologyElement>> topologyEvents = |
| 74 | new LinkedList<EventEntry<TopologyElement>>(); |
| 75 | private List<EventEntry<FlowPath>> flowPathEvents = |
| 76 | new LinkedList<EventEntry<FlowPath>>(); |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 77 | private List<EventEntry<FlowEntry>> flowEntryEvents = |
| 78 | new LinkedList<EventEntry<FlowEntry>>(); |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 79 | private List<EventEntry<Pair<FlowId, Dpid>>> flowIdEvents = |
| 80 | new LinkedList<EventEntry<Pair<FlowId, Dpid>>>(); |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 81 | private List<EventEntry<Pair<FlowEntryId, Dpid>>> flowEntryIdEvents = |
| 82 | new LinkedList<EventEntry<Pair<FlowEntryId, Dpid>>>(); |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 83 | private List<EventEntry<Dpid>> switchDpidEvents = |
| 84 | new LinkedList<EventEntry<Dpid>>(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 85 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 86 | // All internally computed Flow Paths |
| 87 | private Map<Long, FlowPath> allFlowPaths = new HashMap<Long, FlowPath>(); |
| 88 | |
| 89 | // The Flow Entries received as notifications with unmatched Flow Paths |
| 90 | private Map<Long, FlowEntry> unmatchedFlowEntryAdd = |
| 91 | new HashMap<Long, FlowEntry>(); |
| 92 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 93 | // |
| 94 | // Transient state for processing the Flow Paths: |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 95 | // - The Flow Paths that should be recomputed |
| 96 | // - The Flow Paths with modified Flow Entries |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 97 | // - The Flow Paths that we should check if installed in all switches |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 98 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 99 | private Map<Long, FlowPath> shouldRecomputeFlowPaths = |
| 100 | new HashMap<Long, FlowPath>(); |
| 101 | private Map<Long, FlowPath> modifiedFlowPaths = |
| 102 | new HashMap<Long, FlowPath>(); |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 103 | private Map<Long, FlowPath> checkIfInstalledFlowPaths = |
| 104 | new HashMap<Long, FlowPath>(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 105 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Constructor for a given Flow Manager and Datagrid Service. |
| 108 | * |
| 109 | * @param flowManager the Flow Manager to use. |
| 110 | * @param datagridService the Datagrid Service to use. |
| 111 | */ |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 112 | FlowEventHandler(FlowManager flowManager, |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 113 | IDatagridService datagridService) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 114 | this.flowManager = flowManager; |
| 115 | this.datagridService = datagridService; |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 116 | this.topology = new Topology(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 120 | * Get the network topology. |
| 121 | * |
| 122 | * @return the network topology. |
| 123 | */ |
| 124 | protected Topology getTopology() { return this.topology; } |
| 125 | |
| 126 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 127 | * Startup processing. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 128 | */ |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 129 | private void startup() { |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 130 | this.dbHandler = new GraphDBOperation(""); |
| 131 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 132 | // |
| 133 | // Obtain the initial Topology state |
| 134 | // |
| 135 | Collection<TopologyElement> topologyElements = |
| 136 | datagridService.getAllTopologyElements(); |
| 137 | for (TopologyElement topologyElement : topologyElements) { |
| 138 | EventEntry<TopologyElement> eventEntry = |
| 139 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 140 | topologyEvents.add(eventEntry); |
| 141 | } |
| 142 | // |
| 143 | // Obtain the initial Flow Path state |
| 144 | // |
| 145 | Collection<FlowPath> flowPaths = datagridService.getAllFlows(); |
| 146 | for (FlowPath flowPath : flowPaths) { |
| 147 | EventEntry<FlowPath> eventEntry = |
| 148 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 149 | flowPathEvents.add(eventEntry); |
| 150 | } |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 151 | // |
| 152 | // Obtain the initial FlowEntry state |
| 153 | // |
| 154 | Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries(); |
| 155 | for (FlowEntry flowEntry : flowEntries) { |
| 156 | EventEntry<FlowEntry> eventEntry = |
| 157 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 158 | flowEntryEvents.add(eventEntry); |
| 159 | } |
| 160 | |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 161 | // |
| 162 | // Obtain the initial FlowId state |
| 163 | // |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 164 | Collection<Pair<FlowId, Dpid>> flowIds = |
| 165 | datagridService.getAllFlowIds(); |
| 166 | for (Pair<FlowId, Dpid> pair : flowIds) { |
| 167 | EventEntry<Pair<FlowId, Dpid>> eventEntry = |
| 168 | new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, pair); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 169 | flowIdEvents.add(eventEntry); |
| 170 | } |
| 171 | |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 172 | // |
| 173 | // Obtain the initial FlowEntryId state |
| 174 | // |
| 175 | Collection<Pair<FlowEntryId, Dpid>> flowEntryIds = |
| 176 | datagridService.getAllFlowEntryIds(); |
| 177 | for (Pair<FlowEntryId, Dpid> pair : flowEntryIds) { |
| 178 | EventEntry<Pair<FlowEntryId, Dpid>> eventEntry = |
| 179 | new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, pair); |
| 180 | flowEntryIdEvents.add(eventEntry); |
| 181 | } |
| 182 | |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 183 | // Process the initial events (if any) |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 184 | synchronized (allFlowPaths) { |
| 185 | processEvents(); |
| 186 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Run the thread. |
| 191 | */ |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 192 | @Override |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 193 | public void run() { |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 194 | this.setName("FlowEventHandler " + this.getId()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 195 | startup(); |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 196 | |
| 197 | // |
| 198 | // The main loop |
| 199 | // |
| 200 | Collection<EventEntry<?>> collection = new LinkedList<EventEntry<?>>(); |
| 201 | try { |
| 202 | while (true) { |
| 203 | EventEntry<?> eventEntry = networkEvents.take(); |
| 204 | collection.add(eventEntry); |
| 205 | networkEvents.drainTo(collection); |
| 206 | |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 207 | // |
| 208 | // Demultiplex all events: |
| 209 | // - EventEntry<TopologyElement> |
| 210 | // - EventEntry<FlowPath> |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 211 | // - EventEntry<FlowEntry> |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 212 | // - EventEntry<Pair<FlowId, Dpid>> |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 213 | // - EventEntry<Pair<FlowEntryId, Dpid>> |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 214 | // |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 215 | for (EventEntry<?> event : collection) { |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 216 | // Topology event |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 217 | if (event.eventData() instanceof TopologyElement) { |
| 218 | EventEntry<TopologyElement> topologyEventEntry = |
| 219 | (EventEntry<TopologyElement>)event; |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 220 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 221 | topologyEvents.add(topologyEventEntry); |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 222 | continue; |
| 223 | } |
| 224 | |
| 225 | // FlowPath event |
| 226 | if (event.eventData() instanceof FlowPath) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 227 | EventEntry<FlowPath> flowPathEventEntry = |
| 228 | (EventEntry<FlowPath>)event; |
| 229 | flowPathEvents.add(flowPathEventEntry); |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
| 233 | // FlowEntry event |
| 234 | if (event.eventData() instanceof FlowEntry) { |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 235 | EventEntry<FlowEntry> flowEntryEventEntry = |
| 236 | (EventEntry<FlowEntry>)event; |
| 237 | flowEntryEvents.add(flowEntryEventEntry); |
Pavlin Radoslavov | c8038a8 | 2013-12-02 17:43:20 -0800 | [diff] [blame] | 238 | continue; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 239 | } |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 240 | |
| 241 | // FlowId event |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 242 | if (event.eventData() instanceof Pair) { |
| 243 | EventEntry<Pair<FlowId, Dpid>> flowIdEventEntry = |
| 244 | (EventEntry<Pair<FlowId, Dpid>>)event; |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 245 | flowIdEvents.add(flowIdEventEntry); |
| 246 | continue; |
| 247 | } |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 248 | |
| 249 | // Switch Dpid event |
| 250 | if (event.eventData() instanceof Dpid) { |
| 251 | EventEntry<Dpid> switchDpidEventEntry = |
| 252 | (EventEntry<Dpid>)event; |
| 253 | switchDpidEvents.add(switchDpidEventEntry); |
| 254 | continue; |
| 255 | } |
| 256 | |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 257 | // FlowEntryId event |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 258 | // TODO: Fix the code below if we need again to handle |
| 259 | // the FlowEntryId events |
| 260 | /* |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 261 | if (event.eventData() instanceof Pair) { |
| 262 | EventEntry<Pair<FlowEntryId, Dpid>> flowEntryIdEventEntry = |
| 263 | (EventEntry<Pair<FlowEntryId, Dpid>>)event; |
| 264 | flowEntryIdEvents.add(flowEntryIdEventEntry); |
| 265 | continue; |
| 266 | } |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 267 | */ |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 268 | } |
| 269 | collection.clear(); |
Pavlin Radoslavov | ed4c7a9 | 2013-10-26 21:36:21 -0700 | [diff] [blame] | 270 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 271 | // Process the events (if any) |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 272 | synchronized (allFlowPaths) { |
| 273 | processEvents(); |
| 274 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 275 | } |
| 276 | } catch (Exception exception) { |
| 277 | log.debug("Exception processing Network Events: ", exception); |
| 278 | } |
| 279 | } |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 280 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Process the events (if any) |
| 283 | */ |
| 284 | private void processEvents() { |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 285 | Collection<FlowEntry> modifiedFlowEntries; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 286 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 287 | if (enableOnrc2014MeasurementsFlows) { |
| 288 | |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 289 | PerformanceMonitor.start("EventHandler.ProcessAllEvents"); |
| 290 | |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 291 | if (topologyEvents.isEmpty() && flowIdEvents.isEmpty() && |
| 292 | switchDpidEvents.isEmpty()) { |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 293 | return; // Nothing to do |
| 294 | } |
| 295 | |
| 296 | Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches(); |
| 297 | |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 298 | // Process the Switch Dpid events |
| 299 | processSwitchDpidEvents(); |
| 300 | |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 301 | // Process the Flow ID events |
| 302 | processFlowIdEvents(mySwitches); |
| 303 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 304 | // Fetch the topology |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 305 | PerformanceMonitor.start("EventHandler.ReadTopology"); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 306 | processTopologyEvents(); |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 307 | PerformanceMonitor.stop("EventHandler.ReadTopology"); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 308 | |
| 309 | // Recompute all affected Flow Paths and keep only the modified |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 310 | PerformanceMonitor.start("EventHandler.RecomputeFlows"); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 311 | for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) { |
| 312 | if (recomputeFlowPath(flowPath)) |
| 313 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 314 | } |
| 315 | |
| 316 | // Assign the Flow Entry ID as needed |
| 317 | for (FlowPath flowPath : modifiedFlowPaths.values()) { |
| 318 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 319 | if (! flowEntry.isValidFlowEntryId()) { |
| 320 | long id = flowManager.getNextFlowEntryId(); |
| 321 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 322 | } |
| 323 | } |
| 324 | } |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 325 | PerformanceMonitor.stop("EventHandler.RecomputeFlows"); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 326 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 327 | // |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 328 | // Push the modified state to the database |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 329 | // |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 330 | PerformanceMonitor.start("EventHandler.WriteFlowsToDb"); |
Pavlin Radoslavov | 85d81c0 | 2014-01-10 17:22:22 -0800 | [diff] [blame] | 331 | for (FlowPath flowPath : modifiedFlowPaths.values()) { |
| 332 | // |
| 333 | // Delete the Flow Path from the Network Map |
| 334 | // |
| 335 | if (flowPath.flowPathUserState() == |
| 336 | FlowPathUserState.FP_USER_DELETE) { |
| 337 | log.debug("Deleting Flow Path From Database: {}", flowPath); |
Pavlin Radoslavov | 6c3e680 | 2014-01-10 19:27:34 -0800 | [diff] [blame] | 338 | // TODO: For now the deleting of a Flow Path is blocking |
Pavlin Radoslavov | 85d81c0 | 2014-01-10 17:22:22 -0800 | [diff] [blame] | 339 | ParallelFlowDatabaseOperation.deleteFlow(dbHandler, |
Pavlin Radoslavov | 6c3e680 | 2014-01-10 19:27:34 -0800 | [diff] [blame] | 340 | flowPath.flowId()); |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 341 | // |
| 342 | // NOTE: For now the sending of the notifications |
| 343 | // is outside of this loop, so the performance measurements |
| 344 | // are more accurate. |
| 345 | // |
| 346 | /* |
Pavlin Radoslavov | 6c3e680 | 2014-01-10 19:27:34 -0800 | [diff] [blame] | 347 | // Send the notifications for the deleted Flow Entries |
| 348 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 349 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 350 | } |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 351 | */ |
Pavlin Radoslavov | 6c3e680 | 2014-01-10 19:27:34 -0800 | [diff] [blame] | 352 | |
Pavlin Radoslavov | 85d81c0 | 2014-01-10 17:22:22 -0800 | [diff] [blame] | 353 | continue; |
| 354 | } |
| 355 | |
| 356 | log.debug("Pushing Flow Path To Database: {}", flowPath); |
| 357 | // |
| 358 | // Write the Flow Path to the Network Map |
| 359 | // |
| 360 | ParallelFlowDatabaseOperation.addFlow(dbHandler, flowPath, |
| 361 | datagridService); |
| 362 | } |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 363 | PerformanceMonitor.stop("EventHandler.WriteFlowsToDb"); |
| 364 | |
| 365 | // |
| 366 | // Send the notifications for the deleted Flow Entries |
| 367 | // NOTE: This code was pulled outside of the above loop, |
| 368 | // so the performance measurements are more accurate. |
| 369 | // |
| 370 | PerformanceMonitor.start("EventHandler.NotificationSend.FlowEntryRemoved"); |
| 371 | for (FlowPath flowPath : modifiedFlowPaths.values()) { |
| 372 | if (flowPath.flowPathUserState() == |
| 373 | FlowPathUserState.FP_USER_DELETE) { |
| 374 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 375 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 376 | } |
| 377 | } |
| 378 | } |
Pavlin Radoslavov | dbd09cc | 2014-01-12 18:13:35 -0800 | [diff] [blame] | 379 | PerformanceMonitor.stop("EventHandler.NotificationSend.FlowEntryRemoved"); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 380 | |
| 381 | // Cleanup |
| 382 | topologyEvents.clear(); |
| 383 | flowIdEvents.clear(); |
Pavlin Radoslavov | 2d6e5f1 | 2014-01-10 16:34:05 -0800 | [diff] [blame] | 384 | switchDpidEvents.clear(); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 385 | // |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 386 | // NOTE: Keep a cache with my Flow Paths |
| 387 | // allFlowPaths.clear(); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 388 | shouldRecomputeFlowPaths.clear(); |
| 389 | modifiedFlowPaths.clear(); |
| 390 | |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 391 | PerformanceMonitor.stop("EventHandler.ProcessAllEvents"); |
Pavlin Radoslavov | 8bd6d11 | 2014-01-12 20:12:37 -0800 | [diff] [blame^] | 392 | |
| 393 | |
| 394 | PerformanceMonitor.report("EventHandler.ReadTopology"); |
| 395 | PerformanceMonitor.report("EventHandler.RecomputeFlows"); |
| 396 | PerformanceMonitor.report("EventHandler.WriteFlowsToDb"); |
| 397 | PerformanceMonitor.report("EventHandler.NotificationSend.FlowEntryRemoved"); |
| 398 | PerformanceMonitor.report("EventHandler.ProcessAllEvents"); |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 399 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 403 | if (topologyEvents.isEmpty() && flowPathEvents.isEmpty() && |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 404 | flowEntryEvents.isEmpty()) { |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 405 | return; // Nothing to do |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 406 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 407 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 408 | processFlowPathEvents(); |
| 409 | processTopologyEvents(); |
Pavlin Radoslavov | 9bb4055 | 2013-12-18 21:08:30 -0800 | [diff] [blame] | 410 | processUnmatchedFlowEntryAdd(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 411 | processFlowEntryEvents(); |
| 412 | |
| 413 | // Recompute all affected Flow Paths and keep only the modified |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 414 | for (FlowPath flowPath : shouldRecomputeFlowPaths.values()) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 415 | if (recomputeFlowPath(flowPath)) |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 416 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 419 | // Extract the modified Flow Entries |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 420 | modifiedFlowEntries = extractModifiedFlowEntries(modifiedFlowPaths.values()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 421 | |
| 422 | // Assign missing Flow Entry IDs |
| 423 | assignFlowEntryId(modifiedFlowEntries); |
| 424 | |
| 425 | // |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 426 | // Push the modified state to the Flow Manager |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 427 | // |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 428 | flowManager.pushModifiedFlowState(modifiedFlowPaths.values(), |
| 429 | modifiedFlowEntries); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 430 | |
| 431 | // |
| 432 | // Remove Flow Entries that were deleted |
| 433 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 434 | for (FlowPath flowPath : modifiedFlowPaths.values()) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 435 | flowPath.dataPath().removeDeletedFlowEntries(); |
| 436 | |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 437 | // |
| 438 | // Check if Flow Paths have been installed into all switches, |
| 439 | // and generate the appropriate events. |
| 440 | // |
| 441 | checkInstalledFlowPaths(checkIfInstalledFlowPaths.values()); |
| 442 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 443 | // Cleanup |
| 444 | topologyEvents.clear(); |
| 445 | flowPathEvents.clear(); |
| 446 | flowEntryEvents.clear(); |
| 447 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 448 | shouldRecomputeFlowPaths.clear(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 449 | modifiedFlowPaths.clear(); |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 450 | checkIfInstalledFlowPaths.clear(); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Check if Flow Paths have been installed into all switches, |
| 455 | * and generate the appropriate events. |
| 456 | * |
| 457 | * @param flowPaths the flowPaths to process. |
| 458 | */ |
| 459 | private void checkInstalledFlowPaths(Collection<FlowPath> flowPaths) { |
| 460 | List<FlowPath> installedFlowPaths = new LinkedList<FlowPath>(); |
| 461 | |
| 462 | Kryo kryo = kryoFactory.newKryo(); |
| 463 | |
| 464 | for (FlowPath flowPath : flowPaths) { |
| 465 | boolean isInstalled = true; |
| 466 | |
| 467 | // |
| 468 | // Check whether all Flow Entries have been installed |
| 469 | // |
| 470 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 471 | if (flowEntry.flowEntrySwitchState() != |
| 472 | FlowEntrySwitchState.FE_SWITCH_UPDATED) { |
| 473 | isInstalled = false; |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (isInstalled) { |
| 479 | // Create a copy and add it to the list |
| 480 | FlowPath copyFlowPath = kryo.copy(flowPath); |
| 481 | installedFlowPaths.add(copyFlowPath); |
| 482 | } |
| 483 | } |
| 484 | kryoFactory.deleteKryo(kryo); |
| 485 | |
| 486 | // Generate an event for the installed Flow Path. |
| 487 | flowManager.notificationFlowPathsInstalled(installedFlowPaths); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Extract the modified Flow Entries. |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 492 | * |
| 493 | * @param modifiedFlowPaths the Flow Paths to process. |
| 494 | * @return a collection with the modified Flow Entries. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 495 | */ |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 496 | private Collection<FlowEntry> extractModifiedFlowEntries( |
| 497 | Collection<FlowPath> modifiedFlowPaths) { |
| 498 | List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>(); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 499 | |
| 500 | // Extract only the modified Flow Entries |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 501 | for (FlowPath flowPath : modifiedFlowPaths) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 502 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 503 | if (flowEntry.flowEntrySwitchState() == |
| 504 | FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) { |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 505 | modifiedFlowEntries.add(flowEntry); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | return modifiedFlowEntries; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Assign the Flow Entry ID as needed. |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 514 | * |
| 515 | * @param modifiedFlowEnries the collection of Flow Entries that need |
| 516 | * Flow Entry ID assigned. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 517 | */ |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 518 | private void assignFlowEntryId(Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 519 | if (modifiedFlowEntries.isEmpty()) |
| 520 | return; |
| 521 | |
| 522 | Map<Long, IOFSwitch> mySwitches = flowManager.getMySwitches(); |
| 523 | |
| 524 | // |
| 525 | // Assign the Flow Entry ID only for Flow Entries for my switches |
| 526 | // |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 527 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 528 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 529 | if (mySwitch == null) |
| 530 | continue; |
| 531 | if (! flowEntry.isValidFlowEntryId()) { |
| 532 | long id = flowManager.getNextFlowEntryId(); |
| 533 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 539 | * Fix a flow fetched from the database. |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 540 | * |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 541 | * @param flowPath the Flow to fix. |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 542 | */ |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 543 | private void fixFlowFromDatabase(FlowPath flowPath) { |
| 544 | // |
| 545 | // TODO: Bug workaround / fix : |
| 546 | // method FlowDatabaseOperation.extractFlowEntry() doesn't |
| 547 | // fetch the inPort and outPort, hence we assign them here. |
| 548 | // |
| 549 | // Assign the inPort and outPort for the Flow Entries |
| 550 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 551 | // Set the inPort |
| 552 | do { |
| 553 | if (flowEntry.inPort() != null) |
| 554 | break; |
| 555 | if (flowEntry.flowEntryMatch() == null) |
| 556 | break; |
| 557 | Port inPort = new Port(flowEntry.flowEntryMatch().inPort().value()); |
| 558 | flowEntry.setInPort(inPort); |
| 559 | } while (false); |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 560 | |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 561 | // Set the outPort |
| 562 | do { |
| 563 | if (flowEntry.outPort() != null) |
| 564 | break; |
| 565 | for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) { |
| 566 | if (fa.actionOutput() != null) { |
| 567 | Port outPort = new Port(fa.actionOutput().port().value()); |
| 568 | flowEntry.setOutPort(outPort); |
| 569 | break; |
| 570 | } |
Pavlin Radoslavov | 3a7cc90 | 2014-01-09 02:32:08 -0800 | [diff] [blame] | 571 | } |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 572 | } while (false); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 573 | } |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 574 | } |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 575 | |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 576 | /** |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 577 | * Process the Switch Dpid events. |
| 578 | */ |
| 579 | private void processSwitchDpidEvents() { |
| 580 | Map<Long, Dpid> addedSwitches = new HashMap<Long, Dpid>(); |
| 581 | Map<Long, Dpid> removedSwitches = new HashMap<Long, Dpid>(); |
| 582 | |
| 583 | // |
| 584 | // Process all Switch Dpid events and update the appropriate state |
| 585 | // |
| 586 | for (EventEntry<Dpid> eventEntry : switchDpidEvents) { |
| 587 | Dpid dpid = eventEntry.eventData(); |
| 588 | |
| 589 | log.debug("SwitchDpid Event: {} {}", eventEntry.eventType(), dpid); |
| 590 | |
| 591 | // Compute the final set of added and removed switches |
| 592 | switch (eventEntry.eventType()) { |
| 593 | case ENTRY_ADD: |
| 594 | addedSwitches.put(dpid.value(), dpid); |
| 595 | removedSwitches.remove(dpid.value()); |
| 596 | break; |
| 597 | case ENTRY_REMOVE: |
| 598 | addedSwitches.remove(dpid.value()); |
| 599 | removedSwitches.put(dpid.value(), dpid); |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | // |
| 605 | // Remove the Flows from the local cache if the removed |
| 606 | // switch is the Source Switch. |
| 607 | // |
| 608 | // TODO: This search can be expensive for a large number of flows |
| 609 | // and should be optmized. |
| 610 | // |
| 611 | List<FlowId> deleteFlowIds = new LinkedList<FlowId>(); |
| 612 | for (Dpid switchDpid : removedSwitches.values()) { |
| 613 | for (FlowPath flowPath : allFlowPaths.values()) { |
| 614 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 615 | if (srcDpid.value() == switchDpid.value()) |
| 616 | deleteFlowIds.add(flowPath.flowId()); |
| 617 | } |
| 618 | } |
| 619 | // |
| 620 | // Remove the Flows from the local cache |
| 621 | // |
| 622 | for (FlowId flowId : deleteFlowIds) |
| 623 | allFlowPaths.remove(flowId.value()); |
| 624 | |
| 625 | // Get the Flows for the added switches |
| 626 | Collection<FlowPath> flowPaths = |
| 627 | ParallelFlowDatabaseOperation.getFlowsForSwitches(dbHandler, |
| 628 | addedSwitches.values()); |
| 629 | for (FlowPath flowPath : flowPaths) { |
| 630 | allFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 635 | * Process the Flow ID events. |
| 636 | * |
| 637 | * @param mySwitches the collection of my switches. |
| 638 | */ |
| 639 | private void processFlowIdEvents(Map<Long, IOFSwitch> mySwitches) { |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 640 | List<FlowId> shouldFetchMyFlowIds = new LinkedList<FlowId>(); |
| 641 | |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 642 | // |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 643 | // Process all Flow Id events and update the appropriate state |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 644 | // |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 645 | for (EventEntry<Pair<FlowId, Dpid>> eventEntry : flowIdEvents) { |
| 646 | Pair<FlowId, Dpid> pair = eventEntry.eventData(); |
| 647 | FlowId flowId = pair.first; |
| 648 | Dpid dpid = pair.second; |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 649 | |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 650 | log.debug("Flow ID Event: {} {} {}", eventEntry.eventType(), |
| 651 | flowId, dpid); |
| 652 | |
| 653 | // |
| 654 | // Ignore Flows if the Source Switch is not controlled by this |
| 655 | // instance. |
| 656 | // |
| 657 | if (mySwitches.get(dpid.value()) == null) |
| 658 | continue; |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 659 | |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 660 | switch (eventEntry.eventType()) { |
| 661 | case ENTRY_ADD: { |
| 662 | // |
| 663 | // Add a new Flow Path |
| 664 | // |
| 665 | if (allFlowPaths.get(flowId.value()) != null) { |
| 666 | // |
| 667 | // TODO: What to do if the Flow Path already exists? |
| 668 | // Fow now, we just re-add it. |
| 669 | // |
Pavlin Radoslavov | 3a7cc90 | 2014-01-09 02:32:08 -0800 | [diff] [blame] | 670 | } |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 671 | shouldFetchMyFlowIds.add(flowId); |
| 672 | |
| 673 | break; |
Pavlin Radoslavov | 3a7cc90 | 2014-01-09 02:32:08 -0800 | [diff] [blame] | 674 | } |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 675 | |
| 676 | case ENTRY_REMOVE: { |
| 677 | // |
| 678 | // Remove an existing Flow Path. |
| 679 | // |
| 680 | // Find the Flow Path, and mark the Flow and its Flow Entries |
| 681 | // for deletion. |
| 682 | // |
| 683 | FlowPath existingFlowPath = |
| 684 | allFlowPaths.get(flowId.value()); |
| 685 | if (existingFlowPath == null) |
| 686 | continue; // Nothing to do |
| 687 | |
| 688 | existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE); |
| 689 | for (FlowEntry flowEntry : existingFlowPath.flowEntries()) { |
| 690 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 691 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 692 | } |
| 693 | |
| 694 | // Remove the Flow Path from the internal state |
| 695 | Long key = existingFlowPath.flowId().value(); |
| 696 | allFlowPaths.remove(key); |
| 697 | shouldRecomputeFlowPaths.remove(key); |
| 698 | modifiedFlowPaths.put(key, existingFlowPath); |
| 699 | |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // Get my Flows |
| 706 | Collection<FlowPath> myFlows = |
Pavlin Radoslavov | 6602b6a | 2014-01-10 16:43:29 -0800 | [diff] [blame] | 707 | ParallelFlowDatabaseOperation.getFlows(dbHandler, |
| 708 | shouldFetchMyFlowIds); |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 709 | |
| 710 | for (FlowPath flowPath : myFlows) { |
| 711 | fixFlowFromDatabase(flowPath); |
| 712 | |
| 713 | switch (flowPath.flowPathType()) { |
| 714 | case FP_TYPE_SHORTEST_PATH: |
| 715 | // |
| 716 | // Reset the Data Path, in case it was set already, because |
| 717 | // we are going to recompute it anyway. |
| 718 | // |
| 719 | flowPath.flowEntries().clear(); |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 720 | shouldRecomputeFlowPaths.put(flowPath.flowId().value(), |
| 721 | flowPath); |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 722 | break; |
| 723 | case FP_TYPE_EXPLICIT_PATH: |
| 724 | // |
| 725 | // Mark all Flow Entries for installation in the switches. |
| 726 | // |
| 727 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 728 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 729 | } |
| 730 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 731 | break; |
| 732 | case FP_TYPE_UNKNOWN: |
| 733 | log.error("FlowPath event with unknown type"); |
| 734 | break; |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 735 | } |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 736 | allFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 737 | } |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 741 | * Process the Flow Entry ID events. |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 742 | * |
| 743 | * @param mySwitches the collection of my switches. |
| 744 | * @return a collection of modified Flow Entries this instance needs |
| 745 | * to push to its own switches. |
| 746 | */ |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 747 | private Collection<FlowEntry> processFlowEntryIdEvents(Map<Long, IOFSwitch> mySwitches) { |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 748 | List<FlowEntry> modifiedFlowEntries = new LinkedList<FlowEntry>(); |
| 749 | |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 750 | // |
| 751 | // Process all Flow ID events and update the appropriate state |
| 752 | // |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 753 | for (EventEntry<Pair<FlowEntryId, Dpid>> eventEntry : flowEntryIdEvents) { |
| 754 | Pair<FlowEntryId, Dpid> pair = eventEntry.eventData(); |
| 755 | FlowEntryId flowEntryId = pair.first; |
| 756 | Dpid dpid = pair.second; |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 757 | |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 758 | log.debug("Flow Entry ID Event: {} {} {}", eventEntry.eventType(), |
| 759 | flowEntryId, dpid); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 760 | |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 761 | if (mySwitches.get(dpid.value()) == null) |
| 762 | continue; |
| 763 | |
| 764 | // Fetch the Flow Entry |
| 765 | FlowEntry flowEntry = FlowDatabaseOperation.getFlowEntry(dbHandler, |
| 766 | flowEntryId); |
| 767 | if (flowEntry == null) { |
| 768 | log.debug("Flow Entry ID {} : Flow Entry not found!", |
| 769 | flowEntryId); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 770 | continue; |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 771 | } |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 772 | modifiedFlowEntries.add(flowEntry); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 773 | } |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 774 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 775 | return modifiedFlowEntries; |
| 776 | } |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 777 | |
| 778 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 779 | * Process the Flow Path events. |
| 780 | */ |
| 781 | private void processFlowPathEvents() { |
| 782 | // |
| 783 | // Process all Flow Path events and update the appropriate state |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 784 | // |
| 785 | for (EventEntry<FlowPath> eventEntry : flowPathEvents) { |
| 786 | FlowPath flowPath = eventEntry.eventData(); |
| 787 | |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 788 | log.debug("Flow Event: {} {}", eventEntry.eventType(), flowPath); |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 789 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 790 | switch (eventEntry.eventType()) { |
| 791 | case ENTRY_ADD: { |
| 792 | // |
| 793 | // Add a new Flow Path |
| 794 | // |
| 795 | if (allFlowPaths.get(flowPath.flowId().value()) != null) { |
| 796 | // |
| 797 | // TODO: What to do if the Flow Path already exists? |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 798 | // Fow now, we just re-add it. |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 799 | // |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | switch (flowPath.flowPathType()) { |
| 803 | case FP_TYPE_SHORTEST_PATH: |
| 804 | // |
| 805 | // Reset the Data Path, in case it was set already, because |
| 806 | // we are going to recompute it anyway. |
| 807 | // |
| 808 | flowPath.flowEntries().clear(); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 809 | shouldRecomputeFlowPaths.put(flowPath.flowId().value(), |
| 810 | flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 811 | break; |
| 812 | case FP_TYPE_EXPLICIT_PATH: |
| 813 | // |
| 814 | // Mark all Flow Entries for installation in the switches. |
| 815 | // |
| 816 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 817 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 818 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 819 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 820 | break; |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 821 | case FP_TYPE_UNKNOWN: |
| 822 | log.error("FlowPath event with unknown type"); |
| 823 | break; |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 824 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 825 | allFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 826 | |
| 827 | break; |
| 828 | } |
| 829 | |
| 830 | case ENTRY_REMOVE: { |
| 831 | // |
| 832 | // Remove an existing Flow Path. |
| 833 | // |
| 834 | // Find the Flow Path, and mark the Flow and its Flow Entries |
| 835 | // for deletion. |
| 836 | // |
| 837 | FlowPath existingFlowPath = |
| 838 | allFlowPaths.get(flowPath.flowId().value()); |
| 839 | if (existingFlowPath == null) |
| 840 | continue; // Nothing to do |
| 841 | |
| 842 | existingFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_DELETE); |
| 843 | for (FlowEntry flowEntry : existingFlowPath.flowEntries()) { |
| 844 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 845 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 846 | } |
| 847 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 848 | // Remove the Flow Path from the internal state |
| 849 | Long key = existingFlowPath.flowId().value(); |
| 850 | allFlowPaths.remove(key); |
| 851 | shouldRecomputeFlowPaths.remove(key); |
| 852 | modifiedFlowPaths.put(key, existingFlowPath); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 853 | |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 858 | } |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 859 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 860 | /** |
| 861 | * Process the Topology events. |
| 862 | */ |
| 863 | private void processTopologyEvents() { |
Pavlin Radoslavov | 1b45490 | 2014-01-09 16:28:27 -0800 | [diff] [blame] | 864 | boolean isTopologyModified = false; |
| 865 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 866 | if (enableOnrc2014MeasurementsTopology) { |
| 867 | if (topologyEvents.isEmpty()) |
| 868 | return; |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 869 | |
Pavlin Radoslavov | 2a8b9de | 2014-01-09 15:58:32 -0800 | [diff] [blame] | 870 | // TODO: Code for debugging purpose only |
| 871 | for (EventEntry<TopologyElement> eventEntry : topologyEvents) { |
| 872 | TopologyElement topologyElement = eventEntry.eventData(); |
| 873 | log.debug("Topology Event: {} {}", eventEntry.eventType(), |
| 874 | topologyElement.toString()); |
| 875 | } |
| 876 | |
Pavlin Radoslavov | 1b45490 | 2014-01-09 16:28:27 -0800 | [diff] [blame] | 877 | log.debug("[BEFORE] {}", topology.toString()); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 878 | topology.readFromDatabase(dbHandler); |
| 879 | log.debug("[AFTER] {}", topology.toString()); |
| 880 | shouldRecomputeFlowPaths.putAll(allFlowPaths); |
| 881 | return; |
| 882 | } |
| 883 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 884 | // |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 885 | // Process all Topology events and update the appropriate state |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 886 | // |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 887 | for (EventEntry<TopologyElement> eventEntry : topologyEvents) { |
| 888 | TopologyElement topologyElement = eventEntry.eventData(); |
Naoki Shiota | 0abe38d | 2014-01-07 15:31:22 -0800 | [diff] [blame] | 889 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 890 | log.debug("Topology Event: {} {}", eventEntry.eventType(), |
| 891 | topologyElement.toString()); |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 892 | |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 893 | switch (eventEntry.eventType()) { |
| 894 | case ENTRY_ADD: |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 895 | isTopologyModified |= topology.addTopologyElement(topologyElement); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 896 | break; |
| 897 | case ENTRY_REMOVE: |
Naoki Shiota | 9f6fc21 | 2014-01-09 21:38:08 -0800 | [diff] [blame] | 898 | isTopologyModified |= topology.removeTopologyElement(topologyElement); |
Pavlin Radoslavov | 80bc3cf | 2014-01-08 11:17:30 -0800 | [diff] [blame] | 899 | break; |
| 900 | } |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 901 | } |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 902 | if (isTopologyModified) { |
| 903 | // TODO: For now, if the topology changes, we recompute all Flows |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 904 | shouldRecomputeFlowPaths.putAll(allFlowPaths); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 905 | } |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 906 | } |
Pavlin Radoslavov | a23e541 | 2013-10-27 19:56:40 -0700 | [diff] [blame] | 907 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 908 | /** |
Pavlin Radoslavov | 9bb4055 | 2013-12-18 21:08:30 -0800 | [diff] [blame] | 909 | * Process previously received Flow Entries with unmatched Flow Paths. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 910 | */ |
Pavlin Radoslavov | 9bb4055 | 2013-12-18 21:08:30 -0800 | [diff] [blame] | 911 | private void processUnmatchedFlowEntryAdd() { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 912 | FlowPath flowPath; |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 913 | FlowEntry localFlowEntry; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 914 | |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 915 | // |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 916 | // Update Flow Entries with previously unmatched Flow Entry updates |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 917 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 918 | if (! unmatchedFlowEntryAdd.isEmpty()) { |
| 919 | Map<Long, FlowEntry> remainingUpdates = new HashMap<Long, FlowEntry>(); |
| 920 | for (FlowEntry flowEntry : unmatchedFlowEntryAdd.values()) { |
Pavlin Radoslavov | 9bb4055 | 2013-12-18 21:08:30 -0800 | [diff] [blame] | 921 | // log.debug("Processing Unmatched Flow Entry: {}", |
| 922 | // flowEntry.toString()); |
| 923 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 924 | flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
Pavlin Radoslavov | 9bb4055 | 2013-12-18 21:08:30 -0800 | [diff] [blame] | 925 | if (flowPath == null) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 926 | remainingUpdates.put(flowEntry.flowEntryId().value(), |
| 927 | flowEntry); |
| 928 | continue; |
| 929 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 930 | localFlowEntry = findFlowEntryAdd(flowPath, flowEntry); |
| 931 | if (localFlowEntry == null) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 932 | remainingUpdates.put(flowEntry.flowEntryId().value(), |
| 933 | flowEntry); |
| 934 | continue; |
| 935 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 936 | if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) { |
| 937 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 938 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 939 | } |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 940 | unmatchedFlowEntryAdd = remainingUpdates; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 941 | } |
Pavlin Radoslavov | 9bb4055 | 2013-12-18 21:08:30 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Process the Flow Entry events. |
| 946 | */ |
| 947 | private void processFlowEntryEvents() { |
| 948 | FlowPath flowPath; |
| 949 | FlowEntry localFlowEntry; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 950 | |
| 951 | // |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 952 | // Process all Flow Entry events and update the appropriate state |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 953 | // |
| 954 | for (EventEntry<FlowEntry> eventEntry : flowEntryEvents) { |
| 955 | FlowEntry flowEntry = eventEntry.eventData(); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 956 | |
| 957 | log.debug("Flow Entry Event: {} {}", eventEntry.eventType(), |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 958 | flowEntry); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 959 | |
| 960 | if ((! flowEntry.isValidFlowId()) || |
| 961 | (! flowEntry.isValidFlowEntryId())) { |
| 962 | continue; |
| 963 | } |
| 964 | |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 965 | switch (eventEntry.eventType()) { |
| 966 | case ENTRY_ADD: |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 967 | flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
| 968 | if (flowPath == null) { |
| 969 | // Flow Path not found: keep the entry for later matching |
| 970 | unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(), |
| 971 | flowEntry); |
| 972 | break; |
| 973 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 974 | localFlowEntry = findFlowEntryAdd(flowPath, flowEntry); |
| 975 | if (localFlowEntry == null) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 976 | // Flow Entry not found: keep the entry for later matching |
| 977 | unmatchedFlowEntryAdd.put(flowEntry.flowEntryId().value(), |
| 978 | flowEntry); |
| 979 | break; |
| 980 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 981 | if (updateFlowEntryAdd(flowPath, localFlowEntry, flowEntry)) { |
| 982 | // Add the updated Flow Path to the list of updated paths |
| 983 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 984 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 985 | break; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 986 | |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 987 | case ENTRY_REMOVE: |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 988 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 989 | if (unmatchedFlowEntryAdd.remove(flowEntry.flowEntryId().value()) != null) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 990 | continue; // Removed previously unmatched entry |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 991 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 992 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 993 | flowPath = allFlowPaths.get(flowEntry.flowId().value()); |
| 994 | if (flowPath == null) { |
| 995 | // Flow Path not found: ignore the update |
| 996 | break; |
| 997 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 998 | localFlowEntry = findFlowEntryRemove(flowPath, flowEntry); |
| 999 | if (localFlowEntry == null) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 1000 | // Flow Entry not found: ignore it |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1001 | break; |
| 1002 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1003 | if (updateFlowEntryRemove(flowPath, localFlowEntry, |
| 1004 | flowEntry)) { |
| 1005 | // Add the updated Flow Path to the list of updated paths |
| 1006 | modifiedFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 1007 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1008 | break; |
| 1009 | } |
| 1010 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /** |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1014 | * Find a Flow Entry that should be updated because of an external |
| 1015 | * ENTRY_ADD event. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1016 | * |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1017 | * @param flowPath the FlowPath for the Flow Entry to update. |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1018 | * @param newFlowEntry the FlowEntry with the new state. |
| 1019 | * @return the Flow Entry that should be updated if found, otherwise null. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1020 | */ |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1021 | private FlowEntry findFlowEntryAdd(FlowPath flowPath, |
| 1022 | FlowEntry newFlowEntry) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1023 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1024 | // Iterate over all Flow Entries and find a match. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1025 | // |
| 1026 | for (FlowEntry localFlowEntry : flowPath.flowEntries()) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1027 | if (! TopologyManager.isSameFlowEntryDataPath(localFlowEntry, |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1028 | newFlowEntry)) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1029 | continue; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1030 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1031 | |
| 1032 | // |
| 1033 | // Local Flow Entry match found |
| 1034 | // |
| 1035 | if (localFlowEntry.isValidFlowEntryId()) { |
| 1036 | if (localFlowEntry.flowEntryId().value() != |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1037 | newFlowEntry.flowEntryId().value()) { |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1038 | // |
| 1039 | // Find a local Flow Entry, but the Flow Entry ID doesn't |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1040 | // match. Keep looking. |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1041 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1042 | continue; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1043 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1044 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1045 | return localFlowEntry; |
| 1046 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1047 | |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1048 | return null; // Entry not found |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Update a Flow Entry because of an external ENTRY_ADD event. |
| 1053 | * |
| 1054 | * @param flowPath the FlowPath for the Flow Entry to update. |
| 1055 | * @param localFlowEntry the local Flow Entry to update. |
| 1056 | * @param newFlowEntry the FlowEntry with the new state. |
| 1057 | * @return true if the local Flow Entry was updated, otherwise false. |
| 1058 | */ |
| 1059 | private boolean updateFlowEntryAdd(FlowPath flowPath, |
| 1060 | FlowEntry localFlowEntry, |
| 1061 | FlowEntry newFlowEntry) { |
| 1062 | boolean updated = false; |
| 1063 | |
| 1064 | if (localFlowEntry.flowEntryUserState() == |
| 1065 | FlowEntryUserState.FE_USER_DELETE) { |
| 1066 | // Don't add-back a Flow Entry that is already deleted |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | if (! localFlowEntry.isValidFlowEntryId()) { |
| 1071 | // Update the Flow Entry ID |
| 1072 | FlowEntryId flowEntryId = |
| 1073 | new FlowEntryId(newFlowEntry.flowEntryId().value()); |
| 1074 | localFlowEntry.setFlowEntryId(flowEntryId); |
| 1075 | updated = true; |
| 1076 | } |
| 1077 | |
| 1078 | // |
| 1079 | // Update the local Flow Entry, and keep state to check |
| 1080 | // if the Flow Path has been installed. |
| 1081 | // |
| 1082 | if (localFlowEntry.flowEntryUserState() != |
| 1083 | newFlowEntry.flowEntryUserState()) { |
| 1084 | localFlowEntry.setFlowEntryUserState( |
| 1085 | newFlowEntry.flowEntryUserState()); |
| 1086 | updated = true; |
| 1087 | } |
| 1088 | if (localFlowEntry.flowEntrySwitchState() != |
| 1089 | newFlowEntry.flowEntrySwitchState()) { |
| 1090 | localFlowEntry.setFlowEntrySwitchState( |
| 1091 | newFlowEntry.flowEntrySwitchState()); |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 1092 | checkIfInstalledFlowPaths.put(flowPath.flowId().value(), flowPath); |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1093 | updated = true; |
| 1094 | } |
| 1095 | |
| 1096 | return updated; |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * Find a Flow Entry that should be updated because of an external |
| 1101 | * ENTRY_REMOVE event. |
| 1102 | * |
| 1103 | * @param flowPath the FlowPath for the Flow Entry to update. |
| 1104 | * @param newFlowEntry the FlowEntry with the new state. |
| 1105 | * @return the Flow Entry that should be updated if found, otherwise null. |
| 1106 | */ |
| 1107 | private FlowEntry findFlowEntryRemove(FlowPath flowPath, |
| 1108 | FlowEntry newFlowEntry) { |
| 1109 | // |
| 1110 | // Iterate over all Flow Entries and find a match based on |
| 1111 | // the Flow Entry ID. |
| 1112 | // |
| 1113 | for (FlowEntry localFlowEntry : flowPath.flowEntries()) { |
| 1114 | if (! localFlowEntry.isValidFlowEntryId()) |
| 1115 | continue; |
| 1116 | if (localFlowEntry.flowEntryId().value() != |
| 1117 | newFlowEntry.flowEntryId().value()) { |
| 1118 | continue; |
| 1119 | } |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1120 | return localFlowEntry; |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1123 | return null; // Entry not found |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * Update a Flow Entry because of an external ENTRY_REMOVE event. |
| 1128 | * |
| 1129 | * @param flowPath the FlowPath for the Flow Entry to update. |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1130 | * @param localFlowEntry the local Flow Entry to update. |
| 1131 | * @param newFlowEntry the FlowEntry with the new state. |
| 1132 | * @return true if the local Flow Entry was updated, otherwise false. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1133 | */ |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1134 | private boolean updateFlowEntryRemove(FlowPath flowPath, |
| 1135 | FlowEntry localFlowEntry, |
| 1136 | FlowEntry newFlowEntry) { |
| 1137 | boolean updated = false; |
| 1138 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1139 | // |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1140 | // Update the local Flow Entry. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1141 | // |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1142 | if (localFlowEntry.flowEntryUserState() != |
| 1143 | newFlowEntry.flowEntryUserState()) { |
| 1144 | localFlowEntry.setFlowEntryUserState( |
| 1145 | newFlowEntry.flowEntryUserState()); |
| 1146 | updated = true; |
| 1147 | } |
| 1148 | if (localFlowEntry.flowEntrySwitchState() != |
| 1149 | newFlowEntry.flowEntrySwitchState()) { |
| 1150 | localFlowEntry.setFlowEntrySwitchState( |
| 1151 | newFlowEntry.flowEntrySwitchState()); |
| 1152 | updated = true; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 1155 | return updated; |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /** |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1159 | * Recompute a Flow Path. |
| 1160 | * |
| 1161 | * @param flowPath the Flow Path to recompute. |
| 1162 | * @return true if the recomputed Flow Path has changed, otherwise false. |
| 1163 | */ |
| 1164 | private boolean recomputeFlowPath(FlowPath flowPath) { |
| 1165 | boolean hasChanged = false; |
| 1166 | |
Pavlin Radoslavov | 7bf837a | 2014-01-09 14:22:05 -0800 | [diff] [blame] | 1167 | if (enableOnrc2014MeasurementsFlows) { |
| 1168 | // Cleanup the deleted Flow Entries from the earlier iteration |
| 1169 | flowPath.dataPath().removeDeletedFlowEntries(); |
Pavlin Radoslavov | 737aa52 | 2014-01-09 15:35:00 -0800 | [diff] [blame] | 1170 | |
| 1171 | // |
| 1172 | // TODO: Fake it that the Flow Entries have been already pushed |
| 1173 | // into the switches, so we don't push them again. |
| 1174 | // |
| 1175 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 1176 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED); |
| 1177 | } |
Pavlin Radoslavov | 7bf837a | 2014-01-09 14:22:05 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1180 | // |
| 1181 | // Test whether the Flow Path needs to be recomputed |
| 1182 | // |
| 1183 | switch (flowPath.flowPathType()) { |
Yuta HIGUCHI | e1038fb | 2013-10-30 15:35:18 -0700 | [diff] [blame] | 1184 | case FP_TYPE_UNKNOWN: |
| 1185 | return false; // Can't recompute on Unknown FlowType |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1186 | case FP_TYPE_SHORTEST_PATH: |
| 1187 | break; |
| 1188 | case FP_TYPE_EXPLICIT_PATH: |
| 1189 | return false; // An explicit path never changes |
| 1190 | } |
| 1191 | |
| 1192 | DataPath oldDataPath = flowPath.dataPath(); |
| 1193 | |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 1194 | // Compute the new path |
Naoki Shiota | 9f6fc21 | 2014-01-09 21:38:08 -0800 | [diff] [blame] | 1195 | DataPath newDataPath = TopologyManager.computeNetworkPath(topology, |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 1196 | flowPath); |
Naoki Shiota | f74d5f3 | 2014-01-09 21:29:38 -0800 | [diff] [blame] | 1197 | |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1198 | if (newDataPath == null) { |
| 1199 | // We need the DataPath to compare the paths |
| 1200 | newDataPath = new DataPath(); |
| 1201 | } |
| 1202 | newDataPath.applyFlowPathFlags(flowPath.flowPathFlags()); |
| 1203 | |
| 1204 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 1205 | // Test whether the new path is same |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1206 | // |
| 1207 | if (oldDataPath.flowEntries().size() != |
| 1208 | newDataPath.flowEntries().size()) { |
| 1209 | hasChanged = true; |
| 1210 | } else { |
| 1211 | Iterator<FlowEntry> oldIter = oldDataPath.flowEntries().iterator(); |
| 1212 | Iterator<FlowEntry> newIter = newDataPath.flowEntries().iterator(); |
| 1213 | while (oldIter.hasNext() && newIter.hasNext()) { |
| 1214 | FlowEntry oldFlowEntry = oldIter.next(); |
| 1215 | FlowEntry newFlowEntry = newIter.next(); |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 1216 | if (! TopologyManager.isSameFlowEntryDataPath(oldFlowEntry, |
| 1217 | newFlowEntry)) { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1218 | hasChanged = true; |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | if (! hasChanged) |
| 1224 | return hasChanged; |
| 1225 | |
| 1226 | // |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 1227 | // Merge the changes in the path: |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1228 | // - If a Flow Entry for a switch is in the old data path, but not |
| 1229 | // in the new data path, then mark it for deletion. |
| 1230 | // - If a Flow Entry for a switch is in the new data path, but not |
| 1231 | // in the old data path, then mark it for addition. |
| 1232 | // - If a Flow Entry for a switch is in both the old and the new |
| 1233 | // data path, but it has changed, e.g., the incoming and/or outgoing |
| 1234 | // port(s), then mark the old Flow Entry for deletion, and mark |
| 1235 | // the new Flow Entry for addition. |
| 1236 | // - If a Flow Entry for a switch is in both the old and the new |
| 1237 | // data path, and it hasn't changed, then just keep it. |
| 1238 | // |
| 1239 | // NOTE: We use the Switch DPID of each entry to match the entries |
| 1240 | // |
| 1241 | Map<Long, FlowEntry> oldFlowEntriesMap = new HashMap<Long, FlowEntry>(); |
| 1242 | Map<Long, FlowEntry> newFlowEntriesMap = new HashMap<Long, FlowEntry>(); |
| 1243 | ArrayList<FlowEntry> finalFlowEntries = new ArrayList<FlowEntry>(); |
| 1244 | List<FlowEntry> deletedFlowEntries = new LinkedList<FlowEntry>(); |
| 1245 | |
| 1246 | // Prepare maps with the Flow Entries, so they are fast to lookup |
| 1247 | for (FlowEntry flowEntry : oldDataPath.flowEntries()) |
| 1248 | oldFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry); |
| 1249 | for (FlowEntry flowEntry : newDataPath.flowEntries()) |
| 1250 | newFlowEntriesMap.put(flowEntry.dpid().value(), flowEntry); |
| 1251 | |
| 1252 | // |
| 1253 | // Find the old Flow Entries that should be deleted |
| 1254 | // |
| 1255 | for (FlowEntry oldFlowEntry : oldDataPath.flowEntries()) { |
| 1256 | FlowEntry newFlowEntry = |
| 1257 | newFlowEntriesMap.get(oldFlowEntry.dpid().value()); |
| 1258 | if (newFlowEntry == null) { |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 1259 | // The old Flow Entry should be deleted: not on the path |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1260 | oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 1261 | oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 1262 | deletedFlowEntries.add(oldFlowEntry); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | // |
| 1267 | // Find the new Flow Entries that should be added or updated |
| 1268 | // |
| 1269 | int idx = 0; |
| 1270 | for (FlowEntry newFlowEntry : newDataPath.flowEntries()) { |
| 1271 | FlowEntry oldFlowEntry = |
| 1272 | oldFlowEntriesMap.get(newFlowEntry.dpid().value()); |
| 1273 | |
| 1274 | if ((oldFlowEntry != null) && |
Pavlin Radoslavov | 3ecd41e | 2013-10-29 14:29:30 -0700 | [diff] [blame] | 1275 | TopologyManager.isSameFlowEntryDataPath(oldFlowEntry, |
| 1276 | newFlowEntry)) { |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1277 | // |
| 1278 | // Both Flow Entries are same |
| 1279 | // |
| 1280 | finalFlowEntries.add(oldFlowEntry); |
| 1281 | idx++; |
| 1282 | continue; |
| 1283 | } |
| 1284 | |
| 1285 | if (oldFlowEntry != null) { |
| 1286 | // |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 1287 | // The old Flow Entry should be deleted: path diverges |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1288 | // |
| 1289 | oldFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 1290 | oldFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 1291 | deletedFlowEntries.add(oldFlowEntry); |
| 1292 | } |
| 1293 | |
| 1294 | // |
| 1295 | // Add the new Flow Entry |
| 1296 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 1297 | // |
| 1298 | // NOTE: Assign only the Flow ID. |
| 1299 | // The Flow Entry ID is assigned later only for the Flow Entries |
| 1300 | // this instance is responsible for. |
| 1301 | // |
| 1302 | newFlowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1303 | |
Pavlin Radoslavov | a4df952 | 2013-12-03 11:03:04 -0800 | [diff] [blame] | 1304 | // |
Pavlin Radoslavov | 6fde217 | 2013-12-10 11:23:18 -0800 | [diff] [blame] | 1305 | // Copy the Flow timeouts |
| 1306 | // |
| 1307 | newFlowEntry.setIdleTimeout(flowPath.idleTimeout()); |
| 1308 | newFlowEntry.setHardTimeout(flowPath.hardTimeout()); |
| 1309 | |
| 1310 | // |
Pavlin Radoslavov | a4df952 | 2013-12-03 11:03:04 -0800 | [diff] [blame] | 1311 | // Allocate the FlowEntryMatch by copying the default one |
| 1312 | // from the FlowPath (if set). |
| 1313 | // |
| 1314 | FlowEntryMatch flowEntryMatch = null; |
| 1315 | if (flowPath.flowEntryMatch() != null) |
| 1316 | flowEntryMatch = new FlowEntryMatch(flowPath.flowEntryMatch()); |
| 1317 | else |
| 1318 | flowEntryMatch = new FlowEntryMatch(); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1319 | newFlowEntry.setFlowEntryMatch(flowEntryMatch); |
Pavlin Radoslavov | a4df952 | 2013-12-03 11:03:04 -0800 | [diff] [blame] | 1320 | |
| 1321 | // Set the incoming port matching |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1322 | flowEntryMatch.enableInPort(newFlowEntry.inPort()); |
| 1323 | |
| 1324 | // |
| 1325 | // Set the actions: |
| 1326 | // If the first Flow Entry, copy the Flow Path actions to it. |
| 1327 | // |
| 1328 | FlowEntryActions flowEntryActions = newFlowEntry.flowEntryActions(); |
| 1329 | if ((idx == 0) && (flowPath.flowEntryActions() != null)) { |
| 1330 | FlowEntryActions flowActions = |
| 1331 | new FlowEntryActions(flowPath.flowEntryActions()); |
| 1332 | for (FlowEntryAction action : flowActions.actions()) |
| 1333 | flowEntryActions.addAction(action); |
| 1334 | } |
| 1335 | idx++; |
| 1336 | |
| 1337 | // |
| 1338 | // Add the outgoing port output action |
| 1339 | // |
| 1340 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1341 | flowEntryAction.setActionOutput(newFlowEntry.outPort()); |
| 1342 | flowEntryActions.addAction(flowEntryAction); |
| 1343 | |
| 1344 | // |
| 1345 | // Set the state of the new Flow Entry |
| 1346 | // |
| 1347 | newFlowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
| 1348 | newFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 1349 | finalFlowEntries.add(newFlowEntry); |
| 1350 | } |
| 1351 | |
| 1352 | // |
| 1353 | // Replace the old Flow Entries with the new Flow Entries. |
| 1354 | // Note that the Flow Entries that will be deleted are added at |
| 1355 | // the end. |
| 1356 | // |
Pavlin Radoslavov | fb94edc | 2013-11-04 16:29:40 -0800 | [diff] [blame] | 1357 | finalFlowEntries.addAll(deletedFlowEntries); |
Pavlin Radoslavov | fb06a9e | 2013-10-28 23:56:15 -0700 | [diff] [blame] | 1358 | flowPath.dataPath().setFlowEntries(finalFlowEntries); |
| 1359 | |
| 1360 | return hasChanged; |
| 1361 | } |
| 1362 | |
| 1363 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1364 | * Receive a notification that a Flow is added. |
| 1365 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1366 | * @param flowPath the Flow that is added. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1367 | */ |
| 1368 | @Override |
| 1369 | public void notificationRecvFlowAdded(FlowPath flowPath) { |
| 1370 | EventEntry<FlowPath> eventEntry = |
| 1371 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 1372 | networkEvents.add(eventEntry); |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * Receive a notification that a Flow is removed. |
| 1377 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1378 | * @param flowPath the Flow that is removed. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1379 | */ |
| 1380 | @Override |
| 1381 | public void notificationRecvFlowRemoved(FlowPath flowPath) { |
| 1382 | EventEntry<FlowPath> eventEntry = |
| 1383 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_REMOVE, flowPath); |
| 1384 | networkEvents.add(eventEntry); |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * Receive a notification that a Flow is updated. |
| 1389 | * |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1390 | * @param flowPath the Flow that is updated. |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1391 | */ |
| 1392 | @Override |
| 1393 | public void notificationRecvFlowUpdated(FlowPath flowPath) { |
| 1394 | // NOTE: The ADD and UPDATE events are processed in same way |
| 1395 | EventEntry<FlowPath> eventEntry = |
| 1396 | new EventEntry<FlowPath>(EventEntry.Type.ENTRY_ADD, flowPath); |
| 1397 | networkEvents.add(eventEntry); |
| 1398 | } |
| 1399 | |
| 1400 | /** |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1401 | * Receive a notification that a FlowEntry is added. |
| 1402 | * |
| 1403 | * @param flowEntry the FlowEntry that is added. |
| 1404 | */ |
| 1405 | @Override |
| 1406 | public void notificationRecvFlowEntryAdded(FlowEntry flowEntry) { |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1407 | if (enableOnrc2014MeasurementsFlows) { |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 1408 | PerformanceMonitor.start("EventHandler.AddFlowEntryToSwitch"); |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1409 | Collection entries = new ArrayList(); |
| 1410 | entries.add(flowEntry); |
| 1411 | flowManager.pushModifiedFlowEntriesToSwitches(entries); |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 1412 | PerformanceMonitor.stop("EventHandler.AddFlowEntryToSwitch"); |
Pavlin Radoslavov | 8bd6d11 | 2014-01-12 20:12:37 -0800 | [diff] [blame^] | 1413 | PerformanceMonitor.report("EventHandler.AddFlowEntryToSwitch"); |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1414 | return; |
| 1415 | } |
| 1416 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1417 | EventEntry<FlowEntry> eventEntry = |
| 1418 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 1419 | networkEvents.add(eventEntry); |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Receive a notification that a FlowEntry is removed. |
| 1424 | * |
| 1425 | * @param flowEntry the FlowEntry that is removed. |
| 1426 | */ |
| 1427 | @Override |
| 1428 | public void notificationRecvFlowEntryRemoved(FlowEntry flowEntry) { |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1429 | if (enableOnrc2014MeasurementsFlows) { |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 1430 | PerformanceMonitor.start("EventHandler.RemoveFlowEntryFromSwitch"); |
Pavlin Radoslavov | e4d2a43 | 2014-01-10 12:01:08 -0800 | [diff] [blame] | 1431 | // |
| 1432 | // NOTE: Must update the state to DELETE, because |
| 1433 | // the notification contains the original state. |
| 1434 | // |
| 1435 | flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE); |
| 1436 | |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1437 | Collection entries = new ArrayList(); |
| 1438 | entries.add(flowEntry); |
| 1439 | flowManager.pushModifiedFlowEntriesToSwitches(entries); |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 1440 | PerformanceMonitor.stop("EventHandler.RemoveFlowEntryFromSwitch"); |
Pavlin Radoslavov | 8bd6d11 | 2014-01-12 20:12:37 -0800 | [diff] [blame^] | 1441 | PerformanceMonitor.report("EventHandler.RemoveFlowEntryFromSwitch"); |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1442 | return; |
| 1443 | } |
| 1444 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1445 | EventEntry<FlowEntry> eventEntry = |
| 1446 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_REMOVE, flowEntry); |
| 1447 | networkEvents.add(eventEntry); |
| 1448 | } |
| 1449 | |
| 1450 | /** |
| 1451 | * Receive a notification that a FlowEntry is updated. |
| 1452 | * |
| 1453 | * @param flowEntry the FlowEntry that is updated. |
| 1454 | */ |
| 1455 | @Override |
| 1456 | public void notificationRecvFlowEntryUpdated(FlowEntry flowEntry) { |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 1457 | if (enableOnrc2014MeasurementsFlows) { |
| 1458 | Collection entries = new ArrayList(); |
| 1459 | entries.add(flowEntry); |
| 1460 | flowManager.pushModifiedFlowEntriesToSwitches(entries); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
Pavlin Radoslavov | b750684 | 2013-10-29 17:46:54 -0700 | [diff] [blame] | 1464 | // NOTE: The ADD and UPDATE events are processed in same way |
| 1465 | EventEntry<FlowEntry> eventEntry = |
| 1466 | new EventEntry<FlowEntry>(EventEntry.Type.ENTRY_ADD, flowEntry); |
| 1467 | networkEvents.add(eventEntry); |
| 1468 | } |
| 1469 | |
| 1470 | /** |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1471 | * Receive a notification that a FlowId is added. |
| 1472 | * |
| 1473 | * @param flowId the FlowId that is added. |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1474 | * @param dpid the Source Switch Dpid for the corresponding Flow. |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1475 | */ |
| 1476 | @Override |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1477 | public void notificationRecvFlowIdAdded(FlowId flowId, Dpid dpid) { |
| 1478 | Pair flowIdPair = new Pair(flowId, dpid); |
| 1479 | |
| 1480 | EventEntry<Pair<FlowId, Dpid>> eventEntry = |
| 1481 | new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowIdPair); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1482 | networkEvents.add(eventEntry); |
| 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * Receive a notification that a FlowId is removed. |
| 1487 | * |
| 1488 | * @param flowId the FlowId that is removed. |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1489 | * @param dpid the Source Switch Dpid for the corresponding Flow. |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1490 | */ |
| 1491 | @Override |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1492 | public void notificationRecvFlowIdRemoved(FlowId flowId, Dpid dpid) { |
| 1493 | Pair flowIdPair = new Pair(flowId, dpid); |
| 1494 | |
| 1495 | EventEntry<Pair<FlowId, Dpid>> eventEntry = |
| 1496 | new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_REMOVE, flowIdPair); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1497 | networkEvents.add(eventEntry); |
| 1498 | } |
| 1499 | |
| 1500 | /** |
| 1501 | * Receive a notification that a FlowId is updated. |
| 1502 | * |
| 1503 | * @param flowId the FlowId that is updated. |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1504 | * @param dpid the Source Switch Dpid for the corresponding Flow. |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1505 | */ |
| 1506 | @Override |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1507 | public void notificationRecvFlowIdUpdated(FlowId flowId, Dpid dpid) { |
| 1508 | Pair flowIdPair = new Pair(flowId, dpid); |
| 1509 | |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1510 | // NOTE: The ADD and UPDATE events are processed in same way |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 1511 | EventEntry<Pair<FlowId, Dpid>> eventEntry = |
| 1512 | new EventEntry<Pair<FlowId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowIdPair); |
Pavlin Radoslavov | 2004fa0 | 2014-01-07 14:46:42 -0800 | [diff] [blame] | 1513 | networkEvents.add(eventEntry); |
| 1514 | } |
| 1515 | |
| 1516 | /** |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1517 | * Receive a notification that a FlowEntryId is added. |
| 1518 | * |
| 1519 | * @param flowEntryId the FlowEntryId that is added. |
| 1520 | * @param dpid the Switch Dpid for the corresponding Flow Entry. |
| 1521 | */ |
| 1522 | @Override |
| 1523 | public void notificationRecvFlowEntryIdAdded(FlowEntryId flowEntryId, |
| 1524 | Dpid dpid) { |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 1525 | Pair flowEntryIdPair = new Pair(flowEntryId, dpid); |
| 1526 | |
| 1527 | EventEntry<Pair<FlowEntryId, Dpid>> eventEntry = |
| 1528 | new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowEntryIdPair); |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1529 | networkEvents.add(eventEntry); |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | /** |
| 1533 | * Receive a notification that a FlowEntryId is removed. |
| 1534 | * |
| 1535 | * @param flowEntryId the FlowEntryId that is removed. |
| 1536 | * @param dpid the Switch Dpid for the corresponding Flow Entry. |
| 1537 | */ |
| 1538 | @Override |
| 1539 | public void notificationRecvFlowEntryIdRemoved(FlowEntryId flowEntryId, |
| 1540 | Dpid dpid) { |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 1541 | Pair flowEntryIdPair = new Pair(flowEntryId, dpid); |
| 1542 | |
| 1543 | EventEntry<Pair<FlowEntryId, Dpid>> eventEntry = |
| 1544 | new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_REMOVE, flowEntryIdPair); |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1545 | networkEvents.add(eventEntry); |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /** |
| 1549 | * Receive a notification that a FlowEntryId is updated. |
| 1550 | * |
| 1551 | * @param flowEntryId the FlowEntryId that is updated. |
| 1552 | * @param dpid the Switch Dpid for the corresponding Flow Entry. |
| 1553 | */ |
| 1554 | @Override |
| 1555 | public void notificationRecvFlowEntryIdUpdated(FlowEntryId flowEntryId, |
| 1556 | Dpid dpid) { |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 1557 | Pair flowEntryIdPair = new Pair(flowEntryId, dpid); |
| 1558 | |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1559 | // NOTE: The ADD and UPDATE events are processed in same way |
Pavlin Radoslavov | a9c0c3b | 2014-01-09 10:54:45 -0800 | [diff] [blame] | 1560 | EventEntry<Pair<FlowEntryId, Dpid>> eventEntry = |
| 1561 | new EventEntry<Pair<FlowEntryId, Dpid>>(EventEntry.Type.ENTRY_ADD, flowEntryIdPair); |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1562 | networkEvents.add(eventEntry); |
Pavlin Radoslavov | 909da3c | 2014-01-09 04:04:33 -0800 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /** |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1566 | * Receive a notification that a Topology Element is added. |
| 1567 | * |
| 1568 | * @param topologyElement the Topology Element that is added. |
| 1569 | */ |
| 1570 | @Override |
| 1571 | public void notificationRecvTopologyElementAdded(TopologyElement topologyElement) { |
| 1572 | EventEntry<TopologyElement> eventEntry = |
| 1573 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 1574 | networkEvents.add(eventEntry); |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * Receive a notification that a Topology Element is removed. |
| 1579 | * |
| 1580 | * @param topologyElement the Topology Element that is removed. |
| 1581 | */ |
| 1582 | @Override |
| 1583 | public void notificationRecvTopologyElementRemoved(TopologyElement topologyElement) { |
| 1584 | EventEntry<TopologyElement> eventEntry = |
| 1585 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_REMOVE, topologyElement); |
| 1586 | networkEvents.add(eventEntry); |
| 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * Receive a notification that a Topology Element is updated. |
| 1591 | * |
| 1592 | * @param topologyElement the Topology Element that is updated. |
| 1593 | */ |
| 1594 | @Override |
| 1595 | public void notificationRecvTopologyElementUpdated(TopologyElement topologyElement) { |
| 1596 | // NOTE: The ADD and UPDATE events are processed in same way |
| 1597 | EventEntry<TopologyElement> eventEntry = |
| 1598 | new EventEntry<TopologyElement>(EventEntry.Type.ENTRY_ADD, topologyElement); |
| 1599 | networkEvents.add(eventEntry); |
| 1600 | } |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 1601 | |
| 1602 | /** |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 1603 | * Receive a notification that a switch is added to this instance. |
| 1604 | * |
| 1605 | * @param sw the switch that is added. |
| 1606 | */ |
| 1607 | @Override |
| 1608 | public void addedSwitch(IOFSwitch sw) { |
| 1609 | Dpid dpid = new Dpid(sw.getId()); |
| 1610 | EventEntry<Dpid> eventEntry = |
| 1611 | new EventEntry<Dpid>(EventEntry.Type.ENTRY_ADD, dpid); |
| 1612 | networkEvents.add(eventEntry); |
| 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * Receive a notification that a switch is removed from this instance. |
| 1617 | * |
| 1618 | * @param sw the switch that is removed. |
| 1619 | */ |
| 1620 | @Override |
| 1621 | public void removedSwitch(IOFSwitch sw) { |
| 1622 | Dpid dpid = new Dpid(sw.getId()); |
| 1623 | EventEntry<Dpid> eventEntry = |
| 1624 | new EventEntry<Dpid>(EventEntry.Type.ENTRY_REMOVE, dpid); |
| 1625 | networkEvents.add(eventEntry); |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * Receive a notification that the ports on a switch have changed. |
| 1630 | */ |
| 1631 | @Override |
| 1632 | public void switchPortChanged(Long switchId) { |
| 1633 | // Nothing to do |
| 1634 | } |
| 1635 | |
| 1636 | /** |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 1637 | * Get a sorted copy of all Flow Paths. |
| 1638 | * |
| 1639 | * @return a sorted copy of all Flow Paths. |
| 1640 | */ |
| 1641 | synchronized SortedMap<Long, FlowPath> getAllFlowPathsCopy() { |
| 1642 | SortedMap<Long, FlowPath> sortedFlowPaths = |
| 1643 | new TreeMap<Long, FlowPath>(); |
| 1644 | |
| 1645 | // |
| 1646 | // TODO: For now we use serialization/deserialization to create |
| 1647 | // a copy of each Flow Path. In the future, we should use proper |
| 1648 | // copy constructors. |
| 1649 | // |
| 1650 | Kryo kryo = kryoFactory.newKryo(); |
| 1651 | synchronized (allFlowPaths) { |
| 1652 | for (Map.Entry<Long, FlowPath> entry : allFlowPaths.entrySet()) { |
| 1653 | FlowPath origFlowPath = entry.getValue(); |
| 1654 | FlowPath copyFlowPath = kryo.copy(origFlowPath); |
| 1655 | sortedFlowPaths.put(entry.getKey(), copyFlowPath); |
| 1656 | } |
| 1657 | } |
| 1658 | kryoFactory.deleteKryo(kryo); |
| 1659 | |
| 1660 | return sortedFlowPaths; |
| 1661 | } |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 1662 | } |