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 final Map<LinkDiscovered, Date> linkToDate;
24 private final Timer timer;
25 private final NotificationProviderService notificationService;
28 * default ctor - start timer
30 public LLDPLinkAger(final long lldpInterval, final long linkExpirationTime,
31 final NotificationProviderService notificationService) {
32 this.linkExpirationTime = linkExpirationTime;
33 this.notificationService = notificationService;
34 linkToDate = new ConcurrentHashMap<>();
36 timer.schedule(new LLDPAgingTask(), 0, lldpInterval);
39 public void put(LinkDiscovered link) {
40 Date expires = new Date();
41 expires.setTime(expires.getTime() + linkExpirationTime);
42 linkToDate.put(link, expires);
51 private class LLDPAgingTask extends TimerTask {
55 for (Entry<LinkDiscovered,Date> entry : linkToDate.entrySet()) {
56 LinkDiscovered link = entry.getKey();
57 Date expires = entry.getValue();
58 Date now = new Date();
59 if(now.after(expires)) {
60 if (notificationService != null) {
61 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
62 notificationService.publish(lrb.build());
63 linkToDate.remove(link);