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