Merge "Delete ScenarioService"
[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.Timer;
15 import java.util.TimerTask;
16 import java.util.concurrent.ConcurrentHashMap;
17 import javax.annotation.Nonnull;
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.opendaylight.mdsal.binding.api.NotificationPublishService;
23 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
24 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
25 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
26 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Singleton
37 public class LLDPLinkAger implements ConfigurationListener, AutoCloseable {
38     private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAger.class);
39     private final long linkExpirationTime;
40     private final Map<LinkDiscovered, Date> linkToDate;
41     private final Timer timer;
42     private final NotificationPublishService notificationService;
43     private final AutoCloseable configurationServiceRegistration;
44     private final EntityOwnershipService eos;
45
46     /**
47      * default ctor - start timer.
48      */
49     @Inject
50     public LLDPLinkAger(final TopologyLldpDiscoveryConfig topologyLldpDiscoveryConfig,
51             @Reference final NotificationPublishService notificationService,
52             @Reference final ConfigurationService configurationService,
53             @Reference final EntityOwnershipService entityOwnershipService) {
54         this.linkExpirationTime = topologyLldpDiscoveryConfig.getTopologyLldpExpirationInterval().getValue();
55         this.notificationService = notificationService;
56         this.configurationServiceRegistration = configurationService.registerListener(this);
57         this.eos = entityOwnershipService;
58         linkToDate = new ConcurrentHashMap<>();
59         timer = new Timer();
60         timer.schedule(new LLDPAgingTask(), 0, topologyLldpDiscoveryConfig.getTopologyLldpInterval().getValue());
61     }
62
63     public void put(LinkDiscovered link) {
64         Date expires = new Date();
65         expires.setTime(expires.getTime() + linkExpirationTime);
66         linkToDate.put(link, expires);
67     }
68
69     @Override
70     @PreDestroy
71     public void close() throws Exception {
72         timer.cancel();
73         linkToDate.clear();
74         configurationServiceRegistration.close();
75     }
76
77     private class LLDPAgingTask extends TimerTask {
78
79         @Override
80         public void run() {
81             for (Entry<LinkDiscovered, Date> entry : linkToDate.entrySet()) {
82                 LinkDiscovered link = entry.getKey();
83                 Date expires = entry.getValue();
84                 Date now = new Date();
85                 if (now.after(expires)) {
86                     if (notificationService != null) {
87                         LinkRemovedBuilder lrb = new LinkRemovedBuilder(link);
88
89                         NodeKey nodeKey = link.getDestination().getValue().firstKeyOf(Node.class);
90                         LOG.info("No update received for link {} from last {} milliseconds. Removing link from cache.",
91                                 link, linkExpirationTime);
92                         linkToDate.remove(link);
93                         if (nodeKey != null && LLDPDiscoveryUtils.isEntityOwned(eos, nodeKey.getId().getValue())) {
94                             LOG.info("Publish Link Remove event for the link {}", link);
95                             final LinkRemoved lr = lrb.build();
96                             try {
97                                 notificationService.putNotification(lr);
98                             } catch (InterruptedException e) {
99                                 LOG.warn("Interrupted while publishing notification {}", lr, e);
100                             }
101                         } else {
102                             LOG.trace("Skip publishing Link Remove event for the link {} because link destination "
103                                     + "node is not owned by the controller", link);
104                         }
105                     }
106                 }
107             }
108         }
109     }
110
111     @VisibleForTesting
112     public boolean isLinkToDateEmpty() {
113         return linkToDate.isEmpty();
114     }
115
116     @Override
117     public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
118         final TopologyLLDPDiscoveryProperty lldpDiscoveryProperty = TopologyLLDPDiscoveryProperty.forValue(
119             propertyName);
120         if (lldpDiscoveryProperty != null) {
121             switch (lldpDiscoveryProperty) {
122                 case LLDP_SECURE_KEY:
123                 case TOPOLOGY_LLDP_INTERVAL:
124                 case TOPOLOGY_LLDP_EXPIRATION_INTERVAL:
125                     LOG.warn("Runtime update not supported for property {}", lldpDiscoveryProperty);
126                     break;
127                 default:
128                     LOG.warn("No topology lldp discovery property found.");
129                     break;
130             }
131         }
132     }
133 }