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 org.opendaylight.controller.sal.binding.api.NotificationProviderService;
20 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
21 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
22 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
23 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class LLDPLinkAger implements ConfigurationListener, AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
34 private final long linkExpirationTime;
35 private final Map<LinkDiscovered, Date> linkToDate;
36 private final Timer timer;
37 private final NotificationProviderService notificationService;
38 private final AutoCloseable configurationServiceRegistration;
39 private final EntityOwnershipService eos;
42 * default ctor - start timer.
44 public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
45 final NotificationProviderService notificationService,
46 final ConfigurationService configurationService, final EntityOwnershipService entityOwnershipService) {
47 this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue();
48 this.notificationService = notificationService;
49 this.configurationServiceRegistration = configurationService.registerListener(this);
50 this.eos = entityOwnershipService;
51 linkToDate = new ConcurrentHashMap<>();
53 timer.schedule(new LLDPAgingTask(), 0, topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue());
56 public void put(LinkDiscovered link) {
57 Date expires = new Date();
58 expires.setTime(expires.getTime() + linkExpirationTime);
59 linkToDate.put(link, expires);
63 public void close() throws Exception {
66 configurationServiceRegistration.close();
69 private class LLDPAgingTask extends TimerTask {
73 for (Entry<LinkDiscovered, Date> entry : linkToDate.entrySet()) {
74 LinkDiscovered link = entry.getKey();
75 Date expires = entry.getValue();
76 Date now = new Date();
77 if (now.after(expires)) {
78 if (notificationService != null) {
79 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
81 NodeKey nodeKey = link.getDestination().getValue().firstKeyOf(Node.class);
82 LOG.info("No update received for link {} from last {} milliseconds. Removing link from cache.",
83 link, linkExpirationTime);
84 linkToDate.remove(link);
85 if (nodeKey != null && LLDPDiscoveryUtils.isEntityOwned(eos, nodeKey.getId().getValue())) {
86 LOG.info("Publish Link Remove event for the link {}", link);
87 notificationService.publish(lrb.build());
89 LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
90 + "node is not owned by the controller", link);
99 public boolean isLinkToDateEmpty() {
100 return linkToDate.isEmpty();
104 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
105 Optional.ofNullable(TopologyLLDPDiscoveryProperty.forValue(propertyName)).ifPresent(lldpDiscoveryProperty -> {
106 switch (lldpDiscoveryProperty) {
107 case LLDP_SECURE_KEY:
108 LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
110 case TOPOLOGY_LLDP_INTERVAL:
111 LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
113 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
114 LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
117 LOG.warn("No topology lldp discovery property found.");