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