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