0bbf04fc0b2fda71ab38b9c3fb08965a91869649
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / VhostUserCustomizerTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfaces;
18
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyLong;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyZeroInteractions;
32 import static org.mockito.Mockito.when;
33 import static org.mockito.MockitoAnnotations.initMocks;
34
35 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
36 import io.fd.honeycomb.translate.write.WriteContext;
37 import io.fd.honeycomb.translate.MappingContext;
38 import io.fd.honeycomb.translate.ModificationCache;
39 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
40 import io.fd.honeycomb.translate.v3po.util.NamingContext;
41 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
42 import io.fd.honeycomb.translate.write.WriteFailedException;
43 import java.util.concurrent.CompletableFuture;
44 import java.util.concurrent.CompletionStage;
45 import java.util.concurrent.ExecutionException;
46 import java.util.concurrent.TimeUnit;
47 import java.util.concurrent.TimeoutException;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.openvpp.jvpp.VppBaseCallException;
61 import org.openvpp.jvpp.VppInvocationException;
62 import org.openvpp.jvpp.core.dto.CreateVhostUserIf;
63 import org.openvpp.jvpp.core.dto.CreateVhostUserIfReply;
64 import org.openvpp.jvpp.core.dto.DeleteVhostUserIf;
65 import org.openvpp.jvpp.core.dto.DeleteVhostUserIfReply;
66 import org.openvpp.jvpp.core.dto.ModifyVhostUserIf;
67 import org.openvpp.jvpp.core.dto.ModifyVhostUserIfReply;
68 import org.openvpp.jvpp.core.future.FutureJVppCore;
69
70 public class VhostUserCustomizerTest {
71
72     @Mock
73     private FutureJVppCore api;
74     @Mock
75     private WriteContext writeContext;
76     @Mock
77     private MappingContext mappingContext;
78
79     private VhostUserCustomizer customizer;
80     private static final int IFACE_ID = 1;
81     private static final String IFACE_NAME = "eth0";
82     private static final InstanceIdentifier<VhostUser> ID =
83             InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
84                     .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
85
86     @Before
87     public void setUp() throws Exception {
88         initMocks(this);
89         InterfaceTypeTestUtils.setupWriteContext(writeContext,
90             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
91         final NamingContext namingContext = new NamingContext("generatedInterfaceName", "test-instance");
92         final ModificationCache toBeReturned = new ModificationCache();
93         doReturn(toBeReturned).when(writeContext).getModificationCache();
94         doReturn(mappingContext).when(writeContext).getMappingContext();
95
96         // TODO HONEYCOMB-116 create base class for tests using vppApi
97         customizer = new VhostUserCustomizer(api, namingContext);
98     }
99
100     private void whenCreateVhostUserIfThenSuccess()
101         throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
102         final CompletionStage<CreateVhostUserIfReply> replyCS = mock(CompletionStage.class);
103         final CompletableFuture<CreateVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
104         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
105         final CreateVhostUserIfReply reply = new CreateVhostUserIfReply();
106         when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
107         when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
108     }
109
110     /**
111      * Failure response send
112      */
113     private void whenCreateVhostUserIfThenFailure()
114             throws ExecutionException, InterruptedException, VppInvocationException {
115         doReturn(TestHelperUtils.<CreateVhostUserIfReply>createFutureException()).when(api)
116                 .createVhostUserIf(any(CreateVhostUserIf.class));
117     }
118
119     private void whenModifyVhostUserIfThenSuccess()
120         throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
121         final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
122         final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
123         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
124         final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
125         when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
126         when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
127     }
128
129     /**
130      * Failure response send
131      */
132     private void whenModifyVhostUserIfThenFailure()
133             throws ExecutionException, InterruptedException, VppInvocationException {
134         doReturn(TestHelperUtils.<ModifyVhostUserIfReply>createFutureException()).when(api)
135                 .modifyVhostUserIf(any(ModifyVhostUserIf.class));
136     }
137
138     private void whenDeleteVhostUserIfThenSuccess()
139         throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
140         final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
141         final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
142         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
143         final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
144         when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
145         when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
146     }
147
148     /**
149      * Failure response send
150      */
151     private void whenDeleteVhostUserIfThenFailure()
152             throws ExecutionException, InterruptedException, VppInvocationException {
153         doReturn(TestHelperUtils.<DeleteVhostUserIfReply>createFutureException()).when(api)
154                 .deleteVhostUserIf(any(DeleteVhostUserIf.class));
155     }
156
157     private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser) throws VppInvocationException {
158         ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
159         verify(api).createVhostUserIf(argumentCaptor.capture());
160         final CreateVhostUserIf actual = argumentCaptor.getValue();
161         assertEquals(0, actual.customDevInstance);
162
163         assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
164         assertEquals(0, actual.renumber);
165         assertEquals(0, actual.useCustomMac);
166         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
167         assertNotNull(actual.macAddress);
168         return actual;
169     }
170
171     private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex)
172             throws VppInvocationException {
173         ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
174         verify(api).modifyVhostUserIf(argumentCaptor.capture());
175         final ModifyVhostUserIf actual = argumentCaptor.getValue();
176         assertEquals(0, actual.customDevInstance);
177
178         assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
179         assertEquals(0, actual.renumber);
180         assertEquals(swIfIndex, actual.swIfIndex);
181         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
182         return actual;
183     }
184
185     private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) throws VppInvocationException {
186         ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
187         verify(api).deleteVhostUserIf(argumentCaptor.capture());
188         final DeleteVhostUserIf actual = argumentCaptor.getValue();
189         assertEquals(swIfIndex, actual.swIfIndex);
190         return actual;
191     }
192
193     private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
194         VhostUserBuilder builder = new VhostUserBuilder();
195         builder.setRole(role);
196         builder.setSocket(socketName);
197         return builder.build();
198     }
199
200     @Test
201     public void testWriteCurrentAttributes() throws Exception {
202         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
203
204         whenCreateVhostUserIfThenSuccess();
205
206         customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
207         verifyCreateVhostUserIfWasInvoked(vhostUser);
208         verify(mappingContext).put(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")), eq(
209                 ContextTestUtils.getMapping(IFACE_NAME, 0).get()));
210     }
211
212     @Test
213     public void testWriteCurrentAttributesFailed() throws Exception {
214         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
215
216         whenCreateVhostUserIfThenFailure();
217
218         try {
219             customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
220         } catch (WriteFailedException.CreateFailedException e) {
221             assertTrue(e.getCause() instanceof VppBaseCallException);
222             verifyCreateVhostUserIfWasInvoked(vhostUser);
223             verifyZeroInteractions(mappingContext);
224             return;
225         }
226         fail("WriteFailedException.CreateFailedException was expected");
227     }
228
229     @Test
230     public void testUpdateCurrentAttributes() throws Exception {
231         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
232         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
233         doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
234                 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
235
236         whenModifyVhostUserIfThenSuccess();
237
238         customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
239         verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
240     }
241
242     @Test
243     public void testUpdateCurrentAttributesFailed() throws Exception {
244         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
245         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
246         doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
247                 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
248
249         whenModifyVhostUserIfThenFailure();
250
251         try {
252             customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
253         } catch (WriteFailedException.UpdateFailedException e) {
254             assertTrue(e.getCause() instanceof VppBaseCallException);
255             verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
256             return;
257         }
258         fail("WriteFailedException.UpdateFailedException was expected");
259     }
260
261     @Test
262     public void testDeleteCurrentAttributes() throws Exception {
263         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
264         doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
265                 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
266
267         whenDeleteVhostUserIfThenSuccess();
268
269         customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
270         verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
271         verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
272     }
273
274     @Test
275     public void testDeleteCurrentAttributesFailed() throws Exception {
276         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
277         doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
278                 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
279
280         whenDeleteVhostUserIfThenFailure();
281
282         try {
283             customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
284         } catch (WriteFailedException.DeleteFailedException e) {
285             assertTrue(e.getCause() instanceof VppBaseCallException);
286             verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
287             // Delete from context not invoked if delete from VPP failed
288             verify(mappingContext, times(0)).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
289             verify(mappingContext).read(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
290             return;
291         }
292         fail("WriteFailedException.DeleteFailedException was expected");
293     }
294 }