Merge "Drop the odlparent.netty property"
[openflowplugin.git] / applications / lldp-speaker / src / test / java / org / opendaylight / openflowplugin / applications / lldpspeaker / LLDPSpeakerTest.java
1 /*
2  * Copyright (c) 2014 Pacnet 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
9 package org.opendaylight.openflowplugin.applications.lldpspeaker;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyLong;
13 import static org.mockito.Mockito.never;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
17 import static org.mockito.Mockito.when;
18
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.applications.lldp.speaker.rev141023.OperStatus;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 /**
43  * Tests for {@link LLDPSpeaker}.
44  */
45 @RunWith(MockitoJUnitRunner.class)
46 public class LLDPSpeakerTest {
47     private static final InstanceIdentifier<NodeConnector> ID = TestUtils.createNodeConnectorId("openflow:1",
48             "openflow:1:1");
49     private static final MacAddress MAC_ADDRESS = new MacAddress("01:23:45:67:89:AB");
50     private static final FlowCapableNodeConnector FLOW_CAPABLE_NODE_CONNECTOR =
51             TestUtils.createFlowCapableNodeConnector(MAC_ADDRESS, 1L).build();
52     private static final byte[] LLDP_FRAME = LLDPUtil.buildLldpFrame(new NodeId("openflow:1"),
53             new NodeConnectorId("openflow:1:1"), MAC_ADDRESS, 1L);
54     private static final TransmitPacketInput PACKET_INPUT = new TransmitPacketInputBuilder()
55             .setEgress(new NodeConnectorRef(ID))
56             .setNode(new NodeRef(ID.firstIdentifierOf(Node.class)))
57             .setPayload(LLDP_FRAME).build();
58
59     @Mock
60     private PacketProcessingService packetProcessingService;
61     @Mock
62     private ScheduledExecutorService scheduledExecutorService;
63     @Mock
64     private ScheduledFuture scheduledSpeakerTask;
65
66     private final MacAddress destinationMACAddress = null;
67     private LLDPSpeaker lldpSpeaker;
68
69     @Before
70     public void setUp() {
71         when(
72                 scheduledExecutorService.scheduleAtFixedRate(
73                         any(Runnable.class), anyLong(), anyLong(),
74                         any(TimeUnit.class))).thenReturn(scheduledSpeakerTask);
75         lldpSpeaker = new LLDPSpeaker(packetProcessingService,
76                 scheduledExecutorService, destinationMACAddress);
77         lldpSpeaker.setOperationalStatus(OperStatus.RUN);
78     }
79
80     /**
81      * Test that speaker does nothing when in standby mode.
82      */
83     @Test
84     public void testStandBy() {
85         lldpSpeaker.setOperationalStatus(OperStatus.STANDBY);
86         // Add node connector - LLDP packet should be transmitted through
87         // packetProcessingService
88         lldpSpeaker.nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
89
90         // Execute one iteration of periodic task - LLDP packet should be
91         // transmitted second time
92         lldpSpeaker.run();
93
94         // Check packet transmission
95         verify(packetProcessingService, times(1)).transmitPacket(PACKET_INPUT);
96         verifyNoMoreInteractions(packetProcessingService);
97     }
98
99     /**
100      * Test that LLDP frame is transmitted after port appeared in inventory and
101      * periodically after that.
102      */
103     @Test
104     public void testNodeConnectorAdd() {
105         // Add node connector - LLDP packet should be transmitted through
106         // packetProcessingService
107         lldpSpeaker.nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
108
109         // Execute one iteration of periodic task - LLDP packet should be
110         // transmitted second time
111         lldpSpeaker.run();
112
113         // Check packet transmission
114         verify(packetProcessingService, times(2)).transmitPacket(PACKET_INPUT);
115         verifyNoMoreInteractions(packetProcessingService);
116     }
117
118     /**
119      * Test that LLDP frame stop to periodically transmit after port disappeared
120      * from inventory.
121      */
122     @Test
123     public void testNodeConnectorRemoval() {
124         // Prepare for test - add node connector first
125         lldpSpeaker.nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
126
127         // Trigger removal of packet
128         lldpSpeaker.nodeConnectorRemoved(ID);
129
130         // Run one iteration of LLDP flood
131         lldpSpeaker.run();
132
133         // Verify that LLDP frame sent only once (by nodeConnectorAdded),
134         // e.g. no flood after removal
135         verify(packetProcessingService, times(1)).transmitPacket(PACKET_INPUT);
136         verifyNoMoreInteractions(packetProcessingService);
137     }
138
139     /**
140      * Test that when {@link LLDPSpeaker#nodeConnectorAdded} is called multiple times
141      * with same arguments, only the first one have effect.
142      */
143     @Test
144     public void testMultipleSameNodeConnectorAddEvents() {
145         // Add node connector - LLDP packet should be transmitted through
146         // packetProcessingService
147         for (int i = 0; i < 10; i++) {
148             lldpSpeaker.nodeConnectorAdded(ID, FLOW_CAPABLE_NODE_CONNECTOR);
149         }
150
151         // Check packet transmission
152         verify(packetProcessingService, times(1)).transmitPacket(PACKET_INPUT);
153         verifyNoMoreInteractions(packetProcessingService);
154     }
155
156     /**
157      * Test that lldpSpeaker cancels periodic LLDP flood task and stops.
158      */
159     @Test
160     public void testCleanup() {
161         lldpSpeaker.close();
162         verify(scheduledSpeakerTask, times(1)).cancel(true);
163         verify(scheduledExecutorService, times(1)).shutdown();
164     }
165
166     /**
167      * Test that checks if LLDPSpeaker working fine with local ports.
168      */
169     @Test
170     public void testLocalNodeConnectorCreation() {
171         // Call nodeConnectorAdded with local port
172         FlowCapableNodeConnector fcnc = TestUtils
173                 .createFlowCapableNodeConnector()
174                 .setPortNumber(new PortNumberUni("LOCAL")).build();
175         lldpSpeaker.nodeConnectorAdded(ID, fcnc);
176
177         // Verify that nothing happened for local port
178         verify(packetProcessingService, never()).transmitPacket(
179                 any(TransmitPacketInput.class));
180     }
181 }