959d6eb4a89741c0c82ff5e2660a4bbc7efcc157
[hc2vpp.git] /
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.v3po.translate.v3po.interfaces;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static org.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.anyLong;
28 import static org.mockito.Matchers.eq;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.verifyZeroInteractions;
34 import static org.mockito.Mockito.when;
35 import static org.mockito.MockitoAnnotations.initMocks;
36
37 import io.fd.honeycomb.v3po.translate.MappingContext;
38 import io.fd.honeycomb.v3po.translate.ModificationCache;
39 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
40 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
41 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
42 import io.fd.honeycomb.v3po.translate.write.WriteContext;
43 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
44 import java.util.concurrent.CompletableFuture;
45 import java.util.concurrent.CompletionStage;
46 import java.util.concurrent.ExecutionException;
47 import java.util.concurrent.TimeUnit;
48 import java.util.concurrent.TimeoutException;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.ArgumentCaptor;
52 import org.mockito.Mock;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.openvpp.jvpp.VppBaseCallException;
62 import org.openvpp.jvpp.VppInvocationException;
63 import org.openvpp.jvpp.dto.CreateVhostUserIf;
64 import org.openvpp.jvpp.dto.CreateVhostUserIfReply;
65 import org.openvpp.jvpp.dto.DeleteVhostUserIf;
66 import org.openvpp.jvpp.dto.DeleteVhostUserIfReply;
67 import org.openvpp.jvpp.dto.ModifyVhostUserIf;
68 import org.openvpp.jvpp.dto.ModifyVhostUserIfReply;
69 import org.openvpp.jvpp.future.FutureJVpp;
70
71 public class VhostUserCustomizerTest {
72
73     @Mock
74     private FutureJVpp api;
75     @Mock
76     private WriteContext writeContext;
77     @Mock
78     private MappingContext mappingContext;
79
80     private VhostUserCustomizer customizer;
81     private static final int IFACE_ID = 1;
82     private static final String IFACE_NAME = "eth0";
83     private static final InstanceIdentifier<VhostUser> ID =
84             InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
85                     .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
86
87     @Before
88     public void setUp() throws Exception {
89         initMocks(this);
90         InterfaceTypeTestUtils.setupWriteContext(writeContext,
91             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
92         final NamingContext namingContext = new NamingContext("generatedInterfaceName", "test-instance");
93         final ModificationCache toBeReturned = new ModificationCache();
94         doReturn(toBeReturned).when(writeContext).getModificationCache();
95         doReturn(mappingContext).when(writeContext).getMappingContext();
96
97         // TODO create base class for tests using vppApi
98         customizer = new VhostUserCustomizer(api, namingContext);
99     }
100
101     private void whenCreateVhostUserIfThenSuccess()
102         throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
103         final CompletionStage<CreateVhostUserIfReply> replyCS = mock(CompletionStage.class);
104         final CompletableFuture<CreateVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
105         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
106         final CreateVhostUserIfReply reply = new CreateVhostUserIfReply();
107         when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
108         when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
109     }
110
111     /**
112      * Failure response send
113      */
114     private void whenCreateVhostUserIfThenFailure()
115             throws ExecutionException, InterruptedException, VppInvocationException {
116         doReturn(TestHelperUtils.<CreateVhostUserIfReply>createFutureException()).when(api)
117                 .createVhostUserIf(any(CreateVhostUserIf.class));
118     }
119
120     private void whenModifyVhostUserIfThenSuccess()
121         throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
122         final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
123         final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
124         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
125         final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
126         when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
127         when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
128     }
129
130     /**
131      * Failure response send
132      */
133     private void whenModifyVhostUserIfThenFailure()
134             throws ExecutionException, InterruptedException, VppInvocationException {
135         doReturn(TestHelperUtils.<ModifyVhostUserIfReply>createFutureException()).when(api)
136                 .modifyVhostUserIf(any(ModifyVhostUserIf.class));
137     }
138
139     private void whenDeleteVhostUserIfThenSuccess()
140         throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
141         final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
142         final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
143         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
144         final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
145         when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
146         when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
147     }
148
149     /**
150      * Failure response send
151      */
152     private void whenDeleteVhostUserIfThenFailure()
153             throws ExecutionException, InterruptedException, VppInvocationException {
154         doReturn(TestHelperUtils.<DeleteVhostUserIfReply>createFutureException()).when(api)
155                 .deleteVhostUserIf(any(DeleteVhostUserIf.class));
156     }
157
158     private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser) throws VppInvocationException {
159         ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
160         verify(api).createVhostUserIf(argumentCaptor.capture());
161         final CreateVhostUserIf actual = argumentCaptor.getValue();
162         assertEquals(0, actual.customDevInstance);
163
164         assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
165         assertEquals(0, actual.renumber);
166         assertEquals(0, actual.useCustomMac);
167         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
168         assertNotNull(actual.macAddress);
169         return actual;
170     }
171
172     private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex)
173             throws VppInvocationException {
174         ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
175         verify(api).modifyVhostUserIf(argumentCaptor.capture());
176         final ModifyVhostUserIf actual = argumentCaptor.getValue();
177         assertEquals(0, actual.customDevInstance);
178
179         assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
180         assertEquals(0, actual.renumber);
181         assertEquals(swIfIndex, actual.swIfIndex);
182         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
183         return actual;
184     }
185
186     private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) throws VppInvocationException {
187         ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
188         verify(api).deleteVhostUserIf(argumentCaptor.capture());
189         final DeleteVhostUserIf actual = argumentCaptor.getValue();
190         assertEquals(swIfIndex, actual.swIfIndex);
191         return actual;
192     }
193
194     private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
195         VhostUserBuilder builder = new VhostUserBuilder();
196         builder.setRole(role);
197         builder.setSocket(socketName);
198         return builder.build();
199     }
200
201     @Test
202     public void testWriteCurrentAttributes() throws Exception {
203         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
204
205         whenCreateVhostUserIfThenSuccess();
206
207         customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
208         verifyCreateVhostUserIfWasInvoked(vhostUser);
209         verify(mappingContext).put(eq(getMappingIid(IFACE_NAME, "test-instance")), eq(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(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
234
235         whenModifyVhostUserIfThenSuccess();
236
237         customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
238         verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
239     }
240
241     @Test
242     public void testUpdateCurrentAttributesFailed() throws Exception {
243         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
244         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
245         doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
246
247         whenModifyVhostUserIfThenFailure();
248
249         try {
250             customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
251         } catch (WriteFailedException.UpdateFailedException e) {
252             assertTrue(e.getCause() instanceof VppBaseCallException);
253             verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
254             return;
255         }
256         fail("WriteFailedException.UpdateFailedException was expected");
257     }
258
259     @Test
260     public void testDeleteCurrentAttributes() throws Exception {
261         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
262         doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
263
264         whenDeleteVhostUserIfThenSuccess();
265
266         customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
267         verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
268         verify(mappingContext).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
269     }
270
271     @Test
272     public void testDeleteCurrentAttributesFailed() throws Exception {
273         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
274         doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
275
276         whenDeleteVhostUserIfThenFailure();
277
278         try {
279             customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
280         } catch (WriteFailedException.DeleteFailedException e) {
281             assertTrue(e.getCause() instanceof VppBaseCallException);
282             verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
283             // Delete from context not invoked if delete from VPP failed
284             verify(mappingContext, times(0)).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
285             verify(mappingContext).read(eq(getMappingIid(IFACE_NAME, "test-instance")));
286             return;
287         }
288         fail("WriteFailedException.DeleteFailedException was expected");
289     }
290 }