2 * Copyright (c) 2015 Inocybe 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.ovsdb.openstack.netvirt.providers.openflow13.services;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyBoolean;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
30 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import com.google.common.base.Optional;
36 import com.google.common.util.concurrent.CheckedFuture;
39 * Unit test fort {@link L2ForwardingService}
41 @RunWith(MockitoJUnitRunner.class)
42 @SuppressWarnings("unchecked")
43 public class L2FowardingServiceTest {
45 @InjectMocks private L2ForwardingService l2ForwardingService = new L2ForwardingService(Service.ARP_RESPONDER);
47 @Mock private DataBroker dataBroker;
48 @Mock private PipelineOrchestrator orchestrator;
50 @Mock private WriteTransaction writeTransaction;
51 @Mock private ReadOnlyTransaction readOnlyTransaction;
52 @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
54 private static final String SEGMENTATION_ID = "2";
55 private static final String MAC_ADDRESS = "87:1D:5E:02:40:B8";
56 private static final Long DPID = 122L;
57 private static final Long LOCAL_PORT = 451L;
58 private static final Long ETH_PORT = 564L;
59 private static final Long OF_PORT_OUT = 5698L;
62 public void setUp() throws Exception {
63 when(writeTransaction.submit()).thenReturn(commitFuture);
65 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
66 when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
68 CheckedFuture future = mock(CheckedFuture.class);
69 when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future);
71 Optional<Flow> data = mock(Optional.class);
72 when(future.get()).thenReturn(data);
74 //when(mdsalConsumer.getDataBroker()).thenReturn(dataBroker);
76 when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
80 * Test method {@link L2ForwardingService#programLocalUcastOut(Long, String, Long, String, boolean)}
83 public void testProgramLoacalUcastOut() throws Exception {
84 l2ForwardingService.programLocalUcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, MAC_ADDRESS, true);
85 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
86 verify(writeTransaction, times(1)).submit();
87 verify(commitFuture, times(1)).get();
89 l2ForwardingService.programLocalUcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, MAC_ADDRESS, false);
90 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
91 verify(writeTransaction, times(2)).submit();
92 verify(commitFuture, times(2)).get(); // 1 + 1 above
96 * Test method {@link L2ForwardingService#programLocalVlanUcastOut(Long, String, Long, String, boolean)}
99 public void testProgramLocalVlanUcastOut() throws Exception {
100 l2ForwardingService.programLocalVlanUcastOut(DPID, SEGMENTATION_ID, Long.valueOf(124), MAC_ADDRESS, true);
101 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
102 verify(writeTransaction, times(1)).submit();
103 verify(commitFuture, times(1)).get();
105 l2ForwardingService.programLocalVlanUcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, MAC_ADDRESS, false);
106 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
107 verify(writeTransaction, times(2)).submit();
108 verify(commitFuture, times(2)).get(); // 1 + 1 above
112 * Test method {@link L2ForwardingService#programLocalBcastOut(Long, String, Long, boolean)}
115 public void testProgramLocalBcastOut() throws Exception {
116 l2ForwardingService.programLocalBcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, true);
117 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
118 verify(writeTransaction, times(1)).submit();
119 verify(commitFuture, times(1)).get();
121 l2ForwardingService.programLocalBcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, false);
122 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
123 verify(writeTransaction, times(2)).submit();
124 verify(commitFuture, times(2)).get(); // 1 + 1 above
127 /**--------------------------------- TODO go deeper in test
128 * Test method {@link L2ForwardingService#programLocalVlanBcastOut(Long, String, Long, Long, boolean)}
131 public void testProgramLocalVlanBcastOut() throws Exception {
132 l2ForwardingService.programLocalVlanBcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, ETH_PORT, true);
133 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
134 verify(writeTransaction, times(1)).submit();
135 verify(commitFuture, times(1)).get();
137 l2ForwardingService.programLocalVlanBcastOut(DPID, SEGMENTATION_ID, LOCAL_PORT, ETH_PORT, false);
138 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
139 verify(writeTransaction, times(2)).submit();
140 verify(commitFuture, times(2)).get(); // 1 + 1 above
144 * Test method {@link L2ForwardingService#programLocalTableMiss(Long, String, boolean)}
147 public void testProgramLocalTableMiss() throws Exception {
148 l2ForwardingService.programLocalTableMiss(DPID, SEGMENTATION_ID, true);
149 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
150 verify(writeTransaction, times(1)).submit();
151 verify(commitFuture, times(1)).get();
153 l2ForwardingService.programLocalTableMiss(DPID, SEGMENTATION_ID, false);
154 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
155 verify(writeTransaction, times(2)).submit();
156 verify(commitFuture, times(2)).get(); // 1 + 1 above
160 * Test method {@link L2ForwardingService#programLocalVlanTableMiss(Long, String, boolean)}
163 public void testProgramLocalVlanTableMiss() throws Exception {
164 l2ForwardingService.programLocalVlanTableMiss(DPID, SEGMENTATION_ID, true);
165 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
166 verify(writeTransaction, times(1)).submit();
167 verify(commitFuture, times(1)).get();
169 l2ForwardingService.programLocalVlanTableMiss(DPID, SEGMENTATION_ID, false);
170 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
171 verify(writeTransaction, times(2)).submit();
172 verify(commitFuture, times(2)).get(); // 1 + 1 above
176 * Test method {@link L2ForwardingService#programTunnelOut(Long, String, Long, String, boolean)}
179 public void testProgramTunnelOut() throws Exception {
180 l2ForwardingService.programTunnelOut(DPID, SEGMENTATION_ID, OF_PORT_OUT, MAC_ADDRESS, true);
181 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
182 verify(writeTransaction, times(1)).submit();
183 verify(commitFuture, times(1)).get();
185 l2ForwardingService.programTunnelOut(DPID, SEGMENTATION_ID, OF_PORT_OUT, MAC_ADDRESS, false);
186 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
187 verify(writeTransaction, times(2)).submit();
188 verify(commitFuture, times(2)).get(); // 1 + 1 above
192 * Test method {@link L2ForwardingService#programVlanOut(Long, String, Long, String, boolean)}
195 public void testProgramVlanOut() throws Exception {
196 l2ForwardingService.programVlanOut(DPID, SEGMENTATION_ID, ETH_PORT, MAC_ADDRESS, true);
197 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
198 verify(writeTransaction, times(1)).submit();
199 verify(commitFuture, times(1)).get();
201 l2ForwardingService.programVlanOut(DPID, SEGMENTATION_ID, ETH_PORT, MAC_ADDRESS, false);
202 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
203 verify(writeTransaction, times(2)).submit();
204 verify(commitFuture, times(2)).get(); // 1 + 1 above
207 /**--------------------------------- TODO go deeper in test
208 * Test method {@link L2ForwardingService#programTunnelFloodOut(Long, String, Long, boolean)}
211 public void testProgramTunnelFloodOut() throws Exception {
212 l2ForwardingService.programTunnelFloodOut(DPID, SEGMENTATION_ID, OF_PORT_OUT, true);
213 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
214 verify(writeTransaction, times(1)).submit();
215 verify(commitFuture, times(1)).get();
217 l2ForwardingService.programTunnelFloodOut(DPID, SEGMENTATION_ID, OF_PORT_OUT, false);
218 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
219 verify(writeTransaction, times(2)).submit();
220 verify(commitFuture, times(2)).get(); // 1 + 1 above
224 * Test method {@link L2ForwardingService#programVlanFloodOut(Long, String, Long, boolean)}
227 public void testProgramVlanFloodOut() throws Exception {
228 l2ForwardingService.programVlanFloodOut(DPID, SEGMENTATION_ID, ETH_PORT, true);
229 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
230 verify(writeTransaction, times(1)).submit();
231 verify(commitFuture, times(1)).get();
233 l2ForwardingService.programVlanFloodOut(DPID, SEGMENTATION_ID, ETH_PORT, false);
234 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
235 verify(writeTransaction, times(2)).submit();
236 verify(commitFuture, times(2)).get(); // 1 + 1 above
240 * Test method {@link L2ForwardingService#programTunnelMiss(Long, String, boolean)}
243 public void testProgramTunnelMiss() throws Exception {
244 l2ForwardingService.programTunnelMiss(DPID, SEGMENTATION_ID, true);
245 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
246 verify(writeTransaction, times(1)).submit();
247 verify(commitFuture, times(1)).get();
249 l2ForwardingService.programTunnelMiss(DPID, SEGMENTATION_ID, false);
250 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
251 verify(writeTransaction, times(2)).submit();
252 verify(commitFuture, times(2)).get(); // 1 + 1 above
256 * Test method {@link L2ForwardingService#programVlanMiss(Long, String, Long, boolean)}
259 public void testProgramVlanMiss() throws Exception {
260 l2ForwardingService.programTunnelMiss(DPID, SEGMENTATION_ID, true);
261 verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
262 verify(writeTransaction, times(1)).submit();
263 verify(commitFuture, times(1)).get();
265 l2ForwardingService.programTunnelMiss(DPID, SEGMENTATION_ID, false);
266 verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
267 verify(writeTransaction, times(2)).submit();
268 verify(commitFuture, times(2)).get(); // 1 + 1 above