34e5658eaef399d46ebc47aded2f660eb6b36eaf
[netvirt.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / L2FowardingServiceTest.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.ovsdb.openstack.netvirt.providers.openflow13.services;
10
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;
17
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;
34
35 import com.google.common.base.Optional;
36 import com.google.common.util.concurrent.CheckedFuture;
37
38 /**
39  * Unit test fort {@link L2ForwardingService}
40  */
41 @RunWith(MockitoJUnitRunner.class)
42 @SuppressWarnings("unchecked")
43 public class L2FowardingServiceTest {
44
45     @InjectMocks private L2ForwardingService l2ForwardingService = new L2ForwardingService(Service.ARP_RESPONDER);
46
47     @Mock private DataBroker dataBroker;
48     @Mock private PipelineOrchestrator orchestrator;
49
50     @Mock private WriteTransaction writeTransaction;
51     @Mock private ReadOnlyTransaction readOnlyTransaction;
52     @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
53
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;
60
61     @Before
62     public void setUp() throws Exception {
63         when(writeTransaction.submit()).thenReturn(commitFuture);
64
65         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
66         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
67
68         CheckedFuture future = mock(CheckedFuture.class);
69         when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future);
70
71         Optional<Flow> data = mock(Optional.class);
72         when(future.get()).thenReturn(data);
73
74         //when(mdsalConsumer.getDataBroker()).thenReturn(dataBroker);
75
76         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
77     }
78
79     /**
80      * Test method {@link L2ForwardingService#programLocalUcastOut(Long, String, Long, String, boolean)}
81      */
82     @Test
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();
88
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
93     }
94
95     /**
96      * Test method {@link L2ForwardingService#programLocalVlanUcastOut(Long, String, Long, String, boolean)}
97      */
98     @Test
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();
104
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
109     }
110
111     /**
112      * Test method {@link L2ForwardingService#programLocalBcastOut(Long, String, Long, boolean)}
113      */
114     @Test
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();
120
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
125     }
126
127     /**--------------------------------- TODO go deeper in test
128      * Test method {@link L2ForwardingService#programLocalVlanBcastOut(Long, String, Long, Long, boolean)}
129      */
130     @Test
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();
136
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
141     }
142
143     /**
144      * Test method {@link L2ForwardingService#programLocalTableMiss(Long, String, boolean)}
145      */
146     @Test
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();
152
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
157     }
158
159     /**
160      * Test method {@link L2ForwardingService#programLocalVlanTableMiss(Long, String, boolean)}
161      */
162     @Test
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();
168
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
173     }
174
175     /**
176      * Test method {@link L2ForwardingService#programTunnelOut(Long, String, Long, String, boolean)}
177      */
178     @Test
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();
184
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
189     }
190
191     /**
192      * Test method {@link L2ForwardingService#programVlanOut(Long, String, Long, String, boolean)}
193      */
194     @Test
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();
200
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
205     }
206
207     /**--------------------------------- TODO go deeper in test
208      * Test method {@link L2ForwardingService#programTunnelFloodOut(Long, String, Long, boolean)}
209      */
210     @Test
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();
216
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
221     }
222
223     /**
224      * Test method {@link L2ForwardingService#programVlanFloodOut(Long, String, Long, boolean)}
225      */
226     @Test
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();
232
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
237     }
238
239     /**
240     * Test method {@link L2ForwardingService#programTunnelMiss(Long, String, boolean)}
241     */
242    @Test
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();
248
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
253    }
254
255    /**
256     * Test method {@link L2ForwardingService#programVlanMiss(Long, String, Long, boolean)}
257     */
258    @Test
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();
264
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
269    }
270 }