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.PreDestroy;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.DataObjectModification;
25 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
26 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
31 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
32 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
33 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 public class LLDPLinkAger implements ConfigurationListener, ClusteredDataTreeChangeListener<Link>, AutoCloseable {
52 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
53 static final String TOPOLOGY_ID = "flow:1";
54 static final InstanceIdentifier<Link> II_TO_LINK = InstanceIdentifier.create(NetworkTopology.class)
55 .child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))).child(Link.class);
57 private final long linkExpirationTime;
58 private final Map<LinkDiscovered, Date> linkToDate;
59 private final Timer timer;
60 private final NotificationPublishService notificationService;
61 private final AutoCloseable configurationServiceRegistration;
62 private final EntityOwnershipService eos;
63 private ListenerRegistration<DataTreeChangeListener> listenerRegistration;
66 * default ctor - start timer.
69 @SuppressWarnings("checkstyle:IllegalCatch")
70 public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
71 final NotificationPublishService notificationService,
72 final ConfigurationService configurationService,
73 final EntityOwnershipService entityOwnershipService,
74 final DataBroker dataBroker) {
75 this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue().toJava();
76 this.notificationService = notificationService;
77 this.configurationServiceRegistration = configurationService.registerListener(this);
78 this.eos = entityOwnershipService;
79 linkToDate = new ConcurrentHashMap<>();
81 final DataTreeIdentifier dtiToNodeConnector = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
84 listenerRegistration = dataBroker.registerDataTreeChangeListener(dtiToNodeConnector, LLDPLinkAger.this);
85 } catch (Exception e) {
86 LOG.error("DataTreeChangeListeners registration failed:", e);
87 throw new IllegalStateException("LLDPLinkAger startup failed!", e);
89 timer.schedule(new LLDPAgingTask(), 0,
90 topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue().toJava());
93 public void put(LinkDiscovered link) {
94 Date expires = new Date();
95 expires.setTime(expires.getTime() + linkExpirationTime);
96 linkToDate.put(link, expires);
101 public void close() throws Exception {
102 if (listenerRegistration != null) {
103 listenerRegistration.close();
104 listenerRegistration = null;
108 configurationServiceRegistration.close();
112 public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Link>> changes) {
113 for (DataTreeModification modification : changes) {
114 switch (modification.getRootNode().getModificationType()) {
117 case SUBTREE_MODIFIED:
121 processLinkDeleted(modification.getRootNode());
124 LOG.error("Unhandled modification type: {}", modification.getRootNode().getModificationType());
130 public boolean isLinkToDateEmpty() {
131 return linkToDate.isEmpty();
135 public void onPropertyChanged(@NonNull final String propertyName, @NonNull final String propertyValue) {
136 final TopologyLLDPDiscoveryProperty lldpDiscoveryProperty = TopologyLLDPDiscoveryProperty.forValue(
138 if (lldpDiscoveryProperty != null) {
139 switch (lldpDiscoveryProperty) {
140 case LLDP_SECURE_KEY:
141 case TOPOLOGY_LLDP_INTERVAL:
142 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
143 LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
146 LOG.warn("No topology lldp discovery property found.");
152 protected boolean isLinkPresent(final LinkDiscovered linkDiscovered) {
153 return linkToDate.containsKey(linkDiscovered);
156 private void processLinkDeleted(DataObjectModification rootNode) {
157 Link link = (Link) rootNode.getDataBefore();
158 LOG.trace("Removing link {} from linkToDate cache", link);
159 LinkDiscovered linkDiscovered = LLDPDiscoveryUtils.toLLDPLinkDiscovered(link);
160 linkToDate.remove(linkDiscovered);
163 private class LLDPAgingTask extends TimerTask {
166 for (Entry<LinkDiscovered, Date> entry : linkToDate.entrySet()) {
167 LinkDiscovered link = entry.getKey();
168 Date expires = entry.getValue();
169 Date now = new Date();
170 if (now.after(expires)) {
171 if (notificationService != null) {
172 LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
173 NodeKey nodeKey = link.getDestination().getValue().firstKeyOf(Node.class);
174 LOG.info("No update received for link {} from last {} milliseconds. Removing link from cache.",
175 link, linkExpirationTime);
176 linkToDate.remove(link);
177 if (nodeKey != null && LLDPDiscoveryUtils.isEntityOwned(eos, nodeKey.getId().getValue())) {
178 LOG.info("Publish Link Remove event for the link {}", link);
179 final LinkRemoved lr = lrb.build();
181 notificationService.putNotification(lr);
182 } catch (InterruptedException e) {
183 LOG.warn("Interrupted while publishing notification {}", lr, e);
186 LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
187 + "node is not owned by the controller", link);