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