blob: 5e2b192ac0c9e6aaa61090e8e22f6408799459a4 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart4b5bbb52014-02-06 10:09:31 -08002
3import java.util.ArrayList;
4import java.util.Collection;
Jonathan Hart369875b2014-02-13 10:00:31 -08005import java.util.List;
Jonathan Hart4b5bbb52014-02-06 10:09:31 -08006import java.util.Map;
Jonathan Hart369875b2014-02-13 10:00:31 -08007import java.util.concurrent.TimeUnit;
Jonathan Hart4b5bbb52014-02-06 10:09:31 -08008
9import net.floodlightcontroller.core.IFloodlightProviderService;
10import net.floodlightcontroller.core.IOFSwitch;
11import net.floodlightcontroller.core.module.FloodlightModuleContext;
12import net.floodlightcontroller.core.module.FloodlightModuleException;
13import net.floodlightcontroller.core.module.IFloodlightModule;
14import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart369875b2014-02-13 10:00:31 -080015import net.floodlightcontroller.core.util.SingletonTask;
16import net.floodlightcontroller.threadpool.IThreadPoolService;
Jonathan Hart23701d12014-04-03 10:45:48 -070017import net.onrc.onos.core.devicemanager.IOnosDeviceListener;
18import net.onrc.onos.core.devicemanager.IOnosDeviceService;
19import net.onrc.onos.core.devicemanager.OnosDevice;
20import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener;
21import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService;
Jonathan Hart284e70f2014-07-05 12:32:51 -070022import net.onrc.onos.core.linkdiscovery.Link;
Jonathan Hart51f6f5b2014-04-03 10:32:10 -070023import net.onrc.onos.core.main.IOFSwitchPortListener;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070024import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070025import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback;
Jonathan Harta99ec672014-04-03 11:30:34 -070026import net.onrc.onos.core.registry.RegistryException;
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -070027import net.onrc.onos.core.util.Dpid;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070028import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070029import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart4b5bbb52014-02-06 10:09:31 -080030
31import org.openflow.protocol.OFPhysicalPort;
Jonathan Hart369875b2014-02-13 10:00:31 -080032import org.openflow.util.HexString;
Toshio Koide2f570c12014-02-06 16:55:32 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Jonathan Hart4b5bbb52014-02-06 10:09:31 -080035
Jonathan Hart88770672014-04-02 18:08:30 -070036/**
Jonathan Harte37e4e22014-05-13 19:12:02 -070037 * The TopologyPublisher subscribes to topology network events from the
38 * discovery modules. These events are reformatted and relayed to the in-memory
39 * topology instance.
Jonathan Hart88770672014-04-02 18:08:30 -070040 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070041public class TopologyPublisher implements /*IOFSwitchListener,*/
Ray Milkey269ffb92014-04-03 14:43:30 -070042 IOFSwitchPortListener,
43 ILinkDiscoveryListener,
44 IFloodlightModule,
45 IOnosDeviceListener {
Jonathan Hart88770672014-04-02 18:08:30 -070046 private static final Logger log =
Jonathan Harte37e4e22014-05-13 19:12:02 -070047 LoggerFactory.getLogger(TopologyPublisher.class);
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080048
Jonathan Hart88770672014-04-02 18:08:30 -070049 private IFloodlightProviderService floodlightProvider;
50 private ILinkDiscoveryService linkDiscovery;
51 private IControllerRegistryService registryService;
Jonathan Harte37e4e22014-05-13 19:12:02 -070052 private ITopologyService topologyService;
Toshio Koide2f570c12014-02-06 16:55:32 -080053
Jonathan Hart88770672014-04-02 18:08:30 -070054 private IOnosDeviceService onosDeviceService;
Jonathan Hartb3e1b052014-04-02 16:01:12 -070055
Jonathan Harte37e4e22014-05-13 19:12:02 -070056 private Topology topology;
57 private TopologyDiscoveryInterface topologyDiscoveryInterface;
Jonathan Hartb3e1b052014-04-02 16:01:12 -070058
Jonathan Hart88770672014-04-02 18:08:30 -070059 private static final String ENABLE_CLEANUP_PROPERTY = "EnableCleanup";
60 private boolean cleanupEnabled = true;
61 private static final int CLEANUP_TASK_INTERVAL = 60; // in seconds
62 private SingletonTask cleanupTask;
Toshio Koide2f570c12014-02-06 16:55:32 -080063
Jonathan Hart369875b2014-02-13 10:00:31 -080064 /**
Jonathan Harte37e4e22014-05-13 19:12:02 -070065 * Cleanup old switches from the topology. Old switches are those
Jonathan Hart88770672014-04-02 18:08:30 -070066 * which have no controller in the registry.
Jonathan Hart369875b2014-02-13 10:00:31 -080067 */
68 private class SwitchCleanup implements ControlChangeCallback, Runnable {
69 @Override
70 public void run() {
71 String old = Thread.currentThread().getName();
72 Thread.currentThread().setName("SwitchCleanup@" + old);
Jonathan Hartb3e1b052014-04-02 16:01:12 -070073
Jonathan Hart369875b2014-02-13 10:00:31 -080074 try {
Jonathan Hart88770672014-04-02 18:08:30 -070075 if (log.isTraceEnabled()) {
76 log.trace("Running cleanup thread");
77 }
Jonathan Hart369875b2014-02-13 10:00:31 -080078 switchCleanup();
Jonathan Hart369875b2014-02-13 10:00:31 -080079 } finally {
80 cleanupTask.reschedule(CLEANUP_TASK_INTERVAL,
Jonathan Hart88770672014-04-02 18:08:30 -070081 TimeUnit.SECONDS);
Jonathan Hart369875b2014-02-13 10:00:31 -080082 Thread.currentThread().setName(old);
83 }
84 }
Jonathan Hartb3e1b052014-04-02 16:01:12 -070085
Jonathan Hart88770672014-04-02 18:08:30 -070086 /**
87 * First half of the switch cleanup operation. This method will attempt
88 * to get control of any switch it sees without a controller via the
89 * registry.
90 */
Jonathan Hart369875b2014-02-13 10:00:31 -080091 private void switchCleanup() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070092 Iterable<Switch> switches = topology.getSwitches();
Jonathan Hart369875b2014-02-13 10:00:31 -080093
Jonathan Hart88770672014-04-02 18:08:30 -070094 if (log.isTraceEnabled()) {
95 log.trace("Checking for inactive switches");
96 }
97 // For each switch check if a controller exists in controller registry
Ray Milkey269ffb92014-04-03 14:43:30 -070098 for (Switch sw : switches) {
Jonathan Hart88770672014-04-02 18:08:30 -070099 try {
100 String controller =
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700101 registryService.getControllerForSwitch(sw.getDpid().value());
Jonathan Hart88770672014-04-02 18:08:30 -0700102 if (controller == null) {
103 log.debug("Requesting control to set switch {} INACTIVE",
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700104 sw.getDpid());
105 registryService.requestControl(sw.getDpid().value(), this);
Jonathan Hart88770672014-04-02 18:08:30 -0700106 }
107 } catch (RegistryException e) {
108 log.error("Caught RegistryException in cleanup thread", e);
109 }
110 }
Jonathan Hart369875b2014-02-13 10:00:31 -0800111 }
112
Jonathan Hart88770672014-04-02 18:08:30 -0700113 /**
114 * Second half of the switch cleanup operation. If the registry grants
115 * control of a switch, we can be sure no other instance is writing
Jonathan Harte37e4e22014-05-13 19:12:02 -0700116 * this switch to the topology, so we can remove it now.
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 *
118 * @param dpid the dpid of the switch we requested control for
Jonathan Hart88770672014-04-02 18:08:30 -0700119 * @param hasControl whether we got control or not
120 */
121 @Override
122 public void controlChanged(long dpid, boolean hasControl) {
123 if (hasControl) {
124 log.debug("Got control to set switch {} INACTIVE",
125 HexString.toHexString(dpid));
Jonathan Harte02cf542014-04-02 16:24:44 -0700126
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700127 SwitchEvent switchEvent = new SwitchEvent(new Dpid(dpid));
Jonathan Harte37e4e22014-05-13 19:12:02 -0700128 topologyDiscoveryInterface.
Jonathan Hart88770672014-04-02 18:08:30 -0700129 removeSwitchDiscoveryEvent(switchEvent);
130 registryService.releaseControl(dpid);
131 }
132 }
Jonathan Hart369875b2014-02-13 10:00:31 -0800133 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800134
Jonathan Hart88770672014-04-02 18:08:30 -0700135 @Override
Jonathan Hart284e70f2014-07-05 12:32:51 -0700136 public void linkAdded(Link link) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700137 LinkEvent linkEvent = new LinkEvent(
Jonathan Hart284e70f2014-07-05 12:32:51 -0700138 new SwitchPort(link.getSrc(), link.getSrcPort()),
139 new SwitchPort(link.getDst(), link.getDstPort()));
140
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700141 // FIXME should be merging, with existing attrs, etc..
142 // TODO define attr name as constant somewhere.
143 // TODO populate appropriate attributes.
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700144 linkEvent.createStringAttribute(TopologyElement.TYPE,
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700145 TopologyElement.TYPE_PACKET_LAYER);
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700146
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700147 linkEvent.freeze();
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700148
Jonathan Hart284e70f2014-07-05 12:32:51 -0700149 if (!registryService.hasControl(link.getDst())) {
150 // Don't process or send a link event if we're not master for the
151 // destination switch
152 log.debug("Not the master for dst switch {}. Suppressed link add event {}.",
153 link.getDst(), linkEvent);
154 return;
Jonathan Hart88770672014-04-02 18:08:30 -0700155 }
Jonathan Hart284e70f2014-07-05 12:32:51 -0700156 topologyDiscoveryInterface.putLinkDiscoveryEvent(linkEvent);
157 }
158
159 @Override
160 public void linkRemoved(Link link) {
161 LinkEvent linkEvent = new LinkEvent(
162 new SwitchPort(link.getSrc(), link.getSrcPort()),
163 new SwitchPort(link.getDst(), link.getDstPort()));
164
165 // FIXME should be merging, with existing attrs, etc..
166 // TODO define attr name as constant somewhere.
167 // TODO populate appropriate attributes.
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700168 linkEvent.createStringAttribute(TopologyElement.TYPE,
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700169 TopologyElement.TYPE_PACKET_LAYER);
Jonathan Hart284e70f2014-07-05 12:32:51 -0700170 linkEvent.freeze();
171
172 if (!registryService.hasControl(link.getDst())) {
173 // Don't process or send a link event if we're not master for the
174 // destination switch
175 log.debug("Not the master for dst switch {}. Suppressed link remove event {}.",
176 link.getDst(), linkEvent);
177 return;
178 }
179 topologyDiscoveryInterface.removeLinkDiscoveryEvent(linkEvent);
Jonathan Hart88770672014-04-02 18:08:30 -0700180 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800181
Jonathan Hart88770672014-04-02 18:08:30 -0700182 @Override
183 public void switchPortAdded(Long switchId, OFPhysicalPort port) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700184 final Dpid dpid = new Dpid(switchId);
185 PortEvent portEvent = new PortEvent(dpid, new PortNumber(port.getPortNumber()));
186 // FIXME should be merging, with existing attrs, etc..
187 // TODO define attr name as constant somewhere.
188 // TODO populate appropriate attributes.
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700189 portEvent.createStringAttribute(TopologyElement.TYPE,
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700190 TopologyElement.TYPE_PACKET_LAYER);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700191 portEvent.createStringAttribute("name", port.getName());
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -0700192
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700193 portEvent.freeze();
194
Jonathan Hart67b6cba2014-05-30 22:36:37 -0700195 if (registryService.hasControl(switchId)) {
Jonathan Hart67b6cba2014-05-30 22:36:37 -0700196 topologyDiscoveryInterface.putPortDiscoveryEvent(portEvent);
Jonathan Hart284e70f2014-07-05 12:32:51 -0700197 linkDiscovery.enableDiscoveryOnPort(switchId, port.getPortNumber());
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -0700198 } else {
199 log.debug("Not the master for switch {}. Suppressed port add event {}.",
200 new Dpid(switchId), portEvent);
Jonathan Hart67b6cba2014-05-30 22:36:37 -0700201 }
Jonathan Hart88770672014-04-02 18:08:30 -0700202 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800203
Jonathan Hart88770672014-04-02 18:08:30 -0700204 @Override
205 public void switchPortRemoved(Long switchId, OFPhysicalPort port) {
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700206 final Dpid dpid = new Dpid(switchId);
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -0700207
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700208 PortEvent portEvent = new PortEvent(dpid, new PortNumber(port.getPortNumber()));
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700209 // FIXME should be merging, with existing attrs, etc..
210 // TODO define attr name as constant somewhere.
211 // TODO populate appropriate attributes.
212 portEvent.createStringAttribute(TopologyElement.TYPE,
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700213 TopologyElement.TYPE_PACKET_LAYER);
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700214 portEvent.createStringAttribute("name", port.getName());
215
216 portEvent.freeze();
217
Jonathan Hart67b6cba2014-05-30 22:36:37 -0700218 if (registryService.hasControl(switchId)) {
Jonathan Hart67b6cba2014-05-30 22:36:37 -0700219 topologyDiscoveryInterface.removePortDiscoveryEvent(portEvent);
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -0700220 } else {
221 log.debug("Not the master for switch {}. Suppressed port del event {}.",
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700222 dpid, portEvent);
Jonathan Hart67b6cba2014-05-30 22:36:37 -0700223 }
Jonathan Hart88770672014-04-02 18:08:30 -0700224 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800225
Jonathan Hart88770672014-04-02 18:08:30 -0700226 @Override
227 public void addedSwitch(IOFSwitch sw) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700228 final Dpid dpid = new Dpid(sw.getId());
229 SwitchEvent switchEvent = new SwitchEvent(dpid);
230 // FIXME should be merging, with existing attrs, etc..
231 // TODO define attr name as constant somewhere.
232 // TODO populate appropriate attributes.
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700233 switchEvent.createStringAttribute(TopologyElement.TYPE,
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700234 TopologyElement.TYPE_PACKET_LAYER);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700235 switchEvent.createStringAttribute("ConnectedSince",
236 sw.getConnectedSince().toString());
Toshio Koide2f570c12014-02-06 16:55:32 -0800237
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700238 switchEvent.freeze();
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700239
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -0700240 // TODO Not very robust
241 if (!registryService.hasControl(sw.getId())) {
242 log.debug("Not the master for switch {}. Suppressed switch add event {}.",
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700243 dpid, switchEvent);
Yuta HIGUCHI5bbbaca2014-06-09 16:39:08 -0700244 return;
245 }
246
Jonathan Hart88770672014-04-02 18:08:30 -0700247 List<PortEvent> portEvents = new ArrayList<PortEvent>();
248 for (OFPhysicalPort port : sw.getPorts()) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700249 PortEvent portEvent = new PortEvent(dpid, new PortNumber(port.getPortNumber()));
250 // FIXME should be merging, with existing attrs, etc..
251 // TODO define attr name as constant somewhere.
252 // TODO populate appropriate attributes.
253 portEvent.createStringAttribute("name", port.getName());
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700254 portEvent.createStringAttribute(TopologyElement.TYPE,
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700255 TopologyElement.TYPE_PACKET_LAYER);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700256
257 portEvent.freeze();
258 portEvents.add(portEvent);
Jonathan Hart88770672014-04-02 18:08:30 -0700259 }
Jonathan Harte37e4e22014-05-13 19:12:02 -0700260 topologyDiscoveryInterface
Ray Milkey269ffb92014-04-03 14:43:30 -0700261 .putSwitchDiscoveryEvent(switchEvent, portEvents);
Toshio Koide2f570c12014-02-06 16:55:32 -0800262
Jonathan Hart88770672014-04-02 18:08:30 -0700263 for (OFPhysicalPort port : sw.getPorts()) {
264 // Allow links to be discovered on this port now that it's
265 // in the database
Jonathan Hart284e70f2014-07-05 12:32:51 -0700266 linkDiscovery.enableDiscoveryOnPort(sw.getId(), port.getPortNumber());
Jonathan Hart88770672014-04-02 18:08:30 -0700267 }
268 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800269
Jonathan Hart88770672014-04-02 18:08:30 -0700270 @Override
271 public void removedSwitch(IOFSwitch sw) {
272 // We don't use this event - switch remove is done by cleanup thread
273 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800274
Jonathan Hart88770672014-04-02 18:08:30 -0700275 @Override
276 public void switchPortChanged(Long switchId) {
277 // We don't use this event
278 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800279
Jonathan Hart88770672014-04-02 18:08:30 -0700280 @Override
281 public String getName() {
282 // TODO Auto-generated method stub
283 return null;
284 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800285
Jonathan Hart88770672014-04-02 18:08:30 -0700286 /* *****************
287 * IFloodlightModule
288 * *****************/
Toshio Koide2f570c12014-02-06 16:55:32 -0800289
Jonathan Hart88770672014-04-02 18:08:30 -0700290 @Override
291 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
292 return null;
293 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800294
Jonathan Hart88770672014-04-02 18:08:30 -0700295 @Override
296 public Map<Class<? extends IFloodlightService>, IFloodlightService>
297 getServiceImpls() {
298 return null;
299 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800300
Jonathan Hart88770672014-04-02 18:08:30 -0700301 @Override
302 public Collection<Class<? extends IFloodlightService>>
Ray Milkey269ffb92014-04-03 14:43:30 -0700303 getModuleDependencies() {
Jonathan Hart88770672014-04-02 18:08:30 -0700304 Collection<Class<? extends IFloodlightService>> l =
305 new ArrayList<Class<? extends IFloodlightService>>();
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800306 l.add(IFloodlightProviderService.class);
307 l.add(ILinkDiscoveryService.class);
Jonathan Hart369875b2014-02-13 10:00:31 -0800308 l.add(IThreadPoolService.class);
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800309 l.add(IControllerRegistryService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700310 l.add(ITopologyService.class);
Jonathan Hartebbe6a62014-04-02 16:10:25 -0700311 l.add(IOnosDeviceService.class);
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800312 return l;
Jonathan Hart88770672014-04-02 18:08:30 -0700313 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800314
Jonathan Hart88770672014-04-02 18:08:30 -0700315 @Override
316 public void init(FloodlightModuleContext context)
317 throws FloodlightModuleException {
318 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
319 linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class);
320 registryService = context.getServiceImpl(IControllerRegistryService.class);
321 onosDeviceService = context.getServiceImpl(IOnosDeviceService.class);
Toshio Koide2f570c12014-02-06 16:55:32 -0800322
Jonathan Harte37e4e22014-05-13 19:12:02 -0700323 topologyService = context.getServiceImpl(ITopologyService.class);
Jonathan Hart88770672014-04-02 18:08:30 -0700324 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800325
Jonathan Hart88770672014-04-02 18:08:30 -0700326 @Override
327 public void startUp(FloodlightModuleContext context) {
328 floodlightProvider.addOFSwitchListener(this);
329 linkDiscovery.addListener(this);
330 onosDeviceService.addOnosDeviceListener(this);
Toshio Koide2f570c12014-02-06 16:55:32 -0800331
Jonathan Harte37e4e22014-05-13 19:12:02 -0700332 topology = topologyService.getTopology();
333 topologyDiscoveryInterface =
334 topologyService.getTopologyDiscoveryInterface();
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700335
Jonathan Hart88770672014-04-02 18:08:30 -0700336 // Run the cleanup thread
337 String enableCleanup =
338 context.getConfigParams(this).get(ENABLE_CLEANUP_PROPERTY);
339 if (enableCleanup != null
340 && enableCleanup.equalsIgnoreCase("false")) {
341 cleanupEnabled = false;
342 }
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700343
Jonathan Hart88770672014-04-02 18:08:30 -0700344 log.debug("Cleanup thread is {}enabled", (cleanupEnabled) ? "" : "not ");
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700345
Jonathan Hart88770672014-04-02 18:08:30 -0700346 if (cleanupEnabled) {
347 IThreadPoolService threadPool =
348 context.getServiceImpl(IThreadPoolService.class);
349 cleanupTask = new SingletonTask(threadPool.getScheduledExecutor(),
350 new SwitchCleanup());
351 // Run the cleanup task immediately on startup
352 cleanupTask.reschedule(0, TimeUnit.SECONDS);
353 }
354 }
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700355
Jonathan Hart88770672014-04-02 18:08:30 -0700356 @Override
357 public void onosDeviceAdded(OnosDevice device) {
358 log.debug("Called onosDeviceAdded mac {}", device.getMacAddress());
TeruUd1c5b652014-03-24 13:58:46 -0700359
TeruU5d2c9392014-06-09 20:02:02 -0700360 SwitchPort sp = new SwitchPort(device.getSwitchDPID(), device.getSwitchPort());
Jonathan Hart88770672014-04-02 18:08:30 -0700361 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
362 spLists.add(sp);
363 DeviceEvent event = new DeviceEvent(device.getMacAddress());
364 event.setAttachmentPoints(spLists);
365 event.setLastSeenTime(device.getLastSeenTimestamp().getTime());
Jonathan Hart88770672014-04-02 18:08:30 -0700366 // Does not use vlan info now.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700367 event.freeze();
Jonathan Hartb3e1b052014-04-02 16:01:12 -0700368
Jonathan Harte37e4e22014-05-13 19:12:02 -0700369 topologyDiscoveryInterface.putDeviceDiscoveryEvent(event);
Jonathan Hart88770672014-04-02 18:08:30 -0700370 }
TeruUd1c5b652014-03-24 13:58:46 -0700371
Jonathan Hart88770672014-04-02 18:08:30 -0700372 @Override
373 public void onosDeviceRemoved(OnosDevice device) {
374 log.debug("Called onosDeviceRemoved");
375 DeviceEvent event = new DeviceEvent(device.getMacAddress());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700376 // XXX shouldn't we be setting attachment points?
377 event.freeze();
Jonathan Harte37e4e22014-05-13 19:12:02 -0700378 topologyDiscoveryInterface.removeDeviceDiscoveryEvent(event);
Jonathan Hart88770672014-04-02 18:08:30 -0700379 }
Jonathan Hart4b5bbb52014-02-06 10:09:31 -0800380}