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; |
| 10 | import net.floodlightcontroller.core.IOFSwitch; |
| 11 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 12 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.util.SingletonTask; |
| 16 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.hostmanager.Host; |
| 18 | import net.onrc.onos.core.hostmanager.IHostListener; |
| 19 | import net.onrc.onos.core.hostmanager.IHostService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener; |
| 21 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.linkdiscovery.Link; |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.main.IOFSwitchPortListener; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.registry.RegistryException; |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 30 | |
| 31 | import org.openflow.protocol.OFPhysicalPort; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 32 | import org.openflow.util.HexString; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 35 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 36 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 37 | * The TopologyPublisher subscribes to topology network events from the |
| 38 | * discovery modules. These events are reformatted and relayed to the in-memory |
| 39 | * topology instance. |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 40 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 41 | public class TopologyPublisher implements /*IOFSwitchListener,*/ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | IOFSwitchPortListener, |
| 43 | ILinkDiscoveryListener, |
| 44 | IFloodlightModule, |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 45 | IHostListener { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 46 | private static final Logger log = |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 47 | LoggerFactory.getLogger(TopologyPublisher.class); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 49 | private IFloodlightProviderService floodlightProvider; |
| 50 | private ILinkDiscoveryService linkDiscovery; |
| 51 | private IControllerRegistryService registryService; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 52 | private ITopologyService topologyService; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 53 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 54 | private IHostService hostService; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 55 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 56 | private Topology topology; |
| 57 | private TopologyDiscoveryInterface topologyDiscoveryInterface; |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 58 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 59 | private static final String ENABLE_CLEANUP_PROPERTY = "EnableCleanup"; |
| 60 | private boolean cleanupEnabled = true; |
| 61 | private static final int CLEANUP_TASK_INTERVAL = 60; // in seconds |
| 62 | private SingletonTask cleanupTask; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 63 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 64 | /** |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 65 | * Cleanup old switches from the topology. Old switches are those |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 66 | * which have no controller in the registry. |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 67 | */ |
| 68 | private class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 69 | @Override |
| 70 | public void run() { |
| 71 | String old = Thread.currentThread().getName(); |
| 72 | Thread.currentThread().setName("SwitchCleanup@" + old); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 73 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 74 | try { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 75 | if (log.isTraceEnabled()) { |
| 76 | log.trace("Running cleanup thread"); |
| 77 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 78 | switchCleanup(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 79 | } finally { |
| 80 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 81 | TimeUnit.SECONDS); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 82 | Thread.currentThread().setName(old); |
| 83 | } |
| 84 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 85 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 86 | /** |
| 87 | * First half of the switch cleanup operation. This method will attempt |
| 88 | * to get control of any switch it sees without a controller via the |
| 89 | * registry. |
| 90 | */ |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 91 | private void switchCleanup() { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 92 | Iterable<Switch> switches = topology.getSwitches(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 93 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 94 | if (log.isTraceEnabled()) { |
| 95 | log.trace("Checking for inactive switches"); |
| 96 | } |
| 97 | // For each switch check if a controller exists in controller registry |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | for (Switch sw : switches) { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 99 | try { |
| 100 | String controller = |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 101 | registryService.getControllerForSwitch(sw.getDpid().value()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 102 | if (controller == null) { |
| 103 | log.debug("Requesting control to set switch {} INACTIVE", |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 104 | sw.getDpid()); |
| 105 | registryService.requestControl(sw.getDpid().value(), this); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 106 | } |
| 107 | } catch (RegistryException e) { |
| 108 | log.error("Caught RegistryException in cleanup thread", e); |
| 109 | } |
| 110 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Second half of the switch cleanup operation. If the registry grants |
| 115 | * control of a switch, we can be sure no other instance is writing |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 116 | * this switch to the topology, so we can remove it now. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | * |
| 118 | * @param dpid the dpid of the switch we requested control for |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 119 | * @param hasControl whether we got control or not |
| 120 | */ |
| 121 | @Override |
| 122 | public void controlChanged(long dpid, boolean hasControl) { |
| 123 | if (hasControl) { |
| 124 | log.debug("Got control to set switch {} INACTIVE", |
| 125 | HexString.toHexString(dpid)); |
Jonathan Hart | e02cf54 | 2014-04-02 16:24:44 -0700 | [diff] [blame] | 126 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 127 | SwitchEvent switchEvent = new SwitchEvent(new Dpid(dpid)); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 128 | topologyDiscoveryInterface. |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 129 | removeSwitchDiscoveryEvent(switchEvent); |
| 130 | registryService.releaseControl(dpid); |
| 131 | } |
| 132 | } |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 133 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 134 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 135 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 136 | public void linkAdded(Link link) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 137 | LinkEvent linkEvent = new LinkEvent( |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 138 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 139 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 140 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 141 | // FIXME should be merging, with existing attrs, etc.. |
| 142 | // TODO define attr name as constant somewhere. |
| 143 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 144 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 145 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 146 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 147 | linkEvent.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 148 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 149 | if (!registryService.hasControl(link.getDst())) { |
| 150 | // Don't process or send a link event if we're not master for the |
| 151 | // destination switch |
| 152 | log.debug("Not the master for dst switch {}. Suppressed link add event {}.", |
| 153 | link.getDst(), linkEvent); |
| 154 | return; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 155 | } |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 156 | topologyDiscoveryInterface.putLinkDiscoveryEvent(linkEvent); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void linkRemoved(Link link) { |
| 161 | LinkEvent linkEvent = new LinkEvent( |
| 162 | new SwitchPort(link.getSrc(), link.getSrcPort()), |
| 163 | new SwitchPort(link.getDst(), link.getDstPort())); |
| 164 | |
| 165 | // FIXME should be merging, with existing attrs, etc.. |
| 166 | // TODO define attr name as constant somewhere. |
| 167 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 168 | linkEvent.createStringAttribute(TopologyElement.TYPE, |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 169 | TopologyElement.TYPE_PACKET_LAYER); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 170 | linkEvent.freeze(); |
| 171 | |
| 172 | if (!registryService.hasControl(link.getDst())) { |
| 173 | // Don't process or send a link event if we're not master for the |
| 174 | // destination switch |
| 175 | log.debug("Not the master for dst switch {}. Suppressed link remove event {}.", |
| 176 | link.getDst(), linkEvent); |
| 177 | return; |
| 178 | } |
| 179 | topologyDiscoveryInterface.removeLinkDiscoveryEvent(linkEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 180 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 181 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public void switchPortAdded(Long switchId, OFPhysicalPort port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 184 | final Dpid dpid = new Dpid(switchId); |
| 185 | PortEvent portEvent = new PortEvent(dpid, new PortNumber(port.getPortNumber())); |
| 186 | // FIXME should be merging, with existing attrs, etc.. |
| 187 | // TODO define attr name as constant somewhere. |
| 188 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 189 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 190 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 191 | portEvent.createStringAttribute("name", port.getName()); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 192 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 193 | portEvent.freeze(); |
| 194 | |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 195 | if (registryService.hasControl(switchId)) { |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 196 | topologyDiscoveryInterface.putPortDiscoveryEvent(portEvent); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 197 | linkDiscovery.enableDiscoveryOnPort(switchId, port.getPortNumber()); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 198 | } else { |
| 199 | log.debug("Not the master for switch {}. Suppressed port add event {}.", |
| 200 | new Dpid(switchId), portEvent); |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 201 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 202 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 203 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 204 | @Override |
| 205 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 206 | final Dpid dpid = new Dpid(switchId); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 207 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 208 | PortEvent portEvent = new PortEvent(dpid, new PortNumber(port.getPortNumber())); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 209 | // FIXME should be merging, with existing attrs, etc.. |
| 210 | // TODO define attr name as constant somewhere. |
| 211 | // TODO populate appropriate attributes. |
| 212 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 213 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 214 | portEvent.createStringAttribute("name", port.getName()); |
| 215 | |
| 216 | portEvent.freeze(); |
| 217 | |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 218 | if (registryService.hasControl(switchId)) { |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 219 | topologyDiscoveryInterface.removePortDiscoveryEvent(portEvent); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 220 | } else { |
| 221 | log.debug("Not the master for switch {}. Suppressed port del event {}.", |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 222 | dpid, portEvent); |
Jonathan Hart | 67b6cba | 2014-05-30 22:36:37 -0700 | [diff] [blame] | 223 | } |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 224 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 225 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 226 | @Override |
| 227 | public void addedSwitch(IOFSwitch sw) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 228 | final Dpid dpid = new Dpid(sw.getId()); |
| 229 | SwitchEvent switchEvent = new SwitchEvent(dpid); |
| 230 | // FIXME should be merging, with existing attrs, etc.. |
| 231 | // TODO define attr name as constant somewhere. |
| 232 | // TODO populate appropriate attributes. |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 233 | switchEvent.createStringAttribute(TopologyElement.TYPE, |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 234 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 235 | switchEvent.createStringAttribute("ConnectedSince", |
| 236 | sw.getConnectedSince().toString()); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 237 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 238 | switchEvent.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 239 | |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 240 | // TODO Not very robust |
| 241 | if (!registryService.hasControl(sw.getId())) { |
| 242 | log.debug("Not the master for switch {}. Suppressed switch add event {}.", |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 243 | dpid, switchEvent); |
Yuta HIGUCHI | 5bbbaca | 2014-06-09 16:39:08 -0700 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 247 | List<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 248 | for (OFPhysicalPort port : sw.getPorts()) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 249 | PortEvent portEvent = new PortEvent(dpid, new PortNumber(port.getPortNumber())); |
| 250 | // FIXME should be merging, with existing attrs, etc.. |
| 251 | // TODO define attr name as constant somewhere. |
| 252 | // TODO populate appropriate attributes. |
| 253 | portEvent.createStringAttribute("name", port.getName()); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 254 | portEvent.createStringAttribute(TopologyElement.TYPE, |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 255 | TopologyElement.TYPE_PACKET_LAYER); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 256 | |
| 257 | portEvent.freeze(); |
| 258 | portEvents.add(portEvent); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 259 | } |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 260 | topologyDiscoveryInterface |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 261 | .putSwitchDiscoveryEvent(switchEvent, portEvents); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 262 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 263 | for (OFPhysicalPort port : sw.getPorts()) { |
| 264 | // Allow links to be discovered on this port now that it's |
| 265 | // in the database |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 266 | linkDiscovery.enableDiscoveryOnPort(sw.getId(), port.getPortNumber()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 269 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 270 | @Override |
| 271 | public void removedSwitch(IOFSwitch sw) { |
| 272 | // We don't use this event - switch remove is done by cleanup thread |
| 273 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 274 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public void switchPortChanged(Long switchId) { |
| 277 | // We don't use this event |
| 278 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 279 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 280 | @Override |
| 281 | public String getName() { |
| 282 | // TODO Auto-generated method stub |
| 283 | return null; |
| 284 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 285 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 286 | /* ***************** |
| 287 | * IFloodlightModule |
| 288 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 289 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 290 | @Override |
| 291 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 292 | return null; |
| 293 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 294 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 297 | getServiceImpls() { |
| 298 | return null; |
| 299 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 300 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 301 | @Override |
| 302 | public Collection<Class<? extends IFloodlightService>> |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 303 | getModuleDependencies() { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 304 | Collection<Class<? extends IFloodlightService>> l = |
| 305 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 306 | l.add(IFloodlightProviderService.class); |
| 307 | l.add(ILinkDiscoveryService.class); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 308 | l.add(IThreadPoolService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 309 | l.add(IControllerRegistryService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 310 | l.add(ITopologyService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 311 | l.add(IHostService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 312 | return l; |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 313 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 314 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 315 | @Override |
| 316 | public void init(FloodlightModuleContext context) |
| 317 | throws FloodlightModuleException { |
| 318 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 319 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 320 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 321 | hostService = context.getServiceImpl(IHostService.class); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 322 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 323 | topologyService = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 324 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 325 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 326 | @Override |
| 327 | public void startUp(FloodlightModuleContext context) { |
| 328 | floodlightProvider.addOFSwitchListener(this); |
| 329 | linkDiscovery.addListener(this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 330 | hostService.addHostListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 331 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 332 | topology = topologyService.getTopology(); |
| 333 | topologyDiscoveryInterface = |
| 334 | topologyService.getTopologyDiscoveryInterface(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 335 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 336 | // Run the cleanup thread |
| 337 | String enableCleanup = |
| 338 | context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY); |
| 339 | if (enableCleanup != null |
| 340 | && enableCleanup.equalsIgnoreCase("false")) { |
| 341 | cleanupEnabled = false; |
| 342 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 343 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 344 | log.debug("Cleanup thread is {}enabled", (cleanupEnabled) ? "" : "not "); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 345 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 346 | if (cleanupEnabled) { |
| 347 | IThreadPoolService threadPool = |
| 348 | context.getServiceImpl(IThreadPoolService.class); |
| 349 | cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(), |
| 350 | new SwitchCleanup()); |
| 351 | // Run the cleanup task immediately on startup |
| 352 | cleanupTask.reschedule(0, TimeUnit.SECONDS); |
| 353 | } |
| 354 | } |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 355 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 356 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 357 | public void hostAdded(Host host) { |
| 358 | log.debug("Called onosDeviceAdded mac {}", host.getMacAddress()); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 359 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 360 | SwitchPort sp = new SwitchPort(host.getSwitchDPID(), host.getSwitchPort()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 361 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 362 | spLists.add(sp); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 363 | HostEvent event = new HostEvent(host.getMacAddress()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 364 | event.setAttachmentPoints(spLists); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 365 | event.setLastSeenTime(host.getLastSeenTimestamp().getTime()); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 366 | // Does not use vlan info now. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 367 | event.freeze(); |
Jonathan Hart | b3e1b05 | 2014-04-02 16:01:12 -0700 | [diff] [blame] | 368 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 369 | topologyDiscoveryInterface.putHostDiscoveryEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 370 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 371 | |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 372 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 373 | public void hostRemoved(Host host) { |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 374 | log.debug("Called onosDeviceRemoved"); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 375 | HostEvent event = new HostEvent(host.getMacAddress()); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 376 | //XXX shouldn't we be setting attachment points? |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 377 | event.freeze(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 378 | topologyDiscoveryInterface.removeHostDiscoveryEvent(event); |
Jonathan Hart | 8877067 | 2014-04-02 18:08:30 -0700 | [diff] [blame] | 379 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 380 | } |