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