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