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; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 26 | import net.onrc.onos.core.metrics.OnosMetrics.MetricsComponent; |
| 27 | import net.onrc.onos.core.metrics.OnosMetrics.MetricsFeature; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.Dpid; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.util.EventEntry; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.util.SwitchPort; |
| 32 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 36 | import com.codahale.metrics.Gauge; |
| 37 | import com.codahale.metrics.Meter; |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 38 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 39 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 40 | * The TopologyManager receives topology updates from the southbound discovery |
| 41 | * modules and from other ONOS instances. These updates are processed and |
| 42 | * applied to the in-memory topology instance. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 44 | * - Maintain Invariant/Relationships between Topology Objects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | * <p/> |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 46 | * TODO To be synchronized based on TopologyEvent Notification. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | * <p/> |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 48 | * TODO TBD: Caller is expected to maintain parent/child calling order. Parent |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame] | 49 | * Object must exist before adding sub component(Add Switch -> Port). |
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 | * TODO TBD: This class may delay the requested change to handle event |
| 52 | * 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] | 53 | */ |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 54 | public class TopologyManager { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 55 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 56 | private static final Logger log = LoggerFactory |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | .getLogger(TopologyManager.class); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 58 | |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 59 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
Jonathan Hart | 10a7e2b | 2014-02-21 18:30:08 -0800 | [diff] [blame] | 60 | public static final String EVENT_CHANNEL_NAME = "onos.topology"; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 61 | private EventHandler eventHandler = new EventHandler(); |
| 62 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 63 | private final TopologyImpl topology = new TopologyImpl(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 64 | private TopologyEventPreprocessor eventPreprocessor; |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 65 | private CopyOnWriteArrayList<ITopologyListener> topologyListeners = |
| 66 | new CopyOnWriteArrayList<>(); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 67 | private CopyOnWriteArrayList<ITopologyListener> newTopologyListeners = |
| 68 | new CopyOnWriteArrayList<>(); |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 69 | |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 70 | // |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 71 | // Metrics |
| 72 | // |
| 73 | private static final MetricsComponent METRICS_COMPONENT = |
| 74 | OnosMetrics.registerComponent("Topology"); |
| 75 | private static final MetricsFeature METRICS_FEATURE_EVENT_NOTIFICATION = |
| 76 | METRICS_COMPONENT.registerFeature("EventNotification"); |
| 77 | // |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 78 | // Timestamp of the last Topology event (ms from the Epoch) |
| 79 | private volatile long lastEventTimestampEpochMs = 0; |
| 80 | private final Gauge<Long> gaugeLastEventTimestampEpochMs = |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 81 | OnosMetrics.registerMetric(METRICS_COMPONENT, |
| 82 | METRICS_FEATURE_EVENT_NOTIFICATION, |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 83 | "LastEventTimestamp.EpochMs", |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 84 | new Gauge<Long>() { |
| 85 | @Override |
| 86 | public Long getValue() { |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 87 | return lastEventTimestampEpochMs; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 88 | } |
| 89 | }); |
| 90 | // Rate of the Topology events published to the Topology listeners |
| 91 | private final Meter listenerEventRate = |
| 92 | OnosMetrics.createMeter(METRICS_COMPONENT, |
| 93 | METRICS_FEATURE_EVENT_NOTIFICATION, |
| 94 | "ListenerEventRate"); |
| 95 | |
| 96 | // |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 97 | // Local state for keeping the last ADD Mastership Event entries. |
| 98 | // TODO: In the future, we might have to keep this state somewhere else. |
| 99 | // |
| 100 | private Map<ByteBuffer, MastershipEvent> lastAddMastershipEvents = |
| 101 | new HashMap<>(); |
| 102 | |
| 103 | // |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 104 | // Local state for keeping track of the application event notifications |
| 105 | // |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 106 | // - Queue of events, which will be dispatched to local listeners |
| 107 | // on next notification. |
Yuta HIGUCHI | 703696c | 2014-06-25 20:36:45 -0700 | [diff] [blame] | 108 | |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 109 | private List<MastershipEvent> apiAddedMastershipEvents = |
| 110 | new LinkedList<>(); |
| 111 | private List<MastershipEvent> apiRemovedMastershipEvents = |
| 112 | new LinkedList<>(); |
Yuta HIGUCHI | 703696c | 2014-06-25 20:36:45 -0700 | [diff] [blame] | 113 | private List<SwitchEvent> apiAddedSwitchEvents = new LinkedList<>(); |
| 114 | private List<SwitchEvent> apiRemovedSwitchEvents = new LinkedList<>(); |
| 115 | private List<PortEvent> apiAddedPortEvents = new LinkedList<>(); |
| 116 | private List<PortEvent> apiRemovedPortEvents = new LinkedList<>(); |
| 117 | private List<LinkEvent> apiAddedLinkEvents = new LinkedList<>(); |
| 118 | private List<LinkEvent> apiRemovedLinkEvents = new LinkedList<>(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 119 | private List<HostEvent> apiAddedHostEvents = new LinkedList<>(); |
| 120 | private List<HostEvent> apiRemovedHostEvents = new LinkedList<>(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 121 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Constructor. |
| 124 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 125 | * @param registryService the Registry Service to use. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 126 | */ |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 127 | public TopologyManager(IControllerRegistryService registryService) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 128 | this.eventPreprocessor = |
| 129 | new TopologyEventPreprocessor(registryService); |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 130 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 131 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 132 | /** |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 133 | * Get the MutableTopology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 134 | * |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 135 | * @return the MutableTopology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 136 | */ |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 137 | MutableTopology getTopology() { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 138 | return topology; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 141 | /** |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 142 | * Event handler class. |
| 143 | */ |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 144 | class EventHandler extends Thread implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | IEventChannelListener<byte[], TopologyEvent> { |
| 146 | private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents = |
| 147 | new LinkedBlockingQueue<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 148 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Startup processing. |
| 151 | */ |
| 152 | private void startup() { |
| 153 | // |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 154 | // Read all topology state |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | // |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 156 | Collection<TopologyEvent> allTopologyEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | eventChannel.getAllEntries(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 158 | List<EventEntry<TopologyEvent>> events = |
| 159 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 160 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 161 | for (TopologyEvent topologyEvent : allTopologyEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | EventEntry<TopologyEvent> eventEntry = |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 163 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 164 | topologyEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 165 | events.add(eventEntry); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 166 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 167 | processEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 169 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Run the thread. |
| 172 | */ |
| 173 | @Override |
| 174 | public void run() { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 175 | List<EventEntry<TopologyEvent>> events = |
| 176 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 177 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | this.setName("TopologyManager.EventHandler " + this.getId()); |
| 179 | startup(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 180 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | // |
| 182 | // The main loop |
| 183 | // |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 184 | while (true) { |
| 185 | try { |
| 186 | EventEntry<TopologyEvent> eventEntry = |
| 187 | topologyEvents.take(); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 188 | events.add(eventEntry); |
| 189 | topologyEvents.drainTo(events); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 190 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 191 | processEvents(events); |
| 192 | events.clear(); |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 193 | } catch (Exception exception) { |
| 194 | log.debug("Exception processing Topology Events: ", |
| 195 | exception); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 199 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Process all topology events. |
| 202 | * |
| 203 | * @param events the events to process. |
| 204 | */ |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 205 | private void processEvents(List<EventEntry<TopologyEvent>> events) { |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 206 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 207 | // Process pending (new) listeners |
| 208 | // |
| 209 | processPendingListeners(); |
| 210 | |
| 211 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 212 | // Pre-process the events |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 213 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 214 | events = eventPreprocessor.processEvents(events); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 215 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 216 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 217 | // Lock the topology while it is modified |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 218 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 219 | topology.acquireWriteLock(); |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 220 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 221 | try { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 222 | // Apply the events |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 223 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 224 | // NOTE: The events are suppose to be in the proper order |
| 225 | // to naturally build and update the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 226 | // |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 227 | for (EventEntry<TopologyEvent> event : events) { |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 228 | // Ignore NO-OP events |
| 229 | if (event.isNoop()) { |
| 230 | continue; |
| 231 | } |
| 232 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 233 | TopologyEvent topologyEvent = event.eventData(); |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 234 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 235 | // Get the event itself |
| 236 | MastershipEvent mastershipEvent = |
| 237 | topologyEvent.getMastershipEvent(); |
| 238 | SwitchEvent switchEvent = topologyEvent.getSwitchEvent(); |
| 239 | PortEvent portEvent = topologyEvent.getPortEvent(); |
| 240 | LinkEvent linkEvent = topologyEvent.getLinkEvent(); |
| 241 | HostEvent hostEvent = topologyEvent.getHostEvent(); |
| 242 | boolean wasAdded = false; |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 243 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 244 | // |
| 245 | // Extract the events |
| 246 | // |
| 247 | switch (event.eventType()) { |
| 248 | case ENTRY_ADD: |
| 249 | if (mastershipEvent != null) { |
| 250 | wasAdded = addMastershipEvent(mastershipEvent); |
| 251 | } |
| 252 | if (switchEvent != null) { |
| 253 | wasAdded = addSwitch(switchEvent); |
| 254 | } |
| 255 | if (portEvent != null) { |
| 256 | wasAdded = addPort(portEvent); |
| 257 | } |
| 258 | if (linkEvent != null) { |
| 259 | wasAdded = addLink(linkEvent); |
| 260 | } |
| 261 | if (hostEvent != null) { |
| 262 | wasAdded = addHost(hostEvent); |
| 263 | } |
| 264 | // If the item wasn't added, probably it was reordered |
| 265 | if (!wasAdded) { |
| 266 | ByteBuffer id = topologyEvent.getIDasByteBuffer(); |
| 267 | eventPreprocessor.reorderedEvents.put(id, topologyEvent); |
| 268 | } |
| 269 | break; |
| 270 | case ENTRY_REMOVE: |
| 271 | if (mastershipEvent != null) { |
| 272 | removeMastershipEvent(mastershipEvent); |
| 273 | } |
| 274 | if (switchEvent != null) { |
| 275 | removeSwitch(switchEvent); |
| 276 | } |
| 277 | if (portEvent != null) { |
| 278 | removePort(portEvent); |
| 279 | } |
| 280 | if (linkEvent != null) { |
| 281 | removeLink(linkEvent); |
| 282 | } |
| 283 | if (hostEvent != null) { |
| 284 | removeHost(hostEvent); |
| 285 | } |
| 286 | break; |
| 287 | default: |
| 288 | log.error("Unknown topology event {}", |
| 289 | event.eventType()); |
| 290 | } |
| 291 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 292 | } finally { |
| 293 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 294 | // Topology modifications completed: Release the lock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 295 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 296 | topology.releaseWriteLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 297 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 298 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 299 | // |
| 300 | // Dispatch the Topology Notification Events to the applications |
| 301 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 302 | dispatchTopologyEvents(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 303 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 304 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 305 | /** |
| 306 | * Receive a notification that an entry is added. |
| 307 | * |
| 308 | * @param value the value for the entry. |
| 309 | */ |
| 310 | @Override |
| 311 | public void entryAdded(TopologyEvent value) { |
| 312 | EventEntry<TopologyEvent> eventEntry = |
| 313 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 314 | value); |
| 315 | topologyEvents.add(eventEntry); |
| 316 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 317 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Receive a notification that an entry is removed. |
| 320 | * |
| 321 | * @param value the value for the entry. |
| 322 | */ |
| 323 | @Override |
| 324 | public void entryRemoved(TopologyEvent value) { |
| 325 | EventEntry<TopologyEvent> eventEntry = |
| 326 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 327 | value); |
| 328 | topologyEvents.add(eventEntry); |
| 329 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 330 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Receive a notification that an entry is updated. |
| 333 | * |
| 334 | * @param value the value for the entry. |
| 335 | */ |
| 336 | @Override |
| 337 | public void entryUpdated(TopologyEvent value) { |
| 338 | // NOTE: The ADD and UPDATE events are processed in same way |
| 339 | entryAdded(value); |
| 340 | } |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Informs the event handler that a new listener has been added, |
| 344 | * and that listener expects the first event to be a snapshot of the |
| 345 | * current topology. |
| 346 | */ |
| 347 | void listenerAdded() { |
| 348 | // |
| 349 | // Generate a NO-OP event so the Event Handler processing can be |
| 350 | // triggered to generate in-order a snapshot of the current |
| 351 | // topology. |
| 352 | // TODO: This is a hack. |
| 353 | // |
| 354 | EventEntry<TopologyEvent> eventEntry = EventEntry.makeNoop(); |
| 355 | topologyEvents.add(eventEntry); |
| 356 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Startup processing. |
| 361 | * |
| 362 | * @param datagridService the datagrid service to use. |
| 363 | */ |
| 364 | void startup(IDatagridService datagridService) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 365 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 366 | eventHandler, |
| 367 | byte[].class, |
| 368 | TopologyEvent.class); |
| 369 | eventHandler.start(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 372 | /** |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 373 | * Adds a listener for topology events. |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 374 | * |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 375 | * @param listener the listener to add. |
| 376 | * @param startFromSnapshot if true, and if the topology is not |
| 377 | * empty, the first event should be a snapshot of the current topology. |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 378 | */ |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 379 | void addListener(ITopologyListener listener, boolean startFromSnapshot) { |
| 380 | if (startFromSnapshot) { |
| 381 | newTopologyListeners.addIfAbsent(listener); |
| 382 | eventHandler.listenerAdded(); |
| 383 | } else { |
| 384 | topologyListeners.addIfAbsent(listener); |
| 385 | } |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 389 | * Removes a listener for topology events. The listener will no longer |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 390 | * receive topology events after this call. |
| 391 | * |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 392 | * @param listener the listener to remove. |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 393 | */ |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 394 | void removeListener(ITopologyListener listener) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 395 | topologyListeners.remove(listener); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 396 | newTopologyListeners.remove(listener); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Processes pending (new) listeners. |
| 401 | * <p> |
| 402 | * During the processing, we dispatch Topology Snapshot Events to new |
| 403 | * listeners. |
| 404 | */ |
| 405 | private void processPendingListeners() { |
| 406 | if (newTopologyListeners.isEmpty()) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | // |
| 411 | // Create the Topology Snapshot Event |
| 412 | // |
| 413 | TopologyEvents events = null; |
Pavlin Radoslavov | 4163364 | 2014-08-11 14:24:52 -0700 | [diff] [blame] | 414 | Collection<MastershipEvent> mastershipEvents = |
| 415 | lastAddMastershipEvents.values(); |
| 416 | Collection<SwitchEvent> switchEvents = topology.getAllSwitchEvents(); |
| 417 | Collection<PortEvent> portEvents = topology.getAllPortEvents(); |
| 418 | Collection<LinkEvent> linkEvents = topology.getAllLinkEvents(); |
| 419 | Collection<HostEvent> hostEvents = topology.getAllHostEvents(); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 420 | if (!(mastershipEvents.isEmpty() && |
| 421 | switchEvents.isEmpty() && |
| 422 | portEvents.isEmpty() && |
| 423 | linkEvents.isEmpty() && |
| 424 | hostEvents.isEmpty())) { |
| 425 | events = new TopologyEvents(mastershipEvents, |
| 426 | switchEvents, |
| 427 | portEvents, |
| 428 | linkEvents, |
| 429 | hostEvents); |
| 430 | } |
| 431 | |
| 432 | // |
| 433 | // Dispatch Snapshot Event to each new listener, and keep track |
| 434 | // of each processed listener. |
| 435 | // |
| 436 | // NOTE: We deliver the event only if it is not empty. |
| 437 | // NOTE: We need to execute the loop so we can properly |
| 438 | // move the new listeners together with the older listeners. |
| 439 | // |
| 440 | List<ITopologyListener> processedListeners = new LinkedList<>(); |
| 441 | for (ITopologyListener listener : newTopologyListeners) { |
| 442 | processedListeners.add(listener); |
| 443 | // Move the new listener together with the rest of the listeners |
| 444 | topologyListeners.addIfAbsent(listener); |
| 445 | |
| 446 | // Dispatch the event |
| 447 | if (events != null) { |
| 448 | listener.topologyEvents(events); |
| 449 | } |
| 450 | } |
| 451 | newTopologyListeners.removeAll(processedListeners); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 455 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 456 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 457 | private void dispatchTopologyEvents() { |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 458 | if (apiAddedMastershipEvents.isEmpty() && |
| 459 | apiRemovedMastershipEvents.isEmpty() && |
| 460 | apiAddedSwitchEvents.isEmpty() && |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 461 | apiRemovedSwitchEvents.isEmpty() && |
| 462 | apiAddedPortEvents.isEmpty() && |
| 463 | apiRemovedPortEvents.isEmpty() && |
| 464 | apiAddedLinkEvents.isEmpty() && |
| 465 | apiRemovedLinkEvents.isEmpty() && |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 466 | apiAddedHostEvents.isEmpty() && |
| 467 | apiRemovedHostEvents.isEmpty()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 468 | return; // No events to dispatch |
| 469 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 470 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 471 | if (log.isDebugEnabled()) { |
| 472 | // |
| 473 | // Debug statements |
| 474 | // TODO: Those statements should be removed in the future |
| 475 | // |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 476 | for (MastershipEvent mastershipEvent : apiAddedMastershipEvents) { |
| 477 | log.debug("Dispatch Topology Event: ADDED {}", |
| 478 | mastershipEvent); |
| 479 | } |
| 480 | for (MastershipEvent mastershipEvent : apiRemovedMastershipEvents) { |
| 481 | log.debug("Dispatch Topology Event: REMOVED {}", |
| 482 | mastershipEvent); |
| 483 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 484 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 485 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 486 | } |
| 487 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 488 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 489 | } |
| 490 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 491 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 492 | } |
| 493 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 494 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 495 | } |
| 496 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 497 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 498 | } |
| 499 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 500 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 501 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 502 | for (HostEvent hostEvent : apiAddedHostEvents) { |
| 503 | log.debug("Dispatch Topology Event: ADDED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 504 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 505 | for (HostEvent hostEvent : apiRemovedHostEvents) { |
| 506 | log.debug("Dispatch Topology Event: REMOVED {}", hostEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 507 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 508 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 509 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 510 | // |
| 511 | // Update the metrics |
| 512 | // |
| 513 | long totalEvents = |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 514 | apiAddedMastershipEvents.size() + apiRemovedMastershipEvents.size() + |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 515 | apiAddedSwitchEvents.size() + apiRemovedSwitchEvents.size() + |
| 516 | apiAddedPortEvents.size() + apiRemovedPortEvents.size() + |
| 517 | apiAddedLinkEvents.size() + apiRemovedLinkEvents.size() + |
| 518 | apiAddedHostEvents.size() + apiRemovedHostEvents.size(); |
| 519 | this.listenerEventRate.mark(totalEvents); |
Pavlin Radoslavov | c49917c | 2014-07-23 12:16:29 -0700 | [diff] [blame] | 520 | this.lastEventTimestampEpochMs = System.currentTimeMillis(); |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 521 | |
| 522 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 523 | // Allocate the events to deliver. |
| 524 | // |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 525 | TopologyEvents events = new TopologyEvents( |
Pavlin Radoslavov | 4163364 | 2014-08-11 14:24:52 -0700 | [diff] [blame] | 526 | apiAddedMastershipEvents, |
| 527 | apiRemovedMastershipEvents, |
| 528 | apiAddedSwitchEvents, |
| 529 | apiRemovedSwitchEvents, |
| 530 | apiAddedPortEvents, |
| 531 | apiRemovedPortEvents, |
| 532 | apiAddedLinkEvents, |
| 533 | apiRemovedLinkEvents, |
| 534 | apiAddedHostEvents, |
| 535 | apiRemovedHostEvents); |
Pavlin Radoslavov | 054cd59 | 2014-08-07 20:57:16 -0700 | [diff] [blame] | 536 | |
| 537 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 538 | // Deliver the events |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame] | 539 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 540 | for (ITopologyListener listener : this.topologyListeners) { |
Pavlin Radoslavov | 4eaab99 | 2014-07-03 18:39:42 -0700 | [diff] [blame] | 541 | listener.topologyEvents(events); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 542 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 543 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 544 | // |
| 545 | // Cleanup |
| 546 | // |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 547 | apiAddedMastershipEvents.clear(); |
| 548 | apiRemovedMastershipEvents.clear(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 549 | apiAddedSwitchEvents.clear(); |
| 550 | apiRemovedSwitchEvents.clear(); |
| 551 | apiAddedPortEvents.clear(); |
| 552 | apiRemovedPortEvents.clear(); |
| 553 | apiAddedLinkEvents.clear(); |
| 554 | apiRemovedLinkEvents.clear(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 555 | apiAddedHostEvents.clear(); |
| 556 | apiRemovedHostEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 559 | // |
| 560 | // Methods to update topology replica |
| 561 | // |
| 562 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 563 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 564 | * Adds Switch Mastership event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 565 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 566 | * @param mastershipEvent the MastershipEvent to process. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 567 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 568 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 569 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 570 | private boolean addMastershipEvent(MastershipEvent mastershipEvent) { |
| 571 | log.debug("Added Mastership event {}", mastershipEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 572 | lastAddMastershipEvents.put(mastershipEvent.getIDasByteBuffer(), |
| 573 | mastershipEvent); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 574 | apiAddedMastershipEvents.add(mastershipEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 575 | return true; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 579 | * Removes Switch Mastership event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 580 | * |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 581 | * @param mastershipEvent the MastershipEvent to process. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 582 | */ |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 583 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 584 | private void removeMastershipEvent(MastershipEvent mastershipEvent) { |
| 585 | log.debug("Removed Mastership event {}", mastershipEvent); |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 586 | lastAddMastershipEvents.remove(mastershipEvent.getIDasByteBuffer()); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 587 | apiRemovedMastershipEvents.add(mastershipEvent); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 591 | * Adds a switch to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 592 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 593 | * @param switchEvent the SwitchEvent with the switch to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 594 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 595 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 596 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 597 | private boolean addSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 598 | if (log.isDebugEnabled()) { |
| 599 | SwitchEvent sw = topology.getSwitchEvent(switchEvent.getDpid()); |
| 600 | if (sw != null) { |
| 601 | log.debug("Update {}", switchEvent); |
| 602 | } else { |
| 603 | log.debug("Added {}", switchEvent); |
| 604 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 605 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 606 | topology.putSwitch(switchEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 607 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 608 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 611 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 612 | * Removes a switch from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 613 | * <p/> |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 614 | * It will call {@link #removePort(PortEvent)} for each ports on this |
| 615 | * switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 616 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 617 | * @param switchEvent the SwitchEvent with the switch to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 618 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 619 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 620 | private void removeSwitch(SwitchEvent switchEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 621 | final Dpid dpid = switchEvent.getDpid(); |
| 622 | |
| 623 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 624 | if (swInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 625 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 626 | return; |
| 627 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 628 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 629 | // |
| 630 | // Remove all Ports on the Switch |
| 631 | // |
| 632 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 633 | for (Port port : topology.getPorts(dpid)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 634 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 635 | port, switchEvent); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 636 | PortEvent portEvent = new PortEvent(port.getSwitchPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 637 | portsToRemove.add(portEvent); |
| 638 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 639 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 640 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 641 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 642 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 643 | log.debug("Removed {}", swInTopo); |
| 644 | topology.removeSwitch(dpid); |
| 645 | apiRemovedSwitchEvents.add(swInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 648 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 649 | * Adds a port to the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 650 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 651 | * @param portEvent the PortEvent with the port to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 652 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 653 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 654 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 655 | private boolean addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 656 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 657 | if (sw == null) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 658 | // Reordered event |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 659 | log.debug("{} reordered because switch is null", portEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 660 | return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 661 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 662 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 663 | if (log.isDebugEnabled()) { |
| 664 | PortEvent port = topology.getPortEvent(portEvent.getSwitchPort()); |
| 665 | if (port != null) { |
| 666 | log.debug("Update {}", portEvent); |
| 667 | } else { |
| 668 | log.debug("Added {}", portEvent); |
| 669 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 670 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 671 | topology.putPort(portEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 672 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 673 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 676 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 677 | * Removes a port from the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 678 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 679 | * It will remove attachment points from each hosts on this port |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 680 | * and call {@link #removeLink(LinkEvent)} for each links on this port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 681 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 682 | * @param portEvent the PortEvent with the port to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 683 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 684 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 685 | private void removePort(PortEvent portEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 686 | SwitchEvent sw = topology.getSwitchEvent(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 687 | if (sw == null) { |
| 688 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 689 | portEvent); |
| 690 | return; |
| 691 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 692 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 693 | final SwitchPort switchPort = portEvent.getSwitchPort(); |
| 694 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
| 695 | if (portInTopo == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 696 | log.warn("Port {} already removed, ignoring", portEvent); |
| 697 | return; |
| 698 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 699 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 700 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 701 | // Remove all Host attachment points bound to this Port |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 702 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 703 | List<HostEvent> hostsToUpdate = new ArrayList<>(); |
| 704 | for (Host host : topology.getHosts(switchPort)) { |
| 705 | log.debug("Removing Host {} on Port {}", host, portInTopo); |
| 706 | HostEvent hostEvent = topology.getHostEvent(host.getMacAddress()); |
| 707 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 708 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 709 | for (HostEvent hostEvent : hostsToUpdate) { |
| 710 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 711 | newHostEvent.removeAttachmentPoint(switchPort); |
| 712 | newHostEvent.freeze(); |
| 713 | |
| 714 | // TODO should this event be fired inside #addHost? |
| 715 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 716 | // No more attachment point left -> remove Host |
| 717 | removeHost(hostEvent); |
| 718 | } else { |
| 719 | // Update Host |
| 720 | addHost(newHostEvent); |
| 721 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 722 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 723 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 724 | // |
| 725 | // Remove all Links connected to the Port |
| 726 | // |
| 727 | Set<Link> links = new HashSet<>(); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 728 | links.addAll(topology.getOutgoingLinks(switchPort)); |
| 729 | links.addAll(topology.getIncomingLinks(switchPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 730 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 731 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 732 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 733 | } |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 734 | LinkEvent linkEvent = topology.getLinkEvent(link.getLinkTuple()); |
| 735 | if (linkEvent != null) { |
| 736 | log.debug("Removing Link {} on Port {}", link, portInTopo); |
| 737 | removeLink(linkEvent); |
| 738 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 739 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 740 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 741 | // Remove the Port from Topology |
| 742 | log.debug("Removed {}", portInTopo); |
| 743 | topology.removePort(switchPort); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 744 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 745 | apiRemovedPortEvents.add(portInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 748 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 749 | * Adds a link to the topology replica. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 750 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 751 | * It will remove attachment points from each hosts using the same ports. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 752 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 753 | * @param linkEvent the LinkEvent with the link to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 754 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 755 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 756 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 757 | private boolean addLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 758 | PortEvent srcPort = topology.getPortEvent(linkEvent.getSrc()); |
| 759 | PortEvent dstPort = topology.getPortEvent(linkEvent.getDst()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 760 | if ((srcPort == null) || (dstPort == null)) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 761 | // Reordered event |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 762 | log.debug("{} reordered because {} port is null", linkEvent, |
| 763 | (srcPort == null) ? "src" : "dst"); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 764 | return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 765 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 766 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 767 | // |
| 768 | // XXX domain knowledge: Sanity check: Port cannot have both Link and |
| 769 | // Host. |
| 770 | // |
| 771 | // FIXME: Potentially local replica may not be up-to-date yet due to |
| 772 | // Hazelcast delay. |
| 773 | // FIXME: May need to manage local truth and use them instead. |
| 774 | // |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 775 | if (topology.getLinkEvent(linkEvent.getLinkTuple()) == null) { |
| 776 | // Only check for existing Host when adding new Link. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 777 | // Remove all Hosts attached to the ports on both ends |
| 778 | |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 779 | Set<HostEvent> hostsToUpdate = |
| 780 | new TreeSet<>(new Comparator<HostEvent>() { |
| 781 | // Comparison only using ID(=MAC) |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 782 | @Override |
| 783 | public int compare(HostEvent o1, HostEvent o2) { |
| 784 | return Long.compare(o1.getMac().toLong(), o2.getMac().toLong()); |
| 785 | } |
| 786 | }); |
| 787 | |
| 788 | List<SwitchPort> portsToCheck = Arrays.asList( |
| 789 | srcPort.getSwitchPort(), |
| 790 | dstPort.getSwitchPort()); |
| 791 | |
| 792 | // Enumerate Host which needs to be updated by this Link add event |
| 793 | for (SwitchPort port : portsToCheck) { |
| 794 | for (Host host : topology.getHosts(port)) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 795 | log.error("Host {} on Port {} should have been removed prior to adding Link {}", |
| 796 | host, port, linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 797 | |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 798 | HostEvent hostEvent = |
| 799 | topology.getHostEvent(host.getMacAddress()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 800 | hostsToUpdate.add(hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 803 | // Remove attachment point from them |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 804 | for (HostEvent hostEvent : hostsToUpdate) { |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 805 | // Remove port from attachment point and update |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 806 | HostEvent newHostEvent = new HostEvent(hostEvent); |
| 807 | newHostEvent.removeAttachmentPoint(srcPort.getSwitchPort()); |
| 808 | newHostEvent.removeAttachmentPoint(dstPort.getSwitchPort()); |
| 809 | newHostEvent.freeze(); |
| 810 | |
| 811 | // TODO should this event be fired inside #addHost? |
| 812 | if (newHostEvent.getAttachmentPoints().isEmpty()) { |
| 813 | // No more attachment point left -> remove Host |
| 814 | removeHost(hostEvent); |
| 815 | } else { |
| 816 | // Update Host |
| 817 | addHost(newHostEvent); |
| 818 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 819 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 820 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 821 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 822 | if (log.isDebugEnabled()) { |
| 823 | LinkEvent link = topology.getLinkEvent(linkEvent.getLinkTuple()); |
| 824 | if (link != null) { |
| 825 | log.debug("Update {}", linkEvent); |
| 826 | } else { |
| 827 | log.debug("Added {}", linkEvent); |
| 828 | } |
| 829 | } |
| 830 | topology.putLink(linkEvent.freeze()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 831 | apiAddedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 832 | return true; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 835 | /** |
Yuta HIGUCHI | 7926ba3 | 2014-07-09 11:39:32 -0700 | [diff] [blame] | 836 | * Removes a link from the topology replica. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 837 | * |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 838 | * @param linkEvent the LinkEvent with the link to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 839 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 840 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 841 | private void removeLink(LinkEvent linkEvent) { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 842 | Port srcPort = topology.getPort(linkEvent.getSrc().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 843 | linkEvent.getSrc().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 844 | if (srcPort == null) { |
| 845 | log.warn("Src Port for Link {} already removed, ignoring", |
| 846 | linkEvent); |
| 847 | return; |
| 848 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 849 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 850 | Port dstPort = topology.getPort(linkEvent.getDst().getDpid(), |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 851 | linkEvent.getDst().getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 852 | if (dstPort == null) { |
| 853 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 854 | linkEvent); |
| 855 | return; |
| 856 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 857 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 858 | LinkEvent linkInTopo = topology.getLinkEvent(linkEvent.getLinkTuple(), |
| 859 | linkEvent.getType()); |
| 860 | if (linkInTopo == null) { |
| 861 | log.warn("Link {} already removed, ignoring", linkEvent); |
| 862 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 863 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 864 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 865 | if (log.isDebugEnabled()) { |
| 866 | // only do sanity check on debug level |
| 867 | |
| 868 | Link linkIn = dstPort.getIncomingLink(linkEvent.getType()); |
| 869 | if (linkIn == null) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 870 | log.warn("Link {} already removed on destination Port", |
| 871 | linkEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 872 | } |
| 873 | Link linkOut = srcPort.getOutgoingLink(linkEvent.getType()); |
| 874 | if (linkOut == null) { |
| 875 | log.warn("Link {} already removed on src Port", linkEvent); |
| 876 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 877 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 878 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 879 | log.debug("Removed {}", linkInTopo); |
| 880 | topology.removeLink(linkEvent.getLinkTuple(), linkEvent.getType()); |
| 881 | apiRemovedLinkEvents.add(linkInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 884 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 885 | * Adds a host to the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 886 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 887 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 888 | * TODO: Eventually, we might need to consider reordering |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 889 | * or {@link #addLink(LinkEvent)} and {@link #addHost(HostEvent)} events |
| 890 | * on the same port. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 891 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 892 | * @param hostEvent the HostEvent with the host to add. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 893 | * @return true if the item was successfully added, otherwise false. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 894 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 895 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 896 | private boolean addHost(HostEvent hostEvent) { |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 897 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 898 | // TODO Decide how to handle update scenario. |
| 899 | // If the new HostEvent has less attachment point compared to |
| 900 | // existing HostEvent, what should the event be? |
| 901 | // - AddHostEvent with some attachment point removed? (current behavior) |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 902 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 903 | // create unfrozen copy |
| 904 | // for removing attachment points which already has a link |
| 905 | HostEvent modifiedHostEvent = new HostEvent(hostEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 906 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 907 | // Verify each attachment point |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 908 | boolean attachmentFound = false; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 909 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 910 | // XXX domain knowledge: Port must exist before Host |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 911 | // but this knowledge cannot be pushed down to driver. |
| 912 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 913 | // Attached Ports must exist |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 914 | Port port = topology.getPort(swp.getDpid(), swp.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 915 | if (port == null) { |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 916 | log.debug("{} reordered because port {} was not there", |
| 917 | hostEvent, swp); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 918 | // Reordered event |
| 919 | return false; // should not continue if re-applying later |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 920 | } |
| 921 | // Attached Ports must not have Link |
| 922 | if (port.getOutgoingLink() != null || |
| 923 | port.getIncomingLink() != null) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 924 | log.warn("Link (Out:{},In:{}) exist on the attachment point. " |
| 925 | + "Ignoring this attachmentpoint ({}) from {}.", |
| 926 | port.getOutgoingLink(), port.getIncomingLink(), |
| 927 | swp, modifiedHostEvent); |
| 928 | // FIXME Should either reject, reorder this HostEvent, |
| 929 | // or remove attachment point from given HostEvent |
| 930 | // Removing attachment point from given HostEvent for now. |
| 931 | modifiedHostEvent.removeAttachmentPoint(swp); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 932 | continue; |
| 933 | } |
| 934 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 935 | attachmentFound = true; |
| 936 | } |
| 937 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 938 | // Update the host in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 939 | if (attachmentFound) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 940 | if (modifiedHostEvent.getAttachmentPoints().isEmpty()) { |
| 941 | log.warn("No valid attachment point left. Ignoring." |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame] | 942 | + "original: {}, modified: {}", |
| 943 | hostEvent, modifiedHostEvent); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 944 | // TODO Should we call #removeHost to trigger remove event? |
| 945 | // only if this call is update. |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 946 | return false; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | if (log.isDebugEnabled()) { |
| 950 | HostEvent host = topology.getHostEvent(hostEvent.getMac()); |
| 951 | if (host != null) { |
| 952 | log.debug("Update {}", modifiedHostEvent); |
| 953 | } else { |
| 954 | log.debug("Added {}", modifiedHostEvent); |
| 955 | } |
| 956 | } |
| 957 | topology.putHost(modifiedHostEvent.freeze()); |
| 958 | apiAddedHostEvents.add(modifiedHostEvent); |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 959 | return true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 960 | } |
Pavlin Radoslavov | d7b792e | 2014-08-01 02:47:47 -0700 | [diff] [blame] | 961 | return false; |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 962 | } |
| 963 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 964 | /** |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 965 | * Removes a host from the topology replica. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 966 | * <p/> |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 967 | * TODO: Host-related work is incomplete. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 968 | * |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 969 | * @param hostEvent the Host Event with the host to remove. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 970 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame] | 971 | @GuardedBy("topology.writeLock") |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 972 | private void removeHost(HostEvent hostEvent) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 973 | |
| 974 | final MACAddress mac = hostEvent.getMac(); |
| 975 | HostEvent hostInTopo = topology.getHostEvent(mac); |
| 976 | if (hostInTopo == null) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 977 | log.warn("Host {} already removed, ignoring", hostEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 978 | return; |
| 979 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 980 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 981 | log.debug("Removed {}", hostInTopo); |
| 982 | topology.removeHost(mac); |
| 983 | apiRemovedHostEvents.add(hostInTopo); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 984 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 985 | } |