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