2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.applications.topology.lldp;
10 import java.util.Date;
12 import java.util.Map.Entry;
13 import java.util.Timer;
14 import java.util.TimerTask;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
21 public class LLDPLinkAger implements AutoCloseable {
22 private final long linkExpirationTime;
23 private Map<LinkDiscovered, Date> linkToDate;
25 private NotificationProviderService notificationService;
28 * default ctor - start timer
30 public LLDPLinkAger(final long lldpInterval, final long linkExpirationTime) {
31 this.linkExpirationTime = linkExpirationTime;
32 linkToDate = new ConcurrentHashMap<>();
34 timer.schedule(new LLDPAgingTask(), 0, lldpInterval);
37 public void put(LinkDiscovered link) {
38 Date expires = new Date();
39 expires.setTime(expires.getTime() + linkExpirationTime);
40 linkToDate.put(link, expires);
49 public void setNotificationService(NotificationProviderService notificationService) {
50 this.notificationService = notificationService;
53 private class LLDPAgingTask extends TimerTask {
57 for (Entry<LinkDiscovered,Date> entry : linkToDate.entrySet()) {
58 LinkDiscovered link = entry.getKey();
59 Date expires = entry.getValue();
60 Date now = new Date();
61 if(now.after(expires)) {
62 if (notificationService != null) {
63 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
64 notificationService.publish(lrb.build());
65 linkToDate.remove(link);