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