Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.floodlightlistener; |
| 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 | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 17 | import net.onrc.onos.datagrid.IDatagridService; |
| 18 | import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener; |
| 19 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener; |
| 20 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 21 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 22 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraphDiscoveryInterface; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent; |
| 26 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 28 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 29 | import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback; |
| 30 | import net.onrc.onos.registry.controller.RegistryException; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 31 | |
| 32 | import org.openflow.protocol.OFPhysicalPort; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 33 | import org.openflow.util.HexString; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * I've created a copy of the NetworkGraphPublisher so I can integrate |
| 39 | * the new API with ONOS while still having the old NetworkGraphPublisher |
| 40 | * to reference. I've renamed to RCNetworkGraphPublisher. |
| 41 | * TODO Remove old NetworkGraphPublisher once the integration of the new |
| 42 | * API is complete. |
| 43 | * For now, we just write to the database and don't worry about sending |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 44 | * notifications. |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 45 | * TODO Send notification after each database write |
| 46 | */ |
| 47 | public class RCNetworkGraphPublisher implements /*IOFSwitchListener,*/ |
| 48 | IOFSwitchPortListener, |
| 49 | ILinkDiscoveryListener, |
| 50 | IFloodlightModule { |
Jonathan Hart | 4701671 | 2014-02-07 12:41:35 -0800 | [diff] [blame] | 51 | private static final Logger log = LoggerFactory.getLogger(RCNetworkGraphPublisher.class); |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 52 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 53 | private IFloodlightProviderService floodlightProvider; |
| 54 | private ILinkDiscoveryService linkDiscovery; |
| 55 | private IControllerRegistryService registryService; |
| 56 | private IDatagridService datagridService; |
| 57 | private INetworkGraphService networkGraphService; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 58 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 59 | private NetworkGraph networkGraph; |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 60 | private NetworkGraphDiscoveryInterface networkGraphDiscoveryInterface; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 61 | |
| 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 | /** |
| 68 | * Cleanup and synch switch state from registry |
| 69 | */ |
| 70 | private class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 71 | @Override |
| 72 | public void run() { |
| 73 | String old = Thread.currentThread().getName(); |
| 74 | Thread.currentThread().setName("SwitchCleanup@" + old); |
| 75 | |
| 76 | try { |
| 77 | log.debug("Running cleanup thread"); |
| 78 | switchCleanup(); |
| 79 | } |
| 80 | catch (Exception e) { |
| 81 | log.error("Error in cleanup thread", e); |
| 82 | } finally { |
| 83 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
| 84 | TimeUnit.SECONDS); |
| 85 | Thread.currentThread().setName(old); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private void switchCleanup() { |
| 90 | Iterable<Switch> switches = networkGraph.getSwitches(); |
| 91 | |
| 92 | log.debug("Checking for inactive switches"); |
| 93 | // For each switch check if a controller exists in controller registry |
| 94 | for (Switch sw: switches) { |
| 95 | try { |
| 96 | String controller = |
| 97 | registryService.getControllerForSwitch(sw.getDpid()); |
| 98 | if (controller == null) { |
| 99 | log.debug("Requesting control to set switch {} INACTIVE", |
| 100 | HexString.toHexString(sw.getDpid())); |
| 101 | registryService.requestControl(sw.getDpid(), this); |
| 102 | } |
| 103 | } catch (RegistryException e) { |
| 104 | log.error("Caught RegistryException in cleanup thread", e); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void controlChanged(long dpid, boolean hasControl) { |
| 111 | if (hasControl) { |
| 112 | log.debug("Got control to set switch {} INACTIVE", HexString.toHexString(dpid)); |
| 113 | /* |
| 114 | // Get the affected ports |
| 115 | List<Short> ports = swStore.getPorts(HexString.toHexString(dpid)); |
| 116 | // Get the affected links |
| 117 | List<Link> links = linkStore.getLinks(HexString.toHexString(dpid)); |
| 118 | // Get the affected reverse links |
| 119 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid)); |
| 120 | links.addAll(reverseLinks); |
| 121 | */ |
| 122 | SwitchEvent switchEvent = new SwitchEvent(dpid); |
| 123 | networkGraphDiscoveryInterface.removeSwitchEvent(switchEvent); |
| 124 | registryService.releaseControl(dpid); |
| 125 | |
| 126 | // TODO publish UPDATE_SWITCH event here |
| 127 | // |
| 128 | // NOTE: Here we explicitly send |
| 129 | // notification to remove the |
| 130 | // switch, because it is inactive |
| 131 | // |
| 132 | /* |
| 133 | TopologyElement topologyElement = |
| 134 | new TopologyElement(dpid); |
| 135 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 136 | |
| 137 | // Publish: remove the affected ports |
| 138 | for (Short port : ports) { |
| 139 | TopologyElement topologyElementPort = |
| 140 | new TopologyElement(dpid, port); |
| 141 | datagridService.notificationSendTopologyElementRemoved(topologyElementPort); |
| 142 | } |
| 143 | // Publish: remove the affected links |
| 144 | for (Link link : links) { |
| 145 | TopologyElement topologyElementLink = |
| 146 | new TopologyElement(link.getSrc(), |
| 147 | link.getSrcPort(), |
| 148 | link.getDst(), |
| 149 | link.getDstPort()); |
| 150 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 151 | } |
| 152 | */ |
| 153 | } |
| 154 | } |
| 155 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 156 | |
| 157 | @Override |
| 158 | public void linkDiscoveryUpdate(LDUpdate update) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 159 | LinkEvent linkEvent = new LinkEvent(update.getSrc(), |
| 160 | (long)update.getSrcPort(), update.getDst(), |
| 161 | (long)update.getDstPort()); |
| 162 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 163 | switch (update.getOperation()) { |
| 164 | case LINK_ADDED: |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 165 | networkGraphDiscoveryInterface.putLinkEvent(linkEvent); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 166 | /* |
| 167 | TopologyElement topologyElement = |
| 168 | new TopologyElement(update.getSrc(), |
| 169 | update.getSrcPort(), |
| 170 | update.getDst(), |
| 171 | update.getDstPort()); |
| 172 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
| 173 | */ |
| 174 | break; |
| 175 | case LINK_UPDATED: |
| 176 | // I don't know what a LINK_UPDATED event is. |
| 177 | // We never use it. |
| 178 | break; |
| 179 | case LINK_REMOVED: |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 180 | networkGraphDiscoveryInterface.removeLinkEvent(linkEvent); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 181 | /* |
| 182 | TopologyElement topologyElement = |
| 183 | new TopologyElement(update.getSrc(), |
| 184 | update.getSrcPort(), |
| 185 | update.getDst(), |
| 186 | update.getDstPort()); |
| 187 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 188 | */ |
| 189 | break; |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void switchPortAdded(Long switchId, OFPhysicalPort port) { |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 197 | PortEvent portEvent = new PortEvent(switchId, (long)port.getPortNumber()); |
| 198 | networkGraphDiscoveryInterface.putPortEvent(portEvent); |
| 199 | linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber()); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 204 | PortEvent portEvent = new PortEvent(switchId, (long)port.getPortNumber()); |
| 205 | networkGraphDiscoveryInterface.removePortEvent(portEvent); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void addedSwitch(IOFSwitch sw) { |
| 210 | // TODO Not very robust |
| 211 | if (!registryService.hasControl(sw.getId())) { |
| 212 | return; |
| 213 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 214 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 215 | SwitchEvent switchEvent = new SwitchEvent(sw.getId()); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 216 | |
| 217 | List<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 218 | for (OFPhysicalPort port : sw.getPorts()) { |
| 219 | portEvents.add(new PortEvent(sw.getId(), (long)port.getPortNumber())); |
| 220 | } |
| 221 | switchEvent.setPorts(portEvents); |
| 222 | |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 223 | networkGraphDiscoveryInterface.putSwitchEvent(switchEvent); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 224 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 225 | /* |
| 226 | // TODO publish ADD_SWITCH event here |
| 227 | TopologyElement topologyElement = |
| 228 | new TopologyElement(sw.getId()); |
| 229 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 230 | */ |
| 231 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 232 | // Publish: add the ports |
| 233 | // TODO: Add only ports that are UP? |
| 234 | for (OFPhysicalPort port : sw.getPorts()) { |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 235 | //TopologyElement topologyElementPort = |
| 236 | //new TopologyElement(sw.getId(), port.getPortNumber()); |
| 237 | //datagridService.notificationSendTopologyElementAdded(topologyElementPort); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 238 | |
| 239 | // Allow links to be discovered on this port now that it's |
| 240 | // in the database |
| 241 | linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber()); |
| 242 | } |
| 243 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 244 | /* |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 245 | // Add all links that might be connected already |
| 246 | List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId())); |
| 247 | // Add all reverse links as well |
| 248 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId())); |
| 249 | links.addAll(reverseLinks); |
| 250 | |
| 251 | // Publish: add the links |
| 252 | for (Link link : links) { |
| 253 | TopologyElement topologyElementLink = |
| 254 | new TopologyElement(link.getSrc(), |
| 255 | link.getSrcPort(), |
| 256 | link.getDst(), |
| 257 | link.getDstPort()); |
| 258 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 259 | */ |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void removedSwitch(IOFSwitch sw) { |
Jonathan Hart | 22eb988 | 2014-02-11 15:52:59 -0800 | [diff] [blame] | 264 | // TODO move to cleanup thread |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 265 | //SwitchEvent switchEvent = new SwitchEvent(sw.getId()); |
| 266 | //networkGraphDiscoveryInterface.removeSwitchEvent(switchEvent); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void switchPortChanged(Long switchId) { |
| 271 | // TODO Auto-generated method stub |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 272 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public String getName() { |
| 277 | // TODO Auto-generated method stub |
| 278 | return null; |
| 279 | } |
| 280 | |
| 281 | /* ***************** |
| 282 | * IFloodlightModule |
| 283 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 284 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 285 | @Override |
| 286 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 287 | return null; |
| 288 | } |
| 289 | |
| 290 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 291 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 292 | getServiceImpls() { |
| 293 | return null; |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 298 | Collection<Class<? extends IFloodlightService>> l = |
| 299 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 300 | l.add(IFloodlightProviderService.class); |
| 301 | l.add(ILinkDiscoveryService.class); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 302 | l.add(IThreadPoolService.class); |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 303 | l.add(IControllerRegistryService.class); |
| 304 | l.add(IDatagridService.class); |
| 305 | l.add(INetworkGraphService.class); |
| 306 | return l; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void init(FloodlightModuleContext context) |
| 311 | throws FloodlightModuleException { |
| 312 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 313 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 314 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
| 315 | datagridService = context.getServiceImpl(IDatagridService.class); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 316 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 317 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void startUp(FloodlightModuleContext context) { |
| 322 | // TODO enable cleanup thread |
| 323 | floodlightProvider.addOFSwitchListener(this); |
| 324 | linkDiscovery.addListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 325 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 326 | networkGraph = networkGraphService.getNetworkGraph(); |
| 327 | networkGraphDiscoveryInterface = |
| 328 | networkGraphService.getNetworkGraphDiscoveryInterface(); |
| 329 | |
| 330 | // Run the cleanup thread |
| 331 | String enableCleanup = |
| 332 | context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY); |
| 333 | if (enableCleanup != null && enableCleanup.toLowerCase().equals("false")) { |
| 334 | cleanupEnabled = false; |
| 335 | } |
| 336 | |
| 337 | log.debug("Cleanup thread is {}enabled", (cleanupEnabled)? "" : "not "); |
| 338 | |
| 339 | if (cleanupEnabled) { |
| 340 | IThreadPoolService threadPool = |
| 341 | context.getServiceImpl(IThreadPoolService.class); |
| 342 | cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(), |
| 343 | new SwitchCleanup()); |
| 344 | // Run the cleanup task immediately on startup |
| 345 | cleanupTask.reschedule(0, TimeUnit.SECONDS); |
| 346 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 347 | } |
| 348 | } |