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; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 73 | private Kryo kryo = KryoFactory.newKryoObject(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 74 | private TopologyEventPreprocessor eventPreprocessor; |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 75 | private CopyOnWriteArrayList<ITopologyListener> topologyListeners = |
| 76 | new CopyOnWriteArrayList<>(); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 77 | private CopyOnWriteArrayList<ITopologyListener> newTopologyListeners = |
| 78 | new CopyOnWriteArrayList<>(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 79 | |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 80 | // |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 81 | // Metrics |
| 82 | // |
| 83 | private static final MetricsComponent METRICS_COMPONENT = |
| 84 | OnosMetrics.registerComponent("Topology"); |
| 85 | private static final MetricsFeature METRICS_FEATURE_EVENT_NOTIFICATION = |
| 86 | METRICS_COMPONENT.registerFeature("EventNotification"); |
| 87 | // |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 88 | // Timestamp of the last Topology event (ms from the Epoch) |
| 89 | private volatile long lastEventTimestampEpochMs = 0; |
| 90 | private final Gauge<Long> gaugeLastEventTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 91 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 92 | METRICS_FEATURE_EVENT_NOTIFICATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 93 | "LastEventTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 94 | new Gauge<Long>() { |
| 95 | @Override |
| 96 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 97 | return lastEventTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 98 | } |
| 99 | }); |
| 100 | // Rate of the Topology events published to the Topology listeners |
| 101 | private final Meter listenerEventRate = |
| 102 | OnosMetrics.createMeter(METRICS_COMPONENT, |
| 103 | METRICS_FEATURE_EVENT_NOTIFICATION, |
| 104 | "ListenerEventRate"); |
| 105 | |
| 106 | // |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 107 | // Local state for keeping track of locally discovered events so we can |
| 108 | // cleanup properly when a Switch or Port is removed. |
| 109 | // |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 110 | // We keep all Port, (incoming) Link and Host events per Switch DPID: |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 111 | // - If a switch goes down, we remove all corresponding Port, Link and |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 112 | // Host events. |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 113 | // - 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] | 114 | // and Host events discovered by this instance. |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 115 | // |
| 116 | // How to handle side-effect of remote events. |
| 117 | // - Remote Port Down event -> Link Down |
| 118 | // Not handled. (XXX Shouldn't it be removed from discovered.. Map) |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 119 | // - Remote Host Added -> lose ownership of Host) |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 120 | // Not handled. (XXX Shouldn't it be removed from discovered.. Map) |
| 121 | // |
| 122 | // XXX Domain knowledge based invariant maintenance should be moved to |
| 123 | // driver module, since the invariant may be different on optical, etc. |
| 124 | // |
| 125 | // What happens on leadership change? |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 126 | // - Probably should: remove from discovered.. Maps, but not send DELETE |
| 127 | // events |
| 128 | // XXX Switch/Port can be rediscovered by new leader, but Link, Host? |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 129 | // - Current: There is no way to recognize leadership change? |
| 130 | // ZookeeperRegistry.requestControl(long, ControlChangeCallback) |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 131 | // is the only way to register listener, and it allows only one |
| 132 | // listener, which is already used by Controller class. |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 133 | // |
| 134 | // FIXME Replace with concurrent variant. |
| 135 | // #removeSwitchDiscoveryEvent(SwitchEvent) runs in different thread. |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 136 | // |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 137 | private Map<Dpid, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | new HashMap<>(); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 139 | private Map<Dpid, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | new HashMap<>(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 141 | private Map<Dpid, Map<ByteBuffer, HostEvent>> discoveredAddedHostEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 143 | |
| 144 | // |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 145 | // Local state for keeping the last ADD Mastership Event entries. |
| 146 | // TODO: In the future, we might have to keep this state somewhere else. |
| 147 | // |
| 148 | private Map<ByteBuffer, MastershipEvent> lastAddMastershipEvents = |
| 149 | new HashMap<>(); |
| 150 | |
| 151 | // |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 152 | // Local state for keeping track of the application event notifications |
| 153 | // |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 154 | // - Queue of events, which will be dispatched to local listeners |
| 155 | // on next notification. |
Yuta HIGUCHI | 703696c | 2014-06-25 20:36:45 -0700 | [diff] [blame] | 156 | |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 157 | private List<MastershipEvent> apiAddedMastershipEvents = |
| 158 | new LinkedList<>(); |
| 159 | private List<MastershipEvent> apiRemovedMastershipEvents = |
| 160 | new LinkedList<>(); |
Yuta HIGUCHI | 703696c | 2014-06-25 20:36:45 -0700 | [diff] [blame] | 161 | private List<SwitchEvent> apiAddedSwitchEvents = new LinkedList<>(); |
| 162 | private List<SwitchEvent> apiRemovedSwitchEvents = new LinkedList<>(); |
| 163 | private List<PortEvent> apiAddedPortEvents = new LinkedList<>(); |
| 164 | private List<PortEvent> apiRemovedPortEvents = new LinkedList<>(); |
| 165 | private List<LinkEvent> apiAddedLinkEvents = new LinkedList<>(); |
| 166 | private List<LinkEvent> apiRemovedLinkEvents = new LinkedList<>(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 167 | private List<HostEvent> apiAddedHostEvents = new LinkedList<>(); |
| 168 | private List<HostEvent> apiRemovedHostEvents = new LinkedList<>(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 169 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Constructor. |
| 172 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 173 | * @param registryService the Registry Service to use. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 174 | */ |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 175 | public TopologyManager(IControllerRegistryService registryService) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 176 | datastore = new TopologyDatastore(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | this.registryService = registryService; |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 178 | this.eventPreprocessor = |
| 179 | new TopologyEventPreprocessor(registryService); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 180 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 181 | |
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 | * Get the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 184 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 185 | * @return the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 186 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 187 | Topology getTopology() { |
| 188 | return topology; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 191 | /** |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 192 | * Event handler class. |
| 193 | */ |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 194 | class EventHandler extends Thread implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | IEventChannelListener<byte[], TopologyEvent> { |
| 196 | private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents = |
| 197 | new LinkedBlockingQueue<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 198 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Startup processing. |
| 201 | */ |
| 202 | private void startup() { |
| 203 | // |
| 204 | // TODO: Read all state from the database: |
| 205 | // |
| 206 | // Collection<EventEntry<TopologyEvent>> collection = |
| 207 | // readWholeTopologyFromDB(); |
| 208 | // |
| 209 | // For now, as a shortcut we read it from the datagrid |
| 210 | // |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 211 | Collection<TopologyEvent> allTopologyEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 212 | eventChannel.getAllEntries(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 213 | List<EventEntry<TopologyEvent>> events = |
| 214 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 215 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 216 | for (TopologyEvent topologyEvent : allTopologyEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 217 | EventEntry<TopologyEvent> eventEntry = |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 218 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 219 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 220 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 221 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 222 | processEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 223 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 224 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Run the thread. |
| 227 | */ |
| 228 | @Override |
| 229 | public void run() { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 230 | List<EventEntry<TopologyEvent>> events = |
| 231 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 232 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | this.setName("TopologyManager.EventHandler " + this.getId()); |
| 234 | startup(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 235 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 236 | // |
| 237 | // The main loop |
| 238 | // |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 239 | while (true) { |
| 240 | try { |
| 241 | EventEntry<TopologyEvent> eventEntry = |
| 242 | topologyEvents.take(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 243 | events.add(eventEntry); |
| 244 | topologyEvents.drainTo(events); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 245 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 246 | processEvents(events); |
| 247 | events.clear(); |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 248 | } catch (Exception exception) { |
| 249 | log.debug("Exception processing Topology Events: ", |
| 250 | exception); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 251 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 254 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Process all topology events. |
| 257 | * |
| 258 | * @param events the events to process. |
| 259 | */ |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 260 | private void processEvents(List<EventEntry<TopologyEvent>> events) { |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 261 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 262 | // Process pending (new) listeners |
| 263 | // |
| 264 | processPendingListeners(); |
| 265 | |
| 266 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 267 | // Pre-process the events |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 268 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 269 | events = eventPreprocessor.processEvents(events); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 270 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 271 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 272 | // Lock the topology while it is modified |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 273 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 274 | topology.acquireWriteLock(); |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 275 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | try { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 277 | // Apply the events |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 278 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 279 | // NOTE: The events are suppose to be in the proper order |
| 280 | // to naturally build and update the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 281 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 282 | for (EventEntry<TopologyEvent> event : events) { |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 283 | // Ignore NO-OP events |
| 284 | if (event.isNoop()) { |
| 285 | continue; |
| 286 | } |
| 287 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 288 | TopologyEvent topologyEvent = event.eventData(); |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 289 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 290 | // Get the event itself |
| 291 | MastershipEvent mastershipEvent = |
| 292 | topologyEvent.getMastershipEvent(); |
| 293 | SwitchEvent switchEvent = topologyEvent.getSwitchEvent(); |
| 294 | PortEvent portEvent = topologyEvent.getPortEvent(); |
| 295 | LinkEvent linkEvent = topologyEvent.getLinkEvent(); |
| 296 | HostEvent hostEvent = topologyEvent.getHostEvent(); |
| 297 | boolean wasAdded = false; |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 298 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 299 | // |
| 300 | // Extract the events |
| 301 | // |
| 302 | switch (event.eventType()) { |
| 303 | case ENTRY_ADD: |
| 304 | if (mastershipEvent != null) { |
| 305 | wasAdded = addMastershipEvent(mastershipEvent); |
| 306 | } |
| 307 | if (switchEvent != null) { |
| 308 | wasAdded = addSwitch(switchEvent); |
| 309 | } |
| 310 | if (portEvent != null) { |
| 311 | wasAdded = addPort(portEvent); |
| 312 | } |
| 313 | if (linkEvent != null) { |
| 314 | wasAdded = addLink(linkEvent); |
| 315 | } |
| 316 | if (hostEvent != null) { |
| 317 | wasAdded = addHost(hostEvent); |
| 318 | } |
| 319 | // If the item wasn't added, probably it was reordered |
| 320 | if (!wasAdded) { |
| 321 | ByteBuffer id = topologyEvent.getIDasByteBuffer(); |
| 322 | eventPreprocessor.reorderedEvents.put(id, topologyEvent); |
| 323 | } |
| 324 | break; |
| 325 | case ENTRY_REMOVE: |
| 326 | if (mastershipEvent != null) { |
| 327 | removeMastershipEvent(mastershipEvent); |
| 328 | } |
| 329 | if (switchEvent != null) { |
| 330 | removeSwitch(switchEvent); |
| 331 | } |
| 332 | if (portEvent != null) { |
| 333 | removePort(portEvent); |
| 334 | } |
| 335 | if (linkEvent != null) { |
| 336 | removeLink(linkEvent); |
| 337 | } |
| 338 | if (hostEvent != null) { |
| 339 | removeHost(hostEvent); |
| 340 | } |
| 341 | break; |
| 342 | default: |
| 343 | log.error("Unknown topology event {}", |
| 344 | event.eventType()); |
| 345 | } |
| 346 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 347 | } finally { |
| 348 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 349 | // Topology modifications completed: Release the lock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 350 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 351 | topology.releaseWriteLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 352 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 353 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 354 | // |
| 355 | // Dispatch the Topology Notification Events to the applications |
| 356 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 357 | dispatchTopologyEvents(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 358 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 359 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 360 | /** |
| 361 | * Receive a notification that an entry is added. |
| 362 | * |
| 363 | * @param value the value for the entry. |
| 364 | */ |
| 365 | @Override |
| 366 | public void entryAdded(TopologyEvent value) { |
| 367 | EventEntry<TopologyEvent> eventEntry = |
| 368 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 369 | value); |
| 370 | topologyEvents.add(eventEntry); |
| 371 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 372 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 373 | /** |
| 374 | * Receive a notification that an entry is removed. |
| 375 | * |
| 376 | * @param value the value for the entry. |
| 377 | */ |
| 378 | @Override |
| 379 | public void entryRemoved(TopologyEvent value) { |
| 380 | EventEntry<TopologyEvent> eventEntry = |
| 381 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 382 | value); |
| 383 | topologyEvents.add(eventEntry); |
| 384 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 385 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 386 | /** |
| 387 | * Receive a notification that an entry is updated. |
| 388 | * |
| 389 | * @param value the value for the entry. |
| 390 | */ |
| 391 | @Override |
| 392 | public void entryUpdated(TopologyEvent value) { |
| 393 | // NOTE: The ADD and UPDATE events are processed in same way |
| 394 | entryAdded(value); |
| 395 | } |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * Informs the event handler that a new listener has been added, |
| 399 | * and that listener expects the first event to be a snapshot of the |
| 400 | * current topology. |
| 401 | */ |
| 402 | void listenerAdded() { |
| 403 | // |
| 404 | // Generate a NO-OP event so the Event Handler processing can be |
| 405 | // triggered to generate in-order a snapshot of the current |
| 406 | // topology. |
| 407 | // TODO: This is a hack. |
| 408 | // |
| 409 | EventEntry<TopologyEvent> eventEntry = EventEntry.makeNoop(); |
| 410 | topologyEvents.add(eventEntry); |
| 411 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Startup processing. |
| 416 | * |
| 417 | * @param datagridService the datagrid service to use. |
| 418 | */ |
| 419 | void startup(IDatagridService datagridService) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 420 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 421 | eventHandler, |
| 422 | byte[].class, |
| 423 | TopologyEvent.class); |
| 424 | eventHandler.start(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 427 | /** |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 428 | * Adds a listener for topology events. |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 429 | * |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 430 | * @param listener the listener to add. |
| 431 | * @param startFromSnapshot if true, and if the topology is not |
| 432 | * empty, the first event should be a snapshot of the current topology. |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 433 | */ |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 434 | void addListener(ITopologyListener listener, boolean startFromSnapshot) { |
| 435 | if (startFromSnapshot) { |
| 436 | newTopologyListeners.addIfAbsent(listener); |
| 437 | eventHandler.listenerAdded(); |
| 438 | } else { |
| 439 | topologyListeners.addIfAbsent(listener); |
| 440 | } |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 444 | * Removes a listener for topology events. The listener will no longer |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 445 | * receive topology events after this call. |
| 446 | * |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 447 | * @param listener the listener to remove. |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 448 | */ |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 449 | void removeListener(ITopologyListener listener) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 450 | topologyListeners.remove(listener); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 451 | newTopologyListeners.remove(listener); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Processes pending (new) listeners. |
| 456 | * <p> |
| 457 | * During the processing, we dispatch Topology Snapshot Events to new |
| 458 | * listeners. |
| 459 | */ |
| 460 | private void processPendingListeners() { |
| 461 | if (newTopologyListeners.isEmpty()) { |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | // |
| 466 | // Create the Topology Snapshot Event |
| 467 | // |
| 468 | TopologyEvents events = null; |
Pavlin Radoslavov | 4163364 | 2014-08-11 14:24:52 -0700 | [diff] [blame] | 469 | Collection<MastershipEvent> mastershipEvents = |
| 470 | lastAddMastershipEvents.values(); |
| 471 | Collection<SwitchEvent> switchEvents = topology.getAllSwitchEvents(); |
| 472 | Collection<PortEvent> portEvents = topology.getAllPortEvents(); |
| 473 | Collection<LinkEvent> linkEvents = topology.getAllLinkEvents(); |
| 474 | Collection<HostEvent> hostEvents = topology.getAllHostEvents(); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 475 | if (!(mastershipEvents.isEmpty() && |
| 476 | switchEvents.isEmpty() && |
| 477 | portEvents.isEmpty() && |
| 478 | linkEvents.isEmpty() && |
| 479 | hostEvents.isEmpty())) { |
| 480 | events = new TopologyEvents(mastershipEvents, |
| 481 | switchEvents, |
| 482 | portEvents, |
| 483 | linkEvents, |
| 484 | hostEvents); |
| 485 | } |
| 486 | |
| 487 | // |
| 488 | // Dispatch Snapshot Event to each new listener, and keep track |
| 489 | // of each processed listener. |
| 490 | // |
| 491 | // NOTE: We deliver the event only if it is not empty. |
| 492 | // NOTE: We need to execute the loop so we can properly |
| 493 | // move the new listeners together with the older listeners. |
| 494 | // |
| 495 | List<ITopologyListener> processedListeners = new LinkedList<>(); |
| 496 | for (ITopologyListener listener : newTopologyListeners) { |
| 497 | processedListeners.add(listener); |
| 498 | // Move the new listener together with the rest of the listeners |
| 499 | topologyListeners.addIfAbsent(listener); |
| 500 | |
| 501 | // Dispatch the event |
| 502 | if (events != null) { |
| 503 | listener.topologyEvents(events); |
| 504 | } |
| 505 | } |
| 506 | newTopologyListeners.removeAll(processedListeners); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 510 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 511 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 512 | private void dispatchTopologyEvents() { |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 513 | if (apiAddedMastershipEvents.isEmpty() && |
| 514 | apiRemovedMastershipEvents.isEmpty() && |
| 515 | apiAddedSwitchEvents.isEmpty() && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 516 | apiRemovedSwitchEvents.isEmpty() && |
| 517 | apiAddedPortEvents.isEmpty() && |
| 518 | apiRemovedPortEvents.isEmpty() && |
| 519 | apiAddedLinkEvents.isEmpty() && |
| 520 | apiRemovedLinkEvents.isEmpty() && |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 521 | apiAddedHostEvents.isEmpty() && |
| 522 | apiRemovedHostEvents.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 523 | return; // No events to dispatch |
| 524 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 525 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 526 | if (log.isDebugEnabled()) { |
| 527 | // |
| 528 | // Debug statements |
| 529 | // TODO: Those statements should be removed in the future |
| 530 | // |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 531 | for (MastershipEvent mastershipEvent : apiAddedMastershipEvents) { |
| 532 | log.debug("Dispatch Topology Event: ADDED {}", |
| 533 | mastershipEvent); |
| 534 | } |
| 535 | for (MastershipEvent mastershipEvent : apiRemovedMastershipEvents) { |
| 536 | log.debug("Dispatch Topology Event: REMOVED {}", |
| 537 | mastershipEvent); |
| 538 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 539 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 540 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 541 | } |
| 542 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 543 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 544 | } |
| 545 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 546 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 547 | } |
| 548 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 549 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 550 | } |
| 551 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 552 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 553 | } |
| 554 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 555 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 556 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 557 | for (HostEvent hostEvent : apiAddedHostEvents) { |
| 558 | log.debug("Dispatch Topology Event: ADDED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 559 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 560 | for (HostEvent hostEvent : apiRemovedHostEvents) { |
| 561 | log.debug("Dispatch Topology Event: REMOVED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 562 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 564 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 565 | // |
| 566 | // Update the metrics |
| 567 | // |
| 568 | long totalEvents = |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 569 | apiAddedMastershipEvents.size() + apiRemovedMastershipEvents.size() + |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 570 | apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() + |
| 571 | apiAddedPortEvents.size() + apiRemovedPortEvents.size() + |
| 572 | apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() + |
| 573 | apiAddedHostEvents.size() + apiRemovedHostEvents.size(); |
| 574 | this.listenerEventRate.mark(totalEvents); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 575 | this.lastEventTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 576 | |
| 577 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 578 | // Allocate the events to deliver. |
| 579 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 580 | TopologyEvents events = new TopologyEvents( |
Pavlin Radoslavov | 4163364 | 2014-08-11 14:24:52 -0700 | [diff] [blame] | 581 | apiAddedMastershipEvents, |
| 582 | apiRemovedMastershipEvents, |
| 583 | apiAddedSwitchEvents, |
| 584 | apiRemovedSwitchEvents, |
| 585 | apiAddedPortEvents, |
| 586 | apiRemovedPortEvents, |
| 587 | apiAddedLinkEvents, |
| 588 | apiRemovedLinkEvents, |
| 589 | apiAddedHostEvents, |
| 590 | apiRemovedHostEvents); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 591 | |
| 592 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 593 | // Deliver the events |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 594 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 595 | for (ITopologyListener listener : this.topologyListeners) { |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 596 | listener.topologyEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 597 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 598 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 599 | // |
| 600 | // Cleanup |
| 601 | // |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 602 | apiAddedMastershipEvents.clear(); |
| 603 | apiRemovedMastershipEvents.clear(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 604 | apiAddedSwitchEvents.clear(); |
| 605 | apiRemovedSwitchEvents.clear(); |
| 606 | apiAddedPortEvents.clear(); |
| 607 | apiRemovedPortEvents.clear(); |
| 608 | apiAddedLinkEvents.clear(); |
| 609 | apiRemovedLinkEvents.clear(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 610 | apiAddedHostEvents.clear(); |
| 611 | apiRemovedHostEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /** |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 615 | * Mastership updated event. |
| 616 | * |
| 617 | * @param mastershipEvent the mastership event. |
| 618 | */ |
| 619 | @Override |
| 620 | public void putSwitchMastershipEvent(MastershipEvent mastershipEvent) { |
| 621 | // Send out notification |
| 622 | TopologyEvent topologyEvent = |
| 623 | new TopologyEvent(mastershipEvent, |
| 624 | registryService.getOnosInstanceId()); |
| 625 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Mastership removed event. |
| 630 | * |
| 631 | * @param mastershipEvent the mastership event. |
| 632 | */ |
| 633 | @Override |
| 634 | public void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) { |
| 635 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 636 | TopologyEvent topologyEvent = |
| 637 | new TopologyEvent(mastershipEvent, |
| 638 | registryService.getOnosInstanceId()); |
| 639 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 643 | * Switch discovered event. |
| 644 | * |
| 645 | * @param switchEvent the switch event. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 646 | * @param portEvents the corresponding port events for the switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 647 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 648 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 649 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 650 | Collection<PortEvent> portEvents) { |
| 651 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 652 | log.debug("Sending add switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 653 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 654 | TopologyEvent topologyEvent = |
| 655 | new TopologyEvent(switchEvent, |
| 656 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 657 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 658 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 659 | // Send out notification for each port |
| 660 | for (PortEvent portEvent : portEvents) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 661 | log.debug("Sending add port: {}", portEvent); |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 662 | topologyEvent = |
| 663 | new TopologyEvent(portEvent, |
| 664 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 665 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 666 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 667 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 668 | // |
| 669 | // Keep track of the added ports |
| 670 | // |
| 671 | // Get the old Port Events |
| 672 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 673 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 674 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 675 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 676 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 677 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 678 | // Store the new Port Events in the local cache |
| 679 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 680 | for (PortEvent portEvent : portEvents) { |
| 681 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 682 | newPortEvents.put(id, portEvent); |
| 683 | } |
| 684 | discoveredAddedPortEvents.put(switchEvent.getDpid(), |
| 685 | newPortEvents); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 686 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 687 | // |
| 688 | // Extract the removed ports |
| 689 | // |
| 690 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 691 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 692 | ByteBuffer key = entry.getKey(); |
| 693 | PortEvent portEvent = entry.getValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 694 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 695 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 696 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 700 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 701 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 702 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 703 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 706 | /** |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 707 | * {@inheritDoc} |
| 708 | * <p/> |
| 709 | * Called by {@link TopologyPublisher.SwitchCleanup} thread. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 710 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 711 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 712 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 713 | TopologyEvent topologyEvent; |
| 714 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 715 | // Get the old Port Events |
| 716 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 717 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 718 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 719 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 720 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 721 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 722 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 723 | log.debug("Sending remove switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 724 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 725 | topologyEvent = |
| 726 | new TopologyEvent(switchEvent, |
| 727 | registryService.getOnosInstanceId()); |
| 728 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 729 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 730 | // |
| 731 | // Send out notification for each port. |
| 732 | // |
| 733 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 734 | // because it will attempt to remove the port from the database, |
| 735 | // and the deactiveSwitch() call above already removed all ports. |
| 736 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 737 | for (PortEvent portEvent : oldPortEvents.values()) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 738 | log.debug("Sending remove port:", portEvent); |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 739 | topologyEvent = |
| 740 | new TopologyEvent(portEvent, |
| 741 | registryService.getOnosInstanceId()); |
| 742 | eventChannel.removeEntry(topologyEvent.getID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 743 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 744 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 745 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 746 | // Cleanup for each link |
| 747 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 748 | discoveredAddedLinkEvents.get(switchEvent.getDpid()); |
| 749 | if (oldLinkEvents != null) { |
| 750 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 751 | removeLinkDiscoveryEvent(linkEvent); |
| 752 | } |
| 753 | discoveredAddedLinkEvents.remove(switchEvent.getDpid()); |
| 754 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 755 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 756 | // Cleanup for each host |
| 757 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 758 | discoveredAddedHostEvents.get(switchEvent.getDpid()); |
| 759 | if (oldHostEvents != null) { |
| 760 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 761 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 762 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 763 | discoveredAddedHostEvents.remove(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 768 | /** |
| 769 | * Port discovered event. |
| 770 | * |
| 771 | * @param portEvent the port event. |
| 772 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 773 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 774 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 775 | if (datastore.addPort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 776 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 777 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 778 | TopologyEvent topologyEvent = |
| 779 | new TopologyEvent(portEvent, |
| 780 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 781 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 782 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 783 | // Store the new Port Event in the local cache |
| 784 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 785 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 786 | if (oldPortEvents == null) { |
| 787 | oldPortEvents = new HashMap<>(); |
| 788 | discoveredAddedPortEvents.put(portEvent.getDpid(), |
| 789 | oldPortEvents); |
| 790 | } |
| 791 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 792 | oldPortEvents.put(id, portEvent); |
| 793 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 796 | /** |
| 797 | * Port removed event. |
| 798 | * |
| 799 | * @param portEvent the port event. |
| 800 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 801 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 802 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 803 | if (datastore.deactivatePort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 804 | log.debug("Sending remove port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 805 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 806 | TopologyEvent topologyEvent = |
| 807 | new TopologyEvent(portEvent, |
| 808 | registryService.getOnosInstanceId()); |
| 809 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 810 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 811 | // Cleanup the Port Event from the local cache |
| 812 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 813 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 814 | if (oldPortEvents != null) { |
| 815 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 816 | oldPortEvents.remove(id); |
| 817 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 818 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 819 | // Cleanup for the incoming link |
| 820 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 821 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 822 | if (oldLinkEvents != null) { |
| 823 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 824 | if (linkEvent.getDst().equals(portEvent.getSwitchPort())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 825 | removeLinkDiscoveryEvent(linkEvent); |
| 826 | // XXX If we change our model to allow multiple Link on |
| 827 | // a Port, this loop must be fixed to allow continuing. |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 832 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 833 | // Cleanup for the connected hosts |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 834 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 835 | List<HostEvent> removedHostEvents = new LinkedList<>(); |
| 836 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 837 | discoveredAddedHostEvents.get(portEvent.getDpid()); |
| 838 | if (oldHostEvents != null) { |
| 839 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 840 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 841 | if (swp.equals(portEvent.getSwitchPort())) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 842 | removedHostEvents.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 846 | for (HostEvent hostEvent : removedHostEvents) { |
| 847 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 848 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 853 | /** |
| 854 | * Link discovered event. |
| 855 | * |
| 856 | * @param linkEvent the link event. |
| 857 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 858 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 859 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 860 | if (datastore.addLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 861 | log.debug("Sending add link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 862 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 863 | TopologyEvent topologyEvent = |
| 864 | new TopologyEvent(linkEvent, |
| 865 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 866 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 867 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 868 | // Store the new Link Event in the local cache |
| 869 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 870 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 871 | if (oldLinkEvents == null) { |
| 872 | oldLinkEvents = new HashMap<>(); |
| 873 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 874 | oldLinkEvents); |
| 875 | } |
| 876 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 877 | oldLinkEvents.put(id, linkEvent); |
| 878 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 881 | /** |
| 882 | * Link removed event. |
| 883 | * |
| 884 | * @param linkEvent the link event. |
| 885 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 886 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 887 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 888 | if (datastore.removeLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 889 | log.debug("Sending remove link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 890 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 891 | TopologyEvent topologyEvent = |
| 892 | new TopologyEvent(linkEvent, |
| 893 | registryService.getOnosInstanceId()); |
| 894 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 895 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 896 | // Cleanup the Link Event from the local cache |
| 897 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 898 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 899 | if (oldLinkEvents != null) { |
| 900 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 901 | oldLinkEvents.remove(id); |
| 902 | } |
| 903 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 904 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 905 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 906 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 907 | * Host discovered event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 908 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 909 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 910 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 911 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 912 | public void putHostDiscoveryEvent(HostEvent hostEvent) { |
| 913 | if (datastore.addHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 914 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 915 | TopologyEvent topologyEvent = |
| 916 | new TopologyEvent(hostEvent, |
| 917 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 918 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 919 | log.debug("Put the host info into the cache of the topology. mac {}", |
| 920 | hostEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 921 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 922 | // Store the new Host Event in the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 923 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 924 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 925 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 926 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 927 | if (oldHostEvents == null) { |
| 928 | oldHostEvents = new HashMap<>(); |
| 929 | discoveredAddedHostEvents.put(swp.getDpid(), |
| 930 | oldHostEvents); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 931 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 932 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 933 | oldHostEvents.put(id, hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 936 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 937 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 938 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 939 | * Host removed event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 940 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 941 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 942 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 943 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 944 | public void removeHostDiscoveryEvent(HostEvent hostEvent) { |
| 945 | if (datastore.removeHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 946 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 947 | TopologyEvent topologyEvent = |
| 948 | new TopologyEvent(hostEvent, |
| 949 | registryService.getOnosInstanceId()); |
| 950 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 951 | log.debug("Remove the host info into the cache of the topology. mac {}", |
| 952 | hostEvent.getMac()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 953 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 954 | // Cleanup the Host Event from the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 955 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 956 | ByteBuffer id = ByteBuffer.wrap(hostEvent.getID()); |
| 957 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 958 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 959 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 960 | if (oldHostEvents != null) { |
| 961 | oldHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 965 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 966 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 967 | // |
| 968 | // Methods to update topology replica |
| 969 | // |
| 970 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 971 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 972 | * Adds Switch Mastership event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 973 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 974 | * @param mastershipEvent the MastershipEvent to process. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 975 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 976 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 977 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 978 | private boolean addMastershipEvent(MastershipEvent mastershipEvent) { |
| 979 | log.debug("Added Mastership event {}", mastershipEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 980 | lastAddMastershipEvents.put(mastershipEvent.getIDasByteBuffer(), |
| 981 | mastershipEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 982 | apiAddedMastershipEvents.add(mastershipEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 983 | return true; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 987 | * Removes Switch Mastership event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 988 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 989 | * @param mastershipEvent the MastershipEvent to process. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 990 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 991 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 992 | private void removeMastershipEvent(MastershipEvent mastershipEvent) { |
| 993 | log.debug("Removed Mastership event {}", mastershipEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 994 | lastAddMastershipEvents.remove(mastershipEvent.getIDasByteBuffer()); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 995 | apiRemovedMastershipEvents.add(mastershipEvent); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 999 | * Adds a switch to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1000 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1001 | * @param switchEvent the SwitchEvent with the switch to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1002 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1003 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1004 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1005 | private boolean addSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1006 | if (log.isDebugEnabled()) { |
| 1007 | SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid()); |
| 1008 | if (sw != null) { |
| 1009 | log.debug("Update {}", switchEvent); |
| 1010 | } else { |
| 1011 | log.debug("Added {}", switchEvent); |
| 1012 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1013 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1014 | topology.putSwitch(switchEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1015 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1016 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1019 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1020 | * Removes a switch from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1021 | * <p/> |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1022 | * It will call {@link #removePort(PortEvent)} for each ports on this |
| 1023 | * switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1024 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1025 | * @param switchEvent the SwitchEvent with the switch to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1026 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1027 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1028 | private void removeSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1029 | final Dpid dpid = switchEvent.getDpid(); |
| 1030 | |
| 1031 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 1032 | if (swInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1033 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 1034 | return; |
| 1035 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1036 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1037 | // |
| 1038 | // Remove all Ports on the Switch |
| 1039 | // |
| 1040 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1041 | for (Port port : topology.getPorts(dpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1042 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 1043 | port, switchEvent); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 1044 | PortEvent portEvent = new PortEvent(port.getSwitchPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1045 | portsToRemove.add(portEvent); |
| 1046 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1047 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1048 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1049 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1050 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1051 | log.debug("Removed {}", swInTopo); |
| 1052 | topology.removeSwitch(dpid); |
| 1053 | apiRemovedSwitchEvents.add(swInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1056 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1057 | * Adds a port to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1058 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1059 | * @param portEvent the PortEvent with the port to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1060 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1061 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1062 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1063 | private boolean addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1064 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1065 | if (sw == null) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1066 | // Reordered event |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1067 | log.debug("{} reordered because switch is null", portEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1068 | return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1069 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1070 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1071 | if (log.isDebugEnabled()) { |
| 1072 | PortEvent port = topology.getPortEvent(portEvent.getSwitchPort()); |
| 1073 | if (port != null) { |
| 1074 | log.debug("Update {}", portEvent); |
| 1075 | } else { |
| 1076 | log.debug("Added {}", portEvent); |
| 1077 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1078 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1079 | topology.putPort(portEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1080 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1081 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1084 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1085 | * Removes a port from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1086 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1087 | * It will remove attachment points from each hosts on this port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1088 | * and call {@link #removeLink(LinkEvent)} for each links on this port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1089 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1090 | * @param portEvent the PortEvent with the port to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1091 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1092 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1093 | private void removePort(PortEvent portEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1094 | SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1095 | if (sw == null) { |
| 1096 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 1097 | portEvent); |
| 1098 | return; |
| 1099 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1100 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1101 | final SwitchPort switchPort = portEvent.getSwitchPort(); |
| 1102 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
| 1103 | if (portInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1104 | log.warn("Port {} already removed, ignoring", portEvent); |
| 1105 | return; |
| 1106 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1107 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1108 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1109 | // Remove all Host attachment points bound to this Port |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1110 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1111 | List<HostEvent> hostsToUpdate = new ArrayList<>(); |
| 1112 | for (Host host : topology.getHosts(switchPort)) { |
| 1113 | log.debug("Removing Host {} on Port {}", host, portInTopo); |
| 1114 | HostEvent hostEvent = topology.getHostEvent(host.getMacAddress()); |
| 1115 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1116 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1117 | for (HostEvent hostEvent : hostsToUpdate) { |
| 1118 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 1119 | newHostEvent.removeAttachmentPoint(switchPort); |
| 1120 | newHostEvent.freeze(); |
| 1121 | |
| 1122 | // TODO should this event be fired inside #addHost? |
| 1123 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 1124 | // No more attachment point left -> remove Host |
| 1125 | removeHost(hostEvent); |
| 1126 | } else { |
| 1127 | // Update Host |
| 1128 | addHost(newHostEvent); |
| 1129 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1130 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1132 | // |
| 1133 | // Remove all Links connected to the Port |
| 1134 | // |
| 1135 | Set<Link> links = new HashSet<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1136 | links.addAll(topology.getOutgoingLinks(switchPort)); |
| 1137 | links.addAll(topology.getIncomingLinks(switchPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1138 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1139 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1140 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1141 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1142 | LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple()); |
| 1143 | if (linkEvent != null) { |
| 1144 | log.debug("Removing Link {} on Port {}", link, portInTopo); |
| 1145 | removeLink(linkEvent); |
| 1146 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1147 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1148 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1149 | // Remove the Port from Topology |
| 1150 | log.debug("Removed {}", portInTopo); |
| 1151 | topology.removePort(switchPort); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1152 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1153 | apiRemovedPortEvents.add(portInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1156 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1157 | * Adds a link to the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1158 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1159 | * It will remove attachment points from each hosts using the same ports. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1160 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1161 | * @param linkEvent the LinkEvent with the link to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1162 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1163 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1164 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1165 | private boolean addLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1166 | PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc()); |
| 1167 | PortEvent dstPort = topology.getPortEvent(linkEvent.getDst()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1168 | if ((srcPort == null) || (dstPort == null)) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1169 | // Reordered event |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1170 | log.debug("{} reordered because {} port is null", linkEvent, |
| 1171 | (srcPort == null) ? "src" : "dst"); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1172 | return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1173 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1174 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1175 | // |
| 1176 | // XXX domain knowledge: Sanity check: Port cannot have both Link and |
| 1177 | // Host. |
| 1178 | // |
| 1179 | // FIXME: Potentially local replica may not be up-to-date yet due to |
| 1180 | // Hazelcast delay. |
| 1181 | // FIXME: May need to manage local truth and use them instead. |
| 1182 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1183 | if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) { |
| 1184 | // Only check for existing Host when adding new Link. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1185 | // Remove all Hosts attached to the ports on both ends |
| 1186 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1187 | Set<HostEvent> hostsToUpdate = |
| 1188 | new TreeSet<>(new Comparator<HostEvent>() { |
| 1189 | // Comparison only using ID(=MAC) |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1190 | @Override |
| 1191 | public int compare(HostEvent o1, HostEvent o2) { |
| 1192 | return Long.compare(o1.getMac().toLong(), o2.getMac().toLong()); |
| 1193 | } |
| 1194 | }); |
| 1195 | |
| 1196 | List<SwitchPort> portsToCheck = Arrays.asList( |
| 1197 | srcPort.getSwitchPort(), |
| 1198 | dstPort.getSwitchPort()); |
| 1199 | |
| 1200 | // Enumerate Host which needs to be updated by this Link add event |
| 1201 | for (SwitchPort port : portsToCheck) { |
| 1202 | for (Host host : topology.getHosts(port)) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1203 | log.error("Host {} on Port {} should have been removed prior to adding Link {}", |
| 1204 | host, port, linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1205 | |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1206 | HostEvent hostEvent = |
| 1207 | topology.getHostEvent(host.getMacAddress()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1208 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1211 | // Remove attachment point from them |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1212 | for (HostEvent hostEvent : hostsToUpdate) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1213 | // Remove port from attachment point and update |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1214 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 1215 | newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort()); |
| 1216 | newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort()); |
| 1217 | newHostEvent.freeze(); |
| 1218 | |
| 1219 | // TODO should this event be fired inside #addHost? |
| 1220 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 1221 | // No more attachment point left -> remove Host |
| 1222 | removeHost(hostEvent); |
| 1223 | } else { |
| 1224 | // Update Host |
| 1225 | addHost(newHostEvent); |
| 1226 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1227 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1228 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1229 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1230 | if (log.isDebugEnabled()) { |
| 1231 | LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple()); |
| 1232 | if (link != null) { |
| 1233 | log.debug("Update {}", linkEvent); |
| 1234 | } else { |
| 1235 | log.debug("Added {}", linkEvent); |
| 1236 | } |
| 1237 | } |
| 1238 | topology.putLink(linkEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1239 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1240 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1243 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1244 | * Removes a link from the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1245 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1246 | * @param linkEvent the LinkEvent with the link to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1247 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1248 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1249 | private void removeLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1250 | Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1251 | linkEvent.getSrc().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1252 | if (srcPort == null) { |
| 1253 | log.warn("Src Port for Link {} already removed, ignoring", |
| 1254 | linkEvent); |
| 1255 | return; |
| 1256 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1257 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1258 | Port dstPort = topology.getPort(linkEvent.getDst().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1259 | linkEvent.getDst().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1260 | if (dstPort == null) { |
| 1261 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1262 | linkEvent); |
| 1263 | return; |
| 1264 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1265 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1266 | LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(), |
| 1267 | linkEvent.getType()); |
| 1268 | if (linkInTopo == null) { |
| 1269 | log.warn("Link {} already removed, ignoring", linkEvent); |
| 1270 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1271 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1272 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1273 | if (log.isDebugEnabled()) { |
| 1274 | // only do sanity check on debug level |
| 1275 | |
| 1276 | Link linkIn = dstPort.getIncomingLink(linkEvent.getType()); |
| 1277 | if (linkIn == null) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1278 | log.warn("Link {} already removed on destination Port", |
| 1279 | linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1280 | } |
| 1281 | Link linkOut = srcPort.getOutgoingLink(linkEvent.getType()); |
| 1282 | if (linkOut == null) { |
| 1283 | log.warn("Link {} already removed on src Port", linkEvent); |
| 1284 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1285 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1286 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1287 | log.debug("Removed {}", linkInTopo); |
| 1288 | topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType()); |
| 1289 | apiRemovedLinkEvents.add(linkInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1292 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1293 | * Adds a host to the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1294 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1295 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1296 | * TODO: Eventually, we might need to consider reordering |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1297 | * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events |
| 1298 | * on the same port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1299 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1300 | * @param hostEvent the HostEvent with the host to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1301 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1302 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1303 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1304 | private boolean addHost(HostEvent hostEvent) { |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1305 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1306 | // TODO Decide how to handle update scenario. |
| 1307 | // If the new HostEvent has less attachment point compared to |
| 1308 | // existing HostEvent, what should the event be? |
| 1309 | // - AddHostEvent with some attachment point removed? (current behavior) |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1310 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1311 | // create unfrozen copy |
| 1312 | // for removing attachment points which already has a link |
| 1313 | HostEvent modifiedHostEvent = new HostEvent(hostEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1314 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1315 | // Verify each attachment point |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1316 | boolean attachmentFound = false; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1317 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 1318 | // XXX domain knowledge: Port must exist before Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1319 | // but this knowledge cannot be pushed down to driver. |
| 1320 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1321 | // Attached Ports must exist |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1322 | Port port = topology.getPort(swp.getDpid(), swp.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1323 | if (port == null) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1324 | log.debug("{} reordered because port {} was not there", |
| 1325 | hostEvent, swp); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1326 | // Reordered event |
| 1327 | return false; // should not continue if re-applying later |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1328 | } |
| 1329 | // Attached Ports must not have Link |
| 1330 | if (port.getOutgoingLink() != null || |
| 1331 | port.getIncomingLink() != null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1332 | log.warn("Link (Out:{},In:{}) exist on the attachment point. " |
| 1333 | + "Ignoring this attachmentpoint ({}) from {}.", |
| 1334 | port.getOutgoingLink(), port.getIncomingLink(), |
| 1335 | swp, modifiedHostEvent); |
| 1336 | // FIXME Should either reject, reorder this HostEvent, |
| 1337 | // or remove attachment point from given HostEvent |
| 1338 | // Removing attachment point from given HostEvent for now. |
| 1339 | modifiedHostEvent.removeAttachmentPoint(swp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1340 | continue; |
| 1341 | } |
| 1342 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1343 | attachmentFound = true; |
| 1344 | } |
| 1345 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1346 | // Update the host in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1347 | if (attachmentFound) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1348 | if (modifiedHostEvent.getAttachmentPoints().isEmpty()) { |
| 1349 | log.warn("No valid attachment point left. Ignoring." |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1350 | + "original: {}, modified: {}", |
| 1351 | hostEvent, modifiedHostEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1352 | // TODO Should we call #removeHost to trigger remove event? |
| 1353 | // only if this call is update. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1354 | return false; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | if (log.isDebugEnabled()) { |
| 1358 | HostEvent host = topology.getHostEvent(hostEvent.getMac()); |
| 1359 | if (host != null) { |
| 1360 | log.debug("Update {}", modifiedHostEvent); |
| 1361 | } else { |
| 1362 | log.debug("Added {}", modifiedHostEvent); |
| 1363 | } |
| 1364 | } |
| 1365 | topology.putHost(modifiedHostEvent.freeze()); |
| 1366 | apiAddedHostEvents.add(modifiedHostEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1367 | return true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1368 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1369 | return false; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1372 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1373 | * Removes a host from the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1374 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1375 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1376 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1377 | * @param hostEvent the Host Event with the host to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1378 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1379 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1380 | private void removeHost(HostEvent hostEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1381 | |
| 1382 | final MACAddress mac = hostEvent.getMac(); |
| 1383 | HostEvent hostInTopo = topology.getHostEvent(mac); |
| 1384 | if (hostInTopo == null) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1385 | log.warn("Host {} already removed, ignoring", hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1386 | return; |
| 1387 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1388 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1389 | log.debug("Removed {}", hostInTopo); |
| 1390 | topology.removeHost(mac); |
| 1391 | apiRemovedHostEvents.add(hostInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1392 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1393 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1394 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1395 | * Read the whole topology from the database. |
| 1396 | * |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1397 | * @return a list of EventEntry-encapsulated Topology Events for |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1398 | * the whole topology. |
| 1399 | */ |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1400 | private List<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
| 1401 | List<EventEntry<TopologyEvent>> events = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1402 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1403 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1404 | // XXX May need to clear whole topology first, depending on |
| 1405 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1406 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1407 | // Add all active switches |
| 1408 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1409 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1410 | continue; |
| 1411 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1412 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1413 | // |
| 1414 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1415 | // Currently, this code is not used, and it might go away in the |
| 1416 | // future. |
| 1417 | // |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1418 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid())); |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1419 | TopologyEvent topologyEvent = |
| 1420 | new TopologyEvent(switchEvent, |
| 1421 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1422 | EventEntry<TopologyEvent> eventEntry = |
| 1423 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1424 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1425 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1426 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1427 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1428 | // Add all active ports |
| 1429 | for (KVPort p : KVPort.getAllPorts()) { |
| 1430 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1431 | continue; |
| 1432 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1433 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1434 | // |
| 1435 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1436 | // Currently, this code is not used, and it might go away in the |
| 1437 | // future. |
| 1438 | // |
| 1439 | PortEvent portEvent = |
| 1440 | new PortEvent(new Dpid(p.getDpid()), |
| 1441 | new PortNumber(p.getNumber().shortValue())); |
| 1442 | TopologyEvent topologyEvent = |
| 1443 | new TopologyEvent(portEvent, |
| 1444 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1445 | EventEntry<TopologyEvent> eventEntry = |
| 1446 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1447 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1448 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1449 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1450 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1451 | for (KVDevice d : KVDevice.getAllDevices()) { |
| 1452 | // |
| 1453 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1454 | // Currently, this code is not used, and it might go away in the |
| 1455 | // future. |
| 1456 | // |
| 1457 | HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac())); |
| 1458 | for (byte[] portId : d.getAllPortIds()) { |
| 1459 | devEvent.addAttachmentPoint( |
| 1460 | new SwitchPort(KVPort.getDpidFromKey(portId), |
| 1461 | KVPort.getNumberFromKey(portId))); |
| 1462 | } |
| 1463 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1464 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1465 | for (KVLink l : KVLink.getAllLinks()) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1466 | // |
| 1467 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1468 | // Currently, this code is not used, and it might go away in the |
| 1469 | // future. |
| 1470 | // |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1471 | LinkEvent linkEvent = new LinkEvent( |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1472 | new SwitchPort(l.getSrc().dpid, l.getSrc().number), |
| 1473 | new SwitchPort(l.getDst().dpid, l.getDst().number)); |
| 1474 | TopologyEvent topologyEvent = |
| 1475 | new TopologyEvent(linkEvent, |
| 1476 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1477 | EventEntry<TopologyEvent> eventEntry = |
| 1478 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1479 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1480 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1481 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1482 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1483 | return events; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1484 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1485 | } |