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 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 5 | import java.util.List; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 6 | import java.util.Map; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 7 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 8 | |
| 9 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 10 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 12 | import net.floodlightcontroller.core.IOFSwitch.PortChangeType; |
| 13 | import net.floodlightcontroller.core.IOFSwitchListener; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 15 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 17 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.core.util.SingletonTask; |
| 19 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.hostmanager.Host; |
| 21 | import net.onrc.onos.core.hostmanager.IHostListener; |
| 22 | import net.onrc.onos.core.hostmanager.IHostService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener; |
| 24 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.linkdiscovery.Link; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.registry.RegistryException; |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 32 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 34 | import org.projectfloodlight.openflow.util.HexString; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 37 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 38 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 39 | * The TopologyPublisher subscribes to topology network events from the |
| 40 | * discovery modules. These events are reformatted and relayed to the in-memory |
| 41 | * topology instance. |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 42 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 43 | public class TopologyPublisher implements IOFSwitchListener, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | ILinkDiscoveryListener, |
| 45 | IFloodlightModule, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 46 | IHostListener { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 47 | private static final Logger log = |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 48 | LoggerFactory.getLogger(TopologyPublisher.class); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 49 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 50 | private IFloodlightProviderService floodlightProvider; |
| 51 | private ILinkDiscoveryService linkDiscovery; |
| 52 | private IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 53 | private ITopologyService topologyService; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 54 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 55 | private IHostService hostService; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 56 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 57 | private Topology topology; |
| 58 | private TopologyDiscoveryInterface topologyDiscoveryInterface; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 59 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 60 | private static final String ENABLE_CLEANUP_PROPERTY = "EnableCleanup"; |
| 61 | private boolean cleanupEnabled = true; |
| 62 | private static final int CLEANUP_TASK_INTERVAL = 60; // in seconds |
| 63 | private SingletonTask cleanupTask; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 64 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 65 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 66 | * Cleanup old switches from the topology. Old switches are those which have |
| 67 | * no controller in the registry. |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 68 | */ |
| 69 | private class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 70 | @Override |
| 71 | public void run() { |
| 72 | String old = Thread.currentThread().getName(); |
| 73 | Thread.currentThread().setName("SwitchCleanup@" + old); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 74 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 75 | try { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 76 | if (log.isTraceEnabled()) { |
| 77 | log.trace("Running cleanup thread"); |
| 78 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 79 | switchCleanup(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 80 | } finally { |
| 81 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 82 | TimeUnit.SECONDS); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 83 | Thread.currentThread().setName(old); |
| 84 | } |
| 85 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 86 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 87 | /** |
| 88 | * First half of the switch cleanup operation. This method will attempt |
| 89 | * to get control of any switch it sees without a controller via the |
| 90 | * registry. |
| 91 | */ |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 92 | private void switchCleanup() { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 93 | Iterable<Switch> switches = topology.getSwitches(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 95 | if (log.isTraceEnabled()) { |
| 96 | log.trace("Checking for inactive switches"); |
| 97 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 98 | // For each switch check if a controller exists in controller |
| 99 | // registry |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | for (Switch sw : switches) { |
Praseed Balakrishnan | e82adc6 | 2014-08-04 10:59:24 -0700 | [diff] [blame] | 101 | // FIXME How to handle case where Switch has never been |
| 102 | // registered to ZK |
| 103 | if (sw.getConfigState() == ConfigState.CONFIGURED) { |
| 104 | continue; |
| 105 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 106 | try { |
| 107 | String controller = |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 108 | registryService.getControllerForSwitch(sw.getDpid().value()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 109 | if (controller == null) { |
| 110 | log.debug("Requesting control to set switch {} INACTIVE", |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 111 | sw.getDpid()); |
| 112 | registryService.requestControl(sw.getDpid().value(), this); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 113 | } |
| 114 | } catch (RegistryException e) { |
| 115 | log.error("Caught RegistryException in cleanup thread", e); |
| 116 | } |
| 117 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Second half of the switch cleanup operation. If the registry grants |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 122 | * control of a switch, we can be sure no other instance is writing this |
| 123 | * switch to the topology, so we can remove it now. |
| 124 | * <p> |
| 125 | * @param dpid the dpid of the switch we requested control for |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 126 | * @param hasControl whether we got control or not |
| 127 | */ |
| 128 | @Override |
| 129 | public void controlChanged(long dpid, boolean hasControl) { |
| 130 | if (hasControl) { |
| 131 | log.debug("Got control to set switch {} INACTIVE", |
| 132 | HexString.toHexString(dpid)); |
Jonathan Hart | e02cf54 | 2014-04-02 16:24:44 -0700 | [diff] [blame] | 133 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 134 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(dpid)); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 135 | topologyDiscoveryInterface. |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 136 | removeSwitchDiscoveryEvent(switchEvent); |
| 137 | registryService.releaseControl(dpid); |
| 138 | } |
| 139 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 140 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 141 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 142 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 143 | public void linkAdded(Link link) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 144 | LinkEvent linkEvent = new LinkEvent( |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 145 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 146 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 147 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 148 | // FIXME should be merging, with existing attrs, etc.. |
| 149 | // TODO define attr name as constant somewhere. |
| 150 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 151 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 152 | TopologyElement.TYPE_PACKET_LAYER); |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 153 | linkEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 154 | ConfigState.NOT_CONFIGURED.toString()); |
| 155 | linkEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 156 | AdminStatus.ACTIVE.toString()); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 157 | linkEvent.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 158 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 159 | if (!registryService.hasControl(link.getDst())) { |
| 160 | // Don't process or send a link event if we're not master for the |
| 161 | // destination switch |
| 162 | log.debug("Not the master for dst switch {}. Suppressed link add event {}.", |
| 163 | link.getDst(), linkEvent); |
| 164 | return; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 165 | } |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 166 | topologyDiscoveryInterface.putLinkDiscoveryEvent(linkEvent); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void linkRemoved(Link link) { |
| 171 | LinkEvent linkEvent = new LinkEvent( |
| 172 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 173 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 174 | |
| 175 | // FIXME should be merging, with existing attrs, etc.. |
| 176 | // TODO define attr name as constant somewhere. |
| 177 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 178 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 179 | TopologyElement.TYPE_PACKET_LAYER); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 180 | linkEvent.freeze(); |
| 181 | |
| 182 | if (!registryService.hasControl(link.getDst())) { |
| 183 | // Don't process or send a link event if we're not master for the |
| 184 | // destination switch |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 185 | log.debug( |
| 186 | "Not the master for dst switch {}. Suppressed link remove event {}.", |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 187 | link.getDst(), linkEvent); |
| 188 | return; |
| 189 | } |
| 190 | topologyDiscoveryInterface.removeLinkDiscoveryEvent(linkEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 191 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 192 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 193 | /* ***************** |
| 194 | * IOFSwitchListener |
| 195 | * *****************/ |
| 196 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 197 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 198 | public void switchActivatedMaster(long swId) { |
| 199 | IOFSwitch sw = floodlightProvider.getSwitch(swId); |
| 200 | final Dpid dpid = new Dpid(swId); |
| 201 | if (sw == null) { |
| 202 | log.warn("Added switch not available {} ", dpid); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | controllerRoleChanged(dpid, Role.MASTER); |
| 207 | |
| 208 | SwitchEvent switchEvent = new SwitchEvent(dpid); |
| 209 | // FIXME should be merging, with existing attrs, etc.. |
| 210 | // TODO define attr name as constant somewhere. |
| 211 | // TODO populate appropriate attributes. |
| 212 | switchEvent.createStringAttribute(TopologyElement.TYPE, |
| 213 | TopologyElement.TYPE_PACKET_LAYER); |
| 214 | switchEvent.createStringAttribute("ConnectedSince", |
| 215 | sw.getConnectedSince().toString()); |
| 216 | switchEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 217 | ConfigState.NOT_CONFIGURED.toString()); |
| 218 | switchEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 219 | AdminStatus.ACTIVE.toString()); |
| 220 | switchEvent.freeze(); |
| 221 | // TODO Not very robust |
| 222 | if (!registryService.hasControl(swId)) { |
| 223 | log.debug("Not the master for switch {}. Suppressed switch add event {}.", |
| 224 | dpid, switchEvent); |
| 225 | return; |
| 226 | } |
| 227 | List<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 228 | for (OFPortDesc port : sw.getPorts()) { |
| 229 | PortEvent portEvent = new PortEvent(dpid, |
| 230 | new PortNumber(port.getPortNo().getShortPortNumber())); |
| 231 | // FIXME should be merging, with existing attrs, etc.. |
| 232 | // TODO define attr name as constant somewhere. |
| 233 | // TODO populate appropriate attributes. |
| 234 | portEvent.createStringAttribute("name", port.getName()); |
| 235 | portEvent.createStringAttribute(TopologyElement.TYPE, |
| 236 | TopologyElement.TYPE_PACKET_LAYER); |
| 237 | portEvent.createStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE, |
| 238 | ConfigState.NOT_CONFIGURED.toString()); |
| 239 | portEvent.createStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS, |
| 240 | AdminStatus.ACTIVE.toString()); |
| 241 | |
| 242 | portEvent.freeze(); |
| 243 | portEvents.add(portEvent); |
| 244 | } |
| 245 | topologyDiscoveryInterface.putSwitchDiscoveryEvent(switchEvent, portEvents); |
| 246 | |
| 247 | for (OFPortDesc port : sw.getPorts()) { |
| 248 | // Allow links to be discovered on this port now that it's |
| 249 | // in the database |
| 250 | linkDiscovery.enableDiscoveryOnPort(sw.getId(), |
| 251 | port.getPortNo().getShortPortNumber()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void switchActivatedEqual(long swId) { |
| 257 | final Dpid dpid = new Dpid(swId); |
| 258 | controllerRoleChanged(dpid, Role.EQUAL); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void switchMasterToEqual(long swId) { |
| 263 | final Dpid dpid = new Dpid(swId); |
| 264 | controllerRoleChanged(dpid, Role.EQUAL); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void switchEqualToMaster(long swId) { |
| 269 | // for now treat as switchActivatedMaster |
| 270 | switchActivatedMaster(swId); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public void switchDisconnected(long swId) { |
| 275 | final Dpid dpid = new Dpid(swId); |
| 276 | |
| 277 | log.debug("Local switch disconnected: dpid = {} role = {}", dpid); |
| 278 | |
| 279 | Role role = Role.SLAVE; // TODO: Should be Role.UNKNOWN |
| 280 | |
| 281 | MastershipEvent mastershipEvent = |
| 282 | new MastershipEvent(dpid, registryService.getOnosInstanceId(), |
| 283 | role); |
| 284 | // FIXME should be merging, with existing attrs, etc.. |
| 285 | // TODO define attr name as constant somewhere. |
| 286 | // TODO populate appropriate attributes. |
| 287 | mastershipEvent.createStringAttribute(TopologyElement.TYPE, |
| 288 | TopologyElement.TYPE_ALL_LAYERS); |
| 289 | mastershipEvent.freeze(); |
| 290 | topologyDiscoveryInterface.removeSwitchMastershipEvent(mastershipEvent); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void switchPortChanged(long swId, OFPortDesc port, |
| 295 | PortChangeType changeType) { |
| 296 | switch (changeType) { |
| 297 | case ADD: |
| 298 | switchPortAdded(swId, port); |
| 299 | break; |
| 300 | case DELETE: |
| 301 | switchPortRemoved(swId, port); |
| 302 | break; |
| 303 | case DOWN: |
| 304 | case UP: |
| 305 | case OTHER_UPDATE: |
| 306 | default: |
| 307 | // XXX S what is the right set of port change handlers? |
| 308 | log.debug("Topology publisher does not handle these port updates: {}", |
| 309 | changeType); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private void switchPortAdded(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 314 | final Dpid dpid = new Dpid(switchId); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 315 | PortEvent portEvent = new PortEvent(dpid, |
| 316 | new PortNumber(port.getPortNo().getShortPortNumber())); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 317 | // FIXME should be merging, with existing attrs, etc.. |
| 318 | // TODO define attr name as constant somewhere. |
| 319 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 320 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 321 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 322 | portEvent.createStringAttribute("name", port.getName()); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 323 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 324 | portEvent.freeze(); |
| 325 | |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 326 | if (registryService.hasControl(switchId)) { |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 327 | topologyDiscoveryInterface.putPortDiscoveryEvent(portEvent); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 328 | linkDiscovery.enableDiscoveryOnPort(switchId, |
| 329 | port.getPortNo().getShortPortNumber()); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 330 | } else { |
| 331 | log.debug("Not the master for switch {}. Suppressed port add event {}.", |
| 332 | new Dpid(switchId), portEvent); |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 333 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 334 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 335 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 336 | private void switchPortRemoved(long switchId, OFPortDesc port) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 337 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 338 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 339 | PortEvent portEvent = new PortEvent(dpid, new PortNumber( |
| 340 | port.getPortNo().getShortPortNumber())); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 341 | // FIXME should be merging, with existing attrs, etc.. |
| 342 | // TODO define attr name as constant somewhere. |
| 343 | // TODO populate appropriate attributes. |
| 344 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 345 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 346 | portEvent.createStringAttribute("name", port.getName()); |
| 347 | |
| 348 | portEvent.freeze(); |
| 349 | |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 350 | if (registryService.hasControl(switchId)) { |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 351 | topologyDiscoveryInterface.removePortDiscoveryEvent(portEvent); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 352 | } else { |
| 353 | log.debug("Not the master for switch {}. Suppressed port del event {}.", |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 354 | dpid, portEvent); |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 355 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 356 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 357 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 358 | @Override |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 359 | public String getName() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 360 | return "topologyPublisher"; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 361 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 362 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 363 | /* ***************** |
| 364 | * IFloodlightModule |
| 365 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 366 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 367 | @Override |
| 368 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 369 | return null; |
| 370 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 371 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 372 | @Override |
| 373 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 374 | getServiceImpls() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 375 | return null; |
| 376 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 377 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 378 | @Override |
| 379 | public Collection<Class<? extends IFloodlightService>> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 380 | getModuleDependencies() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 381 | Collection<Class<? extends IFloodlightService>> l = |
| 382 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 383 | l.add(IFloodlightProviderService.class); |
| 384 | l.add(ILinkDiscoveryService.class); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 385 | l.add(IThreadPoolService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 386 | l.add(IControllerRegistryService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 387 | l.add(ITopologyService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 388 | l.add(IHostService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 389 | return l; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 390 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 391 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 392 | @Override |
| 393 | public void init(FloodlightModuleContext context) |
| 394 | throws FloodlightModuleException { |
| 395 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 396 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 397 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 398 | hostService = context.getServiceImpl(IHostService.class); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 399 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 400 | topologyService = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 401 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 402 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 403 | @Override |
| 404 | public void startUp(FloodlightModuleContext context) { |
| 405 | floodlightProvider.addOFSwitchListener(this); |
| 406 | linkDiscovery.addListener(this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 407 | hostService.addHostListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 408 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 409 | topology = topologyService.getTopology(); |
| 410 | topologyDiscoveryInterface = |
| 411 | topologyService.getTopologyDiscoveryInterface(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 412 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 413 | // Run the cleanup thread |
| 414 | String enableCleanup = |
| 415 | context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY); |
| 416 | if (enableCleanup != null |
| 417 | && enableCleanup.equalsIgnoreCase("false")) { |
| 418 | cleanupEnabled = false; |
| 419 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 420 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 421 | log.debug("Cleanup thread is {}enabled", (cleanupEnabled) ? "" : "not "); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 422 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 423 | if (cleanupEnabled) { |
| 424 | IThreadPoolService threadPool = |
| 425 | context.getServiceImpl(IThreadPoolService.class); |
| 426 | cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(), |
| 427 | new SwitchCleanup()); |
| 428 | // Run the cleanup task immediately on startup |
| 429 | cleanupTask.reschedule(0, TimeUnit.SECONDS); |
| 430 | } |
| 431 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 432 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 433 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 434 | public void hostAdded(Host host) { |
| 435 | log.debug("Called onosDeviceAdded mac {}", host.getMacAddress()); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 436 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 437 | SwitchPort sp = new SwitchPort(host.getSwitchDPID(), host.getSwitchPort()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 438 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 439 | spLists.add(sp); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 440 | HostEvent event = new HostEvent(host.getMacAddress()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 441 | event.setAttachmentPoints(spLists); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 442 | event.setLastSeenTime(host.getLastSeenTimestamp().getTime()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 443 | // Does not use vlan info now. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 444 | event.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 445 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 446 | topologyDiscoveryInterface.putHostDiscoveryEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 447 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 448 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 449 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 450 | public void hostRemoved(Host host) { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 451 | log.debug("Called onosDeviceRemoved"); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 452 | HostEvent event = new HostEvent(host.getMacAddress()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 453 | // XXX shouldn't we be setting attachment points? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 454 | event.freeze(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 455 | topologyDiscoveryInterface.removeHostDiscoveryEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 456 | } |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 457 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 458 | private void controllerRoleChanged(Dpid dpid, Role role) { |
| 459 | log.debug("Local switch controller mastership role changed: dpid = {} role = {}", |
| 460 | dpid, role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 461 | MastershipEvent mastershipEvent = |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 462 | new MastershipEvent(dpid, registryService.getOnosInstanceId(), |
| 463 | role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 464 | // FIXME should be merging, with existing attrs, etc.. |
| 465 | // TODO define attr name as constant somewhere. |
| 466 | // TODO populate appropriate attributes. |
| 467 | mastershipEvent.createStringAttribute(TopologyElement.TYPE, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 468 | TopologyElement.TYPE_ALL_LAYERS); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 469 | mastershipEvent.freeze(); |
| 470 | topologyDiscoveryInterface.putSwitchMastershipEvent(mastershipEvent); |
| 471 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 472 | } |