Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 5 | import java.util.Arrays; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 6 | import java.util.Collection; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 7 | import java.util.Comparator; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 9 | import java.util.HashSet; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 10 | import java.util.LinkedList; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 11 | import java.util.List; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 12 | import java.util.Map; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 13 | import java.util.Set; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 14 | import java.util.TreeSet; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 15 | import java.util.concurrent.BlockingQueue; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 16 | import java.util.concurrent.CopyOnWriteArrayList; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 17 | import java.util.concurrent.LinkedBlockingQueue; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 18 | |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 19 | import javax.annotation.concurrent.GuardedBy; |
| 20 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 23 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 24 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.datastore.topology.KVDevice; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.datastore.topology.KVLink; |
| 27 | import net.onrc.onos.core.datastore.topology.KVPort; |
| 28 | import net.onrc.onos.core.datastore.topology.KVSwitch; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 30 | import net.onrc.onos.core.metrics.OnosMetrics.MetricsComponent; |
| 31 | import net.onrc.onos.core.metrics.OnosMetrics.MetricsFeature; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.util.Dpid; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.util.EventEntry; |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.util.SwitchPort; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.util.serializers.KryoFactory; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 38 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 42 | import com.codahale.metrics.Gauge; |
| 43 | import com.codahale.metrics.Meter; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 44 | import com.esotericsoftware.kryo.Kryo; |
| 45 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 46 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 47 | * 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 51 | * - Maintain Invariant/Relationships between Topology Objects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | * <p/> |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 53 | * TODO To be synchronized based on TopologyEvent Notification. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | * <p/> |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 55 | * TODO TBD: Caller is expected to maintain parent/child calling order. Parent |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame] | 56 | * Object must exist before adding sub component(Add Switch -> Port). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 58 | * 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 HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 60 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 61 | public class TopologyManager implements TopologyDiscoveryInterface { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 62 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 63 | private static final Logger log = LoggerFactory |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | .getLogger(TopologyManager.class); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 65 | |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 66 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
Jonathan Hart | 10a7e2b | 2014-02-21 18:30:08 -0800 | [diff] [blame] | 67 | public static final String EVENT_CHANNEL_NAME = "onos.topology"; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 68 | private EventHandler eventHandler = new EventHandler(); |
| 69 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 70 | private TopologyDatastore datastore; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 71 | private final TopologyImpl topology = new TopologyImpl(); |
Yuta HIGUCHI | 170229f | 2014-02-17 15:47:54 -0800 | [diff] [blame] | 72 | private final IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 73 | private CopyOnWriteArrayList<ITopologyListener> topologyListeners; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 74 | private Kryo kryo = KryoFactory.newKryoObject(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 75 | |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 76 | // |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 77 | // 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 Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 84 | // Timestamp of the last Topology event (ms from the Epoch) |
| 85 | private volatile long lastEventTimestampEpochMs = 0; |
| 86 | private final Gauge<Long> gaugeLastEventTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 87 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 88 | METRICS_FEATURE_EVENT_NOTIFICATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 89 | "LastEventTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 90 | new Gauge<Long>() { |
| 91 | @Override |
| 92 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 93 | return lastEventTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 94 | } |
| 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 Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 103 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | new HashMap<ByteBuffer, PortEvent>(); |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 108 | private Map<ByteBuffer, LinkEvent> reorderedAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | new HashMap<ByteBuffer, LinkEvent>(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 110 | private Map<ByteBuffer, HostEvent> reorderedAddedHostEvents = |
| 111 | new HashMap<ByteBuffer, HostEvent>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 112 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 113 | // |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 114 | // Local state for keeping track of locally discovered events so we can |
| 115 | // cleanup properly when a Switch or Port is removed. |
| 116 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 117 | // We keep all Port, (incoming) Link and Host events per Switch DPID: |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 118 | // - If a switch goes down, we remove all corresponding Port, Link and |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 119 | // Host events. |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 120 | // - If a port on a switch goes down, we remove all corresponding Link |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 121 | // and Host events discovered by this instance. |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 122 | // |
| 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 126 | // - Remote Host Added -> lose ownership of Host) |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 127 | // 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 134 | // XXX Switch/Port can be rediscovered by new leader, but Link, Host? |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 135 | // - 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 Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 142 | // |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 143 | private Map<Dpid, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | new HashMap<>(); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 145 | private Map<Dpid, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | new HashMap<>(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 147 | private Map<Dpid, Map<ByteBuffer, HostEvent>> discoveredAddedHostEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 149 | |
| 150 | // |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 151 | // Local state for keeping track of the application event notifications |
| 152 | // |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 153 | // - Queue of events, which will be dispatched to local listeners |
| 154 | // on next notification. |
Yuta HIGUCHI | 703696c | 2014-06-25 20:36:45 -0700 | [diff] [blame] | 155 | |
| 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 162 | private List<HostEvent> apiAddedHostEvents = new LinkedList<>(); |
| 163 | private List<HostEvent> apiRemovedHostEvents = new LinkedList<>(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 164 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Constructor. |
| 167 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 168 | * @param registryService the Registry Service to use. |
| 169 | * @param topologyListeners the collection of topology listeners to use. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 170 | */ |
| 171 | public TopologyManager(IControllerRegistryService registryService, |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 172 | CopyOnWriteArrayList<ITopologyListener> topologyListeners) { |
| 173 | datastore = new TopologyDatastore(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | this.registryService = registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 175 | this.topologyListeners = topologyListeners; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 176 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 177 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 178 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 179 | * Get the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 180 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 181 | * @return the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 182 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 183 | Topology getTopology() { |
| 184 | return topology; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 187 | /** |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 188 | * Event handler class. |
| 189 | */ |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 190 | class EventHandler extends Thread implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | IEventChannelListener<byte[], TopologyEvent> { |
| 192 | private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents = |
| 193 | new LinkedBlockingQueue<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 194 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | /** |
| 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 Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 207 | Collection<TopologyEvent> allTopologyEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 208 | eventChannel.getAllEntries(); |
| 209 | Collection<EventEntry<TopologyEvent>> collection = |
| 210 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 211 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 212 | for (TopologyEvent topologyEvent : allTopologyEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 213 | EventEntry<TopologyEvent> eventEntry = |
| 214 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 215 | topologyEvent); |
| 216 | collection.add(eventEntry); |
| 217 | } |
| 218 | processEvents(collection); |
| 219 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 220 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Run the thread. |
| 223 | */ |
| 224 | @Override |
| 225 | public void run() { |
| 226 | Collection<EventEntry<TopologyEvent>> collection = |
| 227 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 228 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 229 | this.setName("TopologyManager.EventHandler " + this.getId()); |
| 230 | startup(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 231 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 232 | // |
| 233 | // The main loop |
| 234 | // |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 235 | while (true) { |
| 236 | try { |
| 237 | EventEntry<TopologyEvent> eventEntry = |
| 238 | topologyEvents.take(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 239 | collection.add(eventEntry); |
| 240 | topologyEvents.drainTo(collection); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 241 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | processEvents(collection); |
| 243 | collection.clear(); |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 244 | } catch (Exception exception) { |
| 245 | log.debug("Exception processing Topology Events: ", |
| 246 | exception); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 247 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 250 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 251 | /** |
| 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 264 | Map<ByteBuffer, HostEvent> addedHostEvents = new HashMap<>(); |
| 265 | Map<ByteBuffer, HostEvent> removedHostEvents = new HashMap<>(); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 266 | Map<ByteBuffer, MastershipEvent> addedMastershipEvents = |
| 267 | new HashMap<>(); |
| 268 | Map<ByteBuffer, MastershipEvent> removedMastershipEvents = |
| 269 | new HashMap<>(); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 270 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 271 | // |
| 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 279 | HostEvent hostEvent = topologyEvent.hostEvent; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 280 | MastershipEvent mastershipEvent = topologyEvent.mastershipEvent; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 281 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 282 | // |
| 283 | // Extract the events |
| 284 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 285 | // FIXME Following event squashing logic based only on ID |
| 286 | // potentially lose attribute change. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 287 | 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 308 | if (hostEvent != null) { |
| 309 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 310 | addedHostEvents.put(id, hostEvent); |
| 311 | removedHostEvents.remove(id); |
| 312 | reorderedAddedHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 313 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 314 | if (mastershipEvent != null) { |
| 315 | ByteBuffer id = mastershipEvent.getIDasByteBuffer(); |
| 316 | addedMastershipEvents.put(id, mastershipEvent); |
| 317 | removedMastershipEvents.remove(id); |
| 318 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 319 | 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 340 | if (hostEvent != null) { |
| 341 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 342 | addedHostEvents.remove(id); |
| 343 | removedHostEvents.put(id, hostEvent); |
| 344 | reorderedAddedHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 345 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 346 | if (mastershipEvent != null) { |
| 347 | ByteBuffer id = mastershipEvent.getIDasByteBuffer(); |
| 348 | addedMastershipEvents.remove(id); |
| 349 | removedMastershipEvents.put(id, mastershipEvent); |
| 350 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 351 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 352 | default: |
| 353 | log.error("Unknown topology event {}", |
| 354 | event.eventType()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 357 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 358 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 359 | // Lock the topology while it is modified |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 360 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 361 | topology.acquireWriteLock(); |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 362 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 363 | try { |
| 364 | // |
| 365 | // Apply the classified events. |
| 366 | // |
| 367 | // Apply the "add" events in the proper order: |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 368 | // mastership, switch, port, link, host |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 369 | // |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 370 | // 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 377 | for (SwitchEvent switchEvent : addedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 378 | addSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 379 | } |
| 380 | for (PortEvent portEvent : addedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 381 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 382 | } |
| 383 | for (LinkEvent linkEvent : addedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 384 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 385 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 386 | for (HostEvent hostEvent : addedHostEvents.values()) { |
| 387 | addHost(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 388 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 389 | // |
| 390 | // Apply the "remove" events in the reverse order: |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 391 | // host, link, port, switch, mastership |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 392 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 393 | for (HostEvent hostEvent : removedHostEvents.values()) { |
| 394 | removeHost(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 395 | } |
| 396 | for (LinkEvent linkEvent : removedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 397 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 398 | } |
| 399 | for (PortEvent portEvent : removedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 400 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 401 | } |
| 402 | for (SwitchEvent switchEvent : removedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 403 | removeSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 404 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 405 | for (MastershipEvent mastershipEvent : |
| 406 | removedMastershipEvents.values()) { |
| 407 | processRemovedMastershipEvent(mastershipEvent); |
| 408 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 409 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 410 | // |
| 411 | // Apply reordered events |
| 412 | // |
| 413 | applyReorderedEvents(!addedSwitchEvents.isEmpty(), |
| 414 | !addedPortEvents.isEmpty()); |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 415 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 416 | } finally { |
| 417 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 418 | // Topology modifications completed: Release the lock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 419 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 420 | topology.releaseWriteLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 421 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 422 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 423 | // |
| 424 | // Dispatch the Topology Notification Events to the applications |
| 425 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 426 | dispatchTopologyEvents(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 427 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 428 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 429 | /** |
| 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 Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 441 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 442 | /** |
| 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 Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 454 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 455 | /** |
| 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 Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Startup processing. |
| 469 | * |
| 470 | * @param datagridService the datagrid service to use. |
| 471 | */ |
| 472 | void startup(IDatagridService datagridService) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 473 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 474 | eventHandler, |
| 475 | byte[].class, |
| 476 | TopologyEvent.class); |
| 477 | eventHandler.start(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 480 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 481 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 482 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 483 | private void dispatchTopologyEvents() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 484 | if (apiAddedSwitchEvents.isEmpty() && |
| 485 | apiRemovedSwitchEvents.isEmpty() && |
| 486 | apiAddedPortEvents.isEmpty() && |
| 487 | apiRemovedPortEvents.isEmpty() && |
| 488 | apiAddedLinkEvents.isEmpty() && |
| 489 | apiRemovedLinkEvents.isEmpty() && |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 490 | apiAddedHostEvents.isEmpty() && |
| 491 | apiRemovedHostEvents.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 492 | return; // No events to dispatch |
| 493 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 494 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 495 | if (log.isDebugEnabled()) { |
| 496 | // |
| 497 | // Debug statements |
| 498 | // TODO: Those statements should be removed in the future |
| 499 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 500 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 501 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 502 | } |
| 503 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 504 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 505 | } |
| 506 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 507 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 508 | } |
| 509 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 510 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 511 | } |
| 512 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 513 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 514 | } |
| 515 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 516 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 517 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 518 | for (HostEvent hostEvent : apiAddedHostEvents) { |
| 519 | log.debug("Dispatch Topology Event: ADDED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 520 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 521 | for (HostEvent hostEvent : apiRemovedHostEvents) { |
| 522 | log.debug("Dispatch Topology Event: REMOVED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 523 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 524 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 525 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 526 | // |
| 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 Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 535 | this.lastEventTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 536 | |
| 537 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 538 | // Deliver the events |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 539 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 540 | for (ITopologyListener listener : this.topologyListeners) { |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 541 | TopologyEvents events = |
Pavlin Radoslavov | c61544e | 2014-07-23 16:27:27 -0700 | [diff] [blame] | 542 | new TopologyEvents(kryo.copy(apiAddedSwitchEvents), |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 543 | kryo.copy(apiRemovedSwitchEvents), |
| 544 | kryo.copy(apiAddedPortEvents), |
| 545 | kryo.copy(apiRemovedPortEvents), |
| 546 | kryo.copy(apiAddedLinkEvents), |
| 547 | kryo.copy(apiRemovedLinkEvents), |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 548 | kryo.copy(apiAddedHostEvents), |
| 549 | kryo.copy(apiRemovedHostEvents)); |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 550 | listener.topologyEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 551 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 552 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 553 | // |
| 554 | // Cleanup |
| 555 | // |
| 556 | apiAddedSwitchEvents.clear(); |
| 557 | apiRemovedSwitchEvents.clear(); |
| 558 | apiAddedPortEvents.clear(); |
| 559 | apiRemovedPortEvents.clear(); |
| 560 | apiAddedLinkEvents.clear(); |
| 561 | apiRemovedLinkEvents.clear(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 562 | apiAddedHostEvents.clear(); |
| 563 | apiRemovedHostEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Apply reordered events. |
| 568 | * |
| 569 | * @param hasAddedSwitchEvents true if there were Added Switch Events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 570 | * @param hasAddedPortEvents true if there were Added Port Events. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 571 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 572 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 573 | private void applyReorderedEvents(boolean hasAddedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 574 | boolean hasAddedPortEvents) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 575 | if (!(hasAddedSwitchEvents || hasAddedPortEvents)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 576 | return; // Nothing to do |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 577 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 578 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 579 | // |
| 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 585 | // - Apply reordered Link and Host Events if Switches or Ports |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 586 | // were added |
| 587 | // |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 588 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 589 | // |
| 590 | // Apply reordered Port Events if Switches were added |
| 591 | // |
| 592 | if (hasAddedSwitchEvents) { |
| 593 | Map<ByteBuffer, PortEvent> portEvents = reorderedAddedPortEvents; |
| 594 | reorderedAddedPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 595 | for (PortEvent portEvent : portEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 596 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 597 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 598 | } |
| 599 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 600 | // Apply reordered Link and Host Events if Switches or Ports |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 601 | // were added. |
| 602 | // |
| 603 | Map<ByteBuffer, LinkEvent> linkEvents = reorderedAddedLinkEvents; |
| 604 | reorderedAddedLinkEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 605 | for (LinkEvent linkEvent : linkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 606 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 607 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 608 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 609 | Map<ByteBuffer, HostEvent> hostEvents = reorderedAddedHostEvents; |
| 610 | reorderedAddedHostEvents = new HashMap<>(); |
| 611 | for (HostEvent hostEvent : hostEvents.values()) { |
| 612 | addHost(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 613 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 616 | /** |
| 617 | * Switch discovered event. |
| 618 | * |
| 619 | * @param switchEvent the switch event. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 620 | * @param portEvents the corresponding port events for the switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 621 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 622 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 623 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 624 | Collection<PortEvent> portEvents) { |
| 625 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 626 | log.debug("Sending add switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 627 | // Send out notification |
| 628 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 629 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 630 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 631 | // Send out notification for each port |
| 632 | for (PortEvent portEvent : portEvents) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 633 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 634 | topologyEvent = new TopologyEvent(portEvent); |
| 635 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 636 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 637 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 638 | // |
| 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 644 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 645 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 646 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 647 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 648 | // 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 Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 656 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 657 | // |
| 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 664 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 665 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 666 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 670 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 671 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 672 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 673 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 676 | /** |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 677 | * {@inheritDoc} |
| 678 | * <p/> |
| 679 | * Called by {@link TopologyPublisher.SwitchCleanup} thread. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 680 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 681 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 682 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 683 | // Get the old Port Events |
| 684 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 685 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 686 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 687 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 688 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 689 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 690 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 691 | log.debug("Sending remove switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 692 | // Send out notification |
| 693 | eventChannel.removeEntry(switchEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 694 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 695 | // |
| 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 702 | for (PortEvent portEvent : oldPortEvents.values()) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 703 | log.debug("Sending remove port:", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 704 | eventChannel.removeEntry(portEvent.getID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 705 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 706 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 707 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 708 | // 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 Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 717 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 718 | // 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 724 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 725 | discoveredAddedHostEvents.remove(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 730 | /** |
| 731 | * Port discovered event. |
| 732 | * |
| 733 | * @param portEvent the port event. |
| 734 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 735 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 736 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 737 | if (datastore.addPort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 738 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 739 | // Send out notification |
| 740 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 741 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 742 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 743 | // 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 Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 756 | /** |
| 757 | * Port removed event. |
| 758 | * |
| 759 | * @param portEvent the port event. |
| 760 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 761 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 762 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 763 | if (datastore.deactivatePort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 764 | log.debug("Sending remove port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 765 | // Send out notification |
| 766 | eventChannel.removeEntry(portEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 767 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 768 | // 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 Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 775 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 776 | // 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 HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 781 | if (linkEvent.getDst().equals(portEvent.getSwitchPort())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 782 | 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 Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 789 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 790 | // Cleanup for the connected hosts |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 791 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 792 | 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 HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 798 | if (swp.equals(portEvent.getSwitchPort())) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 799 | removedHostEvents.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 803 | for (HostEvent hostEvent : removedHostEvents) { |
| 804 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 805 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 810 | /** |
| 811 | * Link discovered event. |
| 812 | * |
| 813 | * @param linkEvent the link event. |
| 814 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 815 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 816 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 817 | if (datastore.addLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 818 | log.debug("Sending add link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 819 | // Send out notification |
| 820 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 821 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 822 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 823 | // 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 Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 836 | /** |
| 837 | * Link removed event. |
| 838 | * |
| 839 | * @param linkEvent the link event. |
| 840 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 841 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 842 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 843 | if (datastore.removeLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 844 | log.debug("Sending remove link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 845 | // Send out notification |
| 846 | eventChannel.removeEntry(linkEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 847 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 848 | // 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 Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 856 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 857 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 858 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 859 | * Host discovered event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 860 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 861 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 862 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 863 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 864 | public void putHostDiscoveryEvent(HostEvent hostEvent) { |
| 865 | if (datastore.addHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 866 | // Send out notification |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 867 | TopologyEvent topologyEvent = new TopologyEvent(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 868 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 869 | log.debug("Put the host info into the cache of the topology. mac {}", hostEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 870 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 871 | // Store the new Host Event in the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 872 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 873 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 880 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 881 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 882 | oldHostEvents.put(id, hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 885 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 886 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 887 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 888 | * Host removed event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 889 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 890 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 891 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 892 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 893 | public void removeHostDiscoveryEvent(HostEvent hostEvent) { |
| 894 | if (datastore.removeHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 895 | // Send out notification |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 896 | eventChannel.removeEntry(hostEvent.getID()); |
| 897 | log.debug("Remove the host info into the cache of the topology. mac {}", hostEvent.getMac()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 898 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 899 | // Cleanup the Host Event from the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 900 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 901 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 910 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 911 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 912 | // |
| 913 | // Methods to update topology replica |
| 914 | // |
| 915 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 916 | /** |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 917 | * 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 HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 940 | * Adds a switch to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 941 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 942 | * @param switchEvent the SwitchEvent with the switch to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 943 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 944 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 945 | private void addSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 946 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 953 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 954 | topology.putSwitch(switchEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 955 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 958 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 959 | * Removes a switch from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 960 | * <p/> |
| 961 | * It will call {@link #removePort(PortEvent)} for each ports on this switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 962 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 963 | * @param switchEvent the SwitchEvent with the switch to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 964 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 965 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 966 | private void removeSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 967 | final Dpid dpid = switchEvent.getDpid(); |
| 968 | |
| 969 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 970 | if (swInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 971 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 972 | return; |
| 973 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 974 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 975 | // |
| 976 | // Remove all Ports on the Switch |
| 977 | // |
| 978 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 979 | for (Port port : topology.getPorts(dpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 980 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 981 | port, switchEvent); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 982 | PortEvent portEvent = new PortEvent(port.getSwitchPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 983 | portsToRemove.add(portEvent); |
| 984 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 985 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 986 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 987 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 988 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 989 | log.debug("Removed {}", swInTopo); |
| 990 | topology.removeSwitch(dpid); |
| 991 | apiRemovedSwitchEvents.add(swInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 992 | } |
| 993 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 994 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 995 | * Adds a port to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 996 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 997 | * @param portEvent the PortEvent with the port to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 998 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 999 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1000 | private void addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1001 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1002 | if (sw == null) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1003 | log.debug("{} reordered because switch is null", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1004 | // Reordered event: delay the event in local cache |
| 1005 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 1006 | reorderedAddedPortEvents.put(id, portEvent); |
| 1007 | return; |
| 1008 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1009 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1010 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1017 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1018 | topology.putPort(portEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1019 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1022 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1023 | * Removes a port from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1024 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1025 | * It will remove attachment points from each hosts on this port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1026 | * and call {@link #removeLink(LinkEvent)} for each links on this port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1027 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1028 | * @param portEvent the PortEvent with the port to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1029 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1030 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1031 | private void removePort(PortEvent portEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1032 | SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1033 | if (sw == null) { |
| 1034 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 1035 | portEvent); |
| 1036 | return; |
| 1037 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1038 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1039 | final SwitchPort switchPort = portEvent.getSwitchPort(); |
| 1040 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
| 1041 | if (portInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1042 | log.warn("Port {} already removed, ignoring", portEvent); |
| 1043 | return; |
| 1044 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1045 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1046 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1047 | // Remove all Host attachment points bound to this Port |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1048 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1049 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1054 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1055 | 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1068 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1069 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1070 | // |
| 1071 | // Remove all Links connected to the Port |
| 1072 | // |
| 1073 | Set<Link> links = new HashSet<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1074 | links.addAll(topology.getOutgoingLinks(switchPort)); |
| 1075 | links.addAll(topology.getIncomingLinks(switchPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1076 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1077 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1078 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1079 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1080 | LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple()); |
| 1081 | if (linkEvent != null) { |
| 1082 | log.debug("Removing Link {} on Port {}", link, portInTopo); |
| 1083 | removeLink(linkEvent); |
| 1084 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1085 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1086 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1087 | // Remove the Port from Topology |
| 1088 | log.debug("Removed {}", portInTopo); |
| 1089 | topology.removePort(switchPort); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1090 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1091 | apiRemovedPortEvents.add(portInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1094 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1095 | * Adds a link to the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1096 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1097 | * It will remove attachment points from each hosts using the same ports. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1098 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1099 | * @param linkEvent the LinkEvent with the link to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1100 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1101 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1102 | private void addLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1103 | PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc()); |
| 1104 | PortEvent dstPort = topology.getPortEvent(linkEvent.getDst()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1105 | if ((srcPort == null) || (dstPort == null)) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1106 | log.debug("{} reordered because {} port is null", linkEvent, |
| 1107 | (srcPort == null) ? "src" : "dst"); |
| 1108 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1109 | // XXX domain knowledge: port must be present before link. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1110 | // Reordered event: delay the event in local cache |
| 1111 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 1112 | reorderedAddedLinkEvents.put(id, linkEvent); |
| 1113 | return; |
| 1114 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1115 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1116 | // XXX domain knowledge: Sanity check: Port cannot have both Link and Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1117 | // 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 HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1119 | if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) { |
| 1120 | // Only check for existing Host when adding new Link. |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1121 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1122 | // 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 HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1139 | log.error("Host {} on Port {} should have been removed prior to adding Link {}", |
| 1140 | host, port, linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1141 | |
| 1142 | HostEvent hostEvent = topology.getHostEvent(host.getMacAddress()); |
| 1143 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1146 | // 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1162 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1163 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1164 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1165 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1174 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1177 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1178 | * Removes a link from the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1179 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1180 | * @param linkEvent the LinkEvent with the link to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1181 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1182 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1183 | private void removeLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1184 | Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1185 | linkEvent.getSrc().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1186 | if (srcPort == null) { |
| 1187 | log.warn("Src Port for Link {} already removed, ignoring", |
| 1188 | linkEvent); |
| 1189 | return; |
| 1190 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1191 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1192 | Port dstPort = topology.getPort(linkEvent.getDst().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1193 | linkEvent.getDst().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1194 | if (dstPort == null) { |
| 1195 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1196 | linkEvent); |
| 1197 | return; |
| 1198 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1199 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1200 | LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(), |
| 1201 | linkEvent.getType()); |
| 1202 | if (linkInTopo == null) { |
| 1203 | log.warn("Link {} already removed, ignoring", linkEvent); |
| 1204 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1205 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1206 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1207 | 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 Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1218 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1219 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1220 | log.debug("Removed {}", linkInTopo); |
| 1221 | topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType()); |
| 1222 | apiRemovedLinkEvents.add(linkInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1225 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1226 | * Adds a host to the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1227 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1228 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1229 | * TODO: Eventually, we might need to consider reordering |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1230 | * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events on the same port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1231 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1232 | * @param hostEvent the HostEvent with the host to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1233 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1234 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1235 | private void addHost(HostEvent hostEvent) { |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1236 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1237 | // 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 Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1241 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1242 | // create unfrozen copy |
| 1243 | // for removing attachment points which already has a link |
| 1244 | HostEvent modifiedHostEvent = new HostEvent(hostEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1245 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1246 | // Verify each attachment point |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1247 | boolean attachmentFound = false; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1248 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 1249 | // XXX domain knowledge: Port must exist before Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1250 | // but this knowledge cannot be pushed down to driver. |
| 1251 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1252 | // Attached Ports must exist |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1253 | Port port = topology.getPort(swp.getDpid(), swp.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1254 | if (port == null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1255 | log.debug("{} reordered because port {} was not there", hostEvent, swp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1256 | // Reordered event: delay the event in local cache |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1257 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 1258 | reorderedAddedHostEvents.put(id, hostEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1259 | return; // should not continue if re-applying later |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1260 | } |
| 1261 | // Attached Ports must not have Link |
| 1262 | if (port.getOutgoingLink() != null || |
| 1263 | port.getIncomingLink() != null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1264 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1272 | continue; |
| 1273 | } |
| 1274 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1275 | attachmentFound = true; |
| 1276 | } |
| 1277 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1278 | // Update the host in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1279 | if (attachmentFound) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1280 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1298 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1301 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1302 | * Removes a host from the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1303 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1304 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1305 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1306 | * @param hostEvent the Host Event with the host to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1307 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1308 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1309 | private void removeHost(HostEvent hostEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1310 | |
| 1311 | final MACAddress mac = hostEvent.getMac(); |
| 1312 | HostEvent hostInTopo = topology.getHostEvent(mac); |
| 1313 | if (hostInTopo == null) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1314 | log.warn("Host {} already removed, ignoring", hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1315 | return; |
| 1316 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1317 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1318 | log.debug("Removed {}", hostInTopo); |
| 1319 | topology.removeHost(mac); |
| 1320 | apiRemovedHostEvents.add(hostInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1321 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1322 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1323 | /** |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 1324 | * 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 Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1348 | * Read the whole topology from the database. |
| 1349 | * |
| 1350 | * @return a collection of EventEntry-encapsulated Topology Events for |
| 1351 | * the whole topology. |
| 1352 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1353 | private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1354 | Collection<EventEntry<TopologyEvent>> collection = |
| 1355 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1356 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1357 | // XXX May need to clear whole topology first, depending on |
| 1358 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1359 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1360 | // Add all active switches |
| 1361 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1362 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1363 | continue; |
| 1364 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1365 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1366 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1367 | 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 HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1373 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1374 | // Add all active ports |
| 1375 | for (KVPort p : KVPort.getAllPorts()) { |
| 1376 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1377 | continue; |
| 1378 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1379 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1380 | PortEvent portEvent = new PortEvent( |
| 1381 | new Dpid(p.getDpid()), |
| 1382 | new PortNumber(p.getNumber().shortValue())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1383 | 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 HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1389 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1390 | for (KVDevice d : KVDevice.getAllDevices()) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1391 | HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac())); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1392 | for (byte[] portId : d.getAllPortIds()) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 1393 | devEvent.addAttachmentPoint( |
| 1394 | new SwitchPort(KVPort.getDpidFromKey(portId), |
| 1395 | KVPort.getNumberFromKey(portId))); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1398 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1399 | for (KVLink l : KVLink.getAllLinks()) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1400 | LinkEvent linkEvent = new LinkEvent( |
| 1401 | new SwitchPort(l.getSrc().dpid, l.getSrc().number), |
| 1402 | new SwitchPort(l.getDst().dpid, l.getDst().number)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1403 | 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 Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1409 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1410 | return collection; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1411 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1412 | } |