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;
12 import java.util.Collection;
13 import java.util.Date;
15 import java.util.Map.Entry;
16 import java.util.Timer;
17 import java.util.TimerTask;
18 import java.util.concurrent.ConcurrentHashMap;
19 import javax.annotation.Nonnull;
20 import javax.annotation.PreDestroy;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.apache.aries.blueprint.annotation.service.Reference;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
26 import org.opendaylight.mdsal.binding.api.DataBroker;
27 import org.opendaylight.mdsal.binding.api.DataObjectModification;
28 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.api.DataTreeModification;
31 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
34 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
35 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
36 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 public class LLDPLinkAger implements ConfigurationListener, ClusteredDataTreeChangeListener<Link>, AutoCloseable {
55 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
56 static final String TOPOLOGY_ID = "flow:1";
57 static final InstanceIdentifier<Link> II_TO_LINK = InstanceIdentifier.create(NetworkTopology.class)
58 .child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))).child(Link.class);
60 private final long linkExpirationTime;
61 private final Map<LinkDiscovered, Date> linkToDate;
62 private final Timer timer;
63 private final NotificationPublishService notificationService;
64 private final AutoCloseable configurationServiceRegistration;
65 private final EntityOwnershipService eos;
66 private ListenerRegistration<DataTreeChangeListener> listenerRegistration;
69 * default ctor - start timer.
72 @SuppressWarnings("checkstyle:IllegalCatch")
73 public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
74 @Reference final NotificationPublishService notificationService,
75 @Reference final ConfigurationService configurationService,
76 @Reference final EntityOwnershipService entityOwnershipService,
77 @Reference final DataBroker dataBroker) {
78 this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue();
79 this.notificationService = notificationService;
80 this.configurationServiceRegistration = configurationService.registerListener(this);
81 this.eos = entityOwnershipService;
82 linkToDate = new ConcurrentHashMap<>();
84 final DataTreeIdentifier dtiToNodeConnector = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
87 listenerRegistration = dataBroker.registerDataTreeChangeListener(dtiToNodeConnector, LLDPLinkAger.this);
88 } catch (Exception e) {
89 LOG.error("DataTreeChangeListeners registration failed:", e);
90 throw new IllegalStateException("LLDPLinkAger startup failed!", e);
92 timer.schedule(new LLDPAgingTask(), 0, topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue());
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);