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 | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 266 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 267 | // |
| 268 | // Classify and suppress matching events |
| 269 | // |
| 270 | for (EventEntry<TopologyEvent> event : events) { |
| 271 | TopologyEvent topologyEvent = event.eventData(); |
| 272 | SwitchEvent switchEvent = topologyEvent.switchEvent; |
| 273 | PortEvent portEvent = topologyEvent.portEvent; |
| 274 | LinkEvent linkEvent = topologyEvent.linkEvent; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 275 | HostEvent hostEvent = topologyEvent.hostEvent; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 276 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 277 | // |
| 278 | // Extract the events |
| 279 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 280 | // FIXME Following event squashing logic based only on ID |
| 281 | // potentially lose attribute change. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 282 | switch (event.eventType()) { |
| 283 | case ENTRY_ADD: |
| 284 | log.debug("Topology event ENTRY_ADD: {}", topologyEvent); |
| 285 | if (switchEvent != null) { |
| 286 | ByteBuffer id = switchEvent.getIDasByteBuffer(); |
| 287 | addedSwitchEvents.put(id, switchEvent); |
| 288 | removedSwitchEvents.remove(id); |
| 289 | // Switch Events are not affected by event reordering |
| 290 | } |
| 291 | if (portEvent != null) { |
| 292 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 293 | addedPortEvents.put(id, portEvent); |
| 294 | removedPortEvents.remove(id); |
| 295 | reorderedAddedPortEvents.remove(id); |
| 296 | } |
| 297 | if (linkEvent != null) { |
| 298 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 299 | addedLinkEvents.put(id, linkEvent); |
| 300 | removedLinkEvents.remove(id); |
| 301 | reorderedAddedLinkEvents.remove(id); |
| 302 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 303 | if (hostEvent != null) { |
| 304 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 305 | addedHostEvents.put(id, hostEvent); |
| 306 | removedHostEvents.remove(id); |
| 307 | reorderedAddedHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 308 | } |
| 309 | break; |
| 310 | case ENTRY_REMOVE: |
| 311 | log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent); |
| 312 | if (switchEvent != null) { |
| 313 | ByteBuffer id = switchEvent.getIDasByteBuffer(); |
| 314 | addedSwitchEvents.remove(id); |
| 315 | removedSwitchEvents.put(id, switchEvent); |
| 316 | // Switch Events are not affected by event reordering |
| 317 | } |
| 318 | if (portEvent != null) { |
| 319 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 320 | addedPortEvents.remove(id); |
| 321 | removedPortEvents.put(id, portEvent); |
| 322 | reorderedAddedPortEvents.remove(id); |
| 323 | } |
| 324 | if (linkEvent != null) { |
| 325 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 326 | addedLinkEvents.remove(id); |
| 327 | removedLinkEvents.put(id, linkEvent); |
| 328 | reorderedAddedLinkEvents.remove(id); |
| 329 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 330 | if (hostEvent != null) { |
| 331 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 332 | addedHostEvents.remove(id); |
| 333 | removedHostEvents.put(id, hostEvent); |
| 334 | reorderedAddedHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 335 | } |
| 336 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 337 | default: |
| 338 | log.error("Unknown topology event {}", |
| 339 | event.eventType()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 342 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 343 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 344 | // Lock the topology while it is modified |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 345 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 346 | topology.acquireWriteLock(); |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 347 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 348 | try { |
| 349 | // |
| 350 | // Apply the classified events. |
| 351 | // |
| 352 | // Apply the "add" events in the proper order: |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 353 | // switch, port, link, host |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 354 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 355 | for (SwitchEvent switchEvent : addedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 356 | addSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 357 | } |
| 358 | for (PortEvent portEvent : addedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 359 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 360 | } |
| 361 | for (LinkEvent linkEvent : addedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 362 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 363 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 364 | for (HostEvent hostEvent : addedHostEvents.values()) { |
| 365 | addHost(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 366 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 367 | // |
| 368 | // Apply the "remove" events in the reverse order: |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 369 | // host, link, port, switch |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 370 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 371 | for (HostEvent hostEvent : removedHostEvents.values()) { |
| 372 | removeHost(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 373 | } |
| 374 | for (LinkEvent linkEvent : removedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 375 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 376 | } |
| 377 | for (PortEvent portEvent : removedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 378 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 379 | } |
| 380 | for (SwitchEvent switchEvent : removedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 381 | removeSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 382 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 383 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 384 | // |
| 385 | // Apply reordered events |
| 386 | // |
| 387 | applyReorderedEvents(!addedSwitchEvents.isEmpty(), |
| 388 | !addedPortEvents.isEmpty()); |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 389 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 390 | } finally { |
| 391 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 392 | // Topology modifications completed: Release the lock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 393 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 394 | topology.releaseWriteLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 395 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 396 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 397 | // |
| 398 | // Dispatch the Topology Notification Events to the applications |
| 399 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 400 | dispatchTopologyEvents(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 401 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 402 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 403 | /** |
| 404 | * Receive a notification that an entry is added. |
| 405 | * |
| 406 | * @param value the value for the entry. |
| 407 | */ |
| 408 | @Override |
| 409 | public void entryAdded(TopologyEvent value) { |
| 410 | EventEntry<TopologyEvent> eventEntry = |
| 411 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 412 | value); |
| 413 | topologyEvents.add(eventEntry); |
| 414 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 415 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 416 | /** |
| 417 | * Receive a notification that an entry is removed. |
| 418 | * |
| 419 | * @param value the value for the entry. |
| 420 | */ |
| 421 | @Override |
| 422 | public void entryRemoved(TopologyEvent value) { |
| 423 | EventEntry<TopologyEvent> eventEntry = |
| 424 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 425 | value); |
| 426 | topologyEvents.add(eventEntry); |
| 427 | } |
Pavlin 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 updated. |
| 431 | * |
| 432 | * @param value the value for the entry. |
| 433 | */ |
| 434 | @Override |
| 435 | public void entryUpdated(TopologyEvent value) { |
| 436 | // NOTE: The ADD and UPDATE events are processed in same way |
| 437 | entryAdded(value); |
| 438 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Startup processing. |
| 443 | * |
| 444 | * @param datagridService the datagrid service to use. |
| 445 | */ |
| 446 | void startup(IDatagridService datagridService) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 447 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 448 | eventHandler, |
| 449 | byte[].class, |
| 450 | TopologyEvent.class); |
| 451 | eventHandler.start(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 454 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 455 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 456 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 457 | private void dispatchTopologyEvents() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 458 | if (apiAddedSwitchEvents.isEmpty() && |
| 459 | apiRemovedSwitchEvents.isEmpty() && |
| 460 | apiAddedPortEvents.isEmpty() && |
| 461 | apiRemovedPortEvents.isEmpty() && |
| 462 | apiAddedLinkEvents.isEmpty() && |
| 463 | apiRemovedLinkEvents.isEmpty() && |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 464 | apiAddedHostEvents.isEmpty() && |
| 465 | apiRemovedHostEvents.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 466 | return; // No events to dispatch |
| 467 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 468 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 469 | if (log.isDebugEnabled()) { |
| 470 | // |
| 471 | // Debug statements |
| 472 | // TODO: Those statements should be removed in the future |
| 473 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 474 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 475 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 476 | } |
| 477 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 478 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 479 | } |
| 480 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 481 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 482 | } |
| 483 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 484 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 485 | } |
| 486 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 487 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 488 | } |
| 489 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 490 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 491 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 492 | for (HostEvent hostEvent : apiAddedHostEvents) { |
| 493 | log.debug("Dispatch Topology Event: ADDED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 494 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 495 | for (HostEvent hostEvent : apiRemovedHostEvents) { |
| 496 | log.debug("Dispatch Topology Event: REMOVED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 497 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 498 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 499 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 500 | // |
| 501 | // Update the metrics |
| 502 | // |
| 503 | long totalEvents = |
| 504 | apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() + |
| 505 | apiAddedPortEvents.size() + apiRemovedPortEvents.size() + |
| 506 | apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() + |
| 507 | apiAddedHostEvents.size() + apiRemovedHostEvents.size(); |
| 508 | this.listenerEventRate.mark(totalEvents); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 509 | this.lastEventTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 510 | |
| 511 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 512 | // Deliver the events |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 513 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 514 | for (ITopologyListener listener : this.topologyListeners) { |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 515 | TopologyEvents events = |
Pavlin Radoslavov | c61544e | 2014-07-23 16:27:27 -0700 | [diff] [blame^] | 516 | new TopologyEvents(kryo.copy(apiAddedSwitchEvents), |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 517 | kryo.copy(apiRemovedSwitchEvents), |
| 518 | kryo.copy(apiAddedPortEvents), |
| 519 | kryo.copy(apiRemovedPortEvents), |
| 520 | kryo.copy(apiAddedLinkEvents), |
| 521 | kryo.copy(apiRemovedLinkEvents), |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 522 | kryo.copy(apiAddedHostEvents), |
| 523 | kryo.copy(apiRemovedHostEvents)); |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 524 | listener.topologyEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 525 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 526 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 527 | // |
| 528 | // Cleanup |
| 529 | // |
| 530 | apiAddedSwitchEvents.clear(); |
| 531 | apiRemovedSwitchEvents.clear(); |
| 532 | apiAddedPortEvents.clear(); |
| 533 | apiRemovedPortEvents.clear(); |
| 534 | apiAddedLinkEvents.clear(); |
| 535 | apiRemovedLinkEvents.clear(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 536 | apiAddedHostEvents.clear(); |
| 537 | apiRemovedHostEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Apply reordered events. |
| 542 | * |
| 543 | * @param hasAddedSwitchEvents true if there were Added Switch Events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 544 | * @param hasAddedPortEvents true if there were Added Port Events. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 545 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 546 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 547 | private void applyReorderedEvents(boolean hasAddedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 548 | boolean hasAddedPortEvents) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 549 | if (!(hasAddedSwitchEvents || hasAddedPortEvents)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 550 | return; // Nothing to do |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -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 | // Try to apply the reordered events. |
| 555 | // |
| 556 | // NOTE: For simplicity we try to apply all events of a particular |
| 557 | // type if any "parent" type event was processed: |
| 558 | // - Apply reordered Port Events if Switches were added |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 559 | // - Apply reordered Link and Host Events if Switches or Ports |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 560 | // were added |
| 561 | // |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 562 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | // |
| 564 | // Apply reordered Port Events if Switches were added |
| 565 | // |
| 566 | if (hasAddedSwitchEvents) { |
| 567 | Map<ByteBuffer, PortEvent> portEvents = reorderedAddedPortEvents; |
| 568 | reorderedAddedPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 569 | for (PortEvent portEvent : portEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 570 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 571 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 572 | } |
| 573 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 574 | // Apply reordered Link and Host Events if Switches or Ports |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 575 | // were added. |
| 576 | // |
| 577 | Map<ByteBuffer, LinkEvent> linkEvents = reorderedAddedLinkEvents; |
| 578 | reorderedAddedLinkEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 579 | for (LinkEvent linkEvent : linkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 580 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 581 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 582 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 583 | Map<ByteBuffer, HostEvent> hostEvents = reorderedAddedHostEvents; |
| 584 | reorderedAddedHostEvents = new HashMap<>(); |
| 585 | for (HostEvent hostEvent : hostEvents.values()) { |
| 586 | addHost(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 587 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 590 | /** |
| 591 | * Switch discovered event. |
| 592 | * |
| 593 | * @param switchEvent the switch event. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 594 | * @param portEvents the corresponding port events for the switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 595 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 596 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 597 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 598 | Collection<PortEvent> portEvents) { |
| 599 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 600 | log.debug("Sending add switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 601 | // Send out notification |
| 602 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 603 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 604 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 605 | // Send out notification for each port |
| 606 | for (PortEvent portEvent : portEvents) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 607 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 608 | topologyEvent = new TopologyEvent(portEvent); |
| 609 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 610 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 611 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 612 | // |
| 613 | // Keep track of the added ports |
| 614 | // |
| 615 | // Get the old Port Events |
| 616 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 617 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 618 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 619 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 620 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 621 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 622 | // Store the new Port Events in the local cache |
| 623 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 624 | for (PortEvent portEvent : portEvents) { |
| 625 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 626 | newPortEvents.put(id, portEvent); |
| 627 | } |
| 628 | discoveredAddedPortEvents.put(switchEvent.getDpid(), |
| 629 | newPortEvents); |
Pavlin 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 | // |
| 632 | // Extract the removed ports |
| 633 | // |
| 634 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 635 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 636 | ByteBuffer key = entry.getKey(); |
| 637 | PortEvent portEvent = entry.getValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 638 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 639 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 640 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 644 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 645 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 646 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 647 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 650 | /** |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 651 | * {@inheritDoc} |
| 652 | * <p/> |
| 653 | * Called by {@link TopologyPublisher.SwitchCleanup} thread. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 654 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 655 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 656 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 657 | // Get the old Port Events |
| 658 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 659 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 660 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 661 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 662 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 663 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 664 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 665 | log.debug("Sending remove switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 666 | // Send out notification |
| 667 | eventChannel.removeEntry(switchEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 668 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 669 | // |
| 670 | // Send out notification for each port. |
| 671 | // |
| 672 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 673 | // because it will attempt to remove the port from the database, |
| 674 | // and the deactiveSwitch() call above already removed all ports. |
| 675 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 676 | for (PortEvent portEvent : oldPortEvents.values()) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 677 | log.debug("Sending remove port:", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 678 | eventChannel.removeEntry(portEvent.getID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 679 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 680 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 681 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 682 | // Cleanup for each link |
| 683 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 684 | discoveredAddedLinkEvents.get(switchEvent.getDpid()); |
| 685 | if (oldLinkEvents != null) { |
| 686 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 687 | removeLinkDiscoveryEvent(linkEvent); |
| 688 | } |
| 689 | discoveredAddedLinkEvents.remove(switchEvent.getDpid()); |
| 690 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 691 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 692 | // Cleanup for each host |
| 693 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 694 | discoveredAddedHostEvents.get(switchEvent.getDpid()); |
| 695 | if (oldHostEvents != null) { |
| 696 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 697 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 698 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 699 | discoveredAddedHostEvents.remove(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 704 | /** |
| 705 | * Port discovered event. |
| 706 | * |
| 707 | * @param portEvent the port event. |
| 708 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 709 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 710 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 711 | if (datastore.addPort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 712 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 713 | // Send out notification |
| 714 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 715 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 716 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 717 | // Store the new Port Event in the local cache |
| 718 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 719 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 720 | if (oldPortEvents == null) { |
| 721 | oldPortEvents = new HashMap<>(); |
| 722 | discoveredAddedPortEvents.put(portEvent.getDpid(), |
| 723 | oldPortEvents); |
| 724 | } |
| 725 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 726 | oldPortEvents.put(id, portEvent); |
| 727 | } |
Pavlin 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 removed 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 removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 737 | if (datastore.deactivatePort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 738 | log.debug("Sending remove port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 739 | // Send out notification |
| 740 | eventChannel.removeEntry(portEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 741 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 742 | // Cleanup the Port Event from the local cache |
| 743 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 744 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 745 | if (oldPortEvents != null) { |
| 746 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 747 | oldPortEvents.remove(id); |
| 748 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 749 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 750 | // Cleanup for the incoming link |
| 751 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 752 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 753 | if (oldLinkEvents != null) { |
| 754 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 755 | if (linkEvent.getDst().equals(portEvent.getSwitchPort())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 756 | removeLinkDiscoveryEvent(linkEvent); |
| 757 | // XXX If we change our model to allow multiple Link on |
| 758 | // a Port, this loop must be fixed to allow continuing. |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 763 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 764 | // Cleanup for the connected hosts |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 765 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 766 | List<HostEvent> removedHostEvents = new LinkedList<>(); |
| 767 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 768 | discoveredAddedHostEvents.get(portEvent.getDpid()); |
| 769 | if (oldHostEvents != null) { |
| 770 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 771 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 772 | if (swp.equals(portEvent.getSwitchPort())) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 773 | removedHostEvents.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 777 | for (HostEvent hostEvent : removedHostEvents) { |
| 778 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 779 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 784 | /** |
| 785 | * Link discovered event. |
| 786 | * |
| 787 | * @param linkEvent the link event. |
| 788 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 789 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 790 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 791 | if (datastore.addLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 792 | log.debug("Sending add link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 793 | // Send out notification |
| 794 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 795 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 796 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 797 | // Store the new Link Event in the local cache |
| 798 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 799 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 800 | if (oldLinkEvents == null) { |
| 801 | oldLinkEvents = new HashMap<>(); |
| 802 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 803 | oldLinkEvents); |
| 804 | } |
| 805 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 806 | oldLinkEvents.put(id, linkEvent); |
| 807 | } |
Pavlin 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 removed 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 removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 817 | if (datastore.removeLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 818 | log.debug("Sending remove link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 819 | // Send out notification |
| 820 | eventChannel.removeEntry(linkEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 821 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 822 | // Cleanup the Link Event from the local cache |
| 823 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 824 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 825 | if (oldLinkEvents != null) { |
| 826 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 827 | oldLinkEvents.remove(id); |
| 828 | } |
| 829 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 830 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 831 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 832 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 833 | * Host discovered event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 834 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 835 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 836 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 837 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 838 | public void putHostDiscoveryEvent(HostEvent hostEvent) { |
| 839 | if (datastore.addHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 840 | // Send out notification |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 841 | TopologyEvent topologyEvent = new TopologyEvent(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 842 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 843 | 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] | 844 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 845 | // Store the new Host Event in the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 846 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 847 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 848 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 849 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 850 | if (oldHostEvents == null) { |
| 851 | oldHostEvents = new HashMap<>(); |
| 852 | discoveredAddedHostEvents.put(swp.getDpid(), |
| 853 | oldHostEvents); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 854 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 855 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 856 | oldHostEvents.put(id, hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 859 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 860 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 861 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 862 | * Host removed event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 863 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 864 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 865 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 866 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 867 | public void removeHostDiscoveryEvent(HostEvent hostEvent) { |
| 868 | if (datastore.removeHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 869 | // Send out notification |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 870 | eventChannel.removeEntry(hostEvent.getID()); |
| 871 | 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] | 872 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 873 | // Cleanup the Host Event from the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 874 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 875 | ByteBuffer id = ByteBuffer.wrap(hostEvent.getID()); |
| 876 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 877 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 878 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 879 | if (oldHostEvents != null) { |
| 880 | oldHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 884 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 885 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 886 | // |
| 887 | // Methods to update topology replica |
| 888 | // |
| 889 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 890 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 891 | * Adds a switch to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 892 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 893 | * @param switchEvent the SwitchEvent with the switch to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 894 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 895 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 896 | private void addSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 897 | if (log.isDebugEnabled()) { |
| 898 | SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid()); |
| 899 | if (sw != null) { |
| 900 | log.debug("Update {}", switchEvent); |
| 901 | } else { |
| 902 | log.debug("Added {}", switchEvent); |
| 903 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 904 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 905 | topology.putSwitch(switchEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 906 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 909 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 910 | * Removes a switch from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 911 | * <p/> |
| 912 | * It will call {@link #removePort(PortEvent)} for each ports on this switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 913 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 914 | * @param switchEvent the SwitchEvent with the switch to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 915 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 916 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 917 | private void removeSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 918 | final Dpid dpid = switchEvent.getDpid(); |
| 919 | |
| 920 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 921 | if (swInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 922 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 923 | return; |
| 924 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 925 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 926 | // |
| 927 | // Remove all Ports on the Switch |
| 928 | // |
| 929 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 930 | for (Port port : topology.getPorts(dpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 931 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 932 | port, switchEvent); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 933 | PortEvent portEvent = new PortEvent(port.getSwitchPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 934 | portsToRemove.add(portEvent); |
| 935 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 936 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 937 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 938 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 939 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 940 | log.debug("Removed {}", swInTopo); |
| 941 | topology.removeSwitch(dpid); |
| 942 | apiRemovedSwitchEvents.add(swInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 945 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 946 | * Adds a port to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 947 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 948 | * @param portEvent the PortEvent with the port to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 949 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 950 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 951 | private void addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 952 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 953 | if (sw == null) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 954 | log.debug("{} reordered because switch is null", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 955 | // Reordered event: delay the event in local cache |
| 956 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 957 | reorderedAddedPortEvents.put(id, portEvent); |
| 958 | return; |
| 959 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 960 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 961 | if (log.isDebugEnabled()) { |
| 962 | PortEvent port = topology.getPortEvent(portEvent.getSwitchPort()); |
| 963 | if (port != null) { |
| 964 | log.debug("Update {}", portEvent); |
| 965 | } else { |
| 966 | log.debug("Added {}", portEvent); |
| 967 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 968 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 969 | topology.putPort(portEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 970 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 973 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 974 | * Removes a port from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 975 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 976 | * It will remove attachment points from each hosts on this port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 977 | * and call {@link #removeLink(LinkEvent)} for each links on this port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 978 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 979 | * @param portEvent the PortEvent with the port to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 980 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 981 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 982 | private void removePort(PortEvent portEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 983 | SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 984 | if (sw == null) { |
| 985 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 986 | portEvent); |
| 987 | return; |
| 988 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 989 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 990 | final SwitchPort switchPort = portEvent.getSwitchPort(); |
| 991 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
| 992 | if (portInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 993 | log.warn("Port {} already removed, ignoring", portEvent); |
| 994 | return; |
| 995 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 996 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 997 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 998 | // Remove all Host attachment points bound to this Port |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 999 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1000 | List<HostEvent> hostsToUpdate = new ArrayList<>(); |
| 1001 | for (Host host : topology.getHosts(switchPort)) { |
| 1002 | log.debug("Removing Host {} on Port {}", host, portInTopo); |
| 1003 | HostEvent hostEvent = topology.getHostEvent(host.getMacAddress()); |
| 1004 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1005 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1006 | for (HostEvent hostEvent : hostsToUpdate) { |
| 1007 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 1008 | newHostEvent.removeAttachmentPoint(switchPort); |
| 1009 | newHostEvent.freeze(); |
| 1010 | |
| 1011 | // TODO should this event be fired inside #addHost? |
| 1012 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 1013 | // No more attachment point left -> remove Host |
| 1014 | removeHost(hostEvent); |
| 1015 | } else { |
| 1016 | // Update Host |
| 1017 | addHost(newHostEvent); |
| 1018 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1019 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1020 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1021 | // |
| 1022 | // Remove all Links connected to the Port |
| 1023 | // |
| 1024 | Set<Link> links = new HashSet<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1025 | links.addAll(topology.getOutgoingLinks(switchPort)); |
| 1026 | links.addAll(topology.getIncomingLinks(switchPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1027 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1028 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1029 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1030 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1031 | LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple()); |
| 1032 | if (linkEvent != null) { |
| 1033 | log.debug("Removing Link {} on Port {}", link, portInTopo); |
| 1034 | removeLink(linkEvent); |
| 1035 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1036 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1037 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1038 | // Remove the Port from Topology |
| 1039 | log.debug("Removed {}", portInTopo); |
| 1040 | topology.removePort(switchPort); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1041 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1042 | apiRemovedPortEvents.add(portInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1045 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1046 | * Adds a link to the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1047 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1048 | * It will remove attachment points from each hosts using the same ports. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1049 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1050 | * @param linkEvent the LinkEvent with the link to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1051 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1052 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1053 | private void addLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1054 | PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc()); |
| 1055 | PortEvent dstPort = topology.getPortEvent(linkEvent.getDst()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1056 | if ((srcPort == null) || (dstPort == null)) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1057 | log.debug("{} reordered because {} port is null", linkEvent, |
| 1058 | (srcPort == null) ? "src" : "dst"); |
| 1059 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1060 | // XXX domain knowledge: port must be present before link. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1061 | // Reordered event: delay the event in local cache |
| 1062 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 1063 | reorderedAddedLinkEvents.put(id, linkEvent); |
| 1064 | return; |
| 1065 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1066 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1067 | // XXX domain knowledge: Sanity check: Port cannot have both Link and Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1068 | // FIXME potentially local replica may not be up-to-date yet due to HZ delay. |
| 1069 | // may need to manage local truth and use them instead. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1070 | if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) { |
| 1071 | // Only check for existing Host when adding new Link. |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1072 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1073 | // Remove all Hosts attached to the ports on both ends |
| 1074 | |
| 1075 | Set<HostEvent> hostsToUpdate = new TreeSet<>(new Comparator<HostEvent>() { |
| 1076 | // comparison only using ID(=MAC) |
| 1077 | @Override |
| 1078 | public int compare(HostEvent o1, HostEvent o2) { |
| 1079 | return Long.compare(o1.getMac().toLong(), o2.getMac().toLong()); |
| 1080 | } |
| 1081 | }); |
| 1082 | |
| 1083 | List<SwitchPort> portsToCheck = Arrays.asList( |
| 1084 | srcPort.getSwitchPort(), |
| 1085 | dstPort.getSwitchPort()); |
| 1086 | |
| 1087 | // Enumerate Host which needs to be updated by this Link add event |
| 1088 | for (SwitchPort port : portsToCheck) { |
| 1089 | for (Host host : topology.getHosts(port)) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1090 | log.error("Host {} on Port {} should have been removed prior to adding Link {}", |
| 1091 | host, port, linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1092 | |
| 1093 | HostEvent hostEvent = topology.getHostEvent(host.getMacAddress()); |
| 1094 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1095 | } |
| 1096 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1097 | // remove attachment point from them. |
| 1098 | for (HostEvent hostEvent : hostsToUpdate) { |
| 1099 | // remove port from attachment point and update |
| 1100 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 1101 | newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort()); |
| 1102 | newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort()); |
| 1103 | newHostEvent.freeze(); |
| 1104 | |
| 1105 | // TODO should this event be fired inside #addHost? |
| 1106 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 1107 | // No more attachment point left -> remove Host |
| 1108 | removeHost(hostEvent); |
| 1109 | } else { |
| 1110 | // Update Host |
| 1111 | addHost(newHostEvent); |
| 1112 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1113 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1114 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1115 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1116 | if (log.isDebugEnabled()) { |
| 1117 | LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple()); |
| 1118 | if (link != null) { |
| 1119 | log.debug("Update {}", linkEvent); |
| 1120 | } else { |
| 1121 | log.debug("Added {}", linkEvent); |
| 1122 | } |
| 1123 | } |
| 1124 | topology.putLink(linkEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1125 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1128 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1129 | * Removes a link from the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1130 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1131 | * @param linkEvent the LinkEvent with the link to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1132 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1133 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1134 | private void removeLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1135 | Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1136 | linkEvent.getSrc().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1137 | if (srcPort == null) { |
| 1138 | log.warn("Src Port for Link {} already removed, ignoring", |
| 1139 | linkEvent); |
| 1140 | return; |
| 1141 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1142 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1143 | Port dstPort = topology.getPort(linkEvent.getDst().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1144 | linkEvent.getDst().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1145 | if (dstPort == null) { |
| 1146 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1147 | linkEvent); |
| 1148 | return; |
| 1149 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1150 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1151 | LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(), |
| 1152 | linkEvent.getType()); |
| 1153 | if (linkInTopo == null) { |
| 1154 | log.warn("Link {} already removed, ignoring", linkEvent); |
| 1155 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1156 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1157 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1158 | if (log.isDebugEnabled()) { |
| 1159 | // only do sanity check on debug level |
| 1160 | |
| 1161 | Link linkIn = dstPort.getIncomingLink(linkEvent.getType()); |
| 1162 | if (linkIn == null) { |
| 1163 | log.warn("Link {} already removed on destination Port", linkEvent); |
| 1164 | } |
| 1165 | Link linkOut = srcPort.getOutgoingLink(linkEvent.getType()); |
| 1166 | if (linkOut == null) { |
| 1167 | log.warn("Link {} already removed on src Port", linkEvent); |
| 1168 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1169 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1170 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1171 | log.debug("Removed {}", linkInTopo); |
| 1172 | topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType()); |
| 1173 | apiRemovedLinkEvents.add(linkInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1176 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1177 | * Adds a host to the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1178 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1179 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1180 | * TODO: Eventually, we might need to consider reordering |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1181 | * 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] | 1182 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1183 | * @param hostEvent the HostEvent with the host to add. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1184 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1185 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1186 | private void addHost(HostEvent hostEvent) { |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1187 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1188 | // TODO Decide how to handle update scenario. |
| 1189 | // If the new HostEvent has less attachment point compared to |
| 1190 | // existing HostEvent, what should the event be? |
| 1191 | // - AddHostEvent with some attachment point removed? (current behavior) |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1192 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1193 | // create unfrozen copy |
| 1194 | // for removing attachment points which already has a link |
| 1195 | HostEvent modifiedHostEvent = new HostEvent(hostEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1196 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1197 | // Verify each attachment point |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1198 | boolean attachmentFound = false; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1199 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 1200 | // XXX domain knowledge: Port must exist before Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1201 | // but this knowledge cannot be pushed down to driver. |
| 1202 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1203 | // Attached Ports must exist |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1204 | Port port = topology.getPort(swp.getDpid(), swp.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1205 | if (port == null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1206 | log.debug("{} reordered because port {} was not there", hostEvent, swp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1207 | // Reordered event: delay the event in local cache |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1208 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 1209 | reorderedAddedHostEvents.put(id, hostEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1210 | return; // should not continue if re-applying later |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1211 | } |
| 1212 | // Attached Ports must not have Link |
| 1213 | if (port.getOutgoingLink() != null || |
| 1214 | port.getIncomingLink() != null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1215 | log.warn("Link (Out:{},In:{}) exist on the attachment point. " |
| 1216 | + "Ignoring this attachmentpoint ({}) from {}.", |
| 1217 | port.getOutgoingLink(), port.getIncomingLink(), |
| 1218 | swp, modifiedHostEvent); |
| 1219 | // FIXME Should either reject, reorder this HostEvent, |
| 1220 | // or remove attachment point from given HostEvent |
| 1221 | // Removing attachment point from given HostEvent for now. |
| 1222 | modifiedHostEvent.removeAttachmentPoint(swp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1223 | continue; |
| 1224 | } |
| 1225 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1226 | attachmentFound = true; |
| 1227 | } |
| 1228 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1229 | // Update the host in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1230 | if (attachmentFound) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1231 | if (modifiedHostEvent.getAttachmentPoints().isEmpty()) { |
| 1232 | log.warn("No valid attachment point left. Ignoring." |
| 1233 | + "original: {}, modified: {}", hostEvent, modifiedHostEvent); |
| 1234 | // TODO Should we call #removeHost to trigger remove event? |
| 1235 | // only if this call is update. |
| 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | if (log.isDebugEnabled()) { |
| 1240 | HostEvent host = topology.getHostEvent(hostEvent.getMac()); |
| 1241 | if (host != null) { |
| 1242 | log.debug("Update {}", modifiedHostEvent); |
| 1243 | } else { |
| 1244 | log.debug("Added {}", modifiedHostEvent); |
| 1245 | } |
| 1246 | } |
| 1247 | topology.putHost(modifiedHostEvent.freeze()); |
| 1248 | apiAddedHostEvents.add(modifiedHostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1249 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1252 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1253 | * Removes a host from the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1254 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1255 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1256 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1257 | * @param hostEvent the Host Event with the host to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1258 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1259 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1260 | private void removeHost(HostEvent hostEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1261 | |
| 1262 | final MACAddress mac = hostEvent.getMac(); |
| 1263 | HostEvent hostInTopo = topology.getHostEvent(mac); |
| 1264 | if (hostInTopo == null) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1265 | log.warn("Host {} already removed, ignoring", hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1266 | return; |
| 1267 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1268 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1269 | log.debug("Removed {}", hostInTopo); |
| 1270 | topology.removeHost(mac); |
| 1271 | apiRemovedHostEvents.add(hostInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1272 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1273 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1274 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1275 | * Read the whole topology from the database. |
| 1276 | * |
| 1277 | * @return a collection of EventEntry-encapsulated Topology Events for |
| 1278 | * the whole topology. |
| 1279 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1280 | private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1281 | Collection<EventEntry<TopologyEvent>> collection = |
| 1282 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1283 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1284 | // XXX May need to clear whole topology first, depending on |
| 1285 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1286 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1287 | // Add all active switches |
| 1288 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1289 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1290 | continue; |
| 1291 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1292 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1293 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1294 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 1295 | EventEntry<TopologyEvent> eventEntry = |
| 1296 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1297 | topologyEvent); |
| 1298 | collection.add(eventEntry); |
| 1299 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1300 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1301 | // Add all active ports |
| 1302 | for (KVPort p : KVPort.getAllPorts()) { |
| 1303 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1304 | continue; |
| 1305 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1306 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1307 | PortEvent portEvent = new PortEvent( |
| 1308 | new Dpid(p.getDpid()), |
| 1309 | new PortNumber(p.getNumber().shortValue())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1310 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 1311 | EventEntry<TopologyEvent> eventEntry = |
| 1312 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1313 | topologyEvent); |
| 1314 | collection.add(eventEntry); |
| 1315 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1316 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1317 | for (KVDevice d : KVDevice.getAllDevices()) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1318 | HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac())); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1319 | for (byte[] portId : d.getAllPortIds()) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 1320 | devEvent.addAttachmentPoint( |
| 1321 | new SwitchPort(KVPort.getDpidFromKey(portId), |
| 1322 | KVPort.getNumberFromKey(portId))); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1325 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1326 | for (KVLink l : KVLink.getAllLinks()) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1327 | LinkEvent linkEvent = new LinkEvent( |
| 1328 | new SwitchPort(l.getSrc().dpid, l.getSrc().number), |
| 1329 | new SwitchPort(l.getDst().dpid, l.getDst().number)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1330 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 1331 | EventEntry<TopologyEvent> eventEntry = |
| 1332 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1333 | topologyEvent); |
| 1334 | collection.add(eventEntry); |
| 1335 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1336 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1337 | return collection; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1338 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1339 | } |