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