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.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.controller.sal.binding.api.NotificationProviderService;
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.LinkRemovedBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class LLDPLinkAger implements ConfigurationListener, AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
39 private final long linkExpirationTime;
40 private final Map<LinkDiscovered, Date> linkToDate;
41 private final Timer timer;
42 private final NotificationProviderService notificationService;
43 private final AutoCloseable configurationServiceRegistration;
44 private final EntityOwnershipService eos;
47 * default ctor - start timer.
50 public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
51 @Reference final NotificationProviderService notificationService,
52 @Reference final ConfigurationService configurationService,
53 @Reference final EntityOwnershipService entityOwnershipService) {
54 this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue();
55 this.notificationService = notificationService;
56 this.configurationServiceRegistration = configurationService.registerListener(this);
57 this.eos = entityOwnershipService;
58 linkToDate = new ConcurrentHashMap<>();
60 timer.schedule(new LLDPAgingTask(), 0, topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue());
63 public void put(LinkDiscovered link) {
64 Date expires = new Date();
65 expires.setTime(expires.getTime() + linkExpirationTime);
66 linkToDate.put(link, expires);
71 public void close() throws Exception {
74 configurationServiceRegistration.close();
77 private class LLDPAgingTask extends TimerTask {
81 for (Entry<LinkDiscovered, Date> entry : linkToDate.entrySet()) {
82 LinkDiscovered link = entry.getKey();
83 Date expires = entry.getValue();
84 Date now = new Date();
85 if (now.after(expires)) {
86 if (notificationService != null) {
87 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
89 NodeKey nodeKey = link.getDestination().getValue().firstKeyOf(Node.class);
90 LOG.info("No update received for link {} from last {} milliseconds. Removing link from cache.",
91 link, linkExpirationTime);
92 linkToDate.remove(link);
93 if (nodeKey != null && LLDPDiscoveryUtils.isEntityOwned(eos, nodeKey.getId().getValue())) {
94 LOG.info("Publish Link Remove event for the link {}", link);
95 notificationService.publish(lrb.build());
97 LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
98 + "node is not owned by the controller", link);
107 public boolean isLinkToDateEmpty() {
108 return linkToDate.isEmpty();
112 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
113 Optional.ofNullable(TopologyLLDPDiscoveryProperty.forValue(propertyName)).ifPresent(lldpDiscoveryProperty -> {
114 switch (lldpDiscoveryProperty) {
115 case LLDP_SECURE_KEY:
116 case TOPOLOGY_LLDP_INTERVAL:
117 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
118 LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
121 LOG.warn("No topology lldp discovery property found.");