blob: b6c2c8681c2995fb77bc5602e9596ad8bfbf021d [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;
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -070073 private Kryo kryo = KryoFactory.newKryoObject();
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -070074 private TopologyEventPreprocessor eventPreprocessor;
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -070075 private CopyOnWriteArrayList<ITopologyListener> topologyListeners =
76 new CopyOnWriteArrayList<>();
Pavlin Radoslavov054cd592014-08-07 20:57:16 -070077 private CopyOnWriteArrayList<ITopologyListener> newTopologyListeners =
78 new CopyOnWriteArrayList<>();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080079
Pavlin Radoslavov706add22014-02-20 12:15:59 -080080 //
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070081 // Metrics
82 //
83 private static final MetricsComponent METRICS_COMPONENT =
84 OnosMetrics.registerComponent("Topology");
85 private static final MetricsFeature METRICS_FEATURE_EVENT_NOTIFICATION =
86 METRICS_COMPONENT.registerFeature("EventNotification");
87 //
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -070088 // Timestamp of the last Topology event (ms from the Epoch)
89 private volatile long lastEventTimestampEpochMs = 0;
90 private final Gauge<Long> gaugeLastEventTimestampEpochMs =
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070091 OnosMetrics.registerMetric(METRICS_COMPONENT,
92 METRICS_FEATURE_EVENT_NOTIFICATION,
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -070093 "LastEventTimestamp.EpochMs",
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070094 new Gauge<Long>() {
95 @Override
96 public Long getValue() {
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -070097 return lastEventTimestampEpochMs;
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070098 }
99 });
100 // Rate of the Topology events published to the Topology listeners
101 private final Meter listenerEventRate =
102 OnosMetrics.createMeter(METRICS_COMPONENT,
103 METRICS_FEATURE_EVENT_NOTIFICATION,
104 "ListenerEventRate");
105
106 //
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800107 // Local state for keeping track of locally discovered events so we can
108 // cleanup properly when a Switch or Port is removed.
109 //
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700110 // We keep all Port, (incoming) Link and Host events per Switch DPID:
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800111 // - If a switch goes down, we remove all corresponding Port, Link and
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700112 // Host events.
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800113 // - If a port on a switch goes down, we remove all corresponding Link
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700114 // and Host events discovered by this instance.
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700115 //
116 // How to handle side-effect of remote events.
117 // - Remote Port Down event -> Link Down
118 // Not handled. (XXX Shouldn't it be removed from discovered.. Map)
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700119 // - Remote Host Added -> lose ownership of Host)
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700120 // Not handled. (XXX Shouldn't it be removed from discovered.. Map)
121 //
122 // XXX Domain knowledge based invariant maintenance should be moved to
123 // driver module, since the invariant may be different on optical, etc.
124 //
125 // What happens on leadership change?
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700126 // - Probably should: remove from discovered.. Maps, but not send DELETE
127 // events
128 // XXX Switch/Port can be rediscovered by new leader, but Link, Host?
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700129 // - Current: There is no way to recognize leadership change?
130 // ZookeeperRegistry.requestControl(long, ControlChangeCallback)
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700131 // is the only way to register listener, and it allows only one
132 // listener, which is already used by Controller class.
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700133 //
134 // FIXME Replace with concurrent variant.
135 // #removeSwitchDiscoveryEvent(SwitchEvent) runs in different thread.
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800136 //
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700137 private Map<Dpid, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 new HashMap<>();
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700139 private Map<Dpid, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 new HashMap<>();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700141 private Map<Dpid, Map<ByteBuffer, HostEvent>> discoveredAddedHostEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 new HashMap<>();
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800143
144 //
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700145 // Local state for keeping the last ADD Mastership Event entries.
146 // TODO: In the future, we might have to keep this state somewhere else.
147 //
148 private Map<ByteBuffer, MastershipEvent> lastAddMastershipEvents =
149 new HashMap<>();
150
151 //
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800152 // Local state for keeping track of the application event notifications
153 //
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700154 // - Queue of events, which will be dispatched to local listeners
155 // on next notification.
Yuta HIGUCHI703696c2014-06-25 20:36:45 -0700156
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700157 private List<MastershipEvent> apiAddedMastershipEvents =
158 new LinkedList<>();
159 private List<MastershipEvent> apiRemovedMastershipEvents =
160 new LinkedList<>();
Yuta HIGUCHI703696c2014-06-25 20:36:45 -0700161 private List<SwitchEvent> apiAddedSwitchEvents = new LinkedList<>();
162 private List<SwitchEvent> apiRemovedSwitchEvents = new LinkedList<>();
163 private List<PortEvent> apiAddedPortEvents = new LinkedList<>();
164 private List<PortEvent> apiRemovedPortEvents = new LinkedList<>();
165 private List<LinkEvent> apiAddedLinkEvents = new LinkedList<>();
166 private List<LinkEvent> apiRemovedLinkEvents = new LinkedList<>();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700167 private List<HostEvent> apiAddedHostEvents = new LinkedList<>();
168 private List<HostEvent> apiRemovedHostEvents = new LinkedList<>();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800169
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800170 /**
171 * Constructor.
172 *
Jonathan Harte37e4e22014-05-13 19:12:02 -0700173 * @param registryService the Registry Service to use.
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800174 */
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700175 public TopologyManager(IControllerRegistryService registryService) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700176 datastore = new TopologyDatastore();
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 this.registryService = registryService;
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700178 this.eventPreprocessor =
179 new TopologyEventPreprocessor(registryService);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800180 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800181
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800182 /**
Jonathan Harte37e4e22014-05-13 19:12:02 -0700183 * Get the Topology.
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800184 *
Jonathan Harte37e4e22014-05-13 19:12:02 -0700185 * @return the Topology.
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800186 */
Jonathan Harte37e4e22014-05-13 19:12:02 -0700187 Topology getTopology() {
188 return topology;
Pavlin Radoslavov6d224ee2014-02-18 16:43:15 -0800189 }
190
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800191 /**
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800192 * Event handler class.
193 */
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700194 class EventHandler extends Thread implements
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 IEventChannelListener<byte[], TopologyEvent> {
196 private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents =
197 new LinkedBlockingQueue<EventEntry<TopologyEvent>>();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800198
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 /**
200 * Startup processing.
201 */
202 private void startup() {
203 //
204 // TODO: Read all state from the database:
205 //
206 // Collection<EventEntry<TopologyEvent>> collection =
207 // readWholeTopologyFromDB();
208 //
209 // For now, as a shortcut we read it from the datagrid
210 //
Ray Milkey5df613b2014-04-15 10:50:56 -0700211 Collection<TopologyEvent> allTopologyEvents =
Ray Milkey269ffb92014-04-03 14:43:30 -0700212 eventChannel.getAllEntries();
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700213 List<EventEntry<TopologyEvent>> events =
214 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800215
Ray Milkey5df613b2014-04-15 10:50:56 -0700216 for (TopologyEvent topologyEvent : allTopologyEvents) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 EventEntry<TopologyEvent> eventEntry =
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700218 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
219 topologyEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700220 events.add(eventEntry);
Ray Milkey269ffb92014-04-03 14:43:30 -0700221 }
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700222 processEvents(events);
Ray Milkey269ffb92014-04-03 14:43:30 -0700223 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800224
Ray Milkey269ffb92014-04-03 14:43:30 -0700225 /**
226 * Run the thread.
227 */
228 @Override
229 public void run() {
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700230 List<EventEntry<TopologyEvent>> events =
231 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800232
Ray Milkey269ffb92014-04-03 14:43:30 -0700233 this.setName("TopologyManager.EventHandler " + this.getId());
234 startup();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800235
Ray Milkey269ffb92014-04-03 14:43:30 -0700236 //
237 // The main loop
238 //
Pavlin Radoslavov8e881a42014-06-24 16:58:07 -0700239 while (true) {
240 try {
241 EventEntry<TopologyEvent> eventEntry =
242 topologyEvents.take();
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700243 events.add(eventEntry);
244 topologyEvents.drainTo(events);
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800245
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700246 processEvents(events);
247 events.clear();
Pavlin Radoslavov8e881a42014-06-24 16:58:07 -0700248 } catch (Exception exception) {
249 log.debug("Exception processing Topology Events: ",
250 exception);
Ray Milkey269ffb92014-04-03 14:43:30 -0700251 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700252 }
253 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800254
Ray Milkey269ffb92014-04-03 14:43:30 -0700255 /**
256 * Process all topology events.
257 *
258 * @param events the events to process.
259 */
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700260 private void processEvents(List<EventEntry<TopologyEvent>> events) {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700261 //
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700262 // Process pending (new) listeners
263 //
264 processPendingListeners();
265
266 //
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700267 // Pre-process the events
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700268 //
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700269 events = eventPreprocessor.processEvents(events);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -0800270
Ray Milkey269ffb92014-04-03 14:43:30 -0700271 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700272 // Lock the topology while it is modified
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700274 topology.acquireWriteLock();
Pavlin Radoslavov8ffb8bf2014-02-20 15:34:26 -0800275
Ray Milkey269ffb92014-04-03 14:43:30 -0700276 try {
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700277 // Apply the events
Ray Milkey269ffb92014-04-03 14:43:30 -0700278 //
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700279 // NOTE: The events are suppose to be in the proper order
280 // to naturally build and update the topology.
Ray Milkey269ffb92014-04-03 14:43:30 -0700281 //
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700282 for (EventEntry<TopologyEvent> event : events) {
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700283 // Ignore NO-OP events
284 if (event.isNoop()) {
285 continue;
286 }
287
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700288 TopologyEvent topologyEvent = event.eventData();
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800289
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700290 // Get the event itself
291 MastershipEvent mastershipEvent =
292 topologyEvent.getMastershipEvent();
293 SwitchEvent switchEvent = topologyEvent.getSwitchEvent();
294 PortEvent portEvent = topologyEvent.getPortEvent();
295 LinkEvent linkEvent = topologyEvent.getLinkEvent();
296 HostEvent hostEvent = topologyEvent.getHostEvent();
297 boolean wasAdded = false;
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800298
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700299 //
300 // Extract the events
301 //
302 switch (event.eventType()) {
303 case ENTRY_ADD:
304 if (mastershipEvent != null) {
305 wasAdded = addMastershipEvent(mastershipEvent);
306 }
307 if (switchEvent != null) {
308 wasAdded = addSwitch(switchEvent);
309 }
310 if (portEvent != null) {
311 wasAdded = addPort(portEvent);
312 }
313 if (linkEvent != null) {
314 wasAdded = addLink(linkEvent);
315 }
316 if (hostEvent != null) {
317 wasAdded = addHost(hostEvent);
318 }
319 // If the item wasn't added, probably it was reordered
320 if (!wasAdded) {
321 ByteBuffer id = topologyEvent.getIDasByteBuffer();
322 eventPreprocessor.reorderedEvents.put(id, topologyEvent);
323 }
324 break;
325 case ENTRY_REMOVE:
326 if (mastershipEvent != null) {
327 removeMastershipEvent(mastershipEvent);
328 }
329 if (switchEvent != null) {
330 removeSwitch(switchEvent);
331 }
332 if (portEvent != null) {
333 removePort(portEvent);
334 }
335 if (linkEvent != null) {
336 removeLink(linkEvent);
337 }
338 if (hostEvent != null) {
339 removeHost(hostEvent);
340 }
341 break;
342 default:
343 log.error("Unknown topology event {}",
344 event.eventType());
345 }
346 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700347 } finally {
348 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700349 // Topology modifications completed: Release the lock
Ray Milkey269ffb92014-04-03 14:43:30 -0700350 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700351 topology.releaseWriteLock();
Ray Milkey269ffb92014-04-03 14:43:30 -0700352 }
Yuta HIGUCHI3aca81a2014-02-23 12:41:19 -0800353
Ray Milkey269ffb92014-04-03 14:43:30 -0700354 //
355 // Dispatch the Topology Notification Events to the applications
356 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700357 dispatchTopologyEvents();
Ray Milkey269ffb92014-04-03 14:43:30 -0700358 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800359
Ray Milkey269ffb92014-04-03 14:43:30 -0700360 /**
361 * Receive a notification that an entry is added.
362 *
363 * @param value the value for the entry.
364 */
365 @Override
366 public void entryAdded(TopologyEvent value) {
367 EventEntry<TopologyEvent> eventEntry =
368 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
369 value);
370 topologyEvents.add(eventEntry);
371 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800372
Ray Milkey269ffb92014-04-03 14:43:30 -0700373 /**
374 * Receive a notification that an entry is removed.
375 *
376 * @param value the value for the entry.
377 */
378 @Override
379 public void entryRemoved(TopologyEvent value) {
380 EventEntry<TopologyEvent> eventEntry =
381 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
382 value);
383 topologyEvents.add(eventEntry);
384 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800385
Ray Milkey269ffb92014-04-03 14:43:30 -0700386 /**
387 * Receive a notification that an entry is updated.
388 *
389 * @param value the value for the entry.
390 */
391 @Override
392 public void entryUpdated(TopologyEvent value) {
393 // NOTE: The ADD and UPDATE events are processed in same way
394 entryAdded(value);
395 }
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700396
397 /**
398 * Informs the event handler that a new listener has been added,
399 * and that listener expects the first event to be a snapshot of the
400 * current topology.
401 */
402 void listenerAdded() {
403 //
404 // Generate a NO-OP event so the Event Handler processing can be
405 // triggered to generate in-order a snapshot of the current
406 // topology.
407 // TODO: This is a hack.
408 //
409 EventEntry<TopologyEvent> eventEntry = EventEntry.makeNoop();
410 topologyEvents.add(eventEntry);
411 }
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800412 }
413
414 /**
415 * Startup processing.
416 *
417 * @param datagridService the datagrid service to use.
418 */
419 void startup(IDatagridService datagridService) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700420 eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME,
421 eventHandler,
422 byte[].class,
423 TopologyEvent.class);
424 eventHandler.start();
Pavlin Radoslavov721a2e02014-02-14 23:40:14 -0800425 }
426
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800427 /**
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700428 * Adds a listener for topology events.
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700429 *
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700430 * @param listener the listener to add.
431 * @param startFromSnapshot if true, and if the topology is not
432 * empty, the first event should be a snapshot of the current topology.
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700433 */
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700434 void addListener(ITopologyListener listener, boolean startFromSnapshot) {
435 if (startFromSnapshot) {
436 newTopologyListeners.addIfAbsent(listener);
437 eventHandler.listenerAdded();
438 } else {
439 topologyListeners.addIfAbsent(listener);
440 }
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700441 }
442
443 /**
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700444 * Removes a listener for topology events. The listener will no longer
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700445 * receive topology events after this call.
446 *
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700447 * @param listener the listener to remove.
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700448 */
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700449 void removeListener(ITopologyListener listener) {
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700450 topologyListeners.remove(listener);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700451 newTopologyListeners.remove(listener);
452 }
453
454 /**
455 * Processes pending (new) listeners.
456 * <p>
457 * During the processing, we dispatch Topology Snapshot Events to new
458 * listeners.
459 */
460 private void processPendingListeners() {
461 if (newTopologyListeners.isEmpty()) {
462 return;
463 }
464
465 //
466 // Create the Topology Snapshot Event
467 //
468 TopologyEvents events = null;
Pavlin Radoslavov41633642014-08-11 14:24:52 -0700469 Collection<MastershipEvent> mastershipEvents =
470 lastAddMastershipEvents.values();
471 Collection<SwitchEvent> switchEvents = topology.getAllSwitchEvents();
472 Collection<PortEvent> portEvents = topology.getAllPortEvents();
473 Collection<LinkEvent> linkEvents = topology.getAllLinkEvents();
474 Collection<HostEvent> hostEvents = topology.getAllHostEvents();
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700475 if (!(mastershipEvents.isEmpty() &&
476 switchEvents.isEmpty() &&
477 portEvents.isEmpty() &&
478 linkEvents.isEmpty() &&
479 hostEvents.isEmpty())) {
480 events = new TopologyEvents(mastershipEvents,
481 switchEvents,
482 portEvents,
483 linkEvents,
484 hostEvents);
485 }
486
487 //
488 // Dispatch Snapshot Event to each new listener, and keep track
489 // of each processed listener.
490 //
491 // NOTE: We deliver the event only if it is not empty.
492 // NOTE: We need to execute the loop so we can properly
493 // move the new listeners together with the older listeners.
494 //
495 List<ITopologyListener> processedListeners = new LinkedList<>();
496 for (ITopologyListener listener : newTopologyListeners) {
497 processedListeners.add(listener);
498 // Move the new listener together with the rest of the listeners
499 topologyListeners.addIfAbsent(listener);
500
501 // Dispatch the event
502 if (events != null) {
503 listener.topologyEvents(events);
504 }
505 }
506 newTopologyListeners.removeAll(processedListeners);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700507 }
508
509 /**
Jonathan Harte37e4e22014-05-13 19:12:02 -0700510 * Dispatch Topology Events to the listeners.
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800511 */
Jonathan Harte37e4e22014-05-13 19:12:02 -0700512 private void dispatchTopologyEvents() {
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700513 if (apiAddedMastershipEvents.isEmpty() &&
514 apiRemovedMastershipEvents.isEmpty() &&
515 apiAddedSwitchEvents.isEmpty() &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700516 apiRemovedSwitchEvents.isEmpty() &&
517 apiAddedPortEvents.isEmpty() &&
518 apiRemovedPortEvents.isEmpty() &&
519 apiAddedLinkEvents.isEmpty() &&
520 apiRemovedLinkEvents.isEmpty() &&
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700521 apiAddedHostEvents.isEmpty() &&
522 apiRemovedHostEvents.isEmpty()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700523 return; // No events to dispatch
524 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800525
Ray Milkey269ffb92014-04-03 14:43:30 -0700526 if (log.isDebugEnabled()) {
527 //
528 // Debug statements
529 // TODO: Those statements should be removed in the future
530 //
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700531 for (MastershipEvent mastershipEvent : apiAddedMastershipEvents) {
532 log.debug("Dispatch Topology Event: ADDED {}",
533 mastershipEvent);
534 }
535 for (MastershipEvent mastershipEvent : apiRemovedMastershipEvents) {
536 log.debug("Dispatch Topology Event: REMOVED {}",
537 mastershipEvent);
538 }
Ray Milkeyb29e6262014-04-09 16:02:14 -0700539 for (SwitchEvent switchEvent : apiAddedSwitchEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700540 log.debug("Dispatch Topology Event: ADDED {}", switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700541 }
542 for (SwitchEvent switchEvent : apiRemovedSwitchEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700543 log.debug("Dispatch Topology Event: REMOVED {}", switchEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700544 }
545 for (PortEvent portEvent : apiAddedPortEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700546 log.debug("Dispatch Topology Event: ADDED {}", portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700547 }
548 for (PortEvent portEvent : apiRemovedPortEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700549 log.debug("Dispatch Topology Event: REMOVED {}", portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700550 }
551 for (LinkEvent linkEvent : apiAddedLinkEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700552 log.debug("Dispatch Topology Event: ADDED {}", linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700553 }
554 for (LinkEvent linkEvent : apiRemovedLinkEvents) {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700555 log.debug("Dispatch Topology Event: REMOVED {}", linkEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700556 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700557 for (HostEvent hostEvent : apiAddedHostEvents) {
558 log.debug("Dispatch Topology Event: ADDED {}", hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700559 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700560 for (HostEvent hostEvent : apiRemovedHostEvents) {
561 log.debug("Dispatch Topology Event: REMOVED {}", hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700562 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700563 }
adminbc181552014-02-21 18:36:42 -0800564
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700565 //
566 // Update the metrics
567 //
568 long totalEvents =
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700569 apiAddedMastershipEvents.size() + apiRemovedMastershipEvents.size() +
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700570 apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() +
571 apiAddedPortEvents.size() + apiRemovedPortEvents.size() +
572 apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() +
573 apiAddedHostEvents.size() + apiRemovedHostEvents.size();
574 this.listenerEventRate.mark(totalEvents);
Pavlin Radoslavovc49917c2014-07-23 12:16:29 -0700575 this.lastEventTimestampEpochMs = System.currentTimeMillis();
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700576
577 //
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700578 // Allocate the events to deliver.
579 //
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700580 TopologyEvents events = new TopologyEvents(
Pavlin Radoslavov41633642014-08-11 14:24:52 -0700581 apiAddedMastershipEvents,
582 apiRemovedMastershipEvents,
583 apiAddedSwitchEvents,
584 apiRemovedSwitchEvents,
585 apiAddedPortEvents,
586 apiRemovedPortEvents,
587 apiAddedLinkEvents,
588 apiRemovedLinkEvents,
589 apiAddedHostEvents,
590 apiRemovedHostEvents);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700591
592 //
Ray Milkey269ffb92014-04-03 14:43:30 -0700593 // Deliver the events
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700594 //
Jonathan Harte37e4e22014-05-13 19:12:02 -0700595 for (ITopologyListener listener : this.topologyListeners) {
Pavlin Radoslavov4eaab992014-07-03 18:39:42 -0700596 listener.topologyEvents(events);
Ray Milkey269ffb92014-04-03 14:43:30 -0700597 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800598
Ray Milkey269ffb92014-04-03 14:43:30 -0700599 //
600 // Cleanup
601 //
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700602 apiAddedMastershipEvents.clear();
603 apiRemovedMastershipEvents.clear();
Ray Milkey269ffb92014-04-03 14:43:30 -0700604 apiAddedSwitchEvents.clear();
605 apiRemovedSwitchEvents.clear();
606 apiAddedPortEvents.clear();
607 apiRemovedPortEvents.clear();
608 apiAddedLinkEvents.clear();
609 apiRemovedLinkEvents.clear();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700610 apiAddedHostEvents.clear();
611 apiRemovedHostEvents.clear();
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -0800612 }
613
614 /**
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700615 * Mastership updated event.
616 *
617 * @param mastershipEvent the mastership event.
618 */
619 @Override
620 public void putSwitchMastershipEvent(MastershipEvent mastershipEvent) {
621 // Send out notification
622 TopologyEvent topologyEvent =
623 new TopologyEvent(mastershipEvent,
624 registryService.getOnosInstanceId());
625 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
626 }
627
628 /**
629 * Mastership removed event.
630 *
631 * @param mastershipEvent the mastership event.
632 */
633 @Override
634 public void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) {
635 // Send out notification
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700636 TopologyEvent topologyEvent =
637 new TopologyEvent(mastershipEvent,
638 registryService.getOnosInstanceId());
639 eventChannel.removeEntry(topologyEvent.getID());
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700640 }
641
642 /**
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800643 * Switch discovered event.
644 *
645 * @param switchEvent the switch event.
Ray Milkey269ffb92014-04-03 14:43:30 -0700646 * @param portEvents the corresponding port events for the switch.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800647 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800648 @Override
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800649 public void putSwitchDiscoveryEvent(SwitchEvent switchEvent,
Ray Milkey269ffb92014-04-03 14:43:30 -0700650 Collection<PortEvent> portEvents) {
651 if (datastore.addSwitch(switchEvent, portEvents)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700652 log.debug("Sending add switch: {}", switchEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700653 // Send out notification
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700654 TopologyEvent topologyEvent =
655 new TopologyEvent(switchEvent,
656 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700657 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800658
Ray Milkey269ffb92014-04-03 14:43:30 -0700659 // Send out notification for each port
660 for (PortEvent portEvent : portEvents) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700661 log.debug("Sending add port: {}", portEvent);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700662 topologyEvent =
663 new TopologyEvent(portEvent,
664 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700665 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
666 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800667
Ray Milkey269ffb92014-04-03 14:43:30 -0700668 //
669 // Keep track of the added ports
670 //
671 // Get the old Port Events
672 Map<ByteBuffer, PortEvent> oldPortEvents =
673 discoveredAddedPortEvents.get(switchEvent.getDpid());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700674 if (oldPortEvents == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700675 oldPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700676 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800677
Ray Milkey269ffb92014-04-03 14:43:30 -0700678 // Store the new Port Events in the local cache
679 Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>();
680 for (PortEvent portEvent : portEvents) {
681 ByteBuffer id = portEvent.getIDasByteBuffer();
682 newPortEvents.put(id, portEvent);
683 }
684 discoveredAddedPortEvents.put(switchEvent.getDpid(),
685 newPortEvents);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800686
Ray Milkey269ffb92014-04-03 14:43:30 -0700687 //
688 // Extract the removed ports
689 //
690 List<PortEvent> removedPortEvents = new LinkedList<>();
691 for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) {
692 ByteBuffer key = entry.getKey();
693 PortEvent portEvent = entry.getValue();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700694 if (!newPortEvents.containsKey(key)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700695 removedPortEvents.add(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700696 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700697 }
698
699 // Cleanup old removed ports
Ray Milkeyb29e6262014-04-09 16:02:14 -0700700 for (PortEvent portEvent : removedPortEvents) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700701 removePortDiscoveryEvent(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700702 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700703 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800704 }
705
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800706 /**
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -0700707 * {@inheritDoc}
708 * <p/>
709 * Called by {@link TopologyPublisher.SwitchCleanup} thread.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800710 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800711 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800712 public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) {
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700713 TopologyEvent topologyEvent;
714
Ray Milkey269ffb92014-04-03 14:43:30 -0700715 // Get the old Port Events
716 Map<ByteBuffer, PortEvent> oldPortEvents =
717 discoveredAddedPortEvents.get(switchEvent.getDpid());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700718 if (oldPortEvents == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700719 oldPortEvents = new HashMap<>();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700720 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800721
Ray Milkey269ffb92014-04-03 14:43:30 -0700722 if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700723 log.debug("Sending remove switch: {}", switchEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700724 // Send out notification
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700725 topologyEvent =
726 new TopologyEvent(switchEvent,
727 registryService.getOnosInstanceId());
728 eventChannel.removeEntry(topologyEvent.getID());
Pavlin Radoslavov018d5332014-02-19 23:08:35 -0800729
Ray Milkey269ffb92014-04-03 14:43:30 -0700730 //
731 // Send out notification for each port.
732 //
733 // NOTE: We don't use removePortDiscoveryEvent() for the cleanup,
734 // because it will attempt to remove the port from the database,
735 // and the deactiveSwitch() call above already removed all ports.
736 //
Ray Milkeyb29e6262014-04-09 16:02:14 -0700737 for (PortEvent portEvent : oldPortEvents.values()) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700738 log.debug("Sending remove port:", portEvent);
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700739 topologyEvent =
740 new TopologyEvent(portEvent,
741 registryService.getOnosInstanceId());
742 eventChannel.removeEntry(topologyEvent.getID());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700743 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700744 discoveredAddedPortEvents.remove(switchEvent.getDpid());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800745
Ray Milkey269ffb92014-04-03 14:43:30 -0700746 // Cleanup for each link
747 Map<ByteBuffer, LinkEvent> oldLinkEvents =
748 discoveredAddedLinkEvents.get(switchEvent.getDpid());
749 if (oldLinkEvents != null) {
750 for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) {
751 removeLinkDiscoveryEvent(linkEvent);
752 }
753 discoveredAddedLinkEvents.remove(switchEvent.getDpid());
754 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800755
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700756 // Cleanup for each host
757 Map<ByteBuffer, HostEvent> oldHostEvents =
758 discoveredAddedHostEvents.get(switchEvent.getDpid());
759 if (oldHostEvents != null) {
760 for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) {
761 removeHostDiscoveryEvent(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700762 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700763 discoveredAddedHostEvents.remove(switchEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700764 }
765 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800766 }
767
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800768 /**
769 * Port discovered event.
770 *
771 * @param portEvent the port event.
772 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800773 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800774 public void putPortDiscoveryEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700775 if (datastore.addPort(portEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700776 log.debug("Sending add port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700777 // Send out notification
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700778 TopologyEvent topologyEvent =
779 new TopologyEvent(portEvent,
780 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700781 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800782
Ray Milkey269ffb92014-04-03 14:43:30 -0700783 // Store the new Port Event in the local cache
784 Map<ByteBuffer, PortEvent> oldPortEvents =
785 discoveredAddedPortEvents.get(portEvent.getDpid());
786 if (oldPortEvents == null) {
787 oldPortEvents = new HashMap<>();
788 discoveredAddedPortEvents.put(portEvent.getDpid(),
789 oldPortEvents);
790 }
791 ByteBuffer id = portEvent.getIDasByteBuffer();
792 oldPortEvents.put(id, portEvent);
793 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800794 }
795
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800796 /**
797 * Port removed event.
798 *
799 * @param portEvent the port event.
800 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800801 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800802 public void removePortDiscoveryEvent(PortEvent portEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700803 if (datastore.deactivatePort(portEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700804 log.debug("Sending remove port: {}", portEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700805 // Send out notification
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700806 TopologyEvent topologyEvent =
807 new TopologyEvent(portEvent,
808 registryService.getOnosInstanceId());
809 eventChannel.removeEntry(topologyEvent.getID());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800810
Ray Milkey269ffb92014-04-03 14:43:30 -0700811 // Cleanup the Port Event from the local cache
812 Map<ByteBuffer, PortEvent> oldPortEvents =
813 discoveredAddedPortEvents.get(portEvent.getDpid());
814 if (oldPortEvents != null) {
815 ByteBuffer id = portEvent.getIDasByteBuffer();
816 oldPortEvents.remove(id);
817 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800818
Ray Milkey269ffb92014-04-03 14:43:30 -0700819 // Cleanup for the incoming link
820 Map<ByteBuffer, LinkEvent> oldLinkEvents =
821 discoveredAddedLinkEvents.get(portEvent.getDpid());
822 if (oldLinkEvents != null) {
823 for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700824 if (linkEvent.getDst().equals(portEvent.getSwitchPort())) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700825 removeLinkDiscoveryEvent(linkEvent);
826 // XXX If we change our model to allow multiple Link on
827 // a Port, this loop must be fixed to allow continuing.
828 break;
829 }
830 }
831 }
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800832
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700833 // Cleanup for the connected hosts
Ray Milkey269ffb92014-04-03 14:43:30 -0700834 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700835 List<HostEvent> removedHostEvents = new LinkedList<>();
836 Map<ByteBuffer, HostEvent> oldHostEvents =
837 discoveredAddedHostEvents.get(portEvent.getDpid());
838 if (oldHostEvents != null) {
839 for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) {
840 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700841 if (swp.equals(portEvent.getSwitchPort())) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700842 removedHostEvents.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700843 }
844 }
845 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700846 for (HostEvent hostEvent : removedHostEvents) {
847 removeHostDiscoveryEvent(hostEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700848 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700849 }
850 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800851 }
852
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800853 /**
854 * Link discovered event.
855 *
856 * @param linkEvent the link event.
857 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800858 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800859 public void putLinkDiscoveryEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700860 if (datastore.addLink(linkEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700861 log.debug("Sending add link: {}", linkEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700862 // Send out notification
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700863 TopologyEvent topologyEvent =
864 new TopologyEvent(linkEvent,
865 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700866 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800867
Ray Milkey269ffb92014-04-03 14:43:30 -0700868 // Store the new Link Event in the local cache
869 Map<ByteBuffer, LinkEvent> oldLinkEvents =
870 discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
871 if (oldLinkEvents == null) {
872 oldLinkEvents = new HashMap<>();
873 discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(),
874 oldLinkEvents);
875 }
876 ByteBuffer id = linkEvent.getIDasByteBuffer();
877 oldLinkEvents.put(id, linkEvent);
878 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800879 }
880
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800881 /**
882 * Link removed event.
883 *
884 * @param linkEvent the link event.
885 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800886 @Override
Pavlin Radoslavov6ea84a42014-02-19 15:50:01 -0800887 public void removeLinkDiscoveryEvent(LinkEvent linkEvent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700888 if (datastore.removeLink(linkEvent)) {
Jonathan Hart92c819f2014-05-30 10:53:30 -0700889 log.debug("Sending remove link: {}", linkEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700890 // Send out notification
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700891 TopologyEvent topologyEvent =
892 new TopologyEvent(linkEvent,
893 registryService.getOnosInstanceId());
894 eventChannel.removeEntry(topologyEvent.getID());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800895
Ray Milkey269ffb92014-04-03 14:43:30 -0700896 // Cleanup the Link Event from the local cache
897 Map<ByteBuffer, LinkEvent> oldLinkEvents =
898 discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
899 if (oldLinkEvents != null) {
900 ByteBuffer id = linkEvent.getIDasByteBuffer();
901 oldLinkEvents.remove(id);
902 }
903 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800904 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800905
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800906 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700907 * Host discovered event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800908 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700909 * @param hostEvent the host event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800910 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800911 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700912 public void putHostDiscoveryEvent(HostEvent hostEvent) {
913 if (datastore.addHost(hostEvent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700914 // Send out notification
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700915 TopologyEvent topologyEvent =
916 new TopologyEvent(hostEvent,
917 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700918 eventChannel.addEntry(topologyEvent.getID(), topologyEvent);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700919 log.debug("Put the host info into the cache of the topology. mac {}",
920 hostEvent.getMac());
Ray Milkey269ffb92014-04-03 14:43:30 -0700921
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700922 // Store the new Host Event in the local cache
Ray Milkey269ffb92014-04-03 14:43:30 -0700923 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700924 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
925 Map<ByteBuffer, HostEvent> oldHostEvents =
926 discoveredAddedHostEvents.get(swp.getDpid());
927 if (oldHostEvents == null) {
928 oldHostEvents = new HashMap<>();
929 discoveredAddedHostEvents.put(swp.getDpid(),
930 oldHostEvents);
Ray Milkey269ffb92014-04-03 14:43:30 -0700931 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700932 ByteBuffer id = hostEvent.getIDasByteBuffer();
933 oldHostEvents.put(id, hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -0700934 }
935 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800936 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800937
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800938 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700939 * Host removed event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800940 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700941 * @param hostEvent the host event.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800942 */
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800943 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700944 public void removeHostDiscoveryEvent(HostEvent hostEvent) {
945 if (datastore.removeHost(hostEvent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700946 // Send out notification
Pavlin Radoslavovbb17de22014-08-06 15:34:37 -0700947 TopologyEvent topologyEvent =
948 new TopologyEvent(hostEvent,
949 registryService.getOnosInstanceId());
950 eventChannel.removeEntry(topologyEvent.getID());
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700951 log.debug("Remove the host info into the cache of the topology. mac {}",
952 hostEvent.getMac());
Pavlin Radoslavov26d83402014-02-20 15:24:30 -0800953
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700954 // Cleanup the Host Event from the local cache
Ray Milkey269ffb92014-04-03 14:43:30 -0700955 // TODO: The implementation below is probably wrong
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700956 ByteBuffer id = ByteBuffer.wrap(hostEvent.getID());
957 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
958 Map<ByteBuffer, HostEvent> oldHostEvents =
959 discoveredAddedHostEvents.get(swp.getDpid());
960 if (oldHostEvents != null) {
961 oldHostEvents.remove(id);
Ray Milkey269ffb92014-04-03 14:43:30 -0700962 }
963 }
964 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -0800965 }
Jonathan Hart22eb9882014-02-11 15:52:59 -0800966
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700967 //
968 // Methods to update topology replica
969 //
970
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -0800971 /**
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700972 * Adds Switch Mastership event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700973 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700974 * @param mastershipEvent the MastershipEvent to process.
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700975 * @return true if the item was successfully added, otherwise false.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700976 */
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700977 @GuardedBy("topology.writeLock")
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700978 private boolean addMastershipEvent(MastershipEvent mastershipEvent) {
979 log.debug("Added Mastership event {}", mastershipEvent);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700980 lastAddMastershipEvents.put(mastershipEvent.getIDasByteBuffer(),
981 mastershipEvent);
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700982 apiAddedMastershipEvents.add(mastershipEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700983 return true;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700984 }
985
986 /**
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700987 * Removes Switch Mastership event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700988 *
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700989 * @param mastershipEvent the MastershipEvent to process.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700990 */
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700991 @GuardedBy("topology.writeLock")
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -0700992 private void removeMastershipEvent(MastershipEvent mastershipEvent) {
993 log.debug("Removed Mastership event {}", mastershipEvent);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700994 lastAddMastershipEvents.remove(mastershipEvent.getIDasByteBuffer());
Pavlin Radoslavovcac157d2014-07-31 13:54:08 -0700995 apiRemovedMastershipEvents.add(mastershipEvent);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700996 }
997
998 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -0700999 * Adds a switch to the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001000 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001001 * @param switchEvent the SwitchEvent with the switch to add.
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001002 * @return true if the item was successfully added, otherwise false.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001003 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001004 @GuardedBy("topology.writeLock")
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001005 private boolean addSwitch(SwitchEvent switchEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001006 if (log.isDebugEnabled()) {
1007 SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid());
1008 if (sw != null) {
1009 log.debug("Update {}", switchEvent);
1010 } else {
1011 log.debug("Added {}", switchEvent);
1012 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001013 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001014 topology.putSwitch(switchEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -07001015 apiAddedSwitchEvents.add(switchEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001016 return true;
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001017 }
1018
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001019 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001020 * Removes a switch from the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001021 * <p/>
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001022 * It will call {@link #removePort(PortEvent)} for each ports on this
1023 * switch.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001024 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001025 * @param switchEvent the SwitchEvent with the switch to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001026 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001027 @GuardedBy("topology.writeLock")
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001028 private void removeSwitch(SwitchEvent switchEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001029 final Dpid dpid = switchEvent.getDpid();
1030
1031 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
1032 if (swInTopo == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001033 log.warn("Switch {} already removed, ignoring", switchEvent);
1034 return;
1035 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001036
Ray Milkey269ffb92014-04-03 14:43:30 -07001037 //
1038 // Remove all Ports on the Switch
1039 //
1040 ArrayList<PortEvent> portsToRemove = new ArrayList<>();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001041 for (Port port : topology.getPorts(dpid)) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001042 log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.",
1043 port, switchEvent);
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -07001044 PortEvent portEvent = new PortEvent(port.getSwitchPort());
Ray Milkey269ffb92014-04-03 14:43:30 -07001045 portsToRemove.add(portEvent);
1046 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001047 for (PortEvent portEvent : portsToRemove) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001048 removePort(portEvent);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001049 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001050
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001051 log.debug("Removed {}", swInTopo);
1052 topology.removeSwitch(dpid);
1053 apiRemovedSwitchEvents.add(swInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001054 }
1055
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001056 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001057 * Adds a port to the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001058 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001059 * @param portEvent the PortEvent with the port to add.
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001060 * @return true if the item was successfully added, otherwise false.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001061 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001062 @GuardedBy("topology.writeLock")
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001063 private boolean addPort(PortEvent portEvent) {
Jonathan Harte37e4e22014-05-13 19:12:02 -07001064 Switch sw = topology.getSwitch(portEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -07001065 if (sw == null) {
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001066 // Reordered event
Jonathan Hartf1675202014-05-23 14:59:07 -07001067 log.debug("{} reordered because switch is null", portEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001068 return false;
Ray Milkey269ffb92014-04-03 14:43:30 -07001069 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001070
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001071 if (log.isDebugEnabled()) {
1072 PortEvent port = topology.getPortEvent(portEvent.getSwitchPort());
1073 if (port != null) {
1074 log.debug("Update {}", portEvent);
1075 } else {
1076 log.debug("Added {}", portEvent);
1077 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001078 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001079 topology.putPort(portEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -07001080 apiAddedPortEvents.add(portEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001081 return true;
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001082 }
1083
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001084 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001085 * Removes a port from the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001086 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001087 * It will remove attachment points from each hosts on this port
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001088 * and call {@link #removeLink(LinkEvent)} for each links on this port.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001089 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001090 * @param portEvent the PortEvent with the port to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001091 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001092 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001093 private void removePort(PortEvent portEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001094 SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -07001095 if (sw == null) {
1096 log.warn("Parent Switch for Port {} already removed, ignoring",
1097 portEvent);
1098 return;
1099 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001100
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001101 final SwitchPort switchPort = portEvent.getSwitchPort();
1102 PortEvent portInTopo = topology.getPortEvent(switchPort);
1103 if (portInTopo == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001104 log.warn("Port {} already removed, ignoring", portEvent);
1105 return;
1106 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001107
Ray Milkey269ffb92014-04-03 14:43:30 -07001108 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001109 // Remove all Host attachment points bound to this Port
Ray Milkey269ffb92014-04-03 14:43:30 -07001110 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001111 List<HostEvent> hostsToUpdate = new ArrayList<>();
1112 for (Host host : topology.getHosts(switchPort)) {
1113 log.debug("Removing Host {} on Port {}", host, portInTopo);
1114 HostEvent hostEvent = topology.getHostEvent(host.getMacAddress());
1115 hostsToUpdate.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001116 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001117 for (HostEvent hostEvent : hostsToUpdate) {
1118 HostEvent newHostEvent = new HostEvent(hostEvent);
1119 newHostEvent.removeAttachmentPoint(switchPort);
1120 newHostEvent.freeze();
1121
1122 // TODO should this event be fired inside #addHost?
1123 if (newHostEvent.getAttachmentPoints().isEmpty()) {
1124 // No more attachment point left -> remove Host
1125 removeHost(hostEvent);
1126 } else {
1127 // Update Host
1128 addHost(newHostEvent);
1129 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001130 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001131
Ray Milkey269ffb92014-04-03 14:43:30 -07001132 //
1133 // Remove all Links connected to the Port
1134 //
1135 Set<Link> links = new HashSet<>();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001136 links.addAll(topology.getOutgoingLinks(switchPort));
1137 links.addAll(topology.getIncomingLinks(switchPort));
Ray Milkey269ffb92014-04-03 14:43:30 -07001138 for (Link link : links) {
Ray Milkeyb29e6262014-04-09 16:02:14 -07001139 if (link == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001140 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001141 }
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001142 LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple());
1143 if (linkEvent != null) {
1144 log.debug("Removing Link {} on Port {}", link, portInTopo);
1145 removeLink(linkEvent);
1146 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001147 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001148
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001149 // Remove the Port from Topology
1150 log.debug("Removed {}", portInTopo);
1151 topology.removePort(switchPort);
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001152
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001153 apiRemovedPortEvents.add(portInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001154 }
1155
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001156 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001157 * Adds a link to the topology replica.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001158 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001159 * It will remove attachment points from each hosts using the same ports.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001160 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001161 * @param linkEvent the LinkEvent with the link to add.
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001162 * @return true if the item was successfully added, otherwise false.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001163 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001164 @GuardedBy("topology.writeLock")
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001165 private boolean addLink(LinkEvent linkEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001166 PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc());
1167 PortEvent dstPort = topology.getPortEvent(linkEvent.getDst());
Ray Milkey269ffb92014-04-03 14:43:30 -07001168 if ((srcPort == null) || (dstPort == null)) {
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001169 // Reordered event
Jonathan Hartf1675202014-05-23 14:59:07 -07001170 log.debug("{} reordered because {} port is null", linkEvent,
1171 (srcPort == null) ? "src" : "dst");
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001172 return false;
Ray Milkey269ffb92014-04-03 14:43:30 -07001173 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001174
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001175 //
1176 // XXX domain knowledge: Sanity check: Port cannot have both Link and
1177 // Host.
1178 //
1179 // FIXME: Potentially local replica may not be up-to-date yet due to
1180 // Hazelcast delay.
1181 // FIXME: May need to manage local truth and use them instead.
1182 //
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001183 if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) {
1184 // Only check for existing Host when adding new Link.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001185 // Remove all Hosts attached to the ports on both ends
1186
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001187 Set<HostEvent> hostsToUpdate =
1188 new TreeSet<>(new Comparator<HostEvent>() {
1189 // Comparison only using ID(=MAC)
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001190 @Override
1191 public int compare(HostEvent o1, HostEvent o2) {
1192 return Long.compare(o1.getMac().toLong(), o2.getMac().toLong());
1193 }
1194 });
1195
1196 List<SwitchPort> portsToCheck = Arrays.asList(
1197 srcPort.getSwitchPort(),
1198 dstPort.getSwitchPort());
1199
1200 // Enumerate Host which needs to be updated by this Link add event
1201 for (SwitchPort port : portsToCheck) {
1202 for (Host host : topology.getHosts(port)) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001203 log.error("Host {} on Port {} should have been removed prior to adding Link {}",
1204 host, port, linkEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001205
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -07001206 HostEvent hostEvent =
1207 topology.getHostEvent(host.getMacAddress());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001208 hostsToUpdate.add(hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001209 }
1210 }
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001211 // Remove attachment point from them
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001212 for (HostEvent hostEvent : hostsToUpdate) {
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001213 // Remove port from attachment point and update
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001214 HostEvent newHostEvent = new HostEvent(hostEvent);
1215 newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort());
1216 newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort());
1217 newHostEvent.freeze();
1218
1219 // TODO should this event be fired inside #addHost?
1220 if (newHostEvent.getAttachmentPoints().isEmpty()) {
1221 // No more attachment point left -> remove Host
1222 removeHost(hostEvent);
1223 } else {
1224 // Update Host
1225 addHost(newHostEvent);
1226 }
Ray Milkeyb29e6262014-04-09 16:02:14 -07001227 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001228 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001229
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001230 if (log.isDebugEnabled()) {
1231 LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple());
1232 if (link != null) {
1233 log.debug("Update {}", linkEvent);
1234 } else {
1235 log.debug("Added {}", linkEvent);
1236 }
1237 }
1238 topology.putLink(linkEvent.freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -07001239 apiAddedLinkEvents.add(linkEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001240 return true;
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001241 }
1242
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001243 /**
Yuta HIGUCHI7926ba32014-07-09 11:39:32 -07001244 * Removes a link from the topology replica.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001245 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001246 * @param linkEvent the LinkEvent with the link to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001247 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001248 @GuardedBy("topology.writeLock")
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001249 private void removeLink(LinkEvent linkEvent) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07001250 Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001251 linkEvent.getSrc().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001252 if (srcPort == null) {
1253 log.warn("Src Port for Link {} already removed, ignoring",
1254 linkEvent);
1255 return;
1256 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001257
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07001258 Port dstPort = topology.getPort(linkEvent.getDst().getDpid(),
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001259 linkEvent.getDst().getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001260 if (dstPort == null) {
1261 log.warn("Dst Port for Link {} already removed, ignoring",
1262 linkEvent);
1263 return;
1264 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001265
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001266 LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(),
1267 linkEvent.getType());
1268 if (linkInTopo == null) {
1269 log.warn("Link {} already removed, ignoring", linkEvent);
1270 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001271 }
Jonathan Hart25bd53e2014-04-30 23:44:09 -07001272
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001273 if (log.isDebugEnabled()) {
1274 // only do sanity check on debug level
1275
1276 Link linkIn = dstPort.getIncomingLink(linkEvent.getType());
1277 if (linkIn == null) {
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -07001278 log.warn("Link {} already removed on destination Port",
1279 linkEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001280 }
1281 Link linkOut = srcPort.getOutgoingLink(linkEvent.getType());
1282 if (linkOut == null) {
1283 log.warn("Link {} already removed on src Port", linkEvent);
1284 }
Jonathan Hart25bd53e2014-04-30 23:44:09 -07001285 }
Pavlin Radoslavov74986ce2014-02-20 13:17:20 -08001286
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001287 log.debug("Removed {}", linkInTopo);
1288 topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType());
1289 apiRemovedLinkEvents.add(linkInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001290 }
1291
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001292 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001293 * Adds a host to the topology replica.
Ray Milkey269ffb92014-04-03 14:43:30 -07001294 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001295 * TODO: Host-related work is incomplete.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001296 * TODO: Eventually, we might need to consider reordering
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001297 * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events
1298 * on the same port.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001299 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001300 * @param hostEvent the HostEvent with the host to add.
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001301 * @return true if the item was successfully added, otherwise false.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001302 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001303 @GuardedBy("topology.writeLock")
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001304 private boolean addHost(HostEvent hostEvent) {
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001305
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001306 // TODO Decide how to handle update scenario.
1307 // If the new HostEvent has less attachment point compared to
1308 // existing HostEvent, what should the event be?
1309 // - AddHostEvent with some attachment point removed? (current behavior)
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001310
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001311 // create unfrozen copy
1312 // for removing attachment points which already has a link
1313 HostEvent modifiedHostEvent = new HostEvent(hostEvent);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001314
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001315 // Verify each attachment point
Ray Milkey269ffb92014-04-03 14:43:30 -07001316 boolean attachmentFound = false;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001317 for (SwitchPort swp : hostEvent.getAttachmentPoints()) {
1318 // XXX domain knowledge: Port must exist before Host
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07001319 // but this knowledge cannot be pushed down to driver.
1320
Ray Milkey269ffb92014-04-03 14:43:30 -07001321 // Attached Ports must exist
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -07001322 Port port = topology.getPort(swp.getDpid(), swp.getPortNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -07001323 if (port == null) {
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -07001324 log.debug("{} reordered because port {} was not there",
1325 hostEvent, swp);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001326 // Reordered event
1327 return false; // should not continue if re-applying later
Ray Milkey269ffb92014-04-03 14:43:30 -07001328 }
1329 // Attached Ports must not have Link
1330 if (port.getOutgoingLink() != null ||
1331 port.getIncomingLink() != null) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001332 log.warn("Link (Out:{},In:{}) exist on the attachment point. "
1333 + "Ignoring this attachmentpoint ({}) from {}.",
1334 port.getOutgoingLink(), port.getIncomingLink(),
1335 swp, modifiedHostEvent);
1336 // FIXME Should either reject, reorder this HostEvent,
1337 // or remove attachment point from given HostEvent
1338 // Removing attachment point from given HostEvent for now.
1339 modifiedHostEvent.removeAttachmentPoint(swp);
Ray Milkey269ffb92014-04-03 14:43:30 -07001340 continue;
1341 }
1342
Ray Milkey269ffb92014-04-03 14:43:30 -07001343 attachmentFound = true;
1344 }
1345
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001346 // Update the host in the topology
Ray Milkey269ffb92014-04-03 14:43:30 -07001347 if (attachmentFound) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001348 if (modifiedHostEvent.getAttachmentPoints().isEmpty()) {
1349 log.warn("No valid attachment point left. Ignoring."
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -07001350 + "original: {}, modified: {}",
1351 hostEvent, modifiedHostEvent);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001352 // TODO Should we call #removeHost to trigger remove event?
1353 // only if this call is update.
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001354 return false;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001355 }
1356
1357 if (log.isDebugEnabled()) {
1358 HostEvent host = topology.getHostEvent(hostEvent.getMac());
1359 if (host != null) {
1360 log.debug("Update {}", modifiedHostEvent);
1361 } else {
1362 log.debug("Added {}", modifiedHostEvent);
1363 }
1364 }
1365 topology.putHost(modifiedHostEvent.freeze());
1366 apiAddedHostEvents.add(modifiedHostEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001367 return true;
Ray Milkey269ffb92014-04-03 14:43:30 -07001368 }
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001369 return false;
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001370 }
1371
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001372 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001373 * Removes a host from the topology replica.
Ray Milkey269ffb92014-04-03 14:43:30 -07001374 * <p/>
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001375 * TODO: Host-related work is incomplete.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001376 *
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001377 * @param hostEvent the Host Event with the host to remove.
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001378 */
Yuta HIGUCHIbc67a052014-06-30 10:37:09 -07001379 @GuardedBy("topology.writeLock")
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001380 private void removeHost(HostEvent hostEvent) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001381
1382 final MACAddress mac = hostEvent.getMac();
1383 HostEvent hostInTopo = topology.getHostEvent(mac);
1384 if (hostInTopo == null) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07001385 log.warn("Host {} already removed, ignoring", hostEvent);
Ray Milkey269ffb92014-04-03 14:43:30 -07001386 return;
1387 }
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001388
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001389 log.debug("Removed {}", hostInTopo);
1390 topology.removeHost(mac);
1391 apiRemovedHostEvents.add(hostInTopo);
Pavlin Radoslavov3c9cc552014-02-20 09:58:38 -08001392 }
Jonathan Hart22eb9882014-02-11 15:52:59 -08001393
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001394 /**
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001395 * Read the whole topology from the database.
1396 *
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001397 * @return a list of EventEntry-encapsulated Topology Events for
Pavlin Radoslavov734ff5a2014-02-26 10:20:43 -08001398 * the whole topology.
1399 */
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001400 private List<EventEntry<TopologyEvent>> readWholeTopologyFromDB() {
1401 List<EventEntry<TopologyEvent>> events =
Ray Milkey269ffb92014-04-03 14:43:30 -07001402 new LinkedList<EventEntry<TopologyEvent>>();
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001403
Ray Milkey269ffb92014-04-03 14:43:30 -07001404 // XXX May need to clear whole topology first, depending on
1405 // how we initially subscribe to replication events
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001406
Ray Milkey269ffb92014-04-03 14:43:30 -07001407 // Add all active switches
1408 for (KVSwitch sw : KVSwitch.getAllSwitches()) {
1409 if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) {
1410 continue;
1411 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001412
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07001413 //
1414 // TODO: Using the local ONOS Instance ID below is incorrect.
1415 // Currently, this code is not used, and it might go away in the
1416 // future.
1417 //
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001418 SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid()));
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07001419 TopologyEvent topologyEvent =
1420 new TopologyEvent(switchEvent,
1421 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -07001422 EventEntry<TopologyEvent> eventEntry =
1423 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1424 topologyEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001425 events.add(eventEntry);
Ray Milkey269ffb92014-04-03 14:43:30 -07001426 }
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001427
Ray Milkey269ffb92014-04-03 14:43:30 -07001428 // Add all active ports
1429 for (KVPort p : KVPort.getAllPorts()) {
1430 if (p.getStatus() != KVPort.STATUS.ACTIVE) {
1431 continue;
1432 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001433
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07001434 //
1435 // TODO: Using the local ONOS Instance ID below is incorrect.
1436 // Currently, this code is not used, and it might go away in the
1437 // future.
1438 //
1439 PortEvent portEvent =
1440 new PortEvent(new Dpid(p.getDpid()),
1441 new PortNumber(p.getNumber().shortValue()));
1442 TopologyEvent topologyEvent =
1443 new TopologyEvent(portEvent,
1444 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -07001445 EventEntry<TopologyEvent> eventEntry =
1446 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1447 topologyEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001448 events.add(eventEntry);
Ray Milkey269ffb92014-04-03 14:43:30 -07001449 }
Yuta HIGUCHIa536e762014-02-17 21:47:28 -08001450
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07001451 for (KVDevice d : KVDevice.getAllDevices()) {
1452 //
1453 // TODO: Using the local ONOS Instance ID below is incorrect.
1454 // Currently, this code is not used, and it might go away in the
1455 // future.
1456 //
1457 HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac()));
1458 for (byte[] portId : d.getAllPortIds()) {
1459 devEvent.addAttachmentPoint(
1460 new SwitchPort(KVPort.getDpidFromKey(portId),
1461 KVPort.getNumberFromKey(portId)));
1462 }
1463 }
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001464
Ray Milkey269ffb92014-04-03 14:43:30 -07001465 for (KVLink l : KVLink.getAllLinks()) {
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07001466 //
1467 // TODO: Using the local ONOS Instance ID below is incorrect.
1468 // Currently, this code is not used, and it might go away in the
1469 // future.
1470 //
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07001471 LinkEvent linkEvent = new LinkEvent(
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07001472 new SwitchPort(l.getSrc().dpid, l.getSrc().number),
1473 new SwitchPort(l.getDst().dpid, l.getDst().number));
1474 TopologyEvent topologyEvent =
1475 new TopologyEvent(linkEvent,
1476 registryService.getOnosInstanceId());
Ray Milkey269ffb92014-04-03 14:43:30 -07001477 EventEntry<TopologyEvent> eventEntry =
1478 new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1479 topologyEvent);
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001480 events.add(eventEntry);
Ray Milkey269ffb92014-04-03 14:43:30 -07001481 }
Pavlin Radoslavov018d5332014-02-19 23:08:35 -08001482
Pavlin Radoslavovd7b792e2014-08-01 02:47:47 -07001483 return events;
Pavlin Radoslavovc1cfde52014-02-19 11:35:29 -08001484 }
Jonathan Hart062a2e82014-02-03 09:41:57 -08001485}