blob: e733c68eee7a15ca8e01835a63052ba862ebb3e5 [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 Radoslavov695f8952014-07-23 16:57:01 -0700266 Map<ByteBuffer, MastershipEvent> addedMastershipEvents =
267 new HashMap<>();
268 Map<ByteBuffer, MastershipEvent> removedMastershipEvents =
269 new HashMap<>();
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800270
Ray Milkey269ffb92014-04-03 14:43:30 -0700271 //
272 // Classify and suppress matching events
273 //
274 for (EventEntry<TopologyEvent> event : events) {
275 TopologyEvent topologyEvent = event.eventData();
276 SwitchEvent switchEvent = topologyEvent.switchEvent;
277 PortEvent portEvent = topologyEvent.portEvent;
278 LinkEvent linkEvent = topologyEvent.linkEvent;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700279 HostEvent hostEvent = topologyEvent.hostEvent;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700280 MastershipEvent mastershipEvent = topologyEvent.mastershipEvent;
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800281
Ray Milkey269ffb92014-04-03 14:43:30 -0700282 //
283 // Extract the events
284 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700285 // FIXME Following event squashing logic based only on ID
286 // potentially lose attribute change.
Ray Milkey269ffb92014-04-03 14:43:30 -0700287 switch (event.eventType()) {
288 case ENTRY_ADD:
289 log.debug("Topology event ENTRY_ADD: {}", topologyEvent);
290 if (switchEvent != null) {
291 ByteBuffer id = switchEvent.getIDasByteBuffer();
292 addedSwitchEvents.put(id, switchEvent);
293 removedSwitchEvents.remove(id);
294 // Switch Events are not affected by event reordering
295 }
296 if (portEvent != null) {
297 ByteBuffer id = portEvent.getIDasByteBuffer();
298 addedPortEvents.put(id, portEvent);
299 removedPortEvents.remove(id);
300 reorderedAddedPortEvents.remove(id);
301 }
302 if (linkEvent != null) {
303 ByteBuffer id = linkEvent.getIDasByteBuffer();
304 addedLinkEvents.put(id, linkEvent);
305 removedLinkEvents.remove(id);
306 reorderedAddedLinkEvents.remove(id);
307 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700308 if (hostEvent != null) {
309 ByteBuffer id = hostEvent.getIDasByteBuffer();
310 addedHostEvents.put(id, hostEvent);
311 removedHostEvents.remove(id);
312 reorderedAddedHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700313 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700314 if (mastershipEvent != null) {
315 ByteBuffer id = mastershipEvent.getIDasByteBuffer();
316 addedMastershipEvents.put(id, mastershipEvent);
317 removedMastershipEvents.remove(id);
318 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700319 break;
320 case ENTRY_REMOVE:
321 log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent);
322 if (switchEvent != null) {
323 ByteBuffer id = switchEvent.getIDasByteBuffer();
324 addedSwitchEvents.remove(id);
325 removedSwitchEvents.put(id, switchEvent);
326 // Switch Events are not affected by event reordering
327 }
328 if (portEvent != null) {
329 ByteBuffer id = portEvent.getIDasByteBuffer();
330 addedPortEvents.remove(id);
331 removedPortEvents.put(id, portEvent);
332 reorderedAddedPortEvents.remove(id);
333 }
334 if (linkEvent != null) {
335 ByteBuffer id = linkEvent.getIDasByteBuffer();
336 addedLinkEvents.remove(id);
337 removedLinkEvents.put(id, linkEvent);
338 reorderedAddedLinkEvents.remove(id);
339 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700340 if (hostEvent != null) {
341 ByteBuffer id = hostEvent.getIDasByteBuffer();
342 addedHostEvents.remove(id);
343 removedHostEvents.put(id, hostEvent);
344 reorderedAddedHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700345 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700346 if (mastershipEvent != null) {
347 ByteBuffer id = mastershipEvent.getIDasByteBuffer();
348 addedMastershipEvents.remove(id);
349 removedMastershipEvents.put(id, mastershipEvent);
350 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700351 break;
Ray Milkey0b122ed2014-04-14 10:06:03 -0700352 default:
353 log.error("Unknown topology event {}",
354 event.eventType());
Ray Milkey269ffb92014-04-03 14:43:30 -0700355 }
356 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800357
Ray Milkey269ffb92014-04-03 14:43:30 -0700358 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700359 // Lock the topology while it is modified
Ray Milkey269ffb92014-04-03 14:43:30 -0700360 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700361 topology.acquireWriteLock();
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800362
Ray Milkey269ffb92014-04-03 14:43:30 -0700363 try {
364 //
365 // Apply the classified events.
366 //
367 // Apply the "add" events in the proper order:
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700368 // mastership, switch, port, link, host
Ray Milkey269ffb92014-04-03 14:43:30 -0700369 //
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700370 // TODO: Currently, the Mastership events are not used,
371 // so their processing ordering is not important (undefined).
372 //
373 for (MastershipEvent mastershipEvent :
374 addedMastershipEvents.values()) {
375 processAddedMastershipEvent(mastershipEvent);
376 }
Ray Milkeyb29e6262014-04-09 16:02:14 -0700377 for (SwitchEvent switchEvent : addedSwitchEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700378 addSwitch(switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700379 }
380 for (PortEvent portEvent : addedPortEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700381 addPort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700382 }
383 for (LinkEvent linkEvent : addedLinkEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700384 addLink(linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700385 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700386 for (HostEvent hostEvent : addedHostEvents.values()) {
387 addHost(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700388 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700389 //
390 // Apply the "remove" events in the reverse order:
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700391 // host, link, port, switch, mastership
Ray Milkey269ffb92014-04-03 14:43:30 -0700392 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700393 for (HostEvent hostEvent : removedHostEvents.values()) {
394 removeHost(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700395 }
396 for (LinkEvent linkEvent : removedLinkEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700397 removeLink(linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700398 }
399 for (PortEvent portEvent : removedPortEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700400 removePort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700401 }
402 for (SwitchEvent switchEvent : removedSwitchEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700403 removeSwitch(switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700404 }
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700405 for (MastershipEvent mastershipEvent :
406 removedMastershipEvents.values()) {
407 processRemovedMastershipEvent(mastershipEvent);
408 }
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800409
Ray Milkey269ffb92014-04-03 14:43:30 -0700410 //
411 // Apply reordered events
412 //
413 applyReorderedEvents(!addedSwitchEvents.isEmpty(),
414 !addedPortEvents.isEmpty());
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800415
Ray Milkey269ffb92014-04-03 14:43:30 -0700416 } finally {
417 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700418 // Topology modifications completed: Release the lock
Ray Milkey269ffb92014-04-03 14:43:30 -0700419 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700420 topology.releaseWriteLock();
Ray Milkey269ffb92014-04-03 14:43:30 -0700421 }
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800422
Ray Milkey269ffb92014-04-03 14:43:30 -0700423 //
424 // Dispatch the Topology Notification Events to the applications
425 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700426 dispatchTopologyEvents();
Ray Milkey269ffb92014-04-03 14:43:30 -0700427 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800428
Ray Milkey269ffb92014-04-03 14:43:30 -0700429 /**
430 * Receive a notification that an entry is added.
431 *
432 * @param value the value for the entry.
433 */
434 @Override
435 public void entryAdded(TopologyEvent value) {
436 EventEntry<TopologyEvent> eventEntry =
437 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
438 value);
439 topologyEvents.add(eventEntry);
440 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800441
Ray Milkey269ffb92014-04-03 14:43:30 -0700442 /**
443 * Receive a notification that an entry is removed.
444 *
445 * @param value the value for the entry.
446 */
447 @Override
448 public void entryRemoved(TopologyEvent value) {
449 EventEntry<TopologyEvent> eventEntry =
450 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
451 value);
452 topologyEvents.add(eventEntry);
453 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800454
Ray Milkey269ffb92014-04-03 14:43:30 -0700455 /**
456 * Receive a notification that an entry is updated.
457 *
458 * @param value the value for the entry.
459 */
460 @Override
461 public void entryUpdated(TopologyEvent value) {
462 // NOTE: The ADD and UPDATE events are processed in same way
463 entryAdded(value);
464 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800465 }
466
467 /**
468 * Startup processing.
469 *
470 * @param datagridService the datagrid service to use.
471 */
472 void startup(IDatagridService datagridService) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700473 eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME,
474 eventHandler,
475 byte[].class,
476 TopologyEvent.class);
477 eventHandler.start();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800478 }
479
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800480 /**
Jonathan Harte37e4e22014-05-13 19:12:02 -0700481 * Dispatch Topology Events to the listeners.
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800482 */
Jonathan Harte37e4e22014-05-13 19:12:02 -0700483 private void dispatchTopologyEvents() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700484 if (apiAddedSwitchEvents.isEmpty() &&
485 apiRemovedSwitchEvents.isEmpty() &&
486 apiAddedPortEvents.isEmpty() &&
487 apiRemovedPortEvents.isEmpty() &&
488 apiAddedLinkEvents.isEmpty() &&
489 apiRemovedLinkEvents.isEmpty() &&
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700490 apiAddedHostEvents.isEmpty() &&
491 apiRemovedHostEvents.isEmpty()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700492 return; // No events to dispatch
493 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800494
Ray Milkey269ffb92014-04-03 14:43:30 -0700495 if (log.isDebugEnabled()) {
496 //
497 // Debug statements
498 // TODO: Those statements should be removed in the future
499 //
Ray Milkeyb29e6262014-04-09 16:02:14 -0700500 for (SwitchEvent switchEvent : apiAddedSwitchEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700501 log.debug("Dispatch Topology Event: ADDED {}", switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700502 }
503 for (SwitchEvent switchEvent : apiRemovedSwitchEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700504 log.debug("Dispatch Topology Event: REMOVED {}", switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700505 }
506 for (PortEvent portEvent : apiAddedPortEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700507 log.debug("Dispatch Topology Event: ADDED {}", portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700508 }
509 for (PortEvent portEvent : apiRemovedPortEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700510 log.debug("Dispatch Topology Event: REMOVED {}", portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700511 }
512 for (LinkEvent linkEvent : apiAddedLinkEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700513 log.debug("Dispatch Topology Event: ADDED {}", linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700514 }
515 for (LinkEvent linkEvent : apiRemovedLinkEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700516 log.debug("Dispatch Topology Event: REMOVED {}", linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700517 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700518 for (HostEvent hostEvent : apiAddedHostEvents) {
519 log.debug("Dispatch Topology Event: ADDED {}", hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700520 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700521 for (HostEvent hostEvent : apiRemovedHostEvents) {
522 log.debug("Dispatch Topology Event: REMOVED {}", hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700523 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700524 }
adminbc181552014-02-21 18:36:42 -0800525
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700526 //
527 // Update the metrics
528 //
529 long totalEvents =
530 apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() +
531 apiAddedPortEvents.size() + apiRemovedPortEvents.size() +
532 apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() +
533 apiAddedHostEvents.size() + apiRemovedHostEvents.size();
534 this.listenerEventRate.mark(totalEvents);
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -0700535 this.lastEventTimestampEpochMs = System.currentTimeMillis();
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700536
537 //
Ray Milkey269ffb92014-04-03 14:43:30 -0700538 // Deliver the events
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700539 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700540 for (ITopologyListener listener : this.topologyListeners) {
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700541 TopologyEvents events =
Pavlin Radoslavovc61544e2014-07-23 16:27:27 -0700542 new TopologyEvents(kryo.copy(apiAddedSwitchEvents),
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700543 kryo.copy(apiRemovedSwitchEvents),
544 kryo.copy(apiAddedPortEvents),
545 kryo.copy(apiRemovedPortEvents),
546 kryo.copy(apiAddedLinkEvents),
547 kryo.copy(apiRemovedLinkEvents),
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700548 kryo.copy(apiAddedHostEvents),
549 kryo.copy(apiRemovedHostEvents));
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700550 listener.topologyEvents(events);
Ray Milkey269ffb92014-04-03 14:43:30 -0700551 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800552
Ray Milkey269ffb92014-04-03 14:43:30 -0700553 //
554 // Cleanup
555 //
556 apiAddedSwitchEvents.clear();
557 apiRemovedSwitchEvents.clear();
558 apiAddedPortEvents.clear();
559 apiRemovedPortEvents.clear();
560 apiAddedLinkEvents.clear();
561 apiRemovedLinkEvents.clear();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700562 apiAddedHostEvents.clear();
563 apiRemovedHostEvents.clear();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800564 }
565
566 /**
567 * Apply reordered events.
568 *
569 * @param hasAddedSwitchEvents true if there were Added Switch Events.
Ray Milkey269ffb92014-04-03 14:43:30 -0700570 * @param hasAddedPortEvents true if there were Added Port Events.
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800571 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700572 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800573 private void applyReorderedEvents(boolean hasAddedSwitchEvents,
Ray Milkey269ffb92014-04-03 14:43:30 -0700574 boolean hasAddedPortEvents) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700575 if (!(hasAddedSwitchEvents || hasAddedPortEvents)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700576 return; // Nothing to do
Ray Milkeyb29e6262014-04-09 16:02:14 -0700577 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800578
Ray Milkey269ffb92014-04-03 14:43:30 -0700579 //
580 // Try to apply the reordered events.
581 //
582 // NOTE: For simplicity we try to apply all events of a particular
583 // type if any "parent" type event was processed:
584 // - Apply reordered Port Events if Switches were added
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700585 // - Apply reordered Link and Host Events if Switches or Ports
Ray Milkey269ffb92014-04-03 14:43:30 -0700586 // were added
587 //
adminbc181552014-02-21 18:36:42 -0800588
Ray Milkey269ffb92014-04-03 14:43:30 -0700589 //
590 // Apply reordered Port Events if Switches were added
591 //
592 if (hasAddedSwitchEvents) {
593 Map<ByteBuffer, PortEvent> portEvents = reorderedAddedPortEvents;
594 reorderedAddedPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700595 for (PortEvent portEvent : portEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700596 addPort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700597 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700598 }
599 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700600 // Apply reordered Link and Host Events if Switches or Ports
Ray Milkey269ffb92014-04-03 14:43:30 -0700601 // were added.
602 //
603 Map<ByteBuffer, LinkEvent> linkEvents = reorderedAddedLinkEvents;
604 reorderedAddedLinkEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700605 for (LinkEvent linkEvent : linkEvents.values()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700606 addLink(linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700607 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700608 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700609 Map<ByteBuffer, HostEvent> hostEvents = reorderedAddedHostEvents;
610 reorderedAddedHostEvents = new HashMap<>();
611 for (HostEvent hostEvent : hostEvents.values()) {
612 addHost(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700613 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800614 }
615
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800616 /**
617 * Switch discovered event.
618 *
619 * @param switchEvent the switch event.
Ray Milkey269ffb92014-04-03 14:43:30 -0700620 * @param portEvents the corresponding port events for the switch.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800621 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800622 @Override
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800623 public void putSwitchDiscoveryEvent(SwitchEvent switchEvent,
Ray Milkey269ffb92014-04-03 14:43:30 -0700624 Collection<PortEvent> portEvents) {
625 if (datastore.addSwitch(switchEvent, portEvents)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700626 log.debug("Sending add switch: {}", switchEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700627 // Send out notification
628 TopologyEvent topologyEvent = new TopologyEvent(switchEvent);
629 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800630
Ray Milkey269ffb92014-04-03 14:43:30 -0700631 // Send out notification for each port
632 for (PortEvent portEvent : portEvents) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700633 log.debug("Sending add port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700634 topologyEvent = new TopologyEvent(portEvent);
635 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
636 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800637
Ray Milkey269ffb92014-04-03 14:43:30 -0700638 //
639 // Keep track of the added ports
640 //
641 // Get the old Port Events
642 Map<ByteBuffer, PortEvent> oldPortEvents =
643 discoveredAddedPortEvents.get(switchEvent.getDpid());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700644 if (oldPortEvents == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700645 oldPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700646 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800647
Ray Milkey269ffb92014-04-03 14:43:30 -0700648 // Store the new Port Events in the local cache
649 Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>();
650 for (PortEvent portEvent : portEvents) {
651 ByteBuffer id = portEvent.getIDasByteBuffer();
652 newPortEvents.put(id, portEvent);
653 }
654 discoveredAddedPortEvents.put(switchEvent.getDpid(),
655 newPortEvents);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800656
Ray Milkey269ffb92014-04-03 14:43:30 -0700657 //
658 // Extract the removed ports
659 //
660 List<PortEvent> removedPortEvents = new LinkedList<>();
661 for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) {
662 ByteBuffer key = entry.getKey();
663 PortEvent portEvent = entry.getValue();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700664 if (!newPortEvents.containsKey(key)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700665 removedPortEvents.add(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700666 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700667 }
668
669 // Cleanup old removed ports
Ray Milkeyb29e6262014-04-09 16:02:14 -0700670 for (PortEvent portEvent : removedPortEvents) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700671 removePortDiscoveryEvent(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700672 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700673 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800674 }
675
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800676 /**
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700677 * {@inheritDoc}
678 * <p/>
679 * Called by {@link TopologyPublisher.SwitchCleanup} thread.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800680 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800681 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800682 public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700683 // Get the old Port Events
684 Map<ByteBuffer, PortEvent> oldPortEvents =
685 discoveredAddedPortEvents.get(switchEvent.getDpid());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700686 if (oldPortEvents == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700687 oldPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700688 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800689
Ray Milkey269ffb92014-04-03 14:43:30 -0700690 if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700691 log.debug("Sending remove switch: {}", switchEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700692 // Send out notification
693 eventChannel.removeEntry(switchEvent.getID());
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800694
Ray Milkey269ffb92014-04-03 14:43:30 -0700695 //
696 // Send out notification for each port.
697 //
698 // NOTE: We don't use removePortDiscoveryEvent() for the cleanup,
699 // because it will attempt to remove the port from the database,
700 // and the deactiveSwitch() call above already removed all ports.
701 //
Ray Milkeyb29e6262014-04-09 16:02:14 -0700702 for (PortEvent portEvent : oldPortEvents.values()) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700703 log.debug("Sending remove port:", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700704 eventChannel.removeEntry(portEvent.getID());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700705 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700706 discoveredAddedPortEvents.remove(switchEvent.getDpid());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800707
Ray Milkey269ffb92014-04-03 14:43:30 -0700708 // Cleanup for each link
709 Map<ByteBuffer, LinkEvent> oldLinkEvents =
710 discoveredAddedLinkEvents.get(switchEvent.getDpid());
711 if (oldLinkEvents != null) {
712 for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) {
713 removeLinkDiscoveryEvent(linkEvent);
714 }
715 discoveredAddedLinkEvents.remove(switchEvent.getDpid());
716 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800717
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700718 // Cleanup for each host
719 Map<ByteBuffer, HostEvent> oldHostEvents =
720 discoveredAddedHostEvents.get(switchEvent.getDpid());
721 if (oldHostEvents != null) {
722 for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) {
723 removeHostDiscoveryEvent(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700724 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700725 discoveredAddedHostEvents.remove(switchEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700726 }
727 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800728 }
729
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800730 /**
731 * Port discovered 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 putPortDiscoveryEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700737 if (datastore.addPort(portEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700738 log.debug("Sending add port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700739 // Send out notification
740 TopologyEvent topologyEvent = new TopologyEvent(portEvent);
741 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800742
Ray Milkey269ffb92014-04-03 14:43:30 -0700743 // Store the new Port Event in the local cache
744 Map<ByteBuffer, PortEvent> oldPortEvents =
745 discoveredAddedPortEvents.get(portEvent.getDpid());
746 if (oldPortEvents == null) {
747 oldPortEvents = new HashMap<>();
748 discoveredAddedPortEvents.put(portEvent.getDpid(),
749 oldPortEvents);
750 }
751 ByteBuffer id = portEvent.getIDasByteBuffer();
752 oldPortEvents.put(id, portEvent);
753 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800754 }
755
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800756 /**
757 * Port removed event.
758 *
759 * @param portEvent the port event.
760 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800761 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800762 public void removePortDiscoveryEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700763 if (datastore.deactivatePort(portEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700764 log.debug("Sending remove port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700765 // Send out notification
766 eventChannel.removeEntry(portEvent.getID());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800767
Ray Milkey269ffb92014-04-03 14:43:30 -0700768 // Cleanup the Port Event from the local cache
769 Map<ByteBuffer, PortEvent> oldPortEvents =
770 discoveredAddedPortEvents.get(portEvent.getDpid());
771 if (oldPortEvents != null) {
772 ByteBuffer id = portEvent.getIDasByteBuffer();
773 oldPortEvents.remove(id);
774 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800775
Ray Milkey269ffb92014-04-03 14:43:30 -0700776 // Cleanup for the incoming link
777 Map<ByteBuffer, LinkEvent> oldLinkEvents =
778 discoveredAddedLinkEvents.get(portEvent.getDpid());
779 if (oldLinkEvents != null) {
780 for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700781 if (linkEvent.getDst().equals(portEvent.getSwitchPort())) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700782 removeLinkDiscoveryEvent(linkEvent);
783 // XXX If we change our model to allow multiple Link on
784 // a Port, this loop must be fixed to allow continuing.
785 break;
786 }
787 }
788 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800789
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700790 // Cleanup for the connected hosts
Ray Milkey269ffb92014-04-03 14:43:30 -0700791 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700792 List<HostEvent> removedHostEvents = new LinkedList<>();
793 Map<ByteBuffer, HostEvent> oldHostEvents =
794 discoveredAddedHostEvents.get(portEvent.getDpid());
795 if (oldHostEvents != null) {
796 for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) {
797 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700798 if (swp.equals(portEvent.getSwitchPort())) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700799 removedHostEvents.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700800 }
801 }
802 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700803 for (HostEvent hostEvent : removedHostEvents) {
804 removeHostDiscoveryEvent(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700805 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700806 }
807 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800808 }
809
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800810 /**
811 * Link discovered 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 putLinkDiscoveryEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700817 if (datastore.addLink(linkEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700818 log.debug("Sending add link: {}", linkEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700819 // Send out notification
820 TopologyEvent topologyEvent = new TopologyEvent(linkEvent);
821 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800822
Ray Milkey269ffb92014-04-03 14:43:30 -0700823 // Store the new Link Event in the local cache
824 Map<ByteBuffer, LinkEvent> oldLinkEvents =
825 discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
826 if (oldLinkEvents == null) {
827 oldLinkEvents = new HashMap<>();
828 discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(),
829 oldLinkEvents);
830 }
831 ByteBuffer id = linkEvent.getIDasByteBuffer();
832 oldLinkEvents.put(id, linkEvent);
833 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800834 }
835
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800836 /**
837 * Link removed event.
838 *
839 * @param linkEvent the link event.
840 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800841 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800842 public void removeLinkDiscoveryEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700843 if (datastore.removeLink(linkEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700844 log.debug("Sending remove link: {}", linkEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700845 // Send out notification
846 eventChannel.removeEntry(linkEvent.getID());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800847
Ray Milkey269ffb92014-04-03 14:43:30 -0700848 // Cleanup the Link Event from the local cache
849 Map<ByteBuffer, LinkEvent> oldLinkEvents =
850 discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
851 if (oldLinkEvents != null) {
852 ByteBuffer id = linkEvent.getIDasByteBuffer();
853 oldLinkEvents.remove(id);
854 }
855 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800856 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800857
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800858 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700859 * Host discovered event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800860 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700861 * @param hostEvent the host event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800862 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800863 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700864 public void putHostDiscoveryEvent(HostEvent hostEvent) {
865 if (datastore.addHost(hostEvent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700866 // Send out notification
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700867 TopologyEvent topologyEvent = new TopologyEvent(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700868 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700869 log.debug("Put the host info into the cache of the topology. mac {}", hostEvent.getMac());
Ray Milkey269ffb92014-04-03 14:43:30 -0700870
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700871 // Store the new Host Event in the local cache
Ray Milkey269ffb92014-04-03 14:43:30 -0700872 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700873 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
874 Map<ByteBuffer, HostEvent> oldHostEvents =
875 discoveredAddedHostEvents.get(swp.getDpid());
876 if (oldHostEvents == null) {
877 oldHostEvents = new HashMap<>();
878 discoveredAddedHostEvents.put(swp.getDpid(),
879 oldHostEvents);
Ray Milkey269ffb92014-04-03 14:43:30 -0700880 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700881 ByteBuffer id = hostEvent.getIDasByteBuffer();
882 oldHostEvents.put(id, hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700883 }
884 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800885 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800886
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800887 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700888 * Host removed event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800889 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700890 * @param hostEvent the host event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800891 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800892 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700893 public void removeHostDiscoveryEvent(HostEvent hostEvent) {
894 if (datastore.removeHost(hostEvent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700895 // Send out notification
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700896 eventChannel.removeEntry(hostEvent.getID());
897 log.debug("Remove the host info into the cache of the topology. mac {}", hostEvent.getMac());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800898
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700899 // Cleanup the Host Event from the local cache
Ray Milkey269ffb92014-04-03 14:43:30 -0700900 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700901 ByteBuffer id = ByteBuffer.wrap(hostEvent.getID());
902 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
903 Map<ByteBuffer, HostEvent> oldHostEvents =
904 discoveredAddedHostEvents.get(swp.getDpid());
905 if (oldHostEvents != null) {
906 oldHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700907 }
908 }
909 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800910 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800911
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700912 //
913 // Methods to update topology replica
914 //
915
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800916 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700917 * Mastership updated event.
918 *
919 * @param mastershipEvent the mastership event.
920 */
921 @Override
922 public void putSwitchMastershipEvent(MastershipEvent mastershipEvent) {
923 // Send out notification
924 TopologyEvent topologyEvent = new TopologyEvent(mastershipEvent);
925 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
926 }
927
928 /**
929 * Mastership removed event.
930 *
931 * @param mastershipEvent the mastership event.
932 */
933 @Override
934 public void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) {
935 // Send out notification
936 eventChannel.removeEntry(mastershipEvent.getID());
937 }
938
939 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700940 * Adds a switch to the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800941 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700942 * @param switchEvent the SwitchEvent with the switch to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800943 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700944 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800945 private void addSwitch(SwitchEvent switchEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700946 if (log.isDebugEnabled()) {
947 SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid());
948 if (sw != null) {
949 log.debug("Update {}", switchEvent);
950 } else {
951 log.debug("Added {}", switchEvent);
952 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700953 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700954 topology.putSwitch(switchEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -0700955 apiAddedSwitchEvents.add(switchEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800956 }
957
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800958 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700959 * Removes a switch from the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700960 * <p/>
961 * It will call {@link #removePort(PortEvent)} for each ports on this switch.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800962 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700963 * @param switchEvent the SwitchEvent with the switch to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800964 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700965 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800966 private void removeSwitch(SwitchEvent switchEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700967 final Dpid dpid = switchEvent.getDpid();
968
969 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
970 if (swInTopo == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700971 log.warn("Switch {} already removed, ignoring", switchEvent);
972 return;
973 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800974
Ray Milkey269ffb92014-04-03 14:43:30 -0700975 //
976 // Remove all Ports on the Switch
977 //
978 ArrayList<PortEvent> portsToRemove = new ArrayList<>();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700979 for (Port port : topology.getPorts(dpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700980 log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.",
981 port, switchEvent);
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -0700982 PortEvent portEvent = new PortEvent(port.getSwitchPort());
Ray Milkey269ffb92014-04-03 14:43:30 -0700983 portsToRemove.add(portEvent);
984 }
Ray Milkeyb29e6262014-04-09 16:02:14 -0700985 for (PortEvent portEvent : portsToRemove) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700986 removePort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700987 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800988
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700989 log.debug("Removed {}", swInTopo);
990 topology.removeSwitch(dpid);
991 apiRemovedSwitchEvents.add(swInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800992 }
993
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800994 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700995 * Adds a port to the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800996 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700997 * @param portEvent the PortEvent with the port to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800998 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700999 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001000 private void addPort(PortEvent portEvent) {
Jonathan Harte37e4e22014-05-13 19:12:02 -07001001 Switch sw = topology.getSwitch(portEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -07001002 if (sw == null) {
Jonathan Hartf1675202014-05-23 14:59:07 -07001003 log.debug("{} reordered because switch is null", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001004 // Reordered event: delay the event in local cache
1005 ByteBuffer id = portEvent.getIDasByteBuffer();
1006 reorderedAddedPortEvents.put(id, portEvent);
1007 return;
1008 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001009
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001010 if (log.isDebugEnabled()) {
1011 PortEvent port = topology.getPortEvent(portEvent.getSwitchPort());
1012 if (port != null) {
1013 log.debug("Update {}", portEvent);
1014 } else {
1015 log.debug("Added {}", portEvent);
1016 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001017 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001018 topology.putPort(portEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -07001019 apiAddedPortEvents.add(portEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001020 }
1021
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001022 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001023 * Removes a port from the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001024 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001025 * It will remove attachment points from each hosts on this port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001026 * and call {@link #removeLink(LinkEvent)} for each links on this port.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001027 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001028 * @param portEvent the PortEvent with the port to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001029 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001030 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001031 private void removePort(PortEvent portEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001032 SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -07001033 if (sw == null) {
1034 log.warn("Parent Switch for Port {} already removed, ignoring",
1035 portEvent);
1036 return;
1037 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001038
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001039 final SwitchPort switchPort = portEvent.getSwitchPort();
1040 PortEvent portInTopo = topology.getPortEvent(switchPort);
1041 if (portInTopo == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001042 log.warn("Port {} already removed, ignoring", portEvent);
1043 return;
1044 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001045
Ray Milkey269ffb92014-04-03 14:43:30 -07001046 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001047 // Remove all Host attachment points bound to this Port
Ray Milkey269ffb92014-04-03 14:43:30 -07001048 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001049 List<HostEvent> hostsToUpdate = new ArrayList<>();
1050 for (Host host : topology.getHosts(switchPort)) {
1051 log.debug("Removing Host {} on Port {}", host, portInTopo);
1052 HostEvent hostEvent = topology.getHostEvent(host.getMacAddress());
1053 hostsToUpdate.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001054 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001055 for (HostEvent hostEvent : hostsToUpdate) {
1056 HostEvent newHostEvent = new HostEvent(hostEvent);
1057 newHostEvent.removeAttachmentPoint(switchPort);
1058 newHostEvent.freeze();
1059
1060 // TODO should this event be fired inside #addHost?
1061 if (newHostEvent.getAttachmentPoints().isEmpty()) {
1062 // No more attachment point left -> remove Host
1063 removeHost(hostEvent);
1064 } else {
1065 // Update Host
1066 addHost(newHostEvent);
1067 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001068 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001069
Ray Milkey269ffb92014-04-03 14:43:30 -07001070 //
1071 // Remove all Links connected to the Port
1072 //
1073 Set<Link> links = new HashSet<>();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001074 links.addAll(topology.getOutgoingLinks(switchPort));
1075 links.addAll(topology.getIncomingLinks(switchPort));
Ray Milkey269ffb92014-04-03 14:43:30 -07001076 for (Link link : links) {
Ray Milkeyb29e6262014-04-09 16:02:14 -07001077 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001078 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001079 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001080 LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple());
1081 if (linkEvent != null) {
1082 log.debug("Removing Link {} on Port {}", link, portInTopo);
1083 removeLink(linkEvent);
1084 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001085 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001086
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001087 // Remove the Port from Topology
1088 log.debug("Removed {}", portInTopo);
1089 topology.removePort(switchPort);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001090
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001091 apiRemovedPortEvents.add(portInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001092 }
1093
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001094 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001095 * Adds a link to the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001096 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001097 * It will remove attachment points from each hosts using the same ports.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001098 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001099 * @param linkEvent the LinkEvent with the link to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001100 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001101 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001102 private void addLink(LinkEvent linkEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001103 PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc());
1104 PortEvent dstPort = topology.getPortEvent(linkEvent.getDst());
Ray Milkey269ffb92014-04-03 14:43:30 -07001105 if ((srcPort == null) || (dstPort == null)) {
Jonathan Hartf1675202014-05-23 14:59:07 -07001106 log.debug("{} reordered because {} port is null", linkEvent,
1107 (srcPort == null) ? "src" : "dst");
1108
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001109 // XXX domain knowledge: port must be present before link.
Ray Milkey269ffb92014-04-03 14:43:30 -07001110 // Reordered event: delay the event in local cache
1111 ByteBuffer id = linkEvent.getIDasByteBuffer();
1112 reorderedAddedLinkEvents.put(id, linkEvent);
1113 return;
1114 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001115
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001116 // XXX domain knowledge: Sanity check: Port cannot have both Link and Host
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001117 // FIXME potentially local replica may not be up-to-date yet due to HZ delay.
1118 // may need to manage local truth and use them instead.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001119 if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) {
1120 // Only check for existing Host when adding new Link.
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001121
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001122 // Remove all Hosts attached to the ports on both ends
1123
1124 Set<HostEvent> hostsToUpdate = new TreeSet<>(new Comparator<HostEvent>() {
1125 // comparison only using ID(=MAC)
1126 @Override
1127 public int compare(HostEvent o1, HostEvent o2) {
1128 return Long.compare(o1.getMac().toLong(), o2.getMac().toLong());
1129 }
1130 });
1131
1132 List<SwitchPort> portsToCheck = Arrays.asList(
1133 srcPort.getSwitchPort(),
1134 dstPort.getSwitchPort());
1135
1136 // Enumerate Host which needs to be updated by this Link add event
1137 for (SwitchPort port : portsToCheck) {
1138 for (Host host : topology.getHosts(port)) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001139 log.error("Host {} on Port {} should have been removed prior to adding Link {}",
1140 host, port, linkEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001141
1142 HostEvent hostEvent = topology.getHostEvent(host.getMacAddress());
1143 hostsToUpdate.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001144 }
1145 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001146 // remove attachment point from them.
1147 for (HostEvent hostEvent : hostsToUpdate) {
1148 // remove port from attachment point and update
1149 HostEvent newHostEvent = new HostEvent(hostEvent);
1150 newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort());
1151 newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort());
1152 newHostEvent.freeze();
1153
1154 // TODO should this event be fired inside #addHost?
1155 if (newHostEvent.getAttachmentPoints().isEmpty()) {
1156 // No more attachment point left -> remove Host
1157 removeHost(hostEvent);
1158 } else {
1159 // Update Host
1160 addHost(newHostEvent);
1161 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001162 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001163 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001164
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001165 if (log.isDebugEnabled()) {
1166 LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple());
1167 if (link != null) {
1168 log.debug("Update {}", linkEvent);
1169 } else {
1170 log.debug("Added {}", linkEvent);
1171 }
1172 }
1173 topology.putLink(linkEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -07001174 apiAddedLinkEvents.add(linkEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001175 }
1176
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001177 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001178 * Removes a link from the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001179 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001180 * @param linkEvent the LinkEvent with the link to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001181 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001182 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001183 private void removeLink(LinkEvent linkEvent) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07001184 Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001185 linkEvent.getSrc().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001186 if (srcPort == null) {
1187 log.warn("Src Port for Link {} already removed, ignoring",
1188 linkEvent);
1189 return;
1190 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001191
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07001192 Port dstPort = topology.getPort(linkEvent.getDst().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001193 linkEvent.getDst().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001194 if (dstPort == null) {
1195 log.warn("Dst Port for Link {} already removed, ignoring",
1196 linkEvent);
1197 return;
1198 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001199
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001200 LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(),
1201 linkEvent.getType());
1202 if (linkInTopo == null) {
1203 log.warn("Link {} already removed, ignoring", linkEvent);
1204 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001205 }
Jonathan Hart25bd53e2014-04-30 23:44:09 -07001206
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001207 if (log.isDebugEnabled()) {
1208 // only do sanity check on debug level
1209
1210 Link linkIn = dstPort.getIncomingLink(linkEvent.getType());
1211 if (linkIn == null) {
1212 log.warn("Link {} already removed on destination Port", linkEvent);
1213 }
1214 Link linkOut = srcPort.getOutgoingLink(linkEvent.getType());
1215 if (linkOut == null) {
1216 log.warn("Link {} already removed on src Port", linkEvent);
1217 }
Jonathan Hart25bd53e2014-04-30 23:44:09 -07001218 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001219
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001220 log.debug("Removed {}", linkInTopo);
1221 topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType());
1222 apiRemovedLinkEvents.add(linkInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001223 }
1224
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001225 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001226 * Adds a host to the topology replica.
Ray Milkey269ffb92014-04-03 14:43:30 -07001227 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001228 * TODO: Host-related work is incomplete.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001229 * TODO: Eventually, we might need to consider reordering
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001230 * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events on the same port.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001231 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001232 * @param hostEvent the HostEvent with the host to add.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001233 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001234 @GuardedBy("topology.writeLock")
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001235 private void addHost(HostEvent hostEvent) {
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001236
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001237 // TODO Decide how to handle update scenario.
1238 // If the new HostEvent has less attachment point compared to
1239 // existing HostEvent, what should the event be?
1240 // - AddHostEvent with some attachment point removed? (current behavior)
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001241
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001242 // create unfrozen copy
1243 // for removing attachment points which already has a link
1244 HostEvent modifiedHostEvent = new HostEvent(hostEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001245
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001246 // Verify each attachment point
Ray Milkey269ffb92014-04-03 14:43:30 -07001247 boolean attachmentFound = false;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001248 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
1249 // XXX domain knowledge: Port must exist before Host
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001250 // but this knowledge cannot be pushed down to driver.
1251
Ray Milkey269ffb92014-04-03 14:43:30 -07001252 // Attached Ports must exist
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001253 Port port = topology.getPort(swp.getDpid(), swp.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001254 if (port == null) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001255 log.debug("{} reordered because port {} was not there", hostEvent, swp);
Ray Milkey269ffb92014-04-03 14:43:30 -07001256 // Reordered event: delay the event in local cache
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001257 ByteBuffer id = hostEvent.getIDasByteBuffer();
1258 reorderedAddedHostEvents.put(id, hostEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001259 return; // should not continue if re-applying later
Ray Milkey269ffb92014-04-03 14:43:30 -07001260 }
1261 // Attached Ports must not have Link
1262 if (port.getOutgoingLink() != null ||
1263 port.getIncomingLink() != null) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001264 log.warn("Link (Out:{},In:{}) exist on the attachment point. "
1265 + "Ignoring this attachmentpoint ({}) from {}.",
1266 port.getOutgoingLink(), port.getIncomingLink(),
1267 swp, modifiedHostEvent);
1268 // FIXME Should either reject, reorder this HostEvent,
1269 // or remove attachment point from given HostEvent
1270 // Removing attachment point from given HostEvent for now.
1271 modifiedHostEvent.removeAttachmentPoint(swp);
Ray Milkey269ffb92014-04-03 14:43:30 -07001272 continue;
1273 }
1274
Ray Milkey269ffb92014-04-03 14:43:30 -07001275 attachmentFound = true;
1276 }
1277
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001278 // Update the host in the topology
Ray Milkey269ffb92014-04-03 14:43:30 -07001279 if (attachmentFound) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001280 if (modifiedHostEvent.getAttachmentPoints().isEmpty()) {
1281 log.warn("No valid attachment point left. Ignoring."
1282 + "original: {}, modified: {}", hostEvent, modifiedHostEvent);
1283 // TODO Should we call #removeHost to trigger remove event?
1284 // only if this call is update.
1285 return;
1286 }
1287
1288 if (log.isDebugEnabled()) {
1289 HostEvent host = topology.getHostEvent(hostEvent.getMac());
1290 if (host != null) {
1291 log.debug("Update {}", modifiedHostEvent);
1292 } else {
1293 log.debug("Added {}", modifiedHostEvent);
1294 }
1295 }
1296 topology.putHost(modifiedHostEvent.freeze());
1297 apiAddedHostEvents.add(modifiedHostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001298 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001299 }
1300
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001301 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001302 * Removes a host from the topology replica.
Ray Milkey269ffb92014-04-03 14:43:30 -07001303 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001304 * TODO: Host-related work is incomplete.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001305 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001306 * @param hostEvent the Host Event with the host to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001307 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001308 @GuardedBy("topology.writeLock")
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001309 private void removeHost(HostEvent hostEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001310
1311 final MACAddress mac = hostEvent.getMac();
1312 HostEvent hostInTopo = topology.getHostEvent(mac);
1313 if (hostInTopo == null) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001314 log.warn("Host {} already removed, ignoring", hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001315 return;
1316 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001317
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001318 log.debug("Removed {}", hostInTopo);
1319 topology.removeHost(mac);
1320 apiRemovedHostEvents.add(hostInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001321 }
Jonathan Hart22eb9882014-02-11 15:52:59 -08001322
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001323 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -07001324 * Processes added Switch Mastership event.
1325 *
1326 * @param mastershipEvent the MastershipEvent to process.
1327 */
1328 @GuardedBy("topology.writeLock")
1329 private void processAddedMastershipEvent(MastershipEvent mastershipEvent) {
1330 log.debug("Processing added Mastership event {}",
1331 mastershipEvent);
1332 // TODO: Not implemented/used yet.
1333 }
1334
1335 /**
1336 * Processes removed Switch Mastership event.
1337 *
1338 * @param mastershipEvent the MastershipEvent to process.
1339 */
1340 @GuardedBy("topology.writeLock")
1341 private void processRemovedMastershipEvent(MastershipEvent mastershipEvent) {
1342 log.debug("Processing removed Mastership event {}",
1343 mastershipEvent);
1344 // TODO: Not implemented/used yet.
1345 }
1346
1347 /**
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001348 * Read the whole topology from the database.
1349 *
1350 * @return a collection of EventEntry-encapsulated Topology Events for
1351 * the whole topology.
1352 */
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001353 private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() {
Ray Milkey269ffb92014-04-03 14:43:30 -07001354 Collection<EventEntry<TopologyEvent>> collection =
1355 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001356
Ray Milkey269ffb92014-04-03 14:43:30 -07001357 // XXX May need to clear whole topology first, depending on
1358 // how we initially subscribe to replication events
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001359
Ray Milkey269ffb92014-04-03 14:43:30 -07001360 // Add all active switches
1361 for (KVSwitch sw : KVSwitch.getAllSwitches()) {
1362 if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) {
1363 continue;
1364 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001365
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001366 SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid()));
Ray Milkey269ffb92014-04-03 14:43:30 -07001367 TopologyEvent topologyEvent = new TopologyEvent(switchEvent);
1368 EventEntry<TopologyEvent> eventEntry =
1369 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1370 topologyEvent);
1371 collection.add(eventEntry);
1372 }
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001373
Ray Milkey269ffb92014-04-03 14:43:30 -07001374 // Add all active ports
1375 for (KVPort p : KVPort.getAllPorts()) {
1376 if (p.getStatus() != KVPort.STATUS.ACTIVE) {
1377 continue;
1378 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001379
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001380 PortEvent portEvent = new PortEvent(
1381 new Dpid(p.getDpid()),
1382 new PortNumber(p.getNumber().shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -07001383 TopologyEvent topologyEvent = new TopologyEvent(portEvent);
1384 EventEntry<TopologyEvent> eventEntry =
1385 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1386 topologyEvent);
1387 collection.add(eventEntry);
1388 }
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001389
TeruU28adcc32014-04-15 17:57:35 -07001390 for (KVDevice d : KVDevice.getAllDevices()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001391 HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac()));
TeruU28adcc32014-04-15 17:57:35 -07001392 for (byte[] portId : d.getAllPortIds()) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -07001393 devEvent.addAttachmentPoint(
1394 new SwitchPort(KVPort.getDpidFromKey(portId),
1395 KVPort.getNumberFromKey(portId)));
TeruU28adcc32014-04-15 17:57:35 -07001396 }
1397 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001398
Ray Milkey269ffb92014-04-03 14:43:30 -07001399 for (KVLink l : KVLink.getAllLinks()) {
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001400 LinkEvent linkEvent = new LinkEvent(
1401 new SwitchPort(l.getSrc().dpid, l.getSrc().number),
1402 new SwitchPort(l.getDst().dpid, l.getDst().number));
Ray Milkey269ffb92014-04-03 14:43:30 -07001403 TopologyEvent topologyEvent = new TopologyEvent(linkEvent);
1404 EventEntry<TopologyEvent> eventEntry =
1405 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1406 topologyEvent);
1407 collection.add(eventEntry);
1408 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001409
Ray Milkey269ffb92014-04-03 14:43:30 -07001410 return collection;
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001411 }
Jonathan Hart062a2e82014-02-03 09:41:57 -08001412}