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