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