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