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; |
| 469 | List<MastershipEvent> mastershipEvents = |
| 470 | new ArrayList<>(lastAddMastershipEvents.values()); |
| 471 | List<SwitchEvent> switchEvents = |
| 472 | new ArrayList<>(topology.getAllSwitchEvents()); |
| 473 | List<PortEvent> portEvents = |
| 474 | new ArrayList<>(topology.getAllPortEvents()); |
| 475 | List<LinkEvent> linkEvents = |
| 476 | new ArrayList<>(topology.getAllLinkEvents()); |
| 477 | List<HostEvent> hostEvents = |
| 478 | new ArrayList<>(topology.getAllHostEvents()); |
| 479 | if (!(mastershipEvents.isEmpty() && |
| 480 | switchEvents.isEmpty() && |
| 481 | portEvents.isEmpty() && |
| 482 | linkEvents.isEmpty() && |
| 483 | hostEvents.isEmpty())) { |
| 484 | events = new TopologyEvents(mastershipEvents, |
| 485 | switchEvents, |
| 486 | portEvents, |
| 487 | linkEvents, |
| 488 | hostEvents); |
| 489 | } |
| 490 | |
| 491 | // |
| 492 | // Dispatch Snapshot Event to each new listener, and keep track |
| 493 | // of each processed listener. |
| 494 | // |
| 495 | // NOTE: We deliver the event only if it is not empty. |
| 496 | // NOTE: We need to execute the loop so we can properly |
| 497 | // move the new listeners together with the older listeners. |
| 498 | // |
| 499 | List<ITopologyListener> processedListeners = new LinkedList<>(); |
| 500 | for (ITopologyListener listener : newTopologyListeners) { |
| 501 | processedListeners.add(listener); |
| 502 | // Move the new listener together with the rest of the listeners |
| 503 | topologyListeners.addIfAbsent(listener); |
| 504 | |
| 505 | // Dispatch the event |
| 506 | if (events != null) { |
| 507 | listener.topologyEvents(events); |
| 508 | } |
| 509 | } |
| 510 | newTopologyListeners.removeAll(processedListeners); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 514 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 515 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 516 | private void dispatchTopologyEvents() { |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 517 | if (apiAddedMastershipEvents.isEmpty() && |
| 518 | apiRemovedMastershipEvents.isEmpty() && |
| 519 | apiAddedSwitchEvents.isEmpty() && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 520 | apiRemovedSwitchEvents.isEmpty() && |
| 521 | apiAddedPortEvents.isEmpty() && |
| 522 | apiRemovedPortEvents.isEmpty() && |
| 523 | apiAddedLinkEvents.isEmpty() && |
| 524 | apiRemovedLinkEvents.isEmpty() && |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 525 | apiAddedHostEvents.isEmpty() && |
| 526 | apiRemovedHostEvents.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 527 | return; // No events to dispatch |
| 528 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 529 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 530 | if (log.isDebugEnabled()) { |
| 531 | // |
| 532 | // Debug statements |
| 533 | // TODO: Those statements should be removed in the future |
| 534 | // |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 535 | for (MastershipEvent mastershipEvent : apiAddedMastershipEvents) { |
| 536 | log.debug("Dispatch Topology Event: ADDED {}", |
| 537 | mastershipEvent); |
| 538 | } |
| 539 | for (MastershipEvent mastershipEvent : apiRemovedMastershipEvents) { |
| 540 | log.debug("Dispatch Topology Event: REMOVED {}", |
| 541 | mastershipEvent); |
| 542 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 543 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 544 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 545 | } |
| 546 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 547 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 548 | } |
| 549 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 550 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 551 | } |
| 552 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 553 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 554 | } |
| 555 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 556 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 557 | } |
| 558 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 559 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 560 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 561 | for (HostEvent hostEvent : apiAddedHostEvents) { |
| 562 | log.debug("Dispatch Topology Event: ADDED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 563 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 564 | for (HostEvent hostEvent : apiRemovedHostEvents) { |
| 565 | log.debug("Dispatch Topology Event: REMOVED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 566 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 567 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 568 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 569 | // |
| 570 | // Update the metrics |
| 571 | // |
| 572 | long totalEvents = |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 573 | apiAddedMastershipEvents.size() + apiRemovedMastershipEvents.size() + |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 574 | apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() + |
| 575 | apiAddedPortEvents.size() + apiRemovedPortEvents.size() + |
| 576 | apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() + |
| 577 | apiAddedHostEvents.size() + apiRemovedHostEvents.size(); |
| 578 | this.listenerEventRate.mark(totalEvents); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 579 | this.lastEventTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 580 | |
| 581 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame^] | 582 | // Allocate the events to deliver. |
| 583 | // |
| 584 | // TODO: We could avoid the extra list allocation and copy |
| 585 | // by using directly the original list. However, during |
| 586 | // the cleanup below, we should create new LinkedList objects |
| 587 | // instead of using clear() |
| 588 | // |
| 589 | TopologyEvents events = new TopologyEvents( |
| 590 | new ArrayList<>(apiAddedMastershipEvents), |
| 591 | new ArrayList<>(apiRemovedMastershipEvents), |
| 592 | new ArrayList<>(apiAddedSwitchEvents), |
| 593 | new ArrayList<>(apiRemovedSwitchEvents), |
| 594 | new ArrayList<>(apiAddedPortEvents), |
| 595 | new ArrayList<>(apiRemovedPortEvents), |
| 596 | new ArrayList<>(apiAddedLinkEvents), |
| 597 | new ArrayList<>(apiRemovedLinkEvents), |
| 598 | new ArrayList<>(apiAddedHostEvents), |
| 599 | new ArrayList<>(apiRemovedHostEvents)); |
| 600 | |
| 601 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 602 | // Deliver the events |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 603 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 604 | for (ITopologyListener listener : this.topologyListeners) { |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 605 | listener.topologyEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 606 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 607 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 608 | // |
| 609 | // Cleanup |
| 610 | // |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 611 | apiAddedMastershipEvents.clear(); |
| 612 | apiRemovedMastershipEvents.clear(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 613 | apiAddedSwitchEvents.clear(); |
| 614 | apiRemovedSwitchEvents.clear(); |
| 615 | apiAddedPortEvents.clear(); |
| 616 | apiRemovedPortEvents.clear(); |
| 617 | apiAddedLinkEvents.clear(); |
| 618 | apiRemovedLinkEvents.clear(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 619 | apiAddedHostEvents.clear(); |
| 620 | apiRemovedHostEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 624 | * Mastership updated event. |
| 625 | * |
| 626 | * @param mastershipEvent the mastership event. |
| 627 | */ |
| 628 | @Override |
| 629 | public void putSwitchMastershipEvent(MastershipEvent mastershipEvent) { |
| 630 | // Send out notification |
| 631 | TopologyEvent topologyEvent = |
| 632 | new TopologyEvent(mastershipEvent, |
| 633 | registryService.getOnosInstanceId()); |
| 634 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Mastership removed event. |
| 639 | * |
| 640 | * @param mastershipEvent the mastership event. |
| 641 | */ |
| 642 | @Override |
| 643 | public void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) { |
| 644 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 645 | TopologyEvent topologyEvent = |
| 646 | new TopologyEvent(mastershipEvent, |
| 647 | registryService.getOnosInstanceId()); |
| 648 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 652 | * Switch discovered event. |
| 653 | * |
| 654 | * @param switchEvent the switch event. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 655 | * @param portEvents the corresponding port events for the switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 656 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 657 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 658 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 659 | Collection<PortEvent> portEvents) { |
| 660 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 661 | log.debug("Sending add switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 662 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 663 | TopologyEvent topologyEvent = |
| 664 | new TopologyEvent(switchEvent, |
| 665 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 666 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
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 | // Send out notification for each port |
| 669 | for (PortEvent portEvent : portEvents) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 670 | log.debug("Sending add port: {}", portEvent); |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 671 | topologyEvent = |
| 672 | new TopologyEvent(portEvent, |
| 673 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 674 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 675 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 676 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 677 | // |
| 678 | // Keep track of the added ports |
| 679 | // |
| 680 | // Get the old Port Events |
| 681 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 682 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 683 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 684 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 685 | } |
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 | // Store the new Port Events in the local cache |
| 688 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 689 | for (PortEvent portEvent : portEvents) { |
| 690 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 691 | newPortEvents.put(id, portEvent); |
| 692 | } |
| 693 | discoveredAddedPortEvents.put(switchEvent.getDpid(), |
| 694 | newPortEvents); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 695 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 696 | // |
| 697 | // Extract the removed ports |
| 698 | // |
| 699 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 700 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 701 | ByteBuffer key = entry.getKey(); |
| 702 | PortEvent portEvent = entry.getValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 703 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 704 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 705 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 709 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 710 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 711 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 712 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 713 | } |
| 714 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 715 | /** |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 716 | * {@inheritDoc} |
| 717 | * <p/> |
| 718 | * Called by {@link TopologyPublisher.SwitchCleanup} thread. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 719 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 720 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 721 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 722 | TopologyEvent topologyEvent; |
| 723 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 724 | // Get the old Port Events |
| 725 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 726 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 727 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 728 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 729 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 730 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 731 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 732 | log.debug("Sending remove switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 733 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 734 | topologyEvent = |
| 735 | new TopologyEvent(switchEvent, |
| 736 | registryService.getOnosInstanceId()); |
| 737 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 738 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 739 | // |
| 740 | // Send out notification for each port. |
| 741 | // |
| 742 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 743 | // because it will attempt to remove the port from the database, |
| 744 | // and the deactiveSwitch() call above already removed all ports. |
| 745 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 746 | for (PortEvent portEvent : oldPortEvents.values()) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 747 | log.debug("Sending remove port:", portEvent); |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 748 | topologyEvent = |
| 749 | new TopologyEvent(portEvent, |
| 750 | registryService.getOnosInstanceId()); |
| 751 | eventChannel.removeEntry(topologyEvent.getID()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 752 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 753 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 754 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 755 | // Cleanup for each link |
| 756 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 757 | discoveredAddedLinkEvents.get(switchEvent.getDpid()); |
| 758 | if (oldLinkEvents != null) { |
| 759 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 760 | removeLinkDiscoveryEvent(linkEvent); |
| 761 | } |
| 762 | discoveredAddedLinkEvents.remove(switchEvent.getDpid()); |
| 763 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 764 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 765 | // Cleanup for each host |
| 766 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 767 | discoveredAddedHostEvents.get(switchEvent.getDpid()); |
| 768 | if (oldHostEvents != null) { |
| 769 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 770 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 771 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 772 | discoveredAddedHostEvents.remove(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 777 | /** |
| 778 | * Port discovered event. |
| 779 | * |
| 780 | * @param portEvent the port event. |
| 781 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 782 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 783 | public void putPortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 784 | if (datastore.addPort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 785 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 786 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 787 | TopologyEvent topologyEvent = |
| 788 | new TopologyEvent(portEvent, |
| 789 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 790 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 791 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 792 | // Store the new Port Event in the local cache |
| 793 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 794 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 795 | if (oldPortEvents == null) { |
| 796 | oldPortEvents = new HashMap<>(); |
| 797 | discoveredAddedPortEvents.put(portEvent.getDpid(), |
| 798 | oldPortEvents); |
| 799 | } |
| 800 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 801 | oldPortEvents.put(id, portEvent); |
| 802 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 805 | /** |
| 806 | * Port removed event. |
| 807 | * |
| 808 | * @param portEvent the port event. |
| 809 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 810 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 811 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 812 | if (datastore.deactivatePort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 813 | log.debug("Sending remove port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 814 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 815 | TopologyEvent topologyEvent = |
| 816 | new TopologyEvent(portEvent, |
| 817 | registryService.getOnosInstanceId()); |
| 818 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 819 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 820 | // Cleanup the Port Event from the local cache |
| 821 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 822 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 823 | if (oldPortEvents != null) { |
| 824 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 825 | oldPortEvents.remove(id); |
| 826 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 827 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 828 | // Cleanup for the incoming link |
| 829 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 830 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 831 | if (oldLinkEvents != null) { |
| 832 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 833 | if (linkEvent.getDst().equals(portEvent.getSwitchPort())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 834 | removeLinkDiscoveryEvent(linkEvent); |
| 835 | // XXX If we change our model to allow multiple Link on |
| 836 | // a Port, this loop must be fixed to allow continuing. |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 841 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 842 | // Cleanup for the connected hosts |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 843 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 844 | List<HostEvent> removedHostEvents = new LinkedList<>(); |
| 845 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 846 | discoveredAddedHostEvents.get(portEvent.getDpid()); |
| 847 | if (oldHostEvents != null) { |
| 848 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 849 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 850 | if (swp.equals(portEvent.getSwitchPort())) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 851 | removedHostEvents.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 855 | for (HostEvent hostEvent : removedHostEvents) { |
| 856 | removeHostDiscoveryEvent(hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 857 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 862 | /** |
| 863 | * Link discovered event. |
| 864 | * |
| 865 | * @param linkEvent the link event. |
| 866 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 867 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 868 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 869 | if (datastore.addLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 870 | log.debug("Sending add link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 871 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 872 | TopologyEvent topologyEvent = |
| 873 | new TopologyEvent(linkEvent, |
| 874 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 875 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 876 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 877 | // Store the new Link Event in the local cache |
| 878 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 879 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 880 | if (oldLinkEvents == null) { |
| 881 | oldLinkEvents = new HashMap<>(); |
| 882 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 883 | oldLinkEvents); |
| 884 | } |
| 885 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 886 | oldLinkEvents.put(id, linkEvent); |
| 887 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 888 | } |
| 889 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 890 | /** |
| 891 | * Link removed event. |
| 892 | * |
| 893 | * @param linkEvent the link event. |
| 894 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 895 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 896 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 897 | if (datastore.removeLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 898 | log.debug("Sending remove link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 899 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 900 | TopologyEvent topologyEvent = |
| 901 | new TopologyEvent(linkEvent, |
| 902 | registryService.getOnosInstanceId()); |
| 903 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 904 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 905 | // Cleanup the Link Event from the local cache |
| 906 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 907 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 908 | if (oldLinkEvents != null) { |
| 909 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 910 | oldLinkEvents.remove(id); |
| 911 | } |
| 912 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 913 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 914 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 915 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 916 | * Host discovered event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 917 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 918 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 919 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 920 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 921 | public void putHostDiscoveryEvent(HostEvent hostEvent) { |
| 922 | if (datastore.addHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 923 | // Send out notification |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 924 | TopologyEvent topologyEvent = |
| 925 | new TopologyEvent(hostEvent, |
| 926 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 927 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 928 | log.debug("Put the host info into the cache of the topology. mac {}", |
| 929 | hostEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 930 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 931 | // Store the new Host Event in the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 932 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 933 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 934 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 935 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 936 | if (oldHostEvents == null) { |
| 937 | oldHostEvents = new HashMap<>(); |
| 938 | discoveredAddedHostEvents.put(swp.getDpid(), |
| 939 | oldHostEvents); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 940 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 941 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 942 | oldHostEvents.put(id, hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 945 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 946 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 947 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 948 | * Host removed event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 949 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 950 | * @param hostEvent the host event. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 951 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 952 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 953 | public void removeHostDiscoveryEvent(HostEvent hostEvent) { |
| 954 | if (datastore.removeHost(hostEvent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 955 | // Send out notification |
Pavlin Radoslavov | bb17de2 | 2014-08-06 15:34:37 -0700 | [diff] [blame] | 956 | TopologyEvent topologyEvent = |
| 957 | new TopologyEvent(hostEvent, |
| 958 | registryService.getOnosInstanceId()); |
| 959 | eventChannel.removeEntry(topologyEvent.getID()); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 960 | log.debug("Remove the host info into the cache of the topology. mac {}", |
| 961 | hostEvent.getMac()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 962 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 963 | // Cleanup the Host Event from the local cache |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 964 | // TODO: The implementation below is probably wrong |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 965 | ByteBuffer id = ByteBuffer.wrap(hostEvent.getID()); |
| 966 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 967 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 968 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 969 | if (oldHostEvents != null) { |
| 970 | oldHostEvents.remove(id); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 974 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 975 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 976 | // |
| 977 | // Methods to update topology replica |
| 978 | // |
| 979 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 980 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 981 | * Adds Switch Mastership event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 982 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 983 | * @param mastershipEvent the MastershipEvent to process. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 984 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 985 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 986 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 987 | private boolean addMastershipEvent(MastershipEvent mastershipEvent) { |
| 988 | log.debug("Added Mastership event {}", mastershipEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 989 | lastAddMastershipEvents.put(mastershipEvent.getIDasByteBuffer(), |
| 990 | mastershipEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 991 | apiAddedMastershipEvents.add(mastershipEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 992 | return true; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 996 | * Removes Switch Mastership event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 997 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 998 | * @param mastershipEvent the MastershipEvent to process. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 999 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 1000 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1001 | private void removeMastershipEvent(MastershipEvent mastershipEvent) { |
| 1002 | log.debug("Removed Mastership event {}", mastershipEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1003 | lastAddMastershipEvents.remove(mastershipEvent.getIDasByteBuffer()); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 1004 | apiRemovedMastershipEvents.add(mastershipEvent); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1008 | * Adds a switch to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1009 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1010 | * @param switchEvent the SwitchEvent with the switch to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1011 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1012 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1013 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1014 | private boolean addSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1015 | if (log.isDebugEnabled()) { |
| 1016 | SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid()); |
| 1017 | if (sw != null) { |
| 1018 | log.debug("Update {}", switchEvent); |
| 1019 | } else { |
| 1020 | log.debug("Added {}", switchEvent); |
| 1021 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1022 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1023 | topology.putSwitch(switchEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1024 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1025 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1028 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1029 | * Removes a switch from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1030 | * <p/> |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1031 | * It will call {@link #removePort(PortEvent)} for each ports on this |
| 1032 | * switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1033 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1034 | * @param switchEvent the SwitchEvent with the switch to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1035 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1036 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1037 | private void removeSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1038 | final Dpid dpid = switchEvent.getDpid(); |
| 1039 | |
| 1040 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 1041 | if (swInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1042 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 1043 | return; |
| 1044 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1045 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1046 | // |
| 1047 | // Remove all Ports on the Switch |
| 1048 | // |
| 1049 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1050 | for (Port port : topology.getPorts(dpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1051 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 1052 | port, switchEvent); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 1053 | PortEvent portEvent = new PortEvent(port.getSwitchPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1054 | portsToRemove.add(portEvent); |
| 1055 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1056 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1057 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1058 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1059 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1060 | log.debug("Removed {}", swInTopo); |
| 1061 | topology.removeSwitch(dpid); |
| 1062 | apiRemovedSwitchEvents.add(swInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1065 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1066 | * Adds a port to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1067 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1068 | * @param portEvent the PortEvent with the port to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1069 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1070 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1071 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1072 | private boolean addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1073 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1074 | if (sw == null) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1075 | // Reordered event |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1076 | log.debug("{} reordered because switch is null", portEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1077 | return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1078 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1079 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1080 | if (log.isDebugEnabled()) { |
| 1081 | PortEvent port = topology.getPortEvent(portEvent.getSwitchPort()); |
| 1082 | if (port != null) { |
| 1083 | log.debug("Update {}", portEvent); |
| 1084 | } else { |
| 1085 | log.debug("Added {}", portEvent); |
| 1086 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1087 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1088 | topology.putPort(portEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1089 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1090 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1093 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1094 | * Removes a port from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1095 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1096 | * It will remove attachment points from each hosts on this port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1097 | * and call {@link #removeLink(LinkEvent)} for each links on this port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1098 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1099 | * @param portEvent the PortEvent with the port to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1100 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1101 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1102 | private void removePort(PortEvent portEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1103 | SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1104 | if (sw == null) { |
| 1105 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 1106 | portEvent); |
| 1107 | return; |
| 1108 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1109 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1110 | final SwitchPort switchPort = portEvent.getSwitchPort(); |
| 1111 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
| 1112 | if (portInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1113 | log.warn("Port {} already removed, ignoring", portEvent); |
| 1114 | return; |
| 1115 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1117 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1118 | // Remove all Host attachment points bound to this Port |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1119 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1120 | List<HostEvent> hostsToUpdate = new ArrayList<>(); |
| 1121 | for (Host host : topology.getHosts(switchPort)) { |
| 1122 | log.debug("Removing Host {} on Port {}", host, portInTopo); |
| 1123 | HostEvent hostEvent = topology.getHostEvent(host.getMacAddress()); |
| 1124 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1125 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1126 | for (HostEvent hostEvent : hostsToUpdate) { |
| 1127 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 1128 | newHostEvent.removeAttachmentPoint(switchPort); |
| 1129 | newHostEvent.freeze(); |
| 1130 | |
| 1131 | // TODO should this event be fired inside #addHost? |
| 1132 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 1133 | // No more attachment point left -> remove Host |
| 1134 | removeHost(hostEvent); |
| 1135 | } else { |
| 1136 | // Update Host |
| 1137 | addHost(newHostEvent); |
| 1138 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1139 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1140 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1141 | // |
| 1142 | // Remove all Links connected to the Port |
| 1143 | // |
| 1144 | Set<Link> links = new HashSet<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1145 | links.addAll(topology.getOutgoingLinks(switchPort)); |
| 1146 | links.addAll(topology.getIncomingLinks(switchPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1147 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1148 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1149 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1150 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1151 | LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple()); |
| 1152 | if (linkEvent != null) { |
| 1153 | log.debug("Removing Link {} on Port {}", link, portInTopo); |
| 1154 | removeLink(linkEvent); |
| 1155 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1156 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1157 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1158 | // Remove the Port from Topology |
| 1159 | log.debug("Removed {}", portInTopo); |
| 1160 | topology.removePort(switchPort); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1161 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1162 | apiRemovedPortEvents.add(portInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1165 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1166 | * Adds a link to the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1167 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1168 | * It will remove attachment points from each hosts using the same ports. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1169 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1170 | * @param linkEvent the LinkEvent with the link to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1171 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1172 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1173 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1174 | private boolean addLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1175 | PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc()); |
| 1176 | PortEvent dstPort = topology.getPortEvent(linkEvent.getDst()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1177 | if ((srcPort == null) || (dstPort == null)) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1178 | // Reordered event |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 1179 | log.debug("{} reordered because {} port is null", linkEvent, |
| 1180 | (srcPort == null) ? "src" : "dst"); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1181 | return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1182 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1183 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1184 | // |
| 1185 | // XXX domain knowledge: Sanity check: Port cannot have both Link and |
| 1186 | // Host. |
| 1187 | // |
| 1188 | // FIXME: Potentially local replica may not be up-to-date yet due to |
| 1189 | // Hazelcast delay. |
| 1190 | // FIXME: May need to manage local truth and use them instead. |
| 1191 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1192 | if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) { |
| 1193 | // Only check for existing Host when adding new Link. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1194 | // Remove all Hosts attached to the ports on both ends |
| 1195 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1196 | Set<HostEvent> hostsToUpdate = |
| 1197 | new TreeSet<>(new Comparator<HostEvent>() { |
| 1198 | // Comparison only using ID(=MAC) |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1199 | @Override |
| 1200 | public int compare(HostEvent o1, HostEvent o2) { |
| 1201 | return Long.compare(o1.getMac().toLong(), o2.getMac().toLong()); |
| 1202 | } |
| 1203 | }); |
| 1204 | |
| 1205 | List<SwitchPort> portsToCheck = Arrays.asList( |
| 1206 | srcPort.getSwitchPort(), |
| 1207 | dstPort.getSwitchPort()); |
| 1208 | |
| 1209 | // Enumerate Host which needs to be updated by this Link add event |
| 1210 | for (SwitchPort port : portsToCheck) { |
| 1211 | for (Host host : topology.getHosts(port)) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1212 | log.error("Host {} on Port {} should have been removed prior to adding Link {}", |
| 1213 | host, port, linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1214 | |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1215 | HostEvent hostEvent = |
| 1216 | topology.getHostEvent(host.getMacAddress()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1217 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1218 | } |
| 1219 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1220 | // Remove attachment point from them |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1221 | for (HostEvent hostEvent : hostsToUpdate) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1222 | // Remove port from attachment point and update |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1223 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 1224 | newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort()); |
| 1225 | newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort()); |
| 1226 | newHostEvent.freeze(); |
| 1227 | |
| 1228 | // TODO should this event be fired inside #addHost? |
| 1229 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 1230 | // No more attachment point left -> remove Host |
| 1231 | removeHost(hostEvent); |
| 1232 | } else { |
| 1233 | // Update Host |
| 1234 | addHost(newHostEvent); |
| 1235 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1236 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1237 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1238 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1239 | if (log.isDebugEnabled()) { |
| 1240 | LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple()); |
| 1241 | if (link != null) { |
| 1242 | log.debug("Update {}", linkEvent); |
| 1243 | } else { |
| 1244 | log.debug("Added {}", linkEvent); |
| 1245 | } |
| 1246 | } |
| 1247 | topology.putLink(linkEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1248 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1249 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1252 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 1253 | * Removes a link from the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1254 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1255 | * @param linkEvent the LinkEvent with the link to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1256 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1257 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1258 | private void removeLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1259 | Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1260 | linkEvent.getSrc().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1261 | if (srcPort == null) { |
| 1262 | log.warn("Src Port for Link {} already removed, ignoring", |
| 1263 | linkEvent); |
| 1264 | return; |
| 1265 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1266 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 1267 | Port dstPort = topology.getPort(linkEvent.getDst().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1268 | linkEvent.getDst().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1269 | if (dstPort == null) { |
| 1270 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1271 | linkEvent); |
| 1272 | return; |
| 1273 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1274 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1275 | LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(), |
| 1276 | linkEvent.getType()); |
| 1277 | if (linkInTopo == null) { |
| 1278 | log.warn("Link {} already removed, ignoring", linkEvent); |
| 1279 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1280 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1281 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1282 | if (log.isDebugEnabled()) { |
| 1283 | // only do sanity check on debug level |
| 1284 | |
| 1285 | Link linkIn = dstPort.getIncomingLink(linkEvent.getType()); |
| 1286 | if (linkIn == null) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1287 | log.warn("Link {} already removed on destination Port", |
| 1288 | linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1289 | } |
| 1290 | Link linkOut = srcPort.getOutgoingLink(linkEvent.getType()); |
| 1291 | if (linkOut == null) { |
| 1292 | log.warn("Link {} already removed on src Port", linkEvent); |
| 1293 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1294 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1295 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1296 | log.debug("Removed {}", linkInTopo); |
| 1297 | topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType()); |
| 1298 | apiRemovedLinkEvents.add(linkInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1301 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1302 | * Adds a host to the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1303 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1304 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1305 | * TODO: Eventually, we might need to consider reordering |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1306 | * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events |
| 1307 | * on the same port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1308 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1309 | * @param hostEvent the HostEvent with the host to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1310 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1311 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1312 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1313 | private boolean addHost(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 | // TODO Decide how to handle update scenario. |
| 1316 | // If the new HostEvent has less attachment point compared to |
| 1317 | // existing HostEvent, what should the event be? |
| 1318 | // - AddHostEvent with some attachment point removed? (current behavior) |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1319 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1320 | // create unfrozen copy |
| 1321 | // for removing attachment points which already has a link |
| 1322 | HostEvent modifiedHostEvent = new HostEvent(hostEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1323 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1324 | // Verify each attachment point |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1325 | boolean attachmentFound = false; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1326 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 1327 | // XXX domain knowledge: Port must exist before Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 1328 | // but this knowledge cannot be pushed down to driver. |
| 1329 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1330 | // Attached Ports must exist |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 1331 | Port port = topology.getPort(swp.getDpid(), swp.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1332 | if (port == null) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1333 | log.debug("{} reordered because port {} was not there", |
| 1334 | hostEvent, swp); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1335 | // Reordered event |
| 1336 | return false; // should not continue if re-applying later |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1337 | } |
| 1338 | // Attached Ports must not have Link |
| 1339 | if (port.getOutgoingLink() != null || |
| 1340 | port.getIncomingLink() != null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1341 | log.warn("Link (Out:{},In:{}) exist on the attachment point. " |
| 1342 | + "Ignoring this attachmentpoint ({}) from {}.", |
| 1343 | port.getOutgoingLink(), port.getIncomingLink(), |
| 1344 | swp, modifiedHostEvent); |
| 1345 | // FIXME Should either reject, reorder this HostEvent, |
| 1346 | // or remove attachment point from given HostEvent |
| 1347 | // Removing attachment point from given HostEvent for now. |
| 1348 | modifiedHostEvent.removeAttachmentPoint(swp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1349 | continue; |
| 1350 | } |
| 1351 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1352 | attachmentFound = true; |
| 1353 | } |
| 1354 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1355 | // Update the host in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1356 | if (attachmentFound) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1357 | if (modifiedHostEvent.getAttachmentPoints().isEmpty()) { |
| 1358 | log.warn("No valid attachment point left. Ignoring." |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 1359 | + "original: {}, modified: {}", |
| 1360 | hostEvent, modifiedHostEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1361 | // TODO Should we call #removeHost to trigger remove event? |
| 1362 | // only if this call is update. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1363 | return false; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | if (log.isDebugEnabled()) { |
| 1367 | HostEvent host = topology.getHostEvent(hostEvent.getMac()); |
| 1368 | if (host != null) { |
| 1369 | log.debug("Update {}", modifiedHostEvent); |
| 1370 | } else { |
| 1371 | log.debug("Added {}", modifiedHostEvent); |
| 1372 | } |
| 1373 | } |
| 1374 | topology.putHost(modifiedHostEvent.freeze()); |
| 1375 | apiAddedHostEvents.add(modifiedHostEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1376 | return true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1377 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1378 | return false; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1379 | } |
| 1380 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1381 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1382 | * Removes a host from the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1383 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1384 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1385 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1386 | * @param hostEvent the Host Event with the host to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1387 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 1388 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1389 | private void removeHost(HostEvent hostEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1390 | |
| 1391 | final MACAddress mac = hostEvent.getMac(); |
| 1392 | HostEvent hostInTopo = topology.getHostEvent(mac); |
| 1393 | if (hostInTopo == null) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 1394 | log.warn("Host {} already removed, ignoring", hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1395 | return; |
| 1396 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1397 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1398 | log.debug("Removed {}", hostInTopo); |
| 1399 | topology.removeHost(mac); |
| 1400 | apiRemovedHostEvents.add(hostInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1401 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1402 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1403 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1404 | * Read the whole topology from the database. |
| 1405 | * |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1406 | * @return a list of EventEntry-encapsulated Topology Events for |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1407 | * the whole topology. |
| 1408 | */ |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1409 | private List<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
| 1410 | List<EventEntry<TopologyEvent>> events = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1411 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1412 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1413 | // XXX May need to clear whole topology first, depending on |
| 1414 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1415 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1416 | // Add all active switches |
| 1417 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1418 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1419 | continue; |
| 1420 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1421 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1422 | // |
| 1423 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1424 | // Currently, this code is not used, and it might go away in the |
| 1425 | // future. |
| 1426 | // |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1427 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(sw.getDpid())); |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1428 | TopologyEvent topologyEvent = |
| 1429 | new TopologyEvent(switchEvent, |
| 1430 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1431 | EventEntry<TopologyEvent> eventEntry = |
| 1432 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1433 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1434 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1435 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1436 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1437 | // Add all active ports |
| 1438 | for (KVPort p : KVPort.getAllPorts()) { |
| 1439 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1440 | continue; |
| 1441 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1442 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1443 | // |
| 1444 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1445 | // Currently, this code is not used, and it might go away in the |
| 1446 | // future. |
| 1447 | // |
| 1448 | PortEvent portEvent = |
| 1449 | new PortEvent(new Dpid(p.getDpid()), |
| 1450 | new PortNumber(p.getNumber().shortValue())); |
| 1451 | TopologyEvent topologyEvent = |
| 1452 | new TopologyEvent(portEvent, |
| 1453 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1454 | EventEntry<TopologyEvent> eventEntry = |
| 1455 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1456 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1457 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1458 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1459 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1460 | for (KVDevice d : KVDevice.getAllDevices()) { |
| 1461 | // |
| 1462 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1463 | // Currently, this code is not used, and it might go away in the |
| 1464 | // future. |
| 1465 | // |
| 1466 | HostEvent devEvent = new HostEvent(MACAddress.valueOf(d.getMac())); |
| 1467 | for (byte[] portId : d.getAllPortIds()) { |
| 1468 | devEvent.addAttachmentPoint( |
| 1469 | new SwitchPort(KVPort.getDpidFromKey(portId), |
| 1470 | KVPort.getNumberFromKey(portId))); |
| 1471 | } |
| 1472 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1473 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1474 | for (KVLink l : KVLink.getAllLinks()) { |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1475 | // |
| 1476 | // TODO: Using the local ONOS Instance ID below is incorrect. |
| 1477 | // Currently, this code is not used, and it might go away in the |
| 1478 | // future. |
| 1479 | // |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 1480 | LinkEvent linkEvent = new LinkEvent( |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 1481 | new SwitchPort(l.getSrc().dpid, l.getSrc().number), |
| 1482 | new SwitchPort(l.getDst().dpid, l.getDst().number)); |
| 1483 | TopologyEvent topologyEvent = |
| 1484 | new TopologyEvent(linkEvent, |
| 1485 | registryService.getOnosInstanceId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1486 | EventEntry<TopologyEvent> eventEntry = |
| 1487 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1488 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1489 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1490 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1491 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 1492 | return events; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1493 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1494 | } |