Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 3 | import java.nio.ByteBuffer; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 6 | import java.util.HashMap; |
| 7 | import java.util.LinkedList; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 8 | import java.util.List; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 9 | import java.util.Map; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 10 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 11 | |
| 12 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 13 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IOFSwitch.PortChangeType; |
| 16 | import net.floodlightcontroller.core.IOFSwitchListener; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.util.SingletonTask; |
| 22 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 23 | |
| 24 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 25 | import net.onrc.onos.core.datagrid.IEventChannel; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.hostmanager.Host; |
| 27 | import net.onrc.onos.core.hostmanager.IHostListener; |
| 28 | import net.onrc.onos.core.hostmanager.IHostService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener; |
| 30 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.linkdiscovery.Link; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.registry.RegistryException; |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 38 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 40 | import org.projectfloodlight.openflow.util.HexString; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 43 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 44 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 45 | * The TopologyPublisher subscribes to topology network events from the |
| 46 | * discovery modules. These events are reformatted and relayed to the in-memory |
| 47 | * topology instance. |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 48 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 49 | public class TopologyPublisher implements IOFSwitchListener, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | ILinkDiscoveryListener, |
| 51 | IFloodlightModule, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 52 | IHostListener { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 53 | private static final Logger log = |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 54 | LoggerFactory.getLogger(TopologyPublisher.class); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 55 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 56 | private IFloodlightProviderService floodlightProvider; |
| 57 | private ILinkDiscoveryService linkDiscovery; |
| 58 | private IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 59 | private ITopologyService topologyService; |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 60 | private IDatagridService datagridService; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 61 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 62 | private IHostService hostService; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 63 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 64 | private Topology topology; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 65 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 66 | private static final String ENABLE_CLEANUP_PROPERTY = "EnableCleanup"; |
| 67 | private boolean cleanupEnabled = true; |
| 68 | private static final int CLEANUP_TASK_INTERVAL = 60; // in seconds |
| 69 | private SingletonTask cleanupTask; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 70 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 71 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
| 72 | |
| 73 | // |
| 74 | // Local state for keeping track of locally discovered events so we can |
| 75 | // cleanup properly when a Switch or Port is removed. |
| 76 | // |
| 77 | // We keep all Port, (incoming) Link and Host events per Switch DPID: |
| 78 | // - If a switch goes down, we remove all corresponding Port, Link and |
| 79 | // Host events. |
| 80 | // - If a port on a switch goes down, we remove all corresponding Link |
| 81 | // and Host events discovered by this instance. |
| 82 | // |
| 83 | // How to handle side-effect of remote events. |
| 84 | // - Remote Port Down event -> Link Down |
| 85 | // Not handled. (XXX Shouldn't it be removed from discovered.. Map) |
| 86 | // - Remote Host Added -> lose ownership of Host) |
| 87 | // Not handled. (XXX Shouldn't it be removed from discovered.. Map) |
| 88 | // |
| 89 | // XXX Domain knowledge based invariant maintenance should be moved to |
| 90 | // driver module, since the invariant may be different on optical, etc. |
| 91 | // |
| 92 | // What happens on leadership change? |
| 93 | // - Probably should: remove from discovered.. Maps, but not send DELETE |
| 94 | // events |
| 95 | // XXX Switch/Port can be rediscovered by new leader, but Link, Host? |
| 96 | // - Current: There is no way to recognize leadership change? |
| 97 | // ZookeeperRegistry.requestControl(long, ControlChangeCallback) |
| 98 | // is the only way to register listener, and it allows only one |
| 99 | // listener, which is already used by Controller class. |
| 100 | // |
| 101 | // FIXME Replace with concurrent variant. |
| 102 | // #removeSwitchDiscoveryEvent(SwitchEvent) runs in different thread. |
| 103 | // |
| 104 | private Map<Dpid, Map<ByteBuffer, PortEvent>> discoveredAddedPortEvents = |
| 105 | new HashMap<>(); |
| 106 | private Map<Dpid, Map<ByteBuffer, LinkEvent>> discoveredAddedLinkEvents = |
| 107 | new HashMap<>(); |
| 108 | private Map<Dpid, Map<ByteBuffer, HostEvent>> discoveredAddedHostEvents = |
| 109 | new HashMap<>(); |
| 110 | |
| 111 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 112 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 113 | * Cleanup old switches from the topology. Old switches are those which have |
| 114 | * no controller in the registry. |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 115 | */ |
| 116 | private class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 117 | @Override |
| 118 | public void run() { |
| 119 | String old = Thread.currentThread().getName(); |
| 120 | Thread.currentThread().setName("SwitchCleanup@" + old); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 121 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 122 | try { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 123 | if (log.isTraceEnabled()) { |
| 124 | log.trace("Running cleanup thread"); |
| 125 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 126 | switchCleanup(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 127 | } finally { |
| 128 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 129 | TimeUnit.SECONDS); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 130 | Thread.currentThread().setName(old); |
| 131 | } |
| 132 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 133 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 134 | /** |
| 135 | * First half of the switch cleanup operation. This method will attempt |
| 136 | * to get control of any switch it sees without a controller via the |
| 137 | * registry. |
| 138 | */ |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 139 | private void switchCleanup() { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 140 | Iterable<Switch> switches = topology.getSwitches(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 141 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 142 | if (log.isTraceEnabled()) { |
| 143 | log.trace("Checking for inactive switches"); |
| 144 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 145 | // For each switch check if a controller exists in controller |
| 146 | // registry |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | for (Switch sw : switches) { |
Praseed Balakrishnan | e82adc6 | 2014-08-04 10:59:24 -0700 | [diff] [blame] | 148 | // FIXME How to handle case where Switch has never been |
| 149 | // registered to ZK |
| 150 | if (sw.getConfigState() == ConfigState.CONFIGURED) { |
| 151 | continue; |
| 152 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 153 | try { |
| 154 | String controller = |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 155 | registryService.getControllerForSwitch(sw.getDpid().value()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 156 | if (controller == null) { |
| 157 | log.debug("Requesting control to set switch {} INACTIVE", |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 158 | sw.getDpid()); |
| 159 | registryService.requestControl(sw.getDpid().value(), this); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 160 | } |
| 161 | } catch (RegistryException e) { |
| 162 | log.error("Caught RegistryException in cleanup thread", e); |
| 163 | } |
| 164 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Second half of the switch cleanup operation. If the registry grants |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 169 | * control of a switch, we can be sure no other instance is writing this |
| 170 | * switch to the topology, so we can remove it now. |
| 171 | * <p> |
| 172 | * @param dpid the dpid of the switch we requested control for |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 173 | * @param hasControl whether we got control or not |
| 174 | */ |
| 175 | @Override |
| 176 | public void controlChanged(long dpid, boolean hasControl) { |
| 177 | if (hasControl) { |
| 178 | log.debug("Got control to set switch {} INACTIVE", |
| 179 | HexString.toHexString(dpid)); |
Jonathan Hart | e02cf54 | 2014-04-02 16:24:44 -0700 | [diff] [blame] | 180 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 181 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(dpid)); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 182 | removeSwitchDiscoveryEvent(switchEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 183 | registryService.releaseControl(dpid); |
| 184 | } |
| 185 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 186 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 187 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 188 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 189 | public void linkAdded(Link link) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 190 | LinkEvent linkEvent = new LinkEvent( |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 191 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 192 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 193 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 194 | // FIXME should be merging, with existing attrs, etc.. |
| 195 | // TODO define attr name as constant somewhere. |
| 196 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 197 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 198 | TopologyElement.TYPE_PACKET_LAYER); |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 199 | linkEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 200 | ConfigState.NOT_CONFIGURED.toString()); |
| 201 | linkEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 202 | AdminStatus.ACTIVE.toString()); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 203 | linkEvent.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 204 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 205 | if (!registryService.hasControl(link.getDst())) { |
| 206 | // Don't process or send a link event if we're not master for the |
| 207 | // destination switch |
| 208 | log.debug("Not the master for dst switch {}. Suppressed link add event {}.", |
| 209 | link.getDst(), linkEvent); |
| 210 | return; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 211 | } |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 212 | putLinkDiscoveryEvent(linkEvent); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void linkRemoved(Link link) { |
| 217 | LinkEvent linkEvent = new LinkEvent( |
| 218 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 219 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 220 | |
| 221 | // FIXME should be merging, with existing attrs, etc.. |
| 222 | // TODO define attr name as constant somewhere. |
| 223 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 224 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 225 | TopologyElement.TYPE_PACKET_LAYER); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 226 | linkEvent.freeze(); |
| 227 | |
| 228 | if (!registryService.hasControl(link.getDst())) { |
| 229 | // Don't process or send a link event if we're not master for the |
| 230 | // destination switch |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 231 | log.debug( |
| 232 | "Not the master for dst switch {}. Suppressed link remove event {}.", |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 233 | link.getDst(), linkEvent); |
| 234 | return; |
| 235 | } |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 236 | removeLinkDiscoveryEvent(linkEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 237 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 238 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 239 | /* ***************** |
| 240 | * IOFSwitchListener |
| 241 | * *****************/ |
| 242 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 243 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 244 | public void switchActivatedMaster(long swId) { |
| 245 | IOFSwitch sw = floodlightProvider.getSwitch(swId); |
| 246 | final Dpid dpid = new Dpid(swId); |
| 247 | if (sw == null) { |
| 248 | log.warn("Added switch not available {} ", dpid); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | controllerRoleChanged(dpid, Role.MASTER); |
| 253 | |
| 254 | SwitchEvent switchEvent = new SwitchEvent(dpid); |
| 255 | // FIXME should be merging, with existing attrs, etc.. |
| 256 | // TODO define attr name as constant somewhere. |
| 257 | // TODO populate appropriate attributes. |
| 258 | switchEvent.createStringAttribute(TopologyElement.TYPE, |
| 259 | TopologyElement.TYPE_PACKET_LAYER); |
| 260 | switchEvent.createStringAttribute("ConnectedSince", |
| 261 | sw.getConnectedSince().toString()); |
| 262 | switchEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 263 | ConfigState.NOT_CONFIGURED.toString()); |
| 264 | switchEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 265 | AdminStatus.ACTIVE.toString()); |
| 266 | switchEvent.freeze(); |
| 267 | // TODO Not very robust |
| 268 | if (!registryService.hasControl(swId)) { |
| 269 | log.debug("Not the master for switch {}. Suppressed switch add event {}.", |
| 270 | dpid, switchEvent); |
| 271 | return; |
| 272 | } |
| 273 | List<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 274 | for (OFPortDesc port : sw.getPorts()) { |
| 275 | PortEvent portEvent = new PortEvent(dpid, |
| 276 | new PortNumber(port.getPortNo().getShortPortNumber())); |
| 277 | // FIXME should be merging, with existing attrs, etc.. |
| 278 | // TODO define attr name as constant somewhere. |
| 279 | // TODO populate appropriate attributes. |
| 280 | portEvent.createStringAttribute("name", port.getName()); |
| 281 | portEvent.createStringAttribute(TopologyElement.TYPE, |
| 282 | TopologyElement.TYPE_PACKET_LAYER); |
| 283 | portEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 284 | ConfigState.NOT_CONFIGURED.toString()); |
| 285 | portEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 286 | AdminStatus.ACTIVE.toString()); |
| 287 | |
| 288 | portEvent.freeze(); |
| 289 | portEvents.add(portEvent); |
| 290 | } |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 291 | putSwitchDiscoveryEvent(switchEvent, portEvents); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 292 | |
| 293 | for (OFPortDesc port : sw.getPorts()) { |
| 294 | // Allow links to be discovered on this port now that it's |
| 295 | // in the database |
| 296 | linkDiscovery.enableDiscoveryOnPort(sw.getId(), |
| 297 | port.getPortNo().getShortPortNumber()); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public void switchActivatedEqual(long swId) { |
| 303 | final Dpid dpid = new Dpid(swId); |
| 304 | controllerRoleChanged(dpid, Role.EQUAL); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void switchMasterToEqual(long swId) { |
| 309 | final Dpid dpid = new Dpid(swId); |
| 310 | controllerRoleChanged(dpid, Role.EQUAL); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void switchEqualToMaster(long swId) { |
| 315 | // for now treat as switchActivatedMaster |
| 316 | switchActivatedMaster(swId); |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public void switchDisconnected(long swId) { |
| 321 | final Dpid dpid = new Dpid(swId); |
| 322 | |
| 323 | log.debug("Local switch disconnected: dpid = {} role = {}", dpid); |
| 324 | |
| 325 | Role role = Role.SLAVE; // TODO: Should be Role.UNKNOWN |
| 326 | |
| 327 | MastershipEvent mastershipEvent = |
| 328 | new MastershipEvent(dpid, registryService.getOnosInstanceId(), |
| 329 | role); |
| 330 | // FIXME should be merging, with existing attrs, etc.. |
| 331 | // TODO define attr name as constant somewhere. |
| 332 | // TODO populate appropriate attributes. |
| 333 | mastershipEvent.createStringAttribute(TopologyElement.TYPE, |
| 334 | TopologyElement.TYPE_ALL_LAYERS); |
| 335 | mastershipEvent.freeze(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 336 | removeSwitchMastershipEvent(mastershipEvent); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void switchPortChanged(long swId, OFPortDesc port, |
| 341 | PortChangeType changeType) { |
| 342 | switch (changeType) { |
| 343 | case ADD: |
| 344 | switchPortAdded(swId, port); |
| 345 | break; |
| 346 | case DELETE: |
| 347 | switchPortRemoved(swId, port); |
| 348 | break; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 349 | case UP: |
Pavlin Radoslavov | 7946c61 | 2014-08-13 15:06:00 -0700 | [diff] [blame] | 350 | // NOTE: Currently, we treat Port UP/DOWN same as Port ADD/DELETE |
| 351 | switchPortAdded(swId, port); |
| 352 | break; |
| 353 | case DOWN: |
| 354 | // NOTE: Currently, we treat Port UP/DOWN same as Port ADD/DELETE |
| 355 | switchPortRemoved(swId, port); |
| 356 | break; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 357 | case OTHER_UPDATE: |
| 358 | default: |
| 359 | // XXX S what is the right set of port change handlers? |
| 360 | log.debug("Topology publisher does not handle these port updates: {}", |
| 361 | changeType); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | private void switchPortAdded(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 366 | final Dpid dpid = new Dpid(switchId); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 367 | PortEvent portEvent = new PortEvent(dpid, |
| 368 | new PortNumber(port.getPortNo().getShortPortNumber())); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 369 | // FIXME should be merging, with existing attrs, etc.. |
| 370 | // TODO define attr name as constant somewhere. |
| 371 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 372 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 373 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 374 | portEvent.createStringAttribute("name", port.getName()); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 375 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 376 | portEvent.freeze(); |
| 377 | |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 378 | if (registryService.hasControl(switchId)) { |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 379 | putPortDiscoveryEvent(portEvent); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 380 | linkDiscovery.enableDiscoveryOnPort(switchId, |
| 381 | port.getPortNo().getShortPortNumber()); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 382 | } else { |
| 383 | log.debug("Not the master for switch {}. Suppressed port add event {}.", |
| 384 | new Dpid(switchId), portEvent); |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 385 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 386 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 387 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 388 | private void switchPortRemoved(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 389 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 390 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 391 | PortEvent portEvent = new PortEvent(dpid, new PortNumber( |
| 392 | port.getPortNo().getShortPortNumber())); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 393 | // FIXME should be merging, with existing attrs, etc.. |
| 394 | // TODO define attr name as constant somewhere. |
| 395 | // TODO populate appropriate attributes. |
| 396 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 397 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 398 | portEvent.createStringAttribute("name", port.getName()); |
| 399 | |
| 400 | portEvent.freeze(); |
| 401 | |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 402 | if (registryService.hasControl(switchId)) { |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 403 | removePortDiscoveryEvent(portEvent); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 404 | } else { |
| 405 | log.debug("Not the master for switch {}. Suppressed port del event {}.", |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 406 | dpid, portEvent); |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 407 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 408 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 409 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 410 | @Override |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 411 | public String getName() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 412 | return "topologyPublisher"; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 413 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 414 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 415 | /* ***************** |
| 416 | * IFloodlightModule |
| 417 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 418 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 419 | @Override |
| 420 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 421 | return null; |
| 422 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 423 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 424 | @Override |
| 425 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 426 | getServiceImpls() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 427 | return null; |
| 428 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 429 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 430 | @Override |
| 431 | public Collection<Class<? extends IFloodlightService>> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 432 | getModuleDependencies() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 433 | Collection<Class<? extends IFloodlightService>> l = |
| 434 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 435 | l.add(IFloodlightProviderService.class); |
| 436 | l.add(ILinkDiscoveryService.class); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 437 | l.add(IThreadPoolService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 438 | l.add(IControllerRegistryService.class); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 439 | l.add(IDatagridService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 440 | l.add(ITopologyService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 441 | l.add(IHostService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 442 | return l; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 443 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 444 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 445 | @Override |
| 446 | public void init(FloodlightModuleContext context) |
| 447 | throws FloodlightModuleException { |
| 448 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 449 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 450 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 451 | datagridService = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 452 | hostService = context.getServiceImpl(IHostService.class); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 453 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 454 | topologyService = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 455 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 456 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public void startUp(FloodlightModuleContext context) { |
| 459 | floodlightProvider.addOFSwitchListener(this); |
| 460 | linkDiscovery.addListener(this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 461 | hostService.addHostListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 462 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 463 | eventChannel = datagridService.createChannel( |
| 464 | TopologyManager.EVENT_CHANNEL_NAME, |
| 465 | byte[].class, |
| 466 | TopologyEvent.class); |
| 467 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 468 | topology = topologyService.getTopology(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 469 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 470 | // Run the cleanup thread |
| 471 | String enableCleanup = |
| 472 | context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY); |
| 473 | if (enableCleanup != null |
| 474 | && enableCleanup.equalsIgnoreCase("false")) { |
| 475 | cleanupEnabled = false; |
| 476 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 477 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 478 | log.debug("Cleanup thread is {}enabled", (cleanupEnabled) ? "" : "not "); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 479 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 480 | if (cleanupEnabled) { |
| 481 | IThreadPoolService threadPool = |
| 482 | context.getServiceImpl(IThreadPoolService.class); |
| 483 | cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(), |
| 484 | new SwitchCleanup()); |
| 485 | // Run the cleanup task immediately on startup |
| 486 | cleanupTask.reschedule(0, TimeUnit.SECONDS); |
| 487 | } |
| 488 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 489 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 490 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 491 | public void hostAdded(Host host) { |
| 492 | log.debug("Called onosDeviceAdded mac {}", host.getMacAddress()); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 493 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 494 | SwitchPort sp = new SwitchPort(host.getSwitchDPID(), host.getSwitchPort()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 495 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 496 | spLists.add(sp); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 497 | HostEvent event = new HostEvent(host.getMacAddress()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 498 | event.setAttachmentPoints(spLists); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 499 | event.setLastSeenTime(host.getLastSeenTimestamp().getTime()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 500 | // Does not use vlan info now. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 501 | event.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 502 | |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 503 | putHostDiscoveryEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 504 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 505 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 506 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 507 | public void hostRemoved(Host host) { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 508 | log.debug("Called onosDeviceRemoved"); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 509 | HostEvent event = new HostEvent(host.getMacAddress()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 510 | // XXX shouldn't we be setting attachment points? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 511 | event.freeze(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 512 | removeHostDiscoveryEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 513 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 514 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 515 | private void controllerRoleChanged(Dpid dpid, Role role) { |
| 516 | log.debug("Local switch controller mastership role changed: dpid = {} role = {}", |
| 517 | dpid, role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 518 | MastershipEvent mastershipEvent = |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 519 | new MastershipEvent(dpid, registryService.getOnosInstanceId(), |
| 520 | role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 521 | // FIXME should be merging, with existing attrs, etc.. |
| 522 | // TODO define attr name as constant somewhere. |
| 523 | // TODO populate appropriate attributes. |
| 524 | mastershipEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 525 | TopologyElement.TYPE_ALL_LAYERS); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 526 | mastershipEvent.freeze(); |
Pavlin Radoslavov | b46f89b | 2014-08-15 23:00:26 -0700 | [diff] [blame] | 527 | putSwitchMastershipEvent(mastershipEvent); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Mastership updated event. |
| 532 | * |
| 533 | * @param mastershipEvent the mastership event. |
| 534 | */ |
| 535 | private void putSwitchMastershipEvent(MastershipEvent mastershipEvent) { |
| 536 | // Send out notification |
| 537 | TopologyEvent topologyEvent = |
| 538 | new TopologyEvent(mastershipEvent, |
| 539 | registryService.getOnosInstanceId()); |
| 540 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Mastership removed event. |
| 545 | * |
| 546 | * @param mastershipEvent the mastership event. |
| 547 | */ |
| 548 | private void removeSwitchMastershipEvent(MastershipEvent mastershipEvent) { |
| 549 | // Send out notification |
| 550 | TopologyEvent topologyEvent = |
| 551 | new TopologyEvent(mastershipEvent, |
| 552 | registryService.getOnosInstanceId()); |
| 553 | eventChannel.removeEntry(topologyEvent.getID()); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Switch discovered event. |
| 558 | * |
| 559 | * @param switchEvent the switch event. |
| 560 | * @param portEvents the corresponding port events for the switch. |
| 561 | */ |
| 562 | private void putSwitchDiscoveryEvent(SwitchEvent switchEvent, |
| 563 | Collection<PortEvent> portEvents) { |
| 564 | log.debug("Sending add switch: {}", switchEvent); |
| 565 | // Send out notification |
| 566 | TopologyEvent topologyEvent = |
| 567 | new TopologyEvent(switchEvent, |
| 568 | registryService.getOnosInstanceId()); |
| 569 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 570 | |
| 571 | // Send out notification for each port |
| 572 | for (PortEvent portEvent : portEvents) { |
| 573 | log.debug("Sending add port: {}", portEvent); |
| 574 | topologyEvent = |
| 575 | new TopologyEvent(portEvent, |
| 576 | registryService.getOnosInstanceId()); |
| 577 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 578 | } |
| 579 | |
| 580 | // |
| 581 | // Keep track of the added ports |
| 582 | // |
| 583 | // Get the old Port Events |
| 584 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 585 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
| 586 | if (oldPortEvents == null) { |
| 587 | oldPortEvents = new HashMap<>(); |
| 588 | } |
| 589 | |
| 590 | // Store the new Port Events in the local cache |
| 591 | Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>(); |
| 592 | for (PortEvent portEvent : portEvents) { |
| 593 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 594 | newPortEvents.put(id, portEvent); |
| 595 | } |
| 596 | discoveredAddedPortEvents.put(switchEvent.getDpid(), newPortEvents); |
| 597 | |
| 598 | // |
| 599 | // Extract the removed ports |
| 600 | // |
| 601 | List<PortEvent> removedPortEvents = new LinkedList<>(); |
| 602 | for (Map.Entry<ByteBuffer, PortEvent> entry : oldPortEvents.entrySet()) { |
| 603 | ByteBuffer key = entry.getKey(); |
| 604 | PortEvent portEvent = entry.getValue(); |
| 605 | if (!newPortEvents.containsKey(key)) { |
| 606 | removedPortEvents.add(portEvent); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | // Cleanup old removed ports |
| 611 | for (PortEvent portEvent : removedPortEvents) { |
| 612 | removePortDiscoveryEvent(portEvent); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Switch removed event. |
| 618 | * |
| 619 | * @param switchEvent the switch event. |
| 620 | */ |
| 621 | private void removeSwitchDiscoveryEvent(SwitchEvent switchEvent) { |
| 622 | TopologyEvent topologyEvent; |
| 623 | |
| 624 | // Get the old Port Events |
| 625 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 626 | discoveredAddedPortEvents.get(switchEvent.getDpid()); |
| 627 | if (oldPortEvents == null) { |
| 628 | oldPortEvents = new HashMap<>(); |
| 629 | } |
| 630 | |
| 631 | log.debug("Sending remove switch: {}", switchEvent); |
| 632 | // Send out notification |
| 633 | topologyEvent = |
| 634 | new TopologyEvent(switchEvent, |
| 635 | registryService.getOnosInstanceId()); |
| 636 | eventChannel.removeEntry(topologyEvent.getID()); |
| 637 | |
| 638 | // |
| 639 | // Send out notification for each port. |
| 640 | // |
| 641 | // NOTE: We don't use removePortDiscoveryEvent() for the cleanup, |
| 642 | // because it will attempt to remove the port from the database, |
| 643 | // and the deactiveSwitch() call above already removed all ports. |
| 644 | // |
| 645 | for (PortEvent portEvent : oldPortEvents.values()) { |
| 646 | log.debug("Sending remove port:", portEvent); |
| 647 | topologyEvent = |
| 648 | new TopologyEvent(portEvent, |
| 649 | registryService.getOnosInstanceId()); |
| 650 | eventChannel.removeEntry(topologyEvent.getID()); |
| 651 | } |
| 652 | discoveredAddedPortEvents.remove(switchEvent.getDpid()); |
| 653 | |
| 654 | // Cleanup for each link |
| 655 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 656 | discoveredAddedLinkEvents.get(switchEvent.getDpid()); |
| 657 | if (oldLinkEvents != null) { |
| 658 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 659 | removeLinkDiscoveryEvent(linkEvent); |
| 660 | } |
| 661 | discoveredAddedLinkEvents.remove(switchEvent.getDpid()); |
| 662 | } |
| 663 | |
| 664 | // Cleanup for each host |
| 665 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 666 | discoveredAddedHostEvents.get(switchEvent.getDpid()); |
| 667 | if (oldHostEvents != null) { |
| 668 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 669 | removeHostDiscoveryEvent(hostEvent); |
| 670 | } |
| 671 | discoveredAddedHostEvents.remove(switchEvent.getDpid()); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Port discovered event. |
| 677 | * |
| 678 | * @param portEvent the port event. |
| 679 | */ |
| 680 | private void putPortDiscoveryEvent(PortEvent portEvent) { |
| 681 | log.debug("Sending add port: {}", portEvent); |
| 682 | // Send out notification |
| 683 | TopologyEvent topologyEvent = |
| 684 | new TopologyEvent(portEvent, |
| 685 | registryService.getOnosInstanceId()); |
| 686 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 687 | |
| 688 | // Store the new Port Event in the local cache |
| 689 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 690 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 691 | if (oldPortEvents == null) { |
| 692 | oldPortEvents = new HashMap<>(); |
| 693 | discoveredAddedPortEvents.put(portEvent.getDpid(), oldPortEvents); |
| 694 | } |
| 695 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 696 | oldPortEvents.put(id, portEvent); |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Port removed event. |
| 701 | * |
| 702 | * @param portEvent the port event. |
| 703 | */ |
| 704 | private void removePortDiscoveryEvent(PortEvent portEvent) { |
| 705 | log.debug("Sending remove port: {}", portEvent); |
| 706 | // Send out notification |
| 707 | TopologyEvent topologyEvent = |
| 708 | new TopologyEvent(portEvent, |
| 709 | registryService.getOnosInstanceId()); |
| 710 | eventChannel.removeEntry(topologyEvent.getID()); |
| 711 | |
| 712 | // Cleanup the Port Event from the local cache |
| 713 | Map<ByteBuffer, PortEvent> oldPortEvents = |
| 714 | discoveredAddedPortEvents.get(portEvent.getDpid()); |
| 715 | if (oldPortEvents != null) { |
| 716 | ByteBuffer id = portEvent.getIDasByteBuffer(); |
| 717 | oldPortEvents.remove(id); |
| 718 | } |
| 719 | |
| 720 | // Cleanup for the incoming link |
| 721 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 722 | discoveredAddedLinkEvents.get(portEvent.getDpid()); |
| 723 | if (oldLinkEvents != null) { |
| 724 | for (LinkEvent linkEvent : new ArrayList<>(oldLinkEvents.values())) { |
| 725 | if (linkEvent.getDst().equals(portEvent.getSwitchPort())) { |
| 726 | removeLinkDiscoveryEvent(linkEvent); |
| 727 | // XXX If we change our model to allow multiple Link on |
| 728 | // a Port, this loop must be fixed to allow continuing. |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Cleanup for the connected hosts |
| 735 | // TODO: The implementation below is probably wrong |
| 736 | List<HostEvent> removedHostEvents = new LinkedList<>(); |
| 737 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 738 | discoveredAddedHostEvents.get(portEvent.getDpid()); |
| 739 | if (oldHostEvents != null) { |
| 740 | for (HostEvent hostEvent : new ArrayList<>(oldHostEvents.values())) { |
| 741 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 742 | if (swp.equals(portEvent.getSwitchPort())) { |
| 743 | removedHostEvents.add(hostEvent); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | for (HostEvent hostEvent : removedHostEvents) { |
| 748 | removeHostDiscoveryEvent(hostEvent); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Link discovered event. |
| 755 | * |
| 756 | * @param linkEvent the link event. |
| 757 | */ |
| 758 | private void putLinkDiscoveryEvent(LinkEvent linkEvent) { |
| 759 | log.debug("Sending add link: {}", linkEvent); |
| 760 | // Send out notification |
| 761 | TopologyEvent topologyEvent = |
| 762 | new TopologyEvent(linkEvent, |
| 763 | registryService.getOnosInstanceId()); |
| 764 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 765 | |
| 766 | // Store the new Link Event in the local cache |
| 767 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 768 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 769 | if (oldLinkEvents == null) { |
| 770 | oldLinkEvents = new HashMap<>(); |
| 771 | discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(), |
| 772 | oldLinkEvents); |
| 773 | } |
| 774 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 775 | oldLinkEvents.put(id, linkEvent); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Link removed event. |
| 780 | * |
| 781 | * @param linkEvent the link event. |
| 782 | */ |
| 783 | private void removeLinkDiscoveryEvent(LinkEvent linkEvent) { |
| 784 | log.debug("Sending remove link: {}", linkEvent); |
| 785 | // Send out notification |
| 786 | TopologyEvent topologyEvent = |
| 787 | new TopologyEvent(linkEvent, |
| 788 | registryService.getOnosInstanceId()); |
| 789 | eventChannel.removeEntry(topologyEvent.getID()); |
| 790 | |
| 791 | // Cleanup the Link Event from the local cache |
| 792 | Map<ByteBuffer, LinkEvent> oldLinkEvents = |
| 793 | discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid()); |
| 794 | if (oldLinkEvents != null) { |
| 795 | ByteBuffer id = linkEvent.getIDasByteBuffer(); |
| 796 | oldLinkEvents.remove(id); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Host discovered event. |
| 802 | * |
| 803 | * @param hostEvent the host event. |
| 804 | */ |
| 805 | private void putHostDiscoveryEvent(HostEvent hostEvent) { |
| 806 | // Send out notification |
| 807 | TopologyEvent topologyEvent = |
| 808 | new TopologyEvent(hostEvent, |
| 809 | registryService.getOnosInstanceId()); |
| 810 | eventChannel.addEntry(topologyEvent.getID(), topologyEvent); |
| 811 | log.debug("Put the host info into the cache of the topology. mac {}", |
| 812 | hostEvent.getMac()); |
| 813 | |
| 814 | // Store the new Host Event in the local cache |
| 815 | // TODO: The implementation below is probably wrong |
| 816 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 817 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 818 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 819 | if (oldHostEvents == null) { |
| 820 | oldHostEvents = new HashMap<>(); |
| 821 | discoveredAddedHostEvents.put(swp.getDpid(), oldHostEvents); |
| 822 | } |
| 823 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 824 | oldHostEvents.put(id, hostEvent); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Host removed event. |
| 830 | * |
| 831 | * @param hostEvent the host event. |
| 832 | */ |
| 833 | private void removeHostDiscoveryEvent(HostEvent hostEvent) { |
| 834 | // Send out notification |
| 835 | TopologyEvent topologyEvent = |
| 836 | new TopologyEvent(hostEvent, |
| 837 | registryService.getOnosInstanceId()); |
| 838 | eventChannel.removeEntry(topologyEvent.getID()); |
| 839 | log.debug("Remove the host info into the cache of the topology. mac {}", |
| 840 | hostEvent.getMac()); |
| 841 | |
| 842 | // Cleanup the Host Event from the local cache |
| 843 | // TODO: The implementation below is probably wrong |
| 844 | ByteBuffer id = hostEvent.getIDasByteBuffer(); |
| 845 | for (SwitchPort swp : hostEvent.getAttachmentPoints()) { |
| 846 | Map<ByteBuffer, HostEvent> oldHostEvents = |
| 847 | discoveredAddedHostEvents.get(swp.getDpid()); |
| 848 | if (oldHostEvents != null) { |
| 849 | oldHostEvents.remove(id); |
| 850 | } |
| 851 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 852 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 853 | } |