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