2 * Copyright (c) 2016 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 static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.verify;
15 import java.util.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.mdsal.eos.binding.api.Entity;
25 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
26 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
27 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.NonZeroUint32Type;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfigBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Test for {@link LLDPLinkAger}.
45 @RunWith(MockitoJUnitRunner.class)
46 public class LLDPLinkAgerTest {
48 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAgerTest.class);
50 private static final long LLDP_INTERVAL = 5L;
51 private static final long LINK_EXPIRATION_TIME = 10L;
54 * We need to wait while other tasks are finished before we can check anything in LLDPAgingTask.
56 private static final int SLEEP = 100;
58 private LLDPLinkAger lldpLinkAger;
61 private LinkDiscovered link;
63 private NotificationPublishService notificationService;
65 private EntityOwnershipService eos;
67 private DataBroker dataBroker;
69 private LinkRemoved linkRemoved;
74 lldpLinkAger = new LLDPLinkAger(getConfig(), notificationService, getConfigurationService(), eos, dataBroker);
75 Mockito.when(link.getDestination()).thenReturn(new NodeConnectorRef(
76 InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")))));
77 Mockito.when(eos.getOwnershipState(any(Entity.class))).thenReturn(
78 Optional.of(EntityOwnershipState.IS_OWNER));
82 public void testPut() {
83 assertTrue(lldpLinkAger.isLinkToDateEmpty());
84 lldpLinkAger.put(link);
85 assertFalse(lldpLinkAger.isLinkToDateEmpty());
89 public void testClose() throws Exception {
91 assertTrue(lldpLinkAger.isLinkToDateEmpty());
95 * Inner class LLDPAgingTask removes all expired records from linkToDate if any (in constructor of LLDPLinkAger).
98 public void testLLDPAgingTask() throws InterruptedException {
99 lldpLinkAger.put(link);
101 verify(notificationService).putNotification(any(LinkRemoved.class));
104 private TopologyLldpDiscoveryConfig getConfig() {
105 TopologyLldpDiscoveryConfigBuilder cfgBuilder = new TopologyLldpDiscoveryConfigBuilder();
106 cfgBuilder.setTopologyLldpInterval(new NonZeroUint32Type(LLDP_INTERVAL));
107 cfgBuilder.setTopologyLldpExpirationInterval(new NonZeroUint32Type(LINK_EXPIRATION_TIME));
108 return cfgBuilder.build();
111 private ConfigurationService getConfigurationService() {
112 final ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
113 final TopologyLldpDiscoveryConfig config = getConfig();
115 Mockito.when(configurationService.registerListener(any())).thenReturn(() -> {
118 Mockito.lenient().when(configurationService.getProperty(Mockito.eq("topology-lldp-interval"), any()))
119 .thenReturn(config.getTopologyLldpInterval());
121 Mockito.lenient().when(configurationService.getProperty(Mockito.eq("topology-lldp-expiration-interval"), any()))
122 .thenReturn(config.getTopologyLldpExpirationInterval());
124 return configurationService;