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