076df67839548ac8c6722db41092e69ffafe72f5
[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.Context;
33 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
34 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
35 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
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 Context ctx;
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         namingContext = new NamingContext("generatedInterfaceName");
79         // TODO create base class for tests using vppApi
80         customizer = new VhostUserCustomizer(api, namingContext);
81     }
82
83     private void whenCreateVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
84         final CompletionStage<CreateVhostUserIfReply> replyCS = mock(CompletionStage.class);
85         final CompletableFuture<CreateVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
86         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
87         final CreateVhostUserIfReply reply = new CreateVhostUserIfReply();
88         reply.retval = retval;
89         when(replyFuture.get()).thenReturn(reply);
90         when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
91     }
92
93     private void whenCreateVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
94         whenCreateVhostUserIfThen(0);
95     }
96
97     private void whenVxlanAddDelTunnelThenFailure() throws ExecutionException, InterruptedException {
98         whenCreateVhostUserIfThen(-1);
99     }
100
101     private void whenModifyVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
102         final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
103         final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
104         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
105         final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
106         reply.retval = retval;
107         when(replyFuture.get()).thenReturn(reply);
108         when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
109     }
110
111     private void whenModifyVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
112         whenModifyVhostUserIfThen(0);
113     }
114
115     private void whenModifyVhostUserIfThenFailure() throws ExecutionException, InterruptedException {
116         whenModifyVhostUserIfThen(-1);
117     }
118
119     private void whenDeleteVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
120         final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
121         final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
122         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
123         final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
124         reply.retval = retval;
125         when(replyFuture.get()).thenReturn(reply);
126         when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
127     }
128
129     private void whenDeleteVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
130         whenDeleteVhostUserIfThen(0);
131     }
132
133     private void whenDeleteVhostUserIfThenFailure() throws ExecutionException, InterruptedException {
134         whenDeleteVhostUserIfThen(-1);
135     }
136
137     private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser) {
138         ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
139         verify(api).createVhostUserIf(argumentCaptor.capture());
140         final CreateVhostUserIf actual = argumentCaptor.getValue();
141         assertEquals(0, actual.customDevInstance);
142
143         assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
144         assertEquals(0, actual.renumber);
145         assertEquals(0, actual.useCustomMac);
146         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
147         assertNotNull(actual.macAddress);
148         return actual;
149     }
150
151     private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex) {
152         ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
153         verify(api).modifyVhostUserIf(argumentCaptor.capture());
154         final ModifyVhostUserIf actual = argumentCaptor.getValue();
155         assertEquals(0, actual.customDevInstance);
156
157         assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
158         assertEquals(0, actual.renumber);
159         assertEquals(swIfIndex, actual.swIfIndex);
160         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
161         return actual;
162     }
163
164     private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) {
165         ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
166         verify(api).deleteVhostUserIf(argumentCaptor.capture());
167         final DeleteVhostUserIf actual = argumentCaptor.getValue();
168         assertEquals(swIfIndex, actual.swIfIndex);
169         return actual;
170     }
171
172     private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
173         VhostUserBuilder builder = new VhostUserBuilder();
174         builder.setRole(role);
175         builder.setSocket(socketName);
176         return builder.build();
177     }
178
179     @Test
180     public void testWriteCurrentAttributes() throws Exception {
181         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
182
183         whenCreateVhostUserIfThenSuccess();
184
185         customizer.writeCurrentAttributes(ID, vhostUser, ctx);
186         verifyCreateVhostUserIfWasInvoked(vhostUser);
187         assertTrue(namingContext.containsIndex(IFACE_NAME));
188     }
189
190     @Test
191     public void testWriteCurrentAttributesFailed() throws Exception {
192         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
193
194         whenVxlanAddDelTunnelThenFailure();
195
196         try {
197             customizer.writeCurrentAttributes(ID, vhostUser, ctx);
198         } catch (WriteFailedException.CreateFailedException e) {
199             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
200             verifyCreateVhostUserIfWasInvoked(vhostUser);
201             assertFalse(namingContext.containsIndex(IFACE_NAME));
202             return;
203         }
204         fail("WriteFailedException.CreateFailedException was expected");
205     }
206
207     @Test
208     public void testUpdateCurrentAttributes() throws Exception {
209         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
210         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
211         namingContext.addName(IFACE_ID, IFACE_NAME);
212
213         whenModifyVhostUserIfThenSuccess();
214
215         customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, ctx);
216         verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
217     }
218
219     @Test
220     public void testUpdateCurrentAttributesNoUpdate() throws Exception {
221         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Server, "socketName");
222         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName");
223         customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, ctx);
224         verify(api, never()).modifyVhostUserIf(any(ModifyVhostUserIf.class));
225     }
226
227     @Test
228     public void testUpdateCurrentAttributesFailed() throws Exception {
229         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
230         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
231         namingContext.addName(IFACE_ID, IFACE_NAME);
232
233         whenModifyVhostUserIfThenFailure();
234
235         try {
236             customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, ctx);
237         } catch (WriteFailedException.UpdateFailedException e) {
238             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
239             verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
240             return;
241         }
242         fail("WriteFailedException.UpdateFailedException was expected");
243     }
244
245     @Test
246     public void testDeleteCurrentAttributes() throws Exception {
247         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
248         namingContext.addName(IFACE_ID, IFACE_NAME);
249
250         whenDeleteVhostUserIfThenSuccess();
251
252         customizer.deleteCurrentAttributes(ID, vhostUser, ctx);
253         verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
254         assertFalse(namingContext.containsIndex(IFACE_NAME));
255     }
256
257     @Test
258     public void testDeleteCurrentAttributesFailed() throws Exception {
259         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
260         namingContext.addName(IFACE_ID, IFACE_NAME);
261
262         whenDeleteVhostUserIfThenFailure();
263
264         try {
265             customizer.deleteCurrentAttributes(ID, vhostUser, ctx);
266         } catch (WriteFailedException.DeleteFailedException e) {
267             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
268             verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
269             assertTrue(namingContext.containsIndex(IFACE_NAME));
270             return;
271         }
272         fail("WriteFailedException.DeleteFailedException was expected");
273     }
274 }