Switch to MD-SAL APIs
[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.Optional;
15 import java.util.Timer;
16 import java.util.TimerTask;
17 import java.util.concurrent.ConcurrentHashMap;
18 import javax.annotation.Nonnull;
19 import javax.annotation.PreDestroy;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.apache.aries.blueprint.annotation.service.Reference;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
25 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
26 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
27 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Singleton
38 public class LLDPLinkAger implements ConfigurationListener, AutoCloseable {
39     private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
40     private final long linkExpirationTime;
41     private final Map<LinkDiscovered, Date> linkToDate;
42     private final Timer timer;
43     private final NotificationPublishService notificationService;
44     private final AutoCloseable configurationServiceRegistration;
45     private final EntityOwnershipService eos;
46
47     /**
48      * default ctor - start timer.
49      */
50     @Inject
51     public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
52             @Reference final NotificationPublishService notificationService,
53             @Reference final ConfigurationService configurationService,
54             @Reference final EntityOwnershipService entityOwnershipService) {
55         this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue();
56         this.notificationService = notificationService;
57         this.configurationServiceRegistration = configurationService.registerListener(this);
58         this.eos = entityOwnershipService;
59         linkToDate = new ConcurrentHashMap<>();
60         timer = new Timer();
61         timer.schedule(new LLDPAgingTask(), 0, topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue());
62     }
63
64     public void put(LinkDiscovered link) {
65         Date expires = new Date();
66         expires.setTime(expires.getTime() + linkExpirationTime);
67         linkToDate.put(link, expires);
68     }
69
70     @Override
71     @PreDestroy
72     public void close() throws Exception {
73         timer.cancel();
74         linkToDate.clear();
75         configurationServiceRegistration.close();
76     }
77
78     private class LLDPAgingTask extends TimerTask {
79
80         @Override
81         public void run() {
82             for (Entry<LinkDiscovered, Date> entry : linkToDate.entrySet()) {
83                 LinkDiscovered link = entry.getKey();
84                 Date expires = entry.getValue();
85                 Date now = new Date();
86                 if (now.after(expires)) {
87                     if (notificationService != null) {
88                         LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
89
90                         NodeKey nodeKey = link.getDestination().getValue().firstKeyOf(Node.class);
91                         LOG.info("No update received for link {} from last {} milliseconds. Removing link from cache.",
92                                 link, linkExpirationTime);
93                         linkToDate.remove(link);
94                         if (nodeKey != null && LLDPDiscoveryUtils.isEntityOwned(eos, nodeKey.getId().getValue())) {
95                             LOG.info("Publish Link Remove event for the link {}", link);
96                             final LinkRemoved lr = lrb.build();
97                             try {
98                                 notificationService.putNotification(lr);
99                             } catch (InterruptedException e) {
100                                 LOG.warn("Interrupted while publishing notification {}", lr, e);
101                             }
102                         } else {
103                             LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
104                                     + "node is not owned by the controller", link);
105                         }
106                     }
107                 }
108             }
109         }
110     }
111
112     @VisibleForTesting
113     public boolean isLinkToDateEmpty() {
114         return linkToDate.isEmpty();
115     }
116
117     @Override
118     public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
119         Optional.ofNullable(TopologyLLDPDiscoveryProperty.forValue(propertyName)).ifPresent(lldpDiscoveryProperty -> {
120             switch (lldpDiscoveryProperty) {
121                 case LLDP_SECURE_KEY:
122                 case TOPOLOGY_LLDP_INTERVAL:
123                 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
124                     LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
125                     break;
126                 default:
127                     LOG.warn("No topology lldp discovery property found.");
128                     break;
129             }
130         });
131     }
132 }