Merge "Bug-5543 - Bo: Update JUnit tests part 2"
[openflowplugin.git] / applications / topology-lldp-discovery / src / main / java / org / opendaylight / openflowplugin / applications / topology / lldp / LLDPLinkAger.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.applications.topology.lldp;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import java.util.Date;
12 import java.util.Map;
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;
20
21
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;
27
28     /**
29      * default ctor - start timer
30      */
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<>();
36         timer = new Timer();
37         timer.schedule(new LLDPAgingTask(), 0, lldpInterval);
38     }
39
40     public void put(LinkDiscovered link) {
41         Date expires = new Date();
42         expires.setTime(expires.getTime() + linkExpirationTime);
43         linkToDate.put(link, expires);
44     }
45
46     @Override
47     public void close() {
48         timer.cancel();
49         linkToDate.clear();
50     }
51
52     private class LLDPAgingTask extends TimerTask {
53
54         @Override
55         public void run() {
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);
65                     }
66                 }
67             }
68
69         }
70
71     }
72
73     @VisibleForTesting
74     public boolean isLinkToDateEmpty() {
75         return linkToDate.isEmpty();
76     }
77
78 }
79