Bulk-add copyright headers to java files
[controller.git] / opendaylight / md-sal / topology-lldp-discovery / src / main / java / org / opendaylight / md / controller / 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.md.controller.topology.lldp;
9
10 import java.util.Date;
11 import java.util.Map;
12 import java.util.Timer;
13 import java.util.Map.Entry;
14 import java.util.TimerTask;
15 import java.util.concurrent.ConcurrentHashMap;
16
17 import org.opendaylight.md.controller.topology.lldp.utils.LLDPDiscoveryUtils;
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 {
23     private static final LLDPLinkAger instance = new LLDPLinkAger();
24     private Map<LinkDiscovered,Date> linkToDate = new ConcurrentHashMap<LinkDiscovered,Date>();
25     private LLDPDiscoveryProvider manager;
26     private Timer timer = new Timer();
27
28     public LLDPDiscoveryProvider getManager() {
29         return manager;
30     }
31     public void setManager(LLDPDiscoveryProvider manager) {
32         this.manager = manager;
33     }
34     private LLDPLinkAger() {
35         timer.schedule(new LLDPAgingTask(), 0,LLDPDiscoveryUtils.LLDP_INTERVAL);
36     }
37     public static LLDPLinkAger getInstance() {
38         return instance;
39     }
40     
41     public void put(LinkDiscovered link) {
42         Date expires = new Date();
43         expires.setTime(expires.getTime() + LLDPDiscoveryUtils.LLDP_EXPIRATION_TIME);
44         linkToDate.put(link, expires);
45     }
46     
47     public void close() {
48         timer.cancel();
49     }
50     
51     private class LLDPAgingTask extends TimerTask {
52
53         @Override
54         public void run() {
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(getInstance().getManager() != null) {
61                         LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
62                         getInstance().getManager().getNotificationService().publish(lrb.build());
63                         linkToDate.remove(link);
64                     }
65                 }
66             }
67             
68         }
69         
70     }
71 }
72