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