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 com.google.common.annotations.VisibleForTesting;
11 import java.util.Date;
13 import java.util.Map.Entry;
14 import java.util.Timer;
15 import java.util.TimerTask;
16 import java.util.concurrent.ConcurrentHashMap;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
22 public class LLDPLinkAger implements AutoCloseable {
23 private final long linkExpirationTime;
24 private final Map<LinkDiscovered, Date> linkToDate;
25 private final Timer timer;
26 private final NotificationProviderService notificationService;
29 * default ctor - start timer
31 public LLDPLinkAger(final long lldpInterval, final long linkExpirationTime,
32 final NotificationProviderService notificationService) {
33 this.linkExpirationTime = linkExpirationTime;
34 this.notificationService = notificationService;
35 linkToDate = new ConcurrentHashMap<>();
37 timer.schedule(new LLDPAgingTask(), 0, lldpInterval);
40 public void put(LinkDiscovered link) {
41 Date expires = new Date();
42 expires.setTime(expires.getTime() + linkExpirationTime);
43 linkToDate.put(link, expires);
52 private class LLDPAgingTask extends TimerTask {
56 for (Entry<LinkDiscovered,Date> entry : linkToDate.entrySet()) {
57 LinkDiscovered link = entry.getKey();
58 Date expires = entry.getValue();
59 Date now = new Date();
60 if(now.after(expires)) {
61 if (notificationService != null) {
62 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
63 notificationService.publish(lrb.build());
64 linkToDate.remove(link);
74 public boolean isLinkToDateEmpty() {
75 return linkToDate.isEmpty();