blob: b12bc0fcaf9bd0145b4f1b83f8ab81e74f3cfd9a [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08003import java.nio.ByteBuffer;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -08004import java.util.ArrayList;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07005import java.util.Arrays;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -08006import java.util.Collection;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07007import java.util.Comparator;
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08008import java.util.HashMap;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -08009import java.util.HashSet;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080010import java.util.LinkedList;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080011import java.util.List;
Pavlin Radoslavov018d5332014-02-19 23:08:35 -080012import java.util.Map;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080013import java.util.Set;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070014import java.util.TreeSet;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080015import java.util.concurrent.BlockingQueue;
Yuta HIGUCHIa536e762014-02-17 21:47:28 -080016import java.util.concurrent.CopyOnWriteArrayList;
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080017import java.util.concurrent.LinkedBlockingQueue;
Yuta HIGUCHI8d762e92014-02-12 14:10:25 -080018
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -070019import javax.annotation.concurrent.GuardedBy;
20
TeruU28adcc32014-04-15 17:57:35 -070021import net.floodlightcontroller.util.MACAddress;
Jonathan Hart6df90172014-04-03 10:13:11 -070022import net.onrc.onos.core.datagrid.IDatagridService;
23import net.onrc.onos.core.datagrid.IEventChannel;
24import net.onrc.onos.core.datagrid.IEventChannelListener;
TeruU28adcc32014-04-15 17:57:35 -070025import net.onrc.onos.core.datastore.topology.KVDevice;
Jonathan Hart6df90172014-04-03 10:13:11 -070026import net.onrc.onos.core.datastore.topology.KVLink;
27import net.onrc.onos.core.datastore.topology.KVPort;
28import net.onrc.onos.core.datastore.topology.KVSwitch;
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070029import net.onrc.onos.core.metrics.OnosMetrics;
30import net.onrc.onos.core.metrics.OnosMetrics.MetricsComponent;
31import net.onrc.onos.core.metrics.OnosMetrics.MetricsFeature;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070032import net.onrc.onos.core.registry.IControllerRegistryService;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070033import net.onrc.onos.core.util.Dpid;
Jonathan Hart23701d12014-04-03 10:45:48 -070034import net.onrc.onos.core.util.EventEntry;
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070035import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070036import net.onrc.onos.core.util.SwitchPort;
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -070037import net.onrc.onos.core.util.serializers.KryoFactory;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070038
Jonathan Hart062a2e82014-02-03 09:41:57 -080039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070042import com.codahale.metrics.Gauge;
43import com.codahale.metrics.Meter;
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -070044import com.esotericsoftware.kryo.Kryo;
45
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080046/**
Jonathan Harte37e4e22014-05-13 19:12:02 -070047 * The TopologyManager receives topology updates from the southbound discovery
48 * modules and from other ONOS instances. These updates are processed and
49 * applied to the in-memory topology instance.
Ray Milkey269ffb92014-04-03 14:43:30 -070050 * <p/>
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080051 * - Maintain Invariant/Relationships between Topology Objects.
Ray Milkey269ffb92014-04-03 14:43:30 -070052 * <p/>
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080053 * TODO To be synchronized based on TopologyEvent Notification.
Ray Milkey269ffb92014-04-03 14:43:30 -070054 * <p/>
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080055 * TODO TBD: Caller is expected to maintain parent/child calling order. Parent
Yuta HIGUCHI1c700102014-02-12 16:30:52 -080056 * Object must exist before adding sub component(Add Switch -> Port).
Ray Milkey269ffb92014-04-03 14:43:30 -070057 * <p/>
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080058 * TODO TBD: This class may delay the requested change to handle event
59 * re-ordering. e.g.) Link Add came in, but Switch was not there.
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080060 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070061public class TopologyManager implements TopologyDiscoveryInterface {
Jonathan Hart062a2e82014-02-03 09:41:57 -080062
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080063 private static final Logger log = LoggerFactory
Ray Milkey269ffb92014-04-03 14:43:30 -070064 .getLogger(TopologyManager.class);
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -080065
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080066 private IEventChannel<byte[], TopologyEvent> eventChannel;
Jonathan Hart10a7e2b2014-02-21 18:30:08 -080067 public static final String EVENT_CHANNEL_NAME = "onos.topology";
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -080068 private EventHandler eventHandler = new EventHandler();
69
weibitf7c31a42014-06-23 16:51:01 -070070 private TopologyDatastore datastore;
Jonathan Harte37e4e22014-05-13 19:12:02 -070071 private final TopologyImpl topology = new TopologyImpl();
Yuta HIGUCHI170229f2014-02-17 15:47:54 -080072 private final IControllerRegistryService registryService;
Jonathan Harte37e4e22014-05-13 19:12:02 -070073 private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -070074 private Kryo kryo = KryoFactory.newKryoObject();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080075
Pavlin Radoslavov706add22014-02-20 12:15:59 -080076 //
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070077 // Metrics
78 //
79 private static final MetricsComponent METRICS_COMPONENT =
80 OnosMetrics.registerComponent("Topology");
81 private static final MetricsFeature METRICS_FEATURE_EVENT_NOTIFICATION =
82 METRICS_COMPONENT.registerFeature("EventNotification");
83 //
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -070084 // Timestamp of the last Topology event (ms from the Epoch)
85 private volatile long lastEventTimestampEpochMs = 0;
86 private final Gauge<Long> gaugeLastEventTimestampEpochMs =
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070087 OnosMetrics.registerMetric(METRICS_COMPONENT,
88 METRICS_FEATURE_EVENT_NOTIFICATION,
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -070089 "LastEventTimestamp.EpochMs",
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070090 new Gauge<Long>() {
91 @Override
92 public Long getValue() {
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -070093 return lastEventTimestampEpochMs;
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070094 }
95 });
96 // Rate of the Topology events published to the Topology listeners
97 private final Meter listenerEventRate =
98 OnosMetrics.createMeter(METRICS_COMPONENT,
99 METRICS_FEATURE_EVENT_NOTIFICATION,
100 "ListenerEventRate");
101
102 //
Pavlin Radoslavov706add22014-02-20 12:15:59 -0800103 // Local state for keeping track of reordered events.
104 // NOTE: Switch Events are not affected by the event reordering.
105 //
106 private Map<ByteBuffer, PortEvent> reorderedAddedPortEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 new HashMap<ByteBuffer, PortEvent>();
Pavlin Radoslavov706add22014-02-20 12:15:59 -0800108 private Map<ByteBuffer, LinkEvent> reorderedAddedLinkEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 new HashMap<ByteBuffer, LinkEvent>();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700110 private Map<ByteBuffer, HostEvent> reorderedAddedHostEvents =
111 new HashMap<ByteBuffer, HostEvent>();
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800112
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800113 //
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800114 // Local state for keeping track of locally discovered events so we can
115 // cleanup properly when a Switch or Port is removed.
116 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700117 // We keep all Port, (incoming) Link and Host events per Switch DPID:
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800118 // - If a switch goes down, we remove all corresponding Port, Link and
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700119 // Host events.
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800120 // - If a port on a switch goes down, we remove all corresponding Link
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700121 // and Host events discovered by this instance.
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700122 //
123 // How to handle side-effect of remote events.
124 // - Remote Port Down event -> Link Down
125 // Not handled. (XXX Shouldn't it be removed from discovered.. Map)
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700126 // - Remote Host Added -> lose ownership of Host)
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700127 // Not handled. (XXX Shouldn't it be removed from discovered.. Map)
128 //
129 // XXX Domain knowledge based invariant maintenance should be moved to
130 // driver module, since the invariant may be different on optical, etc.
131 //
132 // What happens on leadership change?
133 // - Probably should: remove from discovered.. Maps, but not send DELETE events
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700134 // XXX Switch/Port can be rediscovered by new leader, but Link, Host?
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700135 // - Current: There is no way to recognize leadership change?
136 // ZookeeperRegistry.requestControl(long, ControlChangeCallback)
137 // is the only way to register listener, and it allows only 1 listener,
138 // which is already used by Controller class.
139 //
140 // FIXME Replace with concurrent variant.
141 // #removeSwitchDiscoveryEvent(SwitchEvent) runs in different thread.
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800142 //
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700143 private Map<Dpid, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700144 new HashMap<>();
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700145 private Map<Dpid, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 new HashMap<>();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700147 private Map<Dpid, Map<ByteBuffer, HostEvent>> discoveredAddedHostEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 new HashMap<>();
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800149
150 //
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800151 // Local state for keeping track of the application event notifications
152 //
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700153 // - Queue of events, which will be dispatched to local listeners
154 // on next notification.
Yuta HIGUCHI703696c2014-06-25 20:36:45 -0700155
156 private List<SwitchEvent> apiAddedSwitchEvents = new LinkedList<>();
157 private List<SwitchEvent> apiRemovedSwitchEvents = new LinkedList<>();
158 private List<PortEvent> apiAddedPortEvents = new LinkedList<>();
159 private List<PortEvent> apiRemovedPortEvents = new LinkedList<>();
160 private List<LinkEvent> apiAddedLinkEvents = new LinkedList<>();
161 private List<LinkEvent> apiRemovedLinkEvents = new LinkedList<>();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700162 private List<HostEvent> apiAddedHostEvents = new LinkedList<>();
163 private List<HostEvent> apiRemovedHostEvents = new LinkedList<>();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800164
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800165 /**
166 * Constructor.
167 *
Jonathan Harte37e4e22014-05-13 19:12:02 -0700168 * @param registryService the Registry Service to use.
169 * @param topologyListeners the collection of topology listeners to use.
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800170 */
171 public TopologyManager(IControllerRegistryService registryService,
Jonathan Harte37e4e22014-05-13 19:12:02 -0700172 CopyOnWriteArrayList<ITopologyListener> topologyListeners) {
173 datastore = new TopologyDatastore();
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 this.registryService = registryService;
Jonathan Harte37e4e22014-05-13 19:12:02 -0700175 this.topologyListeners = topologyListeners;
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800176 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800177
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800178 /**
Jonathan Harte37e4e22014-05-13 19:12:02 -0700179 * Get the Topology.
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800180 *
Jonathan Harte37e4e22014-05-13 19:12:02 -0700181 * @return the Topology.
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800182 */
Jonathan Harte37e4e22014-05-13 19:12:02 -0700183 Topology getTopology() {
184 return topology;
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800185 }
186
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800187 /**
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800188 * Event handler class.
189 */
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700190 class EventHandler extends Thread implements
Ray Milkey269ffb92014-04-03 14:43:30 -0700191 IEventChannelListener<byte[], TopologyEvent> {
192 private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents =
193 new LinkedBlockingQueue<EventEntry<TopologyEvent>>();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800194
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 /**
196 * Startup processing.
197 */
198 private void startup() {
199 //
200 // TODO: Read all state from the database:
201 //
202 // Collection<EventEntry<TopologyEvent>> collection =
203 // readWholeTopologyFromDB();
204 //
205 // For now, as a shortcut we read it from the datagrid
206 //
Ray Milkey5df613b2014-04-15 10:50:56 -0700207 Collection<TopologyEvent> allTopologyEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700208 eventChannel.getAllEntries();
209 Collection<EventEntry<TopologyEvent>> collection =
210 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800211
Ray Milkey5df613b2014-04-15 10:50:56 -0700212 for (TopologyEvent topologyEvent : allTopologyEvents) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 EventEntry<TopologyEvent> eventEntry =
214 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
215 topologyEvent);
216 collection.add(eventEntry);
217 }
218 processEvents(collection);
219 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800220
Ray Milkey269ffb92014-04-03 14:43:30 -0700221 /**
222 * Run the thread.
223 */
224 @Override
225 public void run() {
226 Collection<EventEntry<TopologyEvent>> collection =
227 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800228
Ray Milkey269ffb92014-04-03 14:43:30 -0700229 this.setName("TopologyManager.EventHandler " + this.getId());
230 startup();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800231
Ray Milkey269ffb92014-04-03 14:43:30 -0700232 //
233 // The main loop
234 //
Pavlin Radoslavov8e881a42014-06-24 16:58:07 -0700235 while (true) {
236 try {
237 EventEntry<TopologyEvent> eventEntry =
238 topologyEvents.take();
Ray Milkey269ffb92014-04-03 14:43:30 -0700239 collection.add(eventEntry);
240 topologyEvents.drainTo(collection);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800241
Ray Milkey269ffb92014-04-03 14:43:30 -0700242 processEvents(collection);
243 collection.clear();
Pavlin Radoslavov8e881a42014-06-24 16:58:07 -0700244 } catch (Exception exception) {
245 log.debug("Exception processing Topology Events: ",
246 exception);
Ray Milkey269ffb92014-04-03 14:43:30 -0700247 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700248 }
249 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800250
Ray Milkey269ffb92014-04-03 14:43:30 -0700251 /**
252 * Process all topology events.
253 *
254 * @param events the events to process.
255 */
256 private void processEvents(Collection<EventEntry<TopologyEvent>> events) {
257 // Local state for computing the final set of events
258 Map<ByteBuffer, SwitchEvent> addedSwitchEvents = new HashMap<>();
259 Map<ByteBuffer, SwitchEvent> removedSwitchEvents = new HashMap<>();
260 Map<ByteBuffer, PortEvent> addedPortEvents = new HashMap<>();
261 Map<ByteBuffer, PortEvent> removedPortEvents = new HashMap<>();
262 Map<ByteBuffer, LinkEvent> addedLinkEvents = new HashMap<>();
263 Map<ByteBuffer, LinkEvent> removedLinkEvents = new HashMap<>();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700264 Map<ByteBuffer, HostEvent> addedHostEvents = new HashMap<>();
265 Map<ByteBuffer, HostEvent> removedHostEvents = new HashMap<>();
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800266
Ray Milkey269ffb92014-04-03 14:43:30 -0700267 //
268 // Classify and suppress matching events
269 //
270 for (EventEntry<TopologyEvent> event : events) {
271 TopologyEvent topologyEvent = event.eventData();
272 SwitchEvent switchEvent = topologyEvent.switchEvent;
273 PortEvent portEvent = topologyEvent.portEvent;
274 LinkEvent linkEvent = topologyEvent.linkEvent;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700275 HostEvent hostEvent = topologyEvent.hostEvent;
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800276
Ray Milkey269ffb92014-04-03 14:43:30 -0700277 //
278 // Extract the events
279 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700280 // FIXME Following event squashing logic based only on ID
281 // potentially lose attribute change.
Ray Milkey269ffb92014-04-03 14:43:30 -0700282 switch (event.eventType()) {
283 case ENTRY_ADD:
284 log.debug("Topology event ENTRY_ADD: {}", topologyEvent);
285 if (switchEvent != null) {
286 ByteBuffer id = switchEvent.getIDasByteBuffer();
287 addedSwitchEvents.put(id, switchEvent);
288 removedSwitchEvents.remove(id);
289 // Switch Events are not affected by event reordering
290 }
291 if (portEvent != null) {
292 ByteBuffer id = portEvent.getIDasByteBuffer();
293 addedPortEvents.put(id, portEvent);
294 removedPortEvents.remove(id);
295 reorderedAddedPortEvents.remove(id);
296 }
297 if (linkEvent != null) {
298 ByteBuffer id = linkEvent.getIDasByteBuffer();
299 addedLinkEvents.put(id, linkEvent);
300 removedLinkEvents.remove(id);
301 reorderedAddedLinkEvents.remove(id);
302 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700303 if (hostEvent != null) {
304 ByteBuffer id = hostEvent.getIDasByteBuffer();
305 addedHostEvents.put(id, hostEvent);
306 removedHostEvents.remove(id);
307 reorderedAddedHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700308 }
309 break;
310 case ENTRY_REMOVE:
311 log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent);
312 if (switchEvent != null) {
313 ByteBuffer id = switchEvent.getIDasByteBuffer();
314 addedSwitchEvents.remove(id);
315 removedSwitchEvents.put(id, switchEvent);
316 // Switch Events are not affected by event reordering
317 }
318 if (portEvent != null) {
319 ByteBuffer id = portEvent.getIDasByteBuffer();
320 addedPortEvents.remove(id);
321 removedPortEvents.put(id, portEvent);
322 reorderedAddedPortEvents.remove(id);
323 }
324 if (linkEvent != null) {
325 ByteBuffer id = linkEvent.getIDasByteBuffer();
326 addedLinkEvents.remove(id);
327 removedLinkEvents.put(id, linkEvent);
328 reorderedAddedLinkEvents.remove(id);
329 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700330 if (hostEvent != null) {
331 ByteBuffer id = hostEvent.getIDasByteBuffer();
332 addedHostEvents.remove(id);
333 removedHostEvents.put(id, hostEvent);
334 reorderedAddedHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700335 }
336 break;
Ray Milkey0b122ed2014-04-14 10:06:03 -0700337 default:
338 log.error("Unknown topology event {}",
339 event.eventType());
Ray Milkey269ffb92014-04-03 14:43:30 -0700340 }
341 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800342
Ray Milkey269ffb92014-04-03 14:43:30 -0700343 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700344 // Lock the topology while it is modified
Ray Milkey269ffb92014-04-03 14:43:30 -0700345 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700346 topology.acquireWriteLock();
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800347
Ray Milkey269ffb92014-04-03 14:43:30 -0700348 try {
349 //
350 // Apply the classified events.
351 //
352 // Apply the "add" events in the proper order:
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700353 // switch, port, link, host
Ray Milkey269ffb92014-04-03 14:43:30 -0700354 //
Ray Milkeyb29e6262014-04-09 16:02:14 -0700355 for (SwitchEvent switchEvent : addedSwitchEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700356 addSwitch(switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700357 }
358 for (PortEvent portEvent : addedPortEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700359 addPort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700360 }
361 for (LinkEvent linkEvent : addedLinkEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700362 addLink(linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700363 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700364 for (HostEvent hostEvent : addedHostEvents.values()) {
365 addHost(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700366 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700367 //
368 // Apply the "remove" events in the reverse order:
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700369 // host, link, port, switch
Ray Milkey269ffb92014-04-03 14:43:30 -0700370 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700371 for (HostEvent hostEvent : removedHostEvents.values()) {
372 removeHost(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700373 }
374 for (LinkEvent linkEvent : removedLinkEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700375 removeLink(linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700376 }
377 for (PortEvent portEvent : removedPortEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700378 removePort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700379 }
380 for (SwitchEvent switchEvent : removedSwitchEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700381 removeSwitch(switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700382 }
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800383
Ray Milkey269ffb92014-04-03 14:43:30 -0700384 //
385 // Apply reordered events
386 //
387 applyReorderedEvents(!addedSwitchEvents.isEmpty(),
388 !addedPortEvents.isEmpty());
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800389
Ray Milkey269ffb92014-04-03 14:43:30 -0700390 } finally {
391 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700392 // Topology modifications completed: Release the lock
Ray Milkey269ffb92014-04-03 14:43:30 -0700393 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700394 topology.releaseWriteLock();
Ray Milkey269ffb92014-04-03 14:43:30 -0700395 }
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800396
Ray Milkey269ffb92014-04-03 14:43:30 -0700397 //
398 // Dispatch the Topology Notification Events to the applications
399 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700400 dispatchTopologyEvents();
Ray Milkey269ffb92014-04-03 14:43:30 -0700401 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800402
Ray Milkey269ffb92014-04-03 14:43:30 -0700403 /**
404 * Receive a notification that an entry is added.
405 *
406 * @param value the value for the entry.
407 */
408 @Override
409 public void entryAdded(TopologyEvent value) {
410 EventEntry<TopologyEvent> eventEntry =
411 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
412 value);
413 topologyEvents.add(eventEntry);
414 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800415
Ray Milkey269ffb92014-04-03 14:43:30 -0700416 /**
417 * Receive a notification that an entry is removed.
418 *
419 * @param value the value for the entry.
420 */
421 @Override
422 public void entryRemoved(TopologyEvent value) {
423 EventEntry<TopologyEvent> eventEntry =
424 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
425 value);
426 topologyEvents.add(eventEntry);
427 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800428
Ray Milkey269ffb92014-04-03 14:43:30 -0700429 /**
430 * Receive a notification that an entry is updated.
431 *
432 * @param value the value for the entry.
433 */
434 @Override
435 public void entryUpdated(TopologyEvent value) {
436 // NOTE: The ADD and UPDATE events are processed in same way
437 entryAdded(value);
438 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800439 }
440
441 /**
442 * Startup processing.
443 *
444 * @param datagridService the datagrid service to use.
445 */
446 void startup(IDatagridService datagridService) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700447 eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME,
448 eventHandler,
449 byte[].class,
450 TopologyEvent.class);
451 eventHandler.start();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800452 }
453
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800454 /**
Jonathan Harte37e4e22014-05-13 19:12:02 -0700455 * Dispatch Topology Events to the listeners.
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800456 */
Jonathan Harte37e4e22014-05-13 19:12:02 -0700457 private void dispatchTopologyEvents() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700458 if (apiAddedSwitchEvents.isEmpty() &&
459 apiRemovedSwitchEvents.isEmpty() &&
460 apiAddedPortEvents.isEmpty() &&
461 apiRemovedPortEvents.isEmpty() &&
462 apiAddedLinkEvents.isEmpty() &&
463 apiRemovedLinkEvents.isEmpty() &&
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700464 apiAddedHostEvents.isEmpty() &&
465 apiRemovedHostEvents.isEmpty()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700466 return; // No events to dispatch
467 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800468
Ray Milkey269ffb92014-04-03 14:43:30 -0700469 if (log.isDebugEnabled()) {
470 //
471 // Debug statements
472 // TODO: Those statements should be removed in the future
473 //
Ray Milkeyb29e6262014-04-09 16:02:14 -0700474 for (SwitchEvent switchEvent : apiAddedSwitchEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700475 log.debug("Dispatch Topology Event: ADDED {}", switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700476 }
477 for (SwitchEvent switchEvent : apiRemovedSwitchEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700478 log.debug("Dispatch Topology Event: REMOVED {}", switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700479 }
480 for (PortEvent portEvent : apiAddedPortEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700481 log.debug("Dispatch Topology Event: ADDED {}", portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700482 }
483 for (PortEvent portEvent : apiRemovedPortEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700484 log.debug("Dispatch Topology Event: REMOVED {}", portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700485 }
486 for (LinkEvent linkEvent : apiAddedLinkEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700487 log.debug("Dispatch Topology Event: ADDED {}", linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700488 }
489 for (LinkEvent linkEvent : apiRemovedLinkEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700490 log.debug("Dispatch Topology Event: REMOVED {}", linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700491 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700492 for (HostEvent hostEvent : apiAddedHostEvents) {
493 log.debug("Dispatch Topology Event: ADDED {}", hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700494 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700495 for (HostEvent hostEvent : apiRemovedHostEvents) {
496 log.debug("Dispatch Topology Event: REMOVED {}", hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700497 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700498 }
adminbc181552014-02-21 18:36:42 -0800499
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700500 //
501 // Update the metrics
502 //
503 long totalEvents =
504 apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() +
505 apiAddedPortEvents.size() + apiRemovedPortEvents.size() +
506 apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() +
507 apiAddedHostEvents.size() + apiRemovedHostEvents.size();
508 this.listenerEventRate.mark(totalEvents);
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -0700509 this.lastEventTimestampEpochMs = System.currentTimeMillis();
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700510
511 //
Ray Milkey269ffb92014-04-03 14:43:30 -0700512 // Deliver the events
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700513 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700514 for (ITopologyListener listener : this.topologyListeners) {
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700515 TopologyEvents events =
Pavlin Radoslavovc61544e2014-07-23 16:27:27 -0700516 new TopologyEvents(kryo.copy(apiAddedSwitchEvents),
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700517 kryo.copy(apiRemovedSwitchEvents),
518 kryo.copy(apiAddedPortEvents),
519 kryo.copy(apiRemovedPortEvents),
520 kryo.copy(apiAddedLinkEvents),
521 kryo.copy(apiRemovedLinkEvents),
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700522 kryo.copy(apiAddedHostEvents),
523 kryo.copy(apiRemovedHostEvents));
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700524 listener.topologyEvents(events);
Ray Milkey269ffb92014-04-03 14:43:30 -0700525 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800526
Ray Milkey269ffb92014-04-03 14:43:30 -0700527 //
528 // Cleanup
529 //
530 apiAddedSwitchEvents.clear();
531 apiRemovedSwitchEvents.clear();
532 apiAddedPortEvents.clear();
533 apiRemovedPortEvents.clear();
534 apiAddedLinkEvents.clear();
535 apiRemovedLinkEvents.clear();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700536 apiAddedHostEvents.clear();
537 apiRemovedHostEvents.clear();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800538 }
539
540 /**
541 * Apply reordered events.
542 *
543 * @param hasAddedSwitchEvents true if there were Added Switch Events.
Ray Milkey269ffb92014-04-03 14:43:30 -0700544 * @param hasAddedPortEvents true if there were Added Port Events.
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800545 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700546 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800547 private void applyReorderedEvents(boolean hasAddedSwitchEvents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700548 boolean hasAddedPortEvents) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700549 if (!(hasAddedSwitchEvents || hasAddedPortEvents)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700550 return; // Nothing to do
Ray Milkeyb29e6262014-04-09 16:02:14 -0700551 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800552
Ray Milkey269ffb92014-04-03 14:43:30 -0700553 //
554 // Try to apply the reordered events.
555 //
556 // NOTE: For simplicity we try to apply all events of a particular
557 // type if any "parent" type event was processed:
558 // - Apply reordered Port Events if Switches were added
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700559 // - Apply reordered Link and Host Events if Switches or Ports
Ray Milkey269ffb92014-04-03 14:43:30 -0700560 // were added
561 //
adminbc181552014-02-21 18:36:42 -0800562
Ray Milkey269ffb92014-04-03 14:43:30 -0700563 //
564 // Apply reordered Port Events if Switches were added
565 //
566 if (hasAddedSwitchEvents) {
567 Map<ByteBuffer, PortEvent> portEvents = reorderedAddedPortEvents;
568 reorderedAddedPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700569 for (PortEvent portEvent : portEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700570 addPort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700571 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700572 }
573 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700574 // Apply reordered Link and Host Events if Switches or Ports
Ray Milkey269ffb92014-04-03 14:43:30 -0700575 // were added.
576 //
577 Map<ByteBuffer, LinkEvent> linkEvents = reorderedAddedLinkEvents;
578 reorderedAddedLinkEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700579 for (LinkEvent linkEvent : linkEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700580 addLink(linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700581 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700582 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700583 Map<ByteBuffer, HostEvent> hostEvents = reorderedAddedHostEvents;
584 reorderedAddedHostEvents = new HashMap<>();
585 for (HostEvent hostEvent : hostEvents.values()) {
586 addHost(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700587 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800588 }
589
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800590 /**
591 * Switch discovered event.
592 *
593 * @param switchEvent the switch event.
Ray Milkey269ffb92014-04-03 14:43:30 -0700594 * @param portEvents the corresponding port events for the switch.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800595 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800596 @Override
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800597 public void putSwitchDiscoveryEvent(SwitchEvent switchEvent,
Ray Milkey269ffb92014-04-03 14:43:30 -0700598 Collection<PortEvent> portEvents) {
599 if (datastore.addSwitch(switchEvent, portEvents)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700600 log.debug("Sending add switch: {}", switchEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700601 // Send out notification
602 TopologyEvent topologyEvent = new TopologyEvent(switchEvent);
603 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800604
Ray Milkey269ffb92014-04-03 14:43:30 -0700605 // Send out notification for each port
606 for (PortEvent portEvent : portEvents) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700607 log.debug("Sending add port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700608 topologyEvent = new TopologyEvent(portEvent);
609 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
610 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800611
Ray Milkey269ffb92014-04-03 14:43:30 -0700612 //
613 // Keep track of the added ports
614 //
615 // Get the old Port Events
616 Map<ByteBuffer, PortEvent> oldPortEvents =
617 discoveredAddedPortEvents.get(switchEvent.getDpid());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700618 if (oldPortEvents == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700619 oldPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700620 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800621
Ray Milkey269ffb92014-04-03 14:43:30 -0700622 // Store the new Port Events in the local cache
623 Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>();
624 for (PortEvent portEvent : portEvents) {
625 ByteBuffer id = portEvent.getIDasByteBuffer();
626 newPortEvents.put(id, portEvent);
627 }
628 discoveredAddedPortEvents.put(switchEvent.getDpid(),
629 newPortEvents);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800630
Ray Milkey269ffb92014-04-03 14:43:30 -0700631 //
632 // Extract the removed ports
633 //
634 List<PortEvent> removedPortEvents = new LinkedList<>();
635 for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) {
636 ByteBuffer key = entry.getKey();
637 PortEvent portEvent = entry.getValue();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700638 if (!newPortEvents.containsKey(key)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700639 removedPortEvents.add(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700640 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700641 }
642
643 // Cleanup old removed ports
Ray Milkeyb29e6262014-04-09 16:02:14 -0700644 for (PortEvent portEvent : removedPortEvents) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700645 removePortDiscoveryEvent(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700646 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700647 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800648 }
649
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800650 /**
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700651 * {@inheritDoc}
652 * <p/>
653 * Called by {@link TopologyPublisher.SwitchCleanup} thread.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800654 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800655 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800656 public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700657 // Get the old Port Events
658 Map<ByteBuffer, PortEvent> oldPortEvents =
659 discoveredAddedPortEvents.get(switchEvent.getDpid());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700660 if (oldPortEvents == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700661 oldPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700662 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800663
Ray Milkey269ffb92014-04-03 14:43:30 -0700664 if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700665 log.debug("Sending remove switch: {}", switchEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700666 // Send out notification
667 eventChannel.removeEntry(switchEvent.getID());
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800668
Ray Milkey269ffb92014-04-03 14:43:30 -0700669 //
670 // Send out notification for each port.
671 //
672 // NOTE: We don't use removePortDiscoveryEvent() for the cleanup,
673 // because it will attempt to remove the port from the database,
674 // and the deactiveSwitch() call above already removed all ports.
675 //
Ray Milkeyb29e6262014-04-09 16:02:14 -0700676 for (PortEvent portEvent : oldPortEvents.values()) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700677 log.debug("Sending remove port:", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700678 eventChannel.removeEntry(portEvent.getID());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700679 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700680 discoveredAddedPortEvents.remove(switchEvent.getDpid());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800681
Ray Milkey269ffb92014-04-03 14:43:30 -0700682 // Cleanup for each link
683 Map<ByteBuffer, LinkEvent> oldLinkEvents =
684 discoveredAddedLinkEvents.get(switchEvent.getDpid());
685 if (oldLinkEvents != null) {
686 for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) {
687 removeLinkDiscoveryEvent(linkEvent);
688 }
689 discoveredAddedLinkEvents.remove(switchEvent.getDpid());
690 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800691
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700692 // Cleanup for each host
693 Map<ByteBuffer, HostEvent> oldHostEvents =
694 discoveredAddedHostEvents.get(switchEvent.getDpid());
695 if (oldHostEvents != null) {
696 for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) {
697 removeHostDiscoveryEvent(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700698 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700699 discoveredAddedHostEvents.remove(switchEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700700 }
701 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800702 }
703
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800704 /**
705 * Port discovered event.
706 *
707 * @param portEvent the port event.
708 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800709 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800710 public void putPortDiscoveryEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700711 if (datastore.addPort(portEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700712 log.debug("Sending add port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700713 // Send out notification
714 TopologyEvent topologyEvent = new TopologyEvent(portEvent);
715 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800716
Ray Milkey269ffb92014-04-03 14:43:30 -0700717 // Store the new Port Event in the local cache
718 Map<ByteBuffer, PortEvent> oldPortEvents =
719 discoveredAddedPortEvents.get(portEvent.getDpid());
720 if (oldPortEvents == null) {
721 oldPortEvents = new HashMap<>();
722 discoveredAddedPortEvents.put(portEvent.getDpid(),
723 oldPortEvents);
724 }
725 ByteBuffer id = portEvent.getIDasByteBuffer();
726 oldPortEvents.put(id, portEvent);
727 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800728 }
729
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800730 /**
731 * Port removed event.
732 *
733 * @param portEvent the port event.
734 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800735 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800736 public void removePortDiscoveryEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700737 if (datastore.deactivatePort(portEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700738 log.debug("Sending remove port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700739 // Send out notification
740 eventChannel.removeEntry(portEvent.getID());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800741
Ray Milkey269ffb92014-04-03 14:43:30 -0700742 // Cleanup the Port Event from the local cache
743 Map<ByteBuffer, PortEvent> oldPortEvents =
744 discoveredAddedPortEvents.get(portEvent.getDpid());
745 if (oldPortEvents != null) {
746 ByteBuffer id = portEvent.getIDasByteBuffer();
747 oldPortEvents.remove(id);
748 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800749
Ray Milkey269ffb92014-04-03 14:43:30 -0700750 // Cleanup for the incoming link
751 Map<ByteBuffer, LinkEvent> oldLinkEvents =
752 discoveredAddedLinkEvents.get(portEvent.getDpid());
753 if (oldLinkEvents != null) {
754 for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700755 if (linkEvent.getDst().equals(portEvent.getSwitchPort())) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700756 removeLinkDiscoveryEvent(linkEvent);
757 // XXX If we change our model to allow multiple Link on
758 // a Port, this loop must be fixed to allow continuing.
759 break;
760 }
761 }
762 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800763
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700764 // Cleanup for the connected hosts
Ray Milkey269ffb92014-04-03 14:43:30 -0700765 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700766 List<HostEvent> removedHostEvents = new LinkedList<>();
767 Map<ByteBuffer, HostEvent> oldHostEvents =
768 discoveredAddedHostEvents.get(portEvent.getDpid());
769 if (oldHostEvents != null) {
770 for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) {
771 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700772 if (swp.equals(portEvent.getSwitchPort())) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700773 removedHostEvents.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700774 }
775 }
776 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700777 for (HostEvent hostEvent : removedHostEvents) {
778 removeHostDiscoveryEvent(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700779 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700780 }
781 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800782 }
783
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800784 /**
785 * Link discovered event.
786 *
787 * @param linkEvent the link event.
788 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800789 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800790 public void putLinkDiscoveryEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700791 if (datastore.addLink(linkEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700792 log.debug("Sending add link: {}", linkEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700793 // Send out notification
794 TopologyEvent topologyEvent = new TopologyEvent(linkEvent);
795 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800796
Ray Milkey269ffb92014-04-03 14:43:30 -0700797 // Store the new Link Event in the local cache
798 Map<ByteBuffer, LinkEvent> oldLinkEvents =
799 discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
800 if (oldLinkEvents == null) {
801 oldLinkEvents = new HashMap<>();
802 discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(),
803 oldLinkEvents);
804 }
805 ByteBuffer id = linkEvent.getIDasByteBuffer();
806 oldLinkEvents.put(id, linkEvent);
807 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800808 }
809
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800810 /**
811 * Link removed event.
812 *
813 * @param linkEvent the link event.
814 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800815 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800816 public void removeLinkDiscoveryEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700817 if (datastore.removeLink(linkEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700818 log.debug("Sending remove link: {}", linkEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700819 // Send out notification
820 eventChannel.removeEntry(linkEvent.getID());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800821
Ray Milkey269ffb92014-04-03 14:43:30 -0700822 // Cleanup the Link Event from the local cache
823 Map<ByteBuffer, LinkEvent> oldLinkEvents =
824 discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
825 if (oldLinkEvents != null) {
826 ByteBuffer id = linkEvent.getIDasByteBuffer();
827 oldLinkEvents.remove(id);
828 }
829 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800830 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800831
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800832 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700833 * Host discovered event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800834 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700835 * @param hostEvent the host event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800836 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800837 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700838 public void putHostDiscoveryEvent(HostEvent hostEvent) {
839 if (datastore.addHost(hostEvent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700840 // Send out notification
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700841 TopologyEvent topologyEvent = new TopologyEvent(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700842 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700843 log.debug("Put the host info into the cache of the topology. mac {}", hostEvent.getMac());
Ray Milkey269ffb92014-04-03 14:43:30 -0700844
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700845 // Store the new Host Event in the local cache
Ray Milkey269ffb92014-04-03 14:43:30 -0700846 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700847 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
848 Map<ByteBuffer, HostEvent> oldHostEvents =
849 discoveredAddedHostEvents.get(swp.getDpid());
850 if (oldHostEvents == null) {
851 oldHostEvents = new HashMap<>();
852 discoveredAddedHostEvents.put(swp.getDpid(),
853 oldHostEvents);
Ray Milkey269ffb92014-04-03 14:43:30 -0700854 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700855 ByteBuffer id = hostEvent.getIDasByteBuffer();
856 oldHostEvents.put(id, hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700857 }
858 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800859 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800860
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800861 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700862 * Host removed event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800863 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700864 * @param hostEvent the host event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800865 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800866 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700867 public void removeHostDiscoveryEvent(HostEvent hostEvent) {
868 if (datastore.removeHost(hostEvent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700869 // Send out notification
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700870 eventChannel.removeEntry(hostEvent.getID());
871 log.debug("Remove the host info into the cache of the topology. mac {}", hostEvent.getMac());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800872
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700873 // Cleanup the Host Event from the local cache
Ray Milkey269ffb92014-04-03 14:43:30 -0700874 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700875 ByteBuffer id = ByteBuffer.wrap(hostEvent.getID());
876 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
877 Map<ByteBuffer, HostEvent> oldHostEvents =
878 discoveredAddedHostEvents.get(swp.getDpid());
879 if (oldHostEvents != null) {
880 oldHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700881 }
882 }
883 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800884 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800885
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700886 //
887 // Methods to update topology replica
888 //
889
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800890 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700891 * Adds a switch to the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800892 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700893 * @param switchEvent the SwitchEvent with the switch to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800894 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700895 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800896 private void addSwitch(SwitchEvent switchEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700897 if (log.isDebugEnabled()) {
898 SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid());
899 if (sw != null) {
900 log.debug("Update {}", switchEvent);
901 } else {
902 log.debug("Added {}", switchEvent);
903 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700904 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700905 topology.putSwitch(switchEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -0700906 apiAddedSwitchEvents.add(switchEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800907 }
908
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800909 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700910 * Removes a switch from the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700911 * <p/>
912 * It will call {@link #removePort(PortEvent)} for each ports on this switch.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800913 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700914 * @param switchEvent the SwitchEvent with the switch to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800915 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700916 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800917 private void removeSwitch(SwitchEvent switchEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700918 final Dpid dpid = switchEvent.getDpid();
919
920 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
921 if (swInTopo == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700922 log.warn("Switch {} already removed, ignoring", switchEvent);
923 return;
924 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800925
Ray Milkey269ffb92014-04-03 14:43:30 -0700926 //
927 // Remove all Ports on the Switch
928 //
929 ArrayList<PortEvent> portsToRemove = new ArrayList<>();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700930 for (Port port : topology.getPorts(dpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700931 log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.",
932 port, switchEvent);
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -0700933 PortEvent portEvent = new PortEvent(port.getSwitchPort());
Ray Milkey269ffb92014-04-03 14:43:30 -0700934 portsToRemove.add(portEvent);
935 }
Ray Milkeyb29e6262014-04-09 16:02:14 -0700936 for (PortEvent portEvent : portsToRemove) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700937 removePort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700938 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800939
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700940 log.debug("Removed {}", swInTopo);
941 topology.removeSwitch(dpid);
942 apiRemovedSwitchEvents.add(swInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800943 }
944
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800945 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700946 * Adds a port to the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800947 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700948 * @param portEvent the PortEvent with the port to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800949 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700950 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800951 private void addPort(PortEvent portEvent) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700952 Switch sw = topology.getSwitch(portEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700953 if (sw == null) {
Jonathan Hartf1675202014-05-23 14:59:07 -0700954 log.debug("{} reordered because switch is null", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700955 // Reordered event: delay the event in local cache
956 ByteBuffer id = portEvent.getIDasByteBuffer();
957 reorderedAddedPortEvents.put(id, portEvent);
958 return;
959 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800960
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700961 if (log.isDebugEnabled()) {
962 PortEvent port = topology.getPortEvent(portEvent.getSwitchPort());
963 if (port != null) {
964 log.debug("Update {}", portEvent);
965 } else {
966 log.debug("Added {}", portEvent);
967 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700968 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700969 topology.putPort(portEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -0700970 apiAddedPortEvents.add(portEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800971 }
972
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800973 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700974 * Removes a port from the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700975 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700976 * It will remove attachment points from each hosts on this port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700977 * and call {@link #removeLink(LinkEvent)} for each links on this port.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800978 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700979 * @param portEvent the PortEvent with the port to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800980 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700981 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800982 private void removePort(PortEvent portEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700983 SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700984 if (sw == null) {
985 log.warn("Parent Switch for Port {} already removed, ignoring",
986 portEvent);
987 return;
988 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800989
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700990 final SwitchPort switchPort = portEvent.getSwitchPort();
991 PortEvent portInTopo = topology.getPortEvent(switchPort);
992 if (portInTopo == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700993 log.warn("Port {} already removed, ignoring", portEvent);
994 return;
995 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800996
Ray Milkey269ffb92014-04-03 14:43:30 -0700997 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700998 // Remove all Host attachment points bound to this Port
Ray Milkey269ffb92014-04-03 14:43:30 -0700999 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001000 List<HostEvent> hostsToUpdate = new ArrayList<>();
1001 for (Host host : topology.getHosts(switchPort)) {
1002 log.debug("Removing Host {} on Port {}", host, portInTopo);
1003 HostEvent hostEvent = topology.getHostEvent(host.getMacAddress());
1004 hostsToUpdate.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001005 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001006 for (HostEvent hostEvent : hostsToUpdate) {
1007 HostEvent newHostEvent = new HostEvent(hostEvent);
1008 newHostEvent.removeAttachmentPoint(switchPort);
1009 newHostEvent.freeze();
1010
1011 // TODO should this event be fired inside #addHost?
1012 if (newHostEvent.getAttachmentPoints().isEmpty()) {
1013 // No more attachment point left -> remove Host
1014 removeHost(hostEvent);
1015 } else {
1016 // Update Host
1017 addHost(newHostEvent);
1018 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001019 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001020
Ray Milkey269ffb92014-04-03 14:43:30 -07001021 //
1022 // Remove all Links connected to the Port
1023 //
1024 Set<Link> links = new HashSet<>();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001025 links.addAll(topology.getOutgoingLinks(switchPort));
1026 links.addAll(topology.getIncomingLinks(switchPort));
Ray Milkey269ffb92014-04-03 14:43:30 -07001027 for (Link link : links) {
Ray Milkeyb29e6262014-04-09 16:02:14 -07001028 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001029 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001030 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001031 LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple());
1032 if (linkEvent != null) {
1033 log.debug("Removing Link {} on Port {}", link, portInTopo);
1034 removeLink(linkEvent);
1035 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001036 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001037
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001038 // Remove the Port from Topology
1039 log.debug("Removed {}", portInTopo);
1040 topology.removePort(switchPort);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001041
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001042 apiRemovedPortEvents.add(portInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001043 }
1044
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001045 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001046 * Adds a link to the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001047 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001048 * It will remove attachment points from each hosts using the same ports.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001049 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001050 * @param linkEvent the LinkEvent with the link to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001051 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001052 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001053 private void addLink(LinkEvent linkEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001054 PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc());
1055 PortEvent dstPort = topology.getPortEvent(linkEvent.getDst());
Ray Milkey269ffb92014-04-03 14:43:30 -07001056 if ((srcPort == null) || (dstPort == null)) {
Jonathan Hartf1675202014-05-23 14:59:07 -07001057 log.debug("{} reordered because {} port is null", linkEvent,
1058 (srcPort == null) ? "src" : "dst");
1059
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001060 // XXX domain knowledge: port must be present before link.
Ray Milkey269ffb92014-04-03 14:43:30 -07001061 // Reordered event: delay the event in local cache
1062 ByteBuffer id = linkEvent.getIDasByteBuffer();
1063 reorderedAddedLinkEvents.put(id, linkEvent);
1064 return;
1065 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001066
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001067 // XXX domain knowledge: Sanity check: Port cannot have both Link and Host
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001068 // FIXME potentially local replica may not be up-to-date yet due to HZ delay.
1069 // may need to manage local truth and use them instead.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001070 if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) {
1071 // Only check for existing Host when adding new Link.
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001072
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001073 // Remove all Hosts attached to the ports on both ends
1074
1075 Set<HostEvent> hostsToUpdate = new TreeSet<>(new Comparator<HostEvent>() {
1076 // comparison only using ID(=MAC)
1077 @Override
1078 public int compare(HostEvent o1, HostEvent o2) {
1079 return Long.compare(o1.getMac().toLong(), o2.getMac().toLong());
1080 }
1081 });
1082
1083 List<SwitchPort> portsToCheck = Arrays.asList(
1084 srcPort.getSwitchPort(),
1085 dstPort.getSwitchPort());
1086
1087 // Enumerate Host which needs to be updated by this Link add event
1088 for (SwitchPort port : portsToCheck) {
1089 for (Host host : topology.getHosts(port)) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001090 log.error("Host {} on Port {} should have been removed prior to adding Link {}",
1091 host, port, linkEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001092
1093 HostEvent hostEvent = topology.getHostEvent(host.getMacAddress());
1094 hostsToUpdate.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001095 }
1096 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001097 // remove attachment point from them.
1098 for (HostEvent hostEvent : hostsToUpdate) {
1099 // remove port from attachment point and update
1100 HostEvent newHostEvent = new HostEvent(hostEvent);
1101 newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort());
1102 newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort());
1103 newHostEvent.freeze();
1104
1105 // TODO should this event be fired inside #addHost?
1106 if (newHostEvent.getAttachmentPoints().isEmpty()) {
1107 // No more attachment point left -> remove Host
1108 removeHost(hostEvent);
1109 } else {
1110 // Update Host
1111 addHost(newHostEvent);
1112 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001113 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001114 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001115
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001116 if (log.isDebugEnabled()) {
1117 LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple());
1118 if (link != null) {
1119 log.debug("Update {}", linkEvent);
1120 } else {
1121 log.debug("Added {}", linkEvent);
1122 }
1123 }
1124 topology.putLink(linkEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -07001125 apiAddedLinkEvents.add(linkEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001126 }
1127
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001128 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001129 * Removes a link from the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001130 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001131 * @param linkEvent the LinkEvent with the link to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001132 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001133 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001134 private void removeLink(LinkEvent linkEvent) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07001135 Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001136 linkEvent.getSrc().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001137 if (srcPort == null) {
1138 log.warn("Src Port for Link {} already removed, ignoring",
1139 linkEvent);
1140 return;
1141 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001142
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07001143 Port dstPort = topology.getPort(linkEvent.getDst().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001144 linkEvent.getDst().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001145 if (dstPort == null) {
1146 log.warn("Dst Port for Link {} already removed, ignoring",
1147 linkEvent);
1148 return;
1149 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001150
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001151 LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(),
1152 linkEvent.getType());
1153 if (linkInTopo == null) {
1154 log.warn("Link {} already removed, ignoring", linkEvent);
1155 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001156 }
Jonathan Hart25bd53e2014-04-30 23:44:09 -07001157
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001158 if (log.isDebugEnabled()) {
1159 // only do sanity check on debug level
1160
1161 Link linkIn = dstPort.getIncomingLink(linkEvent.getType());
1162 if (linkIn == null) {
1163 log.warn("Link {} already removed on destination Port", linkEvent);
1164 }
1165 Link linkOut = srcPort.getOutgoingLink(linkEvent.getType());
1166 if (linkOut == null) {
1167 log.warn("Link {} already removed on src Port", linkEvent);
1168 }
Jonathan Hart25bd53e2014-04-30 23:44:09 -07001169 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001170
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001171 log.debug("Removed {}", linkInTopo);
1172 topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType());
1173 apiRemovedLinkEvents.add(linkInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001174 }
1175
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001176 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001177 * Adds a host to the topology replica.
Ray Milkey269ffb92014-04-03 14:43:30 -07001178 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001179 * TODO: Host-related work is incomplete.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001180 * TODO: Eventually, we might need to consider reordering
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001181 * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events on the same port.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001182 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001183 * @param hostEvent the HostEvent with the host to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001184 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001185 @GuardedBy("topology.writeLock")
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001186 private void addHost(HostEvent hostEvent) {
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001187
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001188 // TODO Decide how to handle update scenario.
1189 // If the new HostEvent has less attachment point compared to
1190 // existing HostEvent, what should the event be?
1191 // - AddHostEvent with some attachment point removed? (current behavior)
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001192
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001193 // create unfrozen copy
1194 // for removing attachment points which already has a link
1195 HostEvent modifiedHostEvent = new HostEvent(hostEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001196
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001197 // Verify each attachment point
Ray Milkey269ffb92014-04-03 14:43:30 -07001198 boolean attachmentFound = false;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001199 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
1200 // XXX domain knowledge: Port must exist before Host
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001201 // but this knowledge cannot be pushed down to driver.
1202
Ray Milkey269ffb92014-04-03 14:43:30 -07001203 // Attached Ports must exist
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001204 Port port = topology.getPort(swp.getDpid(), swp.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001205 if (port == null) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001206 log.debug("{} reordered because port {} was not there", hostEvent, swp);
Ray Milkey269ffb92014-04-03 14:43:30 -07001207 // Reordered event: delay the event in local cache
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001208 ByteBuffer id = hostEvent.getIDasByteBuffer();
1209 reorderedAddedHostEvents.put(id, hostEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001210 return; // should not continue if re-applying later
Ray Milkey269ffb92014-04-03 14:43:30 -07001211 }
1212 // Attached Ports must not have Link
1213 if (port.getOutgoingLink() != null ||
1214 port.getIncomingLink() != null) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001215 log.warn("Link (Out:{},In:{}) exist on the attachment point. "
1216 + "Ignoring this attachmentpoint ({}) from {}.",
1217 port.getOutgoingLink(), port.getIncomingLink(),
1218 swp, modifiedHostEvent);
1219 // FIXME Should either reject, reorder this HostEvent,
1220 // or remove attachment point from given HostEvent
1221 // Removing attachment point from given HostEvent for now.
1222 modifiedHostEvent.removeAttachmentPoint(swp);
Ray Milkey269ffb92014-04-03 14:43:30 -07001223 continue;
1224 }
1225
Ray Milkey269ffb92014-04-03 14:43:30 -07001226 attachmentFound = true;
1227 }
1228
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001229 // Update the host in the topology
Ray Milkey269ffb92014-04-03 14:43:30 -07001230 if (attachmentFound) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001231 if (modifiedHostEvent.getAttachmentPoints().isEmpty()) {
1232 log.warn("No valid attachment point left. Ignoring."
1233 + "original: {}, modified: {}", hostEvent, modifiedHostEvent);
1234 // TODO Should we call #removeHost to trigger remove event?
1235 // only if this call is update.
1236 return;
1237 }
1238
1239 if (log.isDebugEnabled()) {
1240 HostEvent host = topology.getHostEvent(hostEvent.getMac());
1241 if (host != null) {
1242 log.debug("Update {}", modifiedHostEvent);
1243 } else {
1244 log.debug("Added {}", modifiedHostEvent);
1245 }
1246 }
1247 topology.putHost(modifiedHostEvent.freeze());
1248 apiAddedHostEvents.add(modifiedHostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001249 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001250 }
1251
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001252 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001253 * Removes a host from the topology replica.
Ray Milkey269ffb92014-04-03 14:43:30 -07001254 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001255 * TODO: Host-related work is incomplete.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001256 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001257 * @param hostEvent the Host Event with the host to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001258 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001259 @GuardedBy("topology.writeLock")
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001260 private void removeHost(HostEvent hostEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001261
1262 final MACAddress mac = hostEvent.getMac();
1263 HostEvent hostInTopo = topology.getHostEvent(mac);
1264 if (hostInTopo == null) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001265 log.warn("Host {} already removed, ignoring", hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001266 return;
1267 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001268
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001269 log.debug("Removed {}", hostInTopo);
1270 topology.removeHost(mac);
1271 apiRemovedHostEvents.add(hostInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001272 }
Jonathan Hart22eb9882014-02-11 15:52:59 -08001273
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001274 /**
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001275 * Read the whole topology from the database.
1276 *
1277 * @return a collection of EventEntry-encapsulated Topology Events for
1278 * the whole topology.
1279 */
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001280 private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() {
Ray Milkey269ffb92014-04-03 14:43:30 -07001281 Collection<EventEntry<TopologyEvent>> collection =
1282 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001283
Ray Milkey269ffb92014-04-03 14:43:30 -07001284 // XXX May need to clear whole topology first, depending on
1285 // how we initially subscribe to replication events
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001286
Ray Milkey269ffb92014-04-03 14:43:30 -07001287 // Add all active switches
1288 for (KVSwitch sw : KVSwitch.getAllSwitches()) {
1289 if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) {
1290 continue;
1291 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001292
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001293 SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid()));
Ray Milkey269ffb92014-04-03 14:43:30 -07001294 TopologyEvent topologyEvent = new TopologyEvent(switchEvent);
1295 EventEntry<TopologyEvent> eventEntry =
1296 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1297 topologyEvent);
1298 collection.add(eventEntry);
1299 }
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001300
Ray Milkey269ffb92014-04-03 14:43:30 -07001301 // Add all active ports
1302 for (KVPort p : KVPort.getAllPorts()) {
1303 if (p.getStatus() != KVPort.STATUS.ACTIVE) {
1304 continue;
1305 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001306
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001307 PortEvent portEvent = new PortEvent(
1308 new Dpid(p.getDpid()),
1309 new PortNumber(p.getNumber().shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -07001310 TopologyEvent topologyEvent = new TopologyEvent(portEvent);
1311 EventEntry<TopologyEvent> eventEntry =
1312 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1313 topologyEvent);
1314 collection.add(eventEntry);
1315 }
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001316
TeruU28adcc32014-04-15 17:57:35 -07001317 for (KVDevice d : KVDevice.getAllDevices()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001318 HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac()));
TeruU28adcc32014-04-15 17:57:35 -07001319 for (byte[] portId : d.getAllPortIds()) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -07001320 devEvent.addAttachmentPoint(
1321 new SwitchPort(KVPort.getDpidFromKey(portId),
1322 KVPort.getNumberFromKey(portId)));
TeruU28adcc32014-04-15 17:57:35 -07001323 }
1324 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001325
Ray Milkey269ffb92014-04-03 14:43:30 -07001326 for (KVLink l : KVLink.getAllLinks()) {
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001327 LinkEvent linkEvent = new LinkEvent(
1328 new SwitchPort(l.getSrc().dpid, l.getSrc().number),
1329 new SwitchPort(l.getDst().dpid, l.getDst().number));
Ray Milkey269ffb92014-04-03 14:43:30 -07001330 TopologyEvent topologyEvent = new TopologyEvent(linkEvent);
1331 EventEntry<TopologyEvent> eventEntry =
1332 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1333 topologyEvent);
1334 collection.add(eventEntry);
1335 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001336
Ray Milkey269ffb92014-04-03 14:43:30 -07001337 return collection;
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001338 }
Jonathan Hart062a2e82014-02-03 09:41:57 -08001339}