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; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 5 | import java.util.Collection; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 6 | import java.util.HashMap; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 7 | import java.util.HashSet; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 8 | import java.util.LinkedList; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 9 | import java.util.List; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 10 | import java.util.Map; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 11 | import java.util.Set; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 12 | import java.util.concurrent.BlockingQueue; |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 13 | import java.util.concurrent.CopyOnWriteArrayList; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 14 | import java.util.concurrent.LinkedBlockingQueue; |
Yuta HIGUCHI | 8d762e9 | 2014-02-12 14:10:25 -0800 | [diff] [blame] | 15 | |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 16 | import javax.annotation.concurrent.GuardedBy; |
| 17 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 18 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 20 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 21 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datastore.topology.KVDevice; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.datastore.topology.KVLink; |
| 24 | import net.onrc.onos.core.datastore.topology.KVPort; |
| 25 | import net.onrc.onos.core.datastore.topology.KVSwitch; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.topology.PortEvent.SwitchPort; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.EventEntry; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 29 | |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 33 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 34 | * The TopologyManager receives topology updates from the southbound discovery |
| 35 | * modules and from other ONOS instances. These updates are processed and |
| 36 | * applied to the in-memory topology instance. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 38 | * - Maintain Invariant/Relationships between Topology Objects. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | * <p/> |
Yuta HIGUCHI | 765cd0d | 2014-02-06 12:46:41 -0800 | [diff] [blame] | 40 | * TODO To be synchronized based on TopologyEvent Notification. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | * <p/> |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 42 | * TODO TBD: Caller is expected to maintain parent/child calling order. Parent |
Yuta HIGUCHI | 1c70010 | 2014-02-12 16:30:52 -0800 | [diff] [blame] | 43 | * Object must exist before adding sub component(Add Switch -> Port). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | * <p/> |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 45 | * TODO TBD: This class may delay the requested change to handle event |
| 46 | * 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] | 47 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 48 | public class TopologyManager implements TopologyDiscoveryInterface { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 49 | |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 50 | private static final Logger log = LoggerFactory |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | .getLogger(TopologyManager.class); |
Yuta HIGUCHI | cd922f4 | 2014-02-11 18:59:11 -0800 | [diff] [blame] | 52 | |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 53 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
Jonathan Hart | 10a7e2b | 2014-02-21 18:30:08 -0800 | [diff] [blame] | 54 | public static final String EVENT_CHANNEL_NAME = "onos.topology"; |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 55 | private EventHandler eventHandler = new EventHandler(); |
| 56 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 57 | private final TopologyDatastore datastore; |
| 58 | private final TopologyImpl topology = new TopologyImpl(); |
Yuta HIGUCHI | 170229f | 2014-02-17 15:47:54 -0800 | [diff] [blame] | 59 | private final IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 60 | private CopyOnWriteArrayList<ITopologyListener> topologyListeners; |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 61 | |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 62 | // |
| 63 | // Local state for keeping track of reordered events. |
| 64 | // NOTE: Switch Events are not affected by the event reordering. |
| 65 | // |
| 66 | private Map<ByteBuffer, PortEvent> reorderedAddedPortEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | new HashMap<ByteBuffer, PortEvent>(); |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 68 | private Map<ByteBuffer, LinkEvent> reorderedAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | new HashMap<ByteBuffer, LinkEvent>(); |
Pavlin Radoslavov | 706add2 | 2014-02-20 12:15:59 -0800 | [diff] [blame] | 70 | private Map<ByteBuffer, DeviceEvent> reorderedAddedDeviceEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | new HashMap<ByteBuffer, DeviceEvent>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 72 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 73 | // |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 74 | // Local state for keeping track of locally discovered events so we can |
| 75 | // cleanup properly when a Switch or Port is removed. |
| 76 | // |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 77 | // We keep all Port, (incoming) Link and Device events per Switch DPID: |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 78 | // - If a switch goes down, we remove all corresponding Port, Link and |
| 79 | // Device events. |
| 80 | // - If a port on a switch goes down, we remove all corresponding Link |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 81 | // and Device events discovered by this instance. |
| 82 | // |
| 83 | // How to handle side-effect of remote events. |
| 84 | // - Remote Port Down event -> Link Down |
| 85 | // Not handled. (XXX Shouldn't it be removed from discovered.. Map) |
| 86 | // - Remote Device Added -> lose ownership of Device) |
| 87 | // Not handled. (XXX Shouldn't it be removed from discovered.. Map) |
| 88 | // |
| 89 | // XXX Domain knowledge based invariant maintenance should be moved to |
| 90 | // driver module, since the invariant may be different on optical, etc. |
| 91 | // |
| 92 | // What happens on leadership change? |
| 93 | // - Probably should: remove from discovered.. Maps, but not send DELETE events |
| 94 | // XXX Switch/Port can be rediscovered by new leader, but Link, Device? |
| 95 | // - Current: There is no way to recognize leadership change? |
| 96 | // ZookeeperRegistry.requestControl(long, ControlChangeCallback) |
| 97 | // is the only way to register listener, and it allows only 1 listener, |
| 98 | // which is already used by Controller class. |
| 99 | // |
| 100 | // FIXME Replace with concurrent variant. |
| 101 | // #removeSwitchDiscoveryEvent(SwitchEvent) runs in different thread. |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 102 | // |
| 103 | private Map<Long, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 105 | private Map<Long, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 107 | private Map<Long, Map<ByteBuffer, DeviceEvent>> discoveredAddedDeviceEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | new HashMap<>(); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 109 | |
| 110 | // |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 111 | // Local state for keeping track of the application event notifications |
| 112 | // |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 113 | // - Queue of events, which will be dispatched to local listeners |
| 114 | // on next notification. |
Yuta HIGUCHI | 703696c | 2014-06-25 20:36:45 -0700 | [diff] [blame] | 115 | |
| 116 | private List<SwitchEvent> apiAddedSwitchEvents = new LinkedList<>(); |
| 117 | private List<SwitchEvent> apiRemovedSwitchEvents = new LinkedList<>(); |
| 118 | private List<PortEvent> apiAddedPortEvents = new LinkedList<>(); |
| 119 | private List<PortEvent> apiRemovedPortEvents = new LinkedList<>(); |
| 120 | private List<LinkEvent> apiAddedLinkEvents = new LinkedList<>(); |
| 121 | private List<LinkEvent> apiRemovedLinkEvents = new LinkedList<>(); |
| 122 | private List<DeviceEvent> apiAddedDeviceEvents = new LinkedList<>(); |
| 123 | private List<DeviceEvent> apiRemovedDeviceEvents = new LinkedList<>(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 124 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Constructor. |
| 127 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 128 | * @param registryService the Registry Service to use. |
| 129 | * @param topologyListeners the collection of topology listeners to use. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 130 | */ |
| 131 | public TopologyManager(IControllerRegistryService registryService, |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 132 | CopyOnWriteArrayList<ITopologyListener> topologyListeners) { |
| 133 | datastore = new TopologyDatastore(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | this.registryService = registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 135 | this.topologyListeners = topologyListeners; |
Yuta HIGUCHI | 80829d1 | 2014-02-05 20:16:56 -0800 | [diff] [blame] | 136 | } |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 137 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 138 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 139 | * Get the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 140 | * |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 141 | * @return the Topology. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 142 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 143 | Topology getTopology() { |
| 144 | return topology; |
Pavlin Radoslavov | 6d224ee | 2014-02-18 16:43:15 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Yuta HIGUCHI | 4bfdd53 | 2014-02-07 13:47:36 -0800 | [diff] [blame] | 147 | /** |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 148 | * Event handler class. |
| 149 | */ |
| 150 | private class EventHandler extends Thread implements |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 151 | IEventChannelListener<byte[], TopologyEvent> { |
| 152 | private BlockingQueue<EventEntry<TopologyEvent>> topologyEvents = |
| 153 | new LinkedBlockingQueue<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 154 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Startup processing. |
| 157 | */ |
| 158 | private void startup() { |
| 159 | // |
| 160 | // TODO: Read all state from the database: |
| 161 | // |
| 162 | // Collection<EventEntry<TopologyEvent>> collection = |
| 163 | // readWholeTopologyFromDB(); |
| 164 | // |
| 165 | // For now, as a shortcut we read it from the datagrid |
| 166 | // |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 167 | Collection<TopologyEvent> allTopologyEvents = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | eventChannel.getAllEntries(); |
| 169 | Collection<EventEntry<TopologyEvent>> collection = |
| 170 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 171 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 172 | for (TopologyEvent topologyEvent : allTopologyEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 173 | EventEntry<TopologyEvent> eventEntry = |
| 174 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 175 | topologyEvent); |
| 176 | collection.add(eventEntry); |
| 177 | } |
| 178 | processEvents(collection); |
| 179 | } |
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 | * Run the thread. |
| 183 | */ |
| 184 | @Override |
| 185 | public void run() { |
| 186 | Collection<EventEntry<TopologyEvent>> collection = |
| 187 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 188 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | this.setName("TopologyManager.EventHandler " + this.getId()); |
| 190 | startup(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 191 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | // |
| 193 | // The main loop |
| 194 | // |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 195 | while (true) { |
| 196 | try { |
| 197 | EventEntry<TopologyEvent> eventEntry = |
| 198 | topologyEvents.take(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | collection.add(eventEntry); |
| 200 | topologyEvents.drainTo(collection); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 201 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | processEvents(collection); |
| 203 | collection.clear(); |
Pavlin Radoslavov | 8e881a4 | 2014-06-24 16:58:07 -0700 | [diff] [blame] | 204 | } catch (Exception exception) { |
| 205 | log.debug("Exception processing Topology Events: ", |
| 206 | exception); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 207 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 210 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Process all topology events. |
| 213 | * |
| 214 | * @param events the events to process. |
| 215 | */ |
| 216 | private void processEvents(Collection<EventEntry<TopologyEvent>> events) { |
| 217 | // Local state for computing the final set of events |
| 218 | Map<ByteBuffer, SwitchEvent> addedSwitchEvents = new HashMap<>(); |
| 219 | Map<ByteBuffer, SwitchEvent> removedSwitchEvents = new HashMap<>(); |
| 220 | Map<ByteBuffer, PortEvent> addedPortEvents = new HashMap<>(); |
| 221 | Map<ByteBuffer, PortEvent> removedPortEvents = new HashMap<>(); |
| 222 | Map<ByteBuffer, LinkEvent> addedLinkEvents = new HashMap<>(); |
| 223 | Map<ByteBuffer, LinkEvent> removedLinkEvents = new HashMap<>(); |
| 224 | Map<ByteBuffer, DeviceEvent> addedDeviceEvents = new HashMap<>(); |
| 225 | Map<ByteBuffer, DeviceEvent> removedDeviceEvents = new HashMap<>(); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 226 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 227 | // |
| 228 | // Classify and suppress matching events |
| 229 | // |
| 230 | for (EventEntry<TopologyEvent> event : events) { |
| 231 | TopologyEvent topologyEvent = event.eventData(); |
| 232 | SwitchEvent switchEvent = topologyEvent.switchEvent; |
| 233 | PortEvent portEvent = topologyEvent.portEvent; |
| 234 | LinkEvent linkEvent = topologyEvent.linkEvent; |
| 235 | DeviceEvent deviceEvent = topologyEvent.deviceEvent; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 236 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | // |
| 238 | // Extract the events |
| 239 | // |
| 240 | switch (event.eventType()) { |
| 241 | case ENTRY_ADD: |
| 242 | log.debug("Topology event ENTRY_ADD: {}", topologyEvent); |
| 243 | if (switchEvent != null) { |
| 244 | ByteBuffer id = switchEvent.getIDasByteBuffer(); |
| 245 | addedSwitchEvents.put(id, switchEvent); |
| 246 | removedSwitchEvents.remove(id); |
| 247 | // Switch Events are not affected by event reordering |
| 248 | } |
| 249 | if (portEvent != null) { |
| 250 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 251 | addedPortEvents.put(id, portEvent); |
| 252 | removedPortEvents.remove(id); |
| 253 | reorderedAddedPortEvents.remove(id); |
| 254 | } |
| 255 | if (linkEvent != null) { |
| 256 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 257 | addedLinkEvents.put(id, linkEvent); |
| 258 | removedLinkEvents.remove(id); |
| 259 | reorderedAddedLinkEvents.remove(id); |
| 260 | } |
| 261 | if (deviceEvent != null) { |
| 262 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 263 | addedDeviceEvents.put(id, deviceEvent); |
| 264 | removedDeviceEvents.remove(id); |
| 265 | reorderedAddedDeviceEvents.remove(id); |
| 266 | } |
| 267 | break; |
| 268 | case ENTRY_REMOVE: |
| 269 | log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent); |
| 270 | if (switchEvent != null) { |
| 271 | ByteBuffer id = switchEvent.getIDasByteBuffer(); |
| 272 | addedSwitchEvents.remove(id); |
| 273 | removedSwitchEvents.put(id, switchEvent); |
| 274 | // Switch Events are not affected by event reordering |
| 275 | } |
| 276 | if (portEvent != null) { |
| 277 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 278 | addedPortEvents.remove(id); |
| 279 | removedPortEvents.put(id, portEvent); |
| 280 | reorderedAddedPortEvents.remove(id); |
| 281 | } |
| 282 | if (linkEvent != null) { |
| 283 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 284 | addedLinkEvents.remove(id); |
| 285 | removedLinkEvents.put(id, linkEvent); |
| 286 | reorderedAddedLinkEvents.remove(id); |
| 287 | } |
| 288 | if (deviceEvent != null) { |
| 289 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 290 | addedDeviceEvents.remove(id); |
| 291 | removedDeviceEvents.put(id, deviceEvent); |
| 292 | reorderedAddedDeviceEvents.remove(id); |
| 293 | } |
| 294 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 295 | default: |
| 296 | log.error("Unknown topology event {}", |
| 297 | event.eventType()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 300 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 301 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 302 | // Lock the topology while it is modified |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 303 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 304 | topology.acquireWriteLock(); |
Pavlin Radoslavov | 8ffb8bf | 2014-02-20 15:34:26 -0800 | [diff] [blame] | 305 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 306 | try { |
| 307 | // |
| 308 | // Apply the classified events. |
| 309 | // |
| 310 | // Apply the "add" events in the proper order: |
| 311 | // switch, port, link, device |
| 312 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 313 | for (SwitchEvent switchEvent : addedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 314 | addSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 315 | } |
| 316 | for (PortEvent portEvent : addedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 318 | } |
| 319 | for (LinkEvent linkEvent : addedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 321 | } |
| 322 | for (DeviceEvent deviceEvent : addedDeviceEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | addDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 324 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 325 | // |
| 326 | // Apply the "remove" events in the reverse order: |
| 327 | // device, link, port, switch |
| 328 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 329 | for (DeviceEvent deviceEvent : removedDeviceEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 330 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 331 | } |
| 332 | for (LinkEvent linkEvent : removedLinkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 333 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 334 | } |
| 335 | for (PortEvent portEvent : removedPortEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 336 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 337 | } |
| 338 | for (SwitchEvent switchEvent : removedSwitchEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 339 | removeSwitch(switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 340 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 341 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 342 | // |
| 343 | // Apply reordered events |
| 344 | // |
| 345 | applyReorderedEvents(!addedSwitchEvents.isEmpty(), |
| 346 | !addedPortEvents.isEmpty()); |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 347 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 348 | } finally { |
| 349 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 350 | // Topology modifications completed: Release the lock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 351 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 352 | topology.releaseWriteLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 353 | } |
Yuta HIGUCHI | 3aca81a | 2014-02-23 12:41:19 -0800 | [diff] [blame] | 354 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 355 | // |
| 356 | // Dispatch the Topology Notification Events to the applications |
| 357 | // |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 358 | dispatchTopologyEvents(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 359 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 360 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Receive a notification that an entry is added. |
| 363 | * |
| 364 | * @param value the value for the entry. |
| 365 | */ |
| 366 | @Override |
| 367 | public void entryAdded(TopologyEvent value) { |
| 368 | EventEntry<TopologyEvent> eventEntry = |
| 369 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 370 | value); |
| 371 | topologyEvents.add(eventEntry); |
| 372 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 373 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 374 | /** |
| 375 | * Receive a notification that an entry is removed. |
| 376 | * |
| 377 | * @param value the value for the entry. |
| 378 | */ |
| 379 | @Override |
| 380 | public void entryRemoved(TopologyEvent value) { |
| 381 | EventEntry<TopologyEvent> eventEntry = |
| 382 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 383 | value); |
| 384 | topologyEvents.add(eventEntry); |
| 385 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 386 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 387 | /** |
| 388 | * Receive a notification that an entry is updated. |
| 389 | * |
| 390 | * @param value the value for the entry. |
| 391 | */ |
| 392 | @Override |
| 393 | public void entryUpdated(TopologyEvent value) { |
| 394 | // NOTE: The ADD and UPDATE events are processed in same way |
| 395 | entryAdded(value); |
| 396 | } |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Startup processing. |
| 401 | * |
| 402 | * @param datagridService the datagrid service to use. |
| 403 | */ |
| 404 | void startup(IDatagridService datagridService) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 405 | eventChannel = datagridService.addListener(EVENT_CHANNEL_NAME, |
| 406 | eventHandler, |
| 407 | byte[].class, |
| 408 | TopologyEvent.class); |
| 409 | eventHandler.start(); |
Pavlin Radoslavov | 721a2e0 | 2014-02-14 23:40:14 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 412 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 413 | * Dispatch Topology Events to the listeners. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 414 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 415 | private void dispatchTopologyEvents() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 416 | if (apiAddedSwitchEvents.isEmpty() && |
| 417 | apiRemovedSwitchEvents.isEmpty() && |
| 418 | apiAddedPortEvents.isEmpty() && |
| 419 | apiRemovedPortEvents.isEmpty() && |
| 420 | apiAddedLinkEvents.isEmpty() && |
| 421 | apiRemovedLinkEvents.isEmpty() && |
| 422 | apiAddedDeviceEvents.isEmpty() && |
| 423 | apiRemovedDeviceEvents.isEmpty()) { |
| 424 | return; // No events to dispatch |
| 425 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 426 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 427 | if (log.isDebugEnabled()) { |
| 428 | // |
| 429 | // Debug statements |
| 430 | // TODO: Those statements should be removed in the future |
| 431 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 432 | for (SwitchEvent switchEvent : apiAddedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 433 | log.debug("Dispatch Topology Event: ADDED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 434 | } |
| 435 | for (SwitchEvent switchEvent : apiRemovedSwitchEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 436 | log.debug("Dispatch Topology Event: REMOVED {}", switchEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 437 | } |
| 438 | for (PortEvent portEvent : apiAddedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 439 | log.debug("Dispatch Topology Event: ADDED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 440 | } |
| 441 | for (PortEvent portEvent : apiRemovedPortEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 442 | log.debug("Dispatch Topology Event: REMOVED {}", portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 443 | } |
| 444 | for (LinkEvent linkEvent : apiAddedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 445 | log.debug("Dispatch Topology Event: ADDED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 446 | } |
| 447 | for (LinkEvent linkEvent : apiRemovedLinkEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 448 | log.debug("Dispatch Topology Event: REMOVED {}", linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 449 | } |
| 450 | for (DeviceEvent deviceEvent : apiAddedDeviceEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 451 | log.debug("Dispatch Topology Event: ADDED {}", deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 452 | } |
| 453 | for (DeviceEvent deviceEvent : apiRemovedDeviceEvents) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 454 | log.debug("Dispatch Topology Event: REMOVED {}", deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 455 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 456 | } |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 457 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 458 | // Deliver the events |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 459 | for (ITopologyListener listener : this.topologyListeners) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 460 | // TODO: Should copy before handing them over to listener? |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 461 | listener.topologyEvents(apiAddedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 462 | apiRemovedSwitchEvents, |
| 463 | apiAddedPortEvents, |
| 464 | apiRemovedPortEvents, |
| 465 | apiAddedLinkEvents, |
| 466 | apiRemovedLinkEvents, |
| 467 | apiAddedDeviceEvents, |
| 468 | apiRemovedDeviceEvents); |
| 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 | // |
| 472 | // Cleanup |
| 473 | // |
| 474 | apiAddedSwitchEvents.clear(); |
| 475 | apiRemovedSwitchEvents.clear(); |
| 476 | apiAddedPortEvents.clear(); |
| 477 | apiRemovedPortEvents.clear(); |
| 478 | apiAddedLinkEvents.clear(); |
| 479 | apiRemovedLinkEvents.clear(); |
| 480 | apiAddedDeviceEvents.clear(); |
| 481 | apiRemovedDeviceEvents.clear(); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Apply reordered events. |
| 486 | * |
| 487 | * @param hasAddedSwitchEvents true if there were Added Switch Events. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 488 | * @param hasAddedPortEvents true if there were Added Port Events. |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 489 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 490 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 491 | private void applyReorderedEvents(boolean hasAddedSwitchEvents, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 492 | boolean hasAddedPortEvents) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 493 | if (!(hasAddedSwitchEvents || hasAddedPortEvents)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 494 | return; // Nothing to do |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 495 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 496 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 497 | // |
| 498 | // Try to apply the reordered events. |
| 499 | // |
| 500 | // NOTE: For simplicity we try to apply all events of a particular |
| 501 | // type if any "parent" type event was processed: |
| 502 | // - Apply reordered Port Events if Switches were added |
| 503 | // - Apply reordered Link and Device Events if Switches or Ports |
| 504 | // were added |
| 505 | // |
admin | bc18155 | 2014-02-21 18:36:42 -0800 | [diff] [blame] | 506 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 507 | // |
| 508 | // Apply reordered Port Events if Switches were added |
| 509 | // |
| 510 | if (hasAddedSwitchEvents) { |
| 511 | Map<ByteBuffer, PortEvent> portEvents = reorderedAddedPortEvents; |
| 512 | reorderedAddedPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 513 | for (PortEvent portEvent : portEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 514 | addPort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 515 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 516 | } |
| 517 | // |
| 518 | // Apply reordered Link and Device Events if Switches or Ports |
| 519 | // were added. |
| 520 | // |
| 521 | Map<ByteBuffer, LinkEvent> linkEvents = reorderedAddedLinkEvents; |
| 522 | reorderedAddedLinkEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 523 | for (LinkEvent linkEvent : linkEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 524 | addLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 525 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 526 | // |
| 527 | Map<ByteBuffer, DeviceEvent> deviceEvents = reorderedAddedDeviceEvents; |
| 528 | reorderedAddedDeviceEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 529 | for (DeviceEvent deviceEvent : deviceEvents.values()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 530 | addDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 531 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 534 | /** |
| 535 | * Switch discovered event. |
| 536 | * |
| 537 | * @param switchEvent the switch event. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 538 | * @param portEvents the corresponding port events for the switch. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 539 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 540 | @Override |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 541 | public void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 542 | Collection<PortEvent> portEvents) { |
| 543 | if (datastore.addSwitch(switchEvent, portEvents)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 544 | log.debug("Sending add switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 545 | // Send out notification |
| 546 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 547 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 548 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 549 | // Send out notification for each port |
| 550 | for (PortEvent portEvent : portEvents) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 551 | log.debug("Sending add port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 552 | topologyEvent = new TopologyEvent(portEvent); |
| 553 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 554 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 555 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 556 | // |
| 557 | // Keep track of the added ports |
| 558 | // |
| 559 | // Get the old Port Events |
| 560 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 561 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 562 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 564 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 565 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 566 | // Store the new Port Events in the local cache |
| 567 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 568 | for (PortEvent portEvent : portEvents) { |
| 569 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 570 | newPortEvents.put(id, portEvent); |
| 571 | } |
| 572 | discoveredAddedPortEvents.put(switchEvent.getDpid(), |
| 573 | newPortEvents); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 574 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 575 | // |
| 576 | // Extract the removed ports |
| 577 | // |
| 578 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 579 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 580 | ByteBuffer key = entry.getKey(); |
| 581 | PortEvent portEvent = entry.getValue(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 582 | if (!newPortEvents.containsKey(key)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 583 | removedPortEvents.add(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 584 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Cleanup old removed ports |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 588 | for (PortEvent portEvent : removedPortEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 589 | removePortDiscoveryEvent(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 590 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 591 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 594 | /** |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 595 | * {@inheritDoc} |
| 596 | * <p/> |
| 597 | * Called by {@link TopologyPublisher.SwitchCleanup} thread. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 598 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 599 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 600 | public void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 601 | // Get the old Port Events |
| 602 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 603 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 604 | if (oldPortEvents == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 605 | oldPortEvents = new HashMap<>(); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 606 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 607 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 608 | if (datastore.deactivateSwitch(switchEvent, oldPortEvents.values())) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 609 | log.debug("Sending remove switch: {}", switchEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 610 | // Send out notification |
| 611 | eventChannel.removeEntry(switchEvent.getID()); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 612 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 613 | // |
| 614 | // Send out notification for each port. |
| 615 | // |
| 616 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 617 | // because it will attempt to remove the port from the database, |
| 618 | // and the deactiveSwitch() call above already removed all ports. |
| 619 | // |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 620 | for (PortEvent portEvent : oldPortEvents.values()) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 621 | log.debug("Sending remove port:", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 622 | eventChannel.removeEntry(portEvent.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 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 636 | // Cleanup for each device |
| 637 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 638 | discoveredAddedDeviceEvents.get(switchEvent.getDpid()); |
| 639 | if (oldDeviceEvents != null) { |
| 640 | for (DeviceEvent deviceEvent : new ArrayList<>(oldDeviceEvents.values())) { |
| 641 | removeDeviceDiscoveryEvent(deviceEvent); |
| 642 | } |
| 643 | discoveredAddedDeviceEvents.remove(switchEvent.getDpid()); |
| 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 |
| 658 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 659 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 660 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 661 | // Store the new Port Event in the local cache |
| 662 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 663 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 664 | if (oldPortEvents == null) { |
| 665 | oldPortEvents = new HashMap<>(); |
| 666 | discoveredAddedPortEvents.put(portEvent.getDpid(), |
| 667 | oldPortEvents); |
| 668 | } |
| 669 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 670 | oldPortEvents.put(id, portEvent); |
| 671 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 674 | /** |
| 675 | * Port removed event. |
| 676 | * |
| 677 | * @param portEvent the port event. |
| 678 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 679 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 680 | public void removePortDiscoveryEvent(PortEvent portEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 681 | if (datastore.deactivatePort(portEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 682 | log.debug("Sending remove port: {}", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 683 | // Send out notification |
| 684 | eventChannel.removeEntry(portEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 685 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 686 | // Cleanup the Port Event from the local cache |
| 687 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 688 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 689 | if (oldPortEvents != null) { |
| 690 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 691 | oldPortEvents.remove(id); |
| 692 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 693 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 694 | // Cleanup for the incoming link |
| 695 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 696 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 697 | if (oldLinkEvents != null) { |
| 698 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 699 | if (linkEvent.getDst().equals(portEvent.id)) { |
| 700 | removeLinkDiscoveryEvent(linkEvent); |
| 701 | // XXX If we change our model to allow multiple Link on |
| 702 | // a Port, this loop must be fixed to allow continuing. |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | } |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 707 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 708 | // Cleanup for the connected devices |
| 709 | // TODO: The implementation below is probably wrong |
| 710 | List<DeviceEvent> removedDeviceEvents = new LinkedList<>(); |
| 711 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 712 | discoveredAddedDeviceEvents.get(portEvent.getDpid()); |
| 713 | if (oldDeviceEvents != null) { |
| 714 | for (DeviceEvent deviceEvent : new ArrayList<>(oldDeviceEvents.values())) { |
| 715 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 716 | if (swp.equals(portEvent.id)) { |
| 717 | removedDeviceEvents.add(deviceEvent); |
| 718 | } |
| 719 | } |
| 720 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 721 | for (DeviceEvent deviceEvent : removedDeviceEvents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 722 | removeDeviceDiscoveryEvent(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 723 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 728 | /** |
| 729 | * Link discovered event. |
| 730 | * |
| 731 | * @param linkEvent the link event. |
| 732 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 733 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 734 | public void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 735 | if (datastore.addLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 736 | log.debug("Sending add link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 737 | // Send out notification |
| 738 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 739 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 740 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 741 | // Store the new Link Event in the local cache |
| 742 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 743 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 744 | if (oldLinkEvents == null) { |
| 745 | oldLinkEvents = new HashMap<>(); |
| 746 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 747 | oldLinkEvents); |
| 748 | } |
| 749 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 750 | oldLinkEvents.put(id, linkEvent); |
| 751 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 754 | /** |
| 755 | * Link removed event. |
| 756 | * |
| 757 | * @param linkEvent the link event. |
| 758 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 759 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 760 | public void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 761 | if (datastore.removeLink(linkEvent)) { |
Jonathan Hart | 92c819f | 2014-05-30 10:53:30 -0700 | [diff] [blame] | 762 | log.debug("Sending remove link: {}", linkEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 763 | // Send out notification |
| 764 | eventChannel.removeEntry(linkEvent.getID()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 765 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 766 | // Cleanup the Link Event from the local cache |
| 767 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 768 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 769 | if (oldLinkEvents != null) { |
| 770 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 771 | oldLinkEvents.remove(id); |
| 772 | } |
| 773 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 774 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 775 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 776 | /** |
| 777 | * Device discovered event. |
| 778 | * |
| 779 | * @param deviceEvent the device event. |
| 780 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 781 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 782 | public void putDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 783 | if (datastore.addDevice(deviceEvent)) { |
| 784 | // Send out notification |
| 785 | TopologyEvent topologyEvent = new TopologyEvent(deviceEvent); |
| 786 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 787 | log.debug("Put the device info into the cache of the topology. mac {}", deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 788 | |
| 789 | // Store the new Device Event in the local cache |
| 790 | // TODO: The implementation below is probably wrong |
| 791 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 792 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 793 | discoveredAddedDeviceEvents.get(swp.getDpid()); |
| 794 | if (oldDeviceEvents == null) { |
| 795 | oldDeviceEvents = new HashMap<>(); |
| 796 | discoveredAddedDeviceEvents.put(swp.getDpid(), |
| 797 | oldDeviceEvents); |
| 798 | } |
| 799 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 800 | oldDeviceEvents.put(id, deviceEvent); |
| 801 | } |
| 802 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 803 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 804 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 805 | /** |
| 806 | * Device removed event. |
| 807 | * |
| 808 | * @param deviceEvent the device event. |
| 809 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 810 | @Override |
Pavlin Radoslavov | 6ea84a4 | 2014-02-19 15:50:01 -0800 | [diff] [blame] | 811 | public void removeDeviceDiscoveryEvent(DeviceEvent deviceEvent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 812 | if (datastore.removeDevice(deviceEvent)) { |
| 813 | // Send out notification |
| 814 | eventChannel.removeEntry(deviceEvent.getID()); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 815 | log.debug("Remove the device info into the cache of the topology. mac {}", deviceEvent.getMac()); |
Pavlin Radoslavov | 26d8340 | 2014-02-20 15:24:30 -0800 | [diff] [blame] | 816 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 817 | // Cleanup the Device Event from the local cache |
| 818 | // TODO: The implementation below is probably wrong |
| 819 | ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID()); |
| 820 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 821 | Map<ByteBuffer, DeviceEvent> oldDeviceEvents = |
| 822 | discoveredAddedDeviceEvents.get(swp.getDpid()); |
| 823 | if (oldDeviceEvents != null) { |
| 824 | oldDeviceEvents.remove(id); |
| 825 | } |
| 826 | } |
| 827 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 828 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 829 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 830 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 831 | * Add a switch to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 832 | * |
| 833 | * @param switchEvent the Switch Event with the switch to add. |
| 834 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 835 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 836 | private void addSwitch(SwitchEvent switchEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 837 | Switch sw = topology.getSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 838 | if (sw == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 839 | sw = new SwitchImpl(topology, switchEvent.getDpid()); |
| 840 | topology.putSwitch(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 841 | } else { |
| 842 | // TODO: Update the switch attributes |
| 843 | // TODO: Nothing to do for now |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 844 | log.debug("Update switch attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 845 | } |
| 846 | apiAddedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 849 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 850 | * Remove a switch from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 851 | * |
| 852 | * @param switchEvent the Switch Event with the switch to remove. |
| 853 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 854 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 855 | private void removeSwitch(SwitchEvent switchEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 856 | Switch sw = topology.getSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 857 | if (sw == null) { |
| 858 | log.warn("Switch {} already removed, ignoring", switchEvent); |
| 859 | return; |
| 860 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 861 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 862 | // |
| 863 | // Remove all Ports on the Switch |
| 864 | // |
| 865 | ArrayList<PortEvent> portsToRemove = new ArrayList<>(); |
| 866 | for (Port port : sw.getPorts()) { |
| 867 | log.warn("Port {} on Switch {} should be removed prior to removing Switch. Removing Port now.", |
| 868 | port, switchEvent); |
| 869 | PortEvent portEvent = new PortEvent(port.getDpid(), |
| 870 | port.getNumber()); |
| 871 | portsToRemove.add(portEvent); |
| 872 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 873 | for (PortEvent portEvent : portsToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 874 | removePort(portEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 875 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 876 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 877 | topology.removeSwitch(switchEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 878 | apiRemovedSwitchEvents.add(switchEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 881 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 882 | * Add a port to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 883 | * |
| 884 | * @param portEvent the Port Event with the port to add. |
| 885 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 886 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 887 | private void addPort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 888 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 889 | if (sw == null) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 890 | log.debug("{} reordered because switch is null", portEvent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 891 | // Reordered event: delay the event in local cache |
| 892 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 893 | reorderedAddedPortEvents.put(id, portEvent); |
| 894 | return; |
| 895 | } |
| 896 | SwitchImpl switchImpl = getSwitchImpl(sw); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 897 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 898 | Port port = sw.getPort(portEvent.getNumber()); |
| 899 | if (port == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 900 | port = new PortImpl(topology, sw, portEvent.getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 901 | switchImpl.addPort(port); |
| 902 | } else { |
| 903 | // TODO: Update the port attributes |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 904 | log.debug("Update port attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 905 | } |
| 906 | apiAddedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 909 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 910 | * Remove a port from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 911 | * |
| 912 | * @param portEvent the Port Event with the port to remove. |
| 913 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 914 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 915 | private void removePort(PortEvent portEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 916 | Switch sw = topology.getSwitch(portEvent.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 917 | if (sw == null) { |
| 918 | log.warn("Parent Switch for Port {} already removed, ignoring", |
| 919 | portEvent); |
| 920 | return; |
| 921 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 922 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 923 | Port port = sw.getPort(portEvent.getNumber()); |
| 924 | if (port == null) { |
| 925 | log.warn("Port {} already removed, ignoring", portEvent); |
| 926 | return; |
| 927 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 928 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 929 | // |
| 930 | // Remove all Devices attached to the Port |
| 931 | // |
| 932 | ArrayList<DeviceEvent> devicesToRemove = new ArrayList<>(); |
| 933 | for (Device device : port.getDevices()) { |
| 934 | log.debug("Removing Device {} on Port {}", device, portEvent); |
| 935 | DeviceEvent deviceEvent = new DeviceEvent(device.getMacAddress()); |
| 936 | SwitchPort switchPort = new SwitchPort(port.getSwitch().getDpid(), |
| 937 | port.getNumber()); |
| 938 | deviceEvent.addAttachmentPoint(switchPort); |
| 939 | devicesToRemove.add(deviceEvent); |
| 940 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 941 | for (DeviceEvent deviceEvent : devicesToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 942 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 943 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 944 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 945 | // |
| 946 | // Remove all Links connected to the Port |
| 947 | // |
| 948 | Set<Link> links = new HashSet<>(); |
| 949 | links.add(port.getOutgoingLink()); |
| 950 | links.add(port.getIncomingLink()); |
| 951 | ArrayList<LinkEvent> linksToRemove = new ArrayList<>(); |
| 952 | for (Link link : links) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 953 | if (link == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 954 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 955 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 956 | log.debug("Removing Link {} on Port {}", link, portEvent); |
| 957 | LinkEvent linkEvent = new LinkEvent(link.getSrcSwitch().getDpid(), |
| 958 | link.getSrcPort().getNumber(), |
| 959 | link.getDstSwitch().getDpid(), |
| 960 | link.getDstPort().getNumber()); |
| 961 | linksToRemove.add(linkEvent); |
| 962 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 963 | for (LinkEvent linkEvent : linksToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 964 | removeLink(linkEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 965 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 966 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 967 | // Remove the Port from the Switch |
| 968 | SwitchImpl switchImpl = getSwitchImpl(sw); |
| 969 | switchImpl.removePort(port); |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 970 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 971 | apiRemovedPortEvents.add(portEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 974 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 975 | * Add a link to the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 976 | * |
| 977 | * @param linkEvent the Link Event with the link to add. |
| 978 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 979 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 980 | private void addLink(LinkEvent linkEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 981 | Port srcPort = topology.getPort(linkEvent.getSrc().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 982 | linkEvent.getSrc().number); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 983 | Port dstPort = topology.getPort(linkEvent.getDst().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 984 | linkEvent.getDst().number); |
| 985 | if ((srcPort == null) || (dstPort == null)) { |
Jonathan Hart | f167520 | 2014-05-23 14:59:07 -0700 | [diff] [blame] | 986 | log.debug("{} reordered because {} port is null", linkEvent, |
| 987 | (srcPort == null) ? "src" : "dst"); |
| 988 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 989 | // Reordered event: delay the event in local cache |
| 990 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 991 | reorderedAddedLinkEvents.put(id, linkEvent); |
| 992 | return; |
| 993 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 994 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 995 | // Get the Link instance from the Destination Port Incoming Link |
| 996 | Link link = dstPort.getIncomingLink(); |
| 997 | assert (link == srcPort.getOutgoingLink()); |
| 998 | if (link == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 999 | link = new LinkImpl(topology, srcPort, dstPort); |
| 1000 | topology.putLink(link); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1001 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1002 | // Remove all Devices attached to the Ports |
| 1003 | ArrayList<DeviceEvent> devicesToRemove = new ArrayList<>(); |
| 1004 | ArrayList<Port> ports = new ArrayList<>(); |
| 1005 | ports.add(srcPort); |
| 1006 | ports.add(dstPort); |
| 1007 | for (Port port : ports) { |
| 1008 | for (Device device : port.getDevices()) { |
| 1009 | log.error("Device {} on Port {} should have been removed prior to adding Link {}", |
| 1010 | device, port, linkEvent); |
| 1011 | DeviceEvent deviceEvent = |
| 1012 | new DeviceEvent(device.getMacAddress()); |
| 1013 | SwitchPort switchPort = |
| 1014 | new SwitchPort(port.getSwitch().getDpid(), |
| 1015 | port.getNumber()); |
| 1016 | deviceEvent.addAttachmentPoint(switchPort); |
| 1017 | devicesToRemove.add(deviceEvent); |
| 1018 | } |
| 1019 | } |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1020 | for (DeviceEvent deviceEvent : devicesToRemove) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1021 | removeDevice(deviceEvent); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 1022 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1023 | } else { |
| 1024 | // TODO: Update the link attributes |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 1025 | log.debug("Update link attributes"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1026 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1027 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1028 | apiAddedLinkEvents.add(linkEvent); |
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 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1032 | * Remove a link from the topology. |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1033 | * |
| 1034 | * @param linkEvent the Link Event with the link to remove. |
| 1035 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 1036 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1037 | private void removeLink(LinkEvent linkEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1038 | Port srcPort = topology.getPort(linkEvent.getSrc().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1039 | linkEvent.getSrc().number); |
| 1040 | if (srcPort == null) { |
| 1041 | log.warn("Src Port for Link {} already removed, ignoring", |
| 1042 | linkEvent); |
| 1043 | return; |
| 1044 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1045 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1046 | Port dstPort = topology.getPort(linkEvent.getDst().dpid, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1047 | linkEvent.getDst().number); |
| 1048 | if (dstPort == null) { |
| 1049 | log.warn("Dst Port for Link {} already removed, ignoring", |
| 1050 | linkEvent); |
| 1051 | return; |
| 1052 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1053 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1054 | // |
| 1055 | // Remove the Link instance from the Destination Port Incoming Link |
| 1056 | // and the Source Port Outgoing Link. |
| 1057 | // |
| 1058 | Link link = dstPort.getIncomingLink(); |
| 1059 | if (link == null) { |
| 1060 | log.warn("Link {} already removed on destination Port", linkEvent); |
| 1061 | } |
| 1062 | link = srcPort.getOutgoingLink(); |
| 1063 | if (link == null) { |
| 1064 | log.warn("Link {} already removed on src Port", linkEvent); |
| 1065 | } |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1066 | |
| 1067 | // TODO should we check that we get the same link from each port? |
| 1068 | if (link != null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1069 | topology.removeLink(link); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 1070 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1071 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1072 | apiRemovedLinkEvents.add(linkEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1075 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1076 | * Add a device to the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1077 | * <p/> |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1078 | * TODO: Device-related work is incomplete. |
| 1079 | * TODO: Eventually, we might need to consider reordering |
| 1080 | * or addLink() and addDevice() events on the same port. |
| 1081 | * |
| 1082 | * @param deviceEvent the Device Event with the device to add. |
| 1083 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 1084 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1085 | private void addDevice(DeviceEvent deviceEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1086 | log.debug("Adding a device to the topology with mac {}", deviceEvent.getMac()); |
| 1087 | Device device = topology.getDeviceByMac(deviceEvent.getMac()); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1088 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1089 | if (device == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1090 | log.debug("Existing device was not found in the Topology: Adding new device: mac {}", deviceEvent.getMac()); |
| 1091 | device = new DeviceImpl(topology, deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1092 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1093 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1094 | DeviceImpl deviceImpl = getDeviceImpl(device); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1095 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1096 | // Process each attachment point |
| 1097 | boolean attachmentFound = false; |
| 1098 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 1099 | // Attached Ports must exist |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1100 | Port port = topology.getPort(swp.dpid, swp.number); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1101 | if (port == null) { |
| 1102 | // Reordered event: delay the event in local cache |
| 1103 | ByteBuffer id = deviceEvent.getIDasByteBuffer(); |
| 1104 | reorderedAddedDeviceEvents.put(id, deviceEvent); |
| 1105 | continue; |
| 1106 | } |
| 1107 | // Attached Ports must not have Link |
| 1108 | if (port.getOutgoingLink() != null || |
| 1109 | port.getIncomingLink() != null) { |
| 1110 | log.warn("Link (Out:{},In:{}) exist on the attachment point, skipping mutation.", |
| 1111 | port.getOutgoingLink(), |
| 1112 | port.getIncomingLink()); |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
| 1116 | // Add Device <-> Port attachment |
| 1117 | PortImpl portImpl = getPortImpl(port); |
| 1118 | portImpl.addDevice(device); |
| 1119 | deviceImpl.addAttachmentPoint(port); |
| 1120 | attachmentFound = true; |
| 1121 | } |
| 1122 | |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 1123 | deviceImpl.setLastSeenTime(deviceEvent.getLastSeenTime()); |
| 1124 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1125 | // Update the device in the topology |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1126 | if (attachmentFound) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1127 | log.debug("Storing the device info into the Topology: mac {}", deviceEvent.getMac()); |
| 1128 | topology.putDevice(device); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1129 | apiAddedDeviceEvents.add(deviceEvent); |
| 1130 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1133 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1134 | * Remove a device from the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1135 | * <p/> |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1136 | * TODO: Device-related work is incomplete. |
| 1137 | * |
| 1138 | * @param deviceEvent the Device Event with the device to remove. |
| 1139 | */ |
Yuta HIGUCHI | bc67a05 | 2014-06-30 10:37:09 -0700 | [diff] [blame^] | 1140 | @GuardedBy("topology.writeLock") |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1141 | private void removeDevice(DeviceEvent deviceEvent) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1142 | log.debug("Removing a device to the topology: mac {}", deviceEvent.getMac()); |
| 1143 | Device device = topology.getDeviceByMac(deviceEvent.getMac()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1144 | if (device == null) { |
| 1145 | log.warn("Device {} already removed, ignoring", deviceEvent); |
| 1146 | return; |
| 1147 | } |
| 1148 | DeviceImpl deviceImpl = getDeviceImpl(device); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1149 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1150 | // Process each attachment point |
| 1151 | for (SwitchPort swp : deviceEvent.getAttachmentPoints()) { |
| 1152 | // Attached Ports must exist |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1153 | Port port = topology.getPort(swp.dpid, swp.number); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1154 | if (port == null) { |
| 1155 | log.warn("Port for the attachment point {} did not exist. skipping attachment point mutation", swp); |
| 1156 | continue; |
| 1157 | } |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1158 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1159 | // Remove Device <-> Port attachment |
| 1160 | PortImpl portImpl = getPortImpl(port); |
| 1161 | portImpl.removeDevice(device); |
| 1162 | deviceImpl.removeAttachmentPoint(port); |
| 1163 | } |
Pavlin Radoslavov | 74986ce | 2014-02-20 13:17:20 -0800 | [diff] [blame] | 1164 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 1165 | log.debug("Removing the device info into the Topology: mac {}", deviceEvent.getMac()); |
| 1166 | topology.removeDevice(device); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1167 | apiRemovedDeviceEvents.add(deviceEvent); |
Pavlin Radoslavov | 3c9cc55 | 2014-02-20 09:58:38 -0800 | [diff] [blame] | 1168 | } |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 1169 | |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1170 | /** |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1171 | * Get the SwitchImpl-casted switch implementation. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1172 | * |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1173 | * @param sw the Switch to cast. |
| 1174 | * @return the SwitchImpl-casted switch implementation. |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1175 | */ |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1176 | private SwitchImpl getSwitchImpl(Switch sw) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1177 | if (sw instanceof SwitchImpl) { |
| 1178 | return (SwitchImpl) sw; |
| 1179 | } |
| 1180 | throw new ClassCastException("SwitchImpl expected, but found: " + sw); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1183 | /** |
| 1184 | * Get the PortImpl-casted port implementation. |
| 1185 | * |
| 1186 | * @param port the Port to cast. |
| 1187 | * @return the PortImpl-casted port implementation. |
| 1188 | */ |
| 1189 | private PortImpl getPortImpl(Port port) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1190 | if (port instanceof PortImpl) { |
| 1191 | return (PortImpl) port; |
| 1192 | } |
| 1193 | throw new ClassCastException("PortImpl expected, but found: " + port); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1196 | /** |
| 1197 | * Get the LinkImpl-casted link implementation. |
| 1198 | * |
| 1199 | * @param link the Link to cast. |
| 1200 | * @return the LinkImpl-casted link implementation. |
| 1201 | */ |
| 1202 | private LinkImpl getLinkImpl(Link link) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1203 | if (link instanceof LinkImpl) { |
| 1204 | return (LinkImpl) link; |
| 1205 | } |
| 1206 | throw new ClassCastException("LinkImpl expected, but found: " + link); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1209 | /** |
| 1210 | * Get the DeviceImpl-casted device implementation. |
| 1211 | * |
| 1212 | * @param device the Device to cast. |
| 1213 | * @return the DeviceImpl-casted device implementation. |
| 1214 | */ |
| 1215 | private DeviceImpl getDeviceImpl(Device device) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1216 | if (device instanceof DeviceImpl) { |
| 1217 | return (DeviceImpl) device; |
| 1218 | } |
| 1219 | throw new ClassCastException("DeviceImpl expected, but found: " + device); |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1220 | } |
| 1221 | |
Pavlin Radoslavov | 734ff5a | 2014-02-26 10:20:43 -0800 | [diff] [blame] | 1222 | /** |
| 1223 | * Read the whole topology from the database. |
| 1224 | * |
| 1225 | * @return a collection of EventEntry-encapsulated Topology Events for |
| 1226 | * the whole topology. |
| 1227 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1228 | private Collection<EventEntry<TopologyEvent>> readWholeTopologyFromDB() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1229 | Collection<EventEntry<TopologyEvent>> collection = |
| 1230 | new LinkedList<EventEntry<TopologyEvent>>(); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1231 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1232 | // XXX May need to clear whole topology first, depending on |
| 1233 | // how we initially subscribe to replication events |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1234 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1235 | // Add all active switches |
| 1236 | for (KVSwitch sw : KVSwitch.getAllSwitches()) { |
| 1237 | if (sw.getStatus() != KVSwitch.STATUS.ACTIVE) { |
| 1238 | continue; |
| 1239 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1240 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1241 | SwitchEvent switchEvent = new SwitchEvent(sw.getDpid()); |
| 1242 | TopologyEvent topologyEvent = new TopologyEvent(switchEvent); |
| 1243 | EventEntry<TopologyEvent> eventEntry = |
| 1244 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1245 | topologyEvent); |
| 1246 | collection.add(eventEntry); |
| 1247 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1248 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1249 | // Add all active ports |
| 1250 | for (KVPort p : KVPort.getAllPorts()) { |
| 1251 | if (p.getStatus() != KVPort.STATUS.ACTIVE) { |
| 1252 | continue; |
| 1253 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1254 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1255 | PortEvent portEvent = new PortEvent(p.getDpid(), p.getNumber()); |
| 1256 | TopologyEvent topologyEvent = new TopologyEvent(portEvent); |
| 1257 | EventEntry<TopologyEvent> eventEntry = |
| 1258 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1259 | topologyEvent); |
| 1260 | collection.add(eventEntry); |
| 1261 | } |
Yuta HIGUCHI | a536e76 | 2014-02-17 21:47:28 -0800 | [diff] [blame] | 1262 | |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1263 | for (KVDevice d : KVDevice.getAllDevices()) { |
| 1264 | DeviceEvent devEvent = new DeviceEvent(MACAddress.valueOf(d.getMac())); |
| 1265 | for (byte[] portId : d.getAllPortIds()) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 1266 | devEvent.addAttachmentPoint( |
| 1267 | new SwitchPort(KVPort.getDpidFromKey(portId), |
| 1268 | KVPort.getNumberFromKey(portId))); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1271 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1272 | for (KVLink l : KVLink.getAllLinks()) { |
| 1273 | LinkEvent linkEvent = new LinkEvent(l.getSrc().dpid, |
| 1274 | l.getSrc().number, |
| 1275 | l.getDst().dpid, |
| 1276 | l.getDst().number); |
| 1277 | TopologyEvent topologyEvent = new TopologyEvent(linkEvent); |
| 1278 | EventEntry<TopologyEvent> eventEntry = |
| 1279 | new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1280 | topologyEvent); |
| 1281 | collection.add(eventEntry); |
| 1282 | } |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 1283 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1284 | return collection; |
Pavlin Radoslavov | c1cfde5 | 2014-02-19 11:35:29 -0800 | [diff] [blame] | 1285 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1286 | } |