Merge "BUG-86: Fixed echo response processing"
[openflowplugin.git] / applications / topology-lldp-discovery / src / main / java / org / opendaylight / openflowplugin / applications / topology / lldp / LLDPLinkAger.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.applications.topology.lldp;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import java.util.Date;
12 import java.util.Map;
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.applications.topology.lldp.utils.LLDPDiscoveryUtils;
22 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
23 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
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.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31
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;
40
41     /**
42      * default ctor - start timer.
43      */
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<>();
52         timer = new Timer();
53         timer.schedule(new LLDPAgingTask(), 0, topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue());
54     }
55
56     public void put(LinkDiscovered link) {
57         Date expires = new Date();
58         expires.setTime(expires.getTime() + linkExpirationTime);
59         linkToDate.put(link, expires);
60     }
61
62     @Override
63     public void close() throws Exception {
64         timer.cancel();
65         linkToDate.clear();
66         configurationServiceRegistration.close();
67     }
68
69     private class LLDPAgingTask extends TimerTask {
70
71         @Override
72         public void run() {
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);
80
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());
88                         } else {
89                             LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
90                                     + "node is not owned by the controller", link);
91                         }
92                     }
93                 }
94             }
95         }
96     }
97
98     @VisibleForTesting
99     public boolean isLinkToDateEmpty() {
100         return linkToDate.isEmpty();
101     }
102
103     @Override
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);
109                     break;
110                 case TOPOLOGY_LLDP_INTERVAL:
111                     LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
112                     break;
113                 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
114                     LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
115                     break;
116                 default:
117                     LOG.warn("No topology lldp discovery property found.");
118                     break;
119             }
120         });
121     }
122 }