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.Collection;
12 import java.util.Date;
14 import java.util.Map.Entry;
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.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.DataObjectModification;
27 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
28 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.mdsal.binding.api.DataTreeModification;
30 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
33 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
34 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
35 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 public class LLDPLinkAger implements ConfigurationListener, ClusteredDataTreeChangeListener<Link>, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
55 static final String TOPOLOGY_ID = "flow:1";
56 static final InstanceIdentifier<Link> II_TO_LINK = InstanceIdentifier.create(NetworkTopology.class)
57 .child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))).child(Link.class);
59 private final long linkExpirationTime;
60 private final Map<LinkDiscovered, Date> linkToDate;
61 private final Timer timer;
62 private final NotificationPublishService notificationService;
63 private final AutoCloseable configurationServiceRegistration;
64 private final EntityOwnershipService eos;
65 private ListenerRegistration<DataTreeChangeListener> listenerRegistration;
68 * default ctor - start timer.
71 @SuppressWarnings("checkstyle:IllegalCatch")
72 public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
73 @Reference final NotificationPublishService notificationService,
74 @Reference final ConfigurationService configurationService,
75 @Reference final EntityOwnershipService entityOwnershipService,
76 @Reference final DataBroker dataBroker) {
77 this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue().toJava();
78 this.notificationService = notificationService;
79 this.configurationServiceRegistration = configurationService.registerListener(this);
80 this.eos = entityOwnershipService;
81 linkToDate = new ConcurrentHashMap<>();
83 final DataTreeIdentifier dtiToNodeConnector = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
86 listenerRegistration = dataBroker.registerDataTreeChangeListener(dtiToNodeConnector, LLDPLinkAger.this);
87 } catch (Exception e) {
88 LOG.error("DataTreeChangeListeners registration failed:", e);
89 throw new IllegalStateException("LLDPLinkAger startup failed!", e);
91 timer.schedule(new LLDPAgingTask(), 0,
92 topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue().toJava());
95 public void put(LinkDiscovered link) {
96 Date expires = new Date();
97 expires.setTime(expires.getTime() + linkExpirationTime);
98 linkToDate.put(link, expires);
103 public void close() throws Exception {
104 if (listenerRegistration != null) {
105 listenerRegistration.close();
106 listenerRegistration = null;
110 configurationServiceRegistration.close();
114 public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Link>> changes) {
115 for (DataTreeModification modification : changes) {
116 switch (modification.getRootNode().getModificationType()) {
119 case SUBTREE_MODIFIED:
123 processLinkDeleted(modification.getRootNode());
126 LOG.error("Unhandled modification type: {}", modification.getRootNode().getModificationType());
132 public boolean isLinkToDateEmpty() {
133 return linkToDate.isEmpty();
137 public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
138 final TopologyLLDPDiscoveryProperty lldpDiscoveryProperty = TopologyLLDPDiscoveryProperty.forValue(
140 if (lldpDiscoveryProperty != null) {
141 switch (lldpDiscoveryProperty) {
142 case LLDP_SECURE_KEY:
143 case TOPOLOGY_LLDP_INTERVAL:
144 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
145 LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
148 LOG.warn("No topology lldp discovery property found.");
154 protected boolean isLinkPresent(final LinkDiscovered linkDiscovered) {
155 return linkToDate.containsKey(linkDiscovered);
158 private void processLinkDeleted(DataObjectModification rootNode) {
159 Link link = (Link) rootNode.getDataBefore();
160 LOG.trace("Removing link {} from linkToDate cache", link);
161 LinkDiscovered linkDiscovered = LLDPDiscoveryUtils.toLLDPLinkDiscovered(link);
162 linkToDate.remove(linkDiscovered);
165 private class LLDPAgingTask extends TimerTask {
168 for (Entry<LinkDiscovered, Date> entry : linkToDate.entrySet()) {
169 LinkDiscovered link = entry.getKey();
170 Date expires = entry.getValue();
171 Date now = new Date();
172 if (now.after(expires)) {
173 if (notificationService != null) {
174 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
175 NodeKey nodeKey = link.getDestination().getValue().firstKeyOf(Node.class);
176 LOG.info("No update received for link {} from last {} milliseconds. Removing link from cache.",
177 link, linkExpirationTime);
178 linkToDate.remove(link);
179 if (nodeKey != null && LLDPDiscoveryUtils.isEntityOwned(eos, nodeKey.getId().getValue())) {
180 LOG.info("Publish Link Remove event for the link {}", link);
181 final LinkRemoved lr = lrb.build();
183 notificationService.putNotification(lr);
184 } catch (InterruptedException e) {
185 LOG.warn("Interrupted while publishing notification {}", lr, e);
188 LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
189 + "node is not owned by the controller", link);