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
9 package org.opendaylight.openflowplugin.applications.topology.lldp;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.verify;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
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.LinkRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.NonZeroUint32Type;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfigBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Test for {@link LLDPLinkAger}.
35 @RunWith(MockitoJUnitRunner.class)
36 public class LLDPLinkAgerTest {
38 private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAgerTest.class);
40 private static final long LLDP_INTERVAL = 5L;
41 private static final long LINK_EXPIRATION_TIME = 10L;
44 * We need to wait while other tasks are finished before we can check anything in LLDPAgingTask.
46 private static final int SLEEP = 100;
48 private LLDPLinkAger lldpLinkAger;
51 private LinkDiscovered link;
53 private NotificationProviderService notificationService;
56 public void setUp() throws Exception {
57 lldpLinkAger = new LLDPLinkAger(getConfig(), notificationService, getConfigurationService());
61 public void testPut() {
62 assertTrue(lldpLinkAger.isLinkToDateEmpty());
63 lldpLinkAger.put(link);
64 assertFalse(lldpLinkAger.isLinkToDateEmpty());
68 public void testClose() throws Exception {
70 assertTrue(lldpLinkAger.isLinkToDateEmpty());
74 * Inner class LLDPAgingTask removes all expired records from linkToDate if any (in constructor of LLDPLinkAger).
77 public void testLLDPAgingTask() throws InterruptedException {
78 lldpLinkAger.put(link);
80 verify(notificationService).publish(Matchers.any(LinkRemoved.class));
83 private TopologyLldpDiscoveryConfig getConfig() {
84 TopologyLldpDiscoveryConfigBuilder cfgBuilder = new TopologyLldpDiscoveryConfigBuilder();
85 cfgBuilder.setTopologyLldpInterval(new NonZeroUint32Type(LLDP_INTERVAL));
86 cfgBuilder.setTopologyLldpExpirationInterval(new NonZeroUint32Type(LINK_EXPIRATION_TIME));
87 return cfgBuilder.build();
90 private ConfigurationService getConfigurationService() {
91 final ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
92 final TopologyLldpDiscoveryConfig config = getConfig();
94 Mockito.when(configurationService.registerListener(Mockito.any())).thenReturn(() -> {
97 Mockito.when(configurationService.getProperty(Mockito.eq("topology-lldp-interval"), Mockito.any()))
98 .thenReturn(config.getTopologyLldpInterval());
100 Mockito.when(configurationService.getProperty(Mockito.eq("topology-lldp-expiration-interval"), Mockito.any()))
101 .thenReturn(config.getTopologyLldpExpirationInterval());
103 return configurationService;