2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfaces;
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;
45 import java.util.concurrent.CompletableFuture;
46 import java.util.concurrent.CompletionStage;
47 import java.util.concurrent.ExecutionException;
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;
55 public class VhostUserCustomizerTest {
58 private FutureJVpp api;
60 private WriteContext writeContext;
62 private MappingContext mappingContext;
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);
72 public void setUp() throws Exception {
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();
81 // TODO create base class for tests using vppApi
82 customizer = new VhostUserCustomizer(api, namingContext);
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);
95 * Failure response send
97 private void whenCreateVhostUserIfFailedThen(final int retval) throws ExecutionException, InterruptedException, VppInvocationException {
98 doReturn(TestHelperUtils.<CreateVhostUserIfReply>createFutureException(retval)).when(api).createVhostUserIf(any(CreateVhostUserIf.class));
101 private void whenCreateVhostUserIfThenSuccess() throws ExecutionException, InterruptedException, VppInvocationException {
102 whenCreateVhostUserIfThen();
105 private void whenVxlanAddDelTunnelThenFailure() throws ExecutionException, InterruptedException, VppInvocationException {
106 whenCreateVhostUserIfFailedThen(-1);
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);
119 * Failure response send
121 private void whenModifyVhostUserIfFailedThen(final int retval) throws ExecutionException, InterruptedException, VppInvocationException {
122 doReturn(TestHelperUtils.<ModifyVhostUserIfReply>createFutureException(retval)).when(api).modifyVhostUserIf(any(ModifyVhostUserIf.class));
125 private void whenModifyVhostUserIfThenSuccess() throws ExecutionException, InterruptedException, VppInvocationException {
126 whenModifyVhostUserIfThen();
129 private void whenModifyVhostUserIfThenFailure() throws ExecutionException, InterruptedException, VppInvocationException {
130 whenModifyVhostUserIfFailedThen(-1);
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);
143 * Failure response send
145 private void whenDeleteVhostUserIfFailedThen(final int retval) throws ExecutionException, InterruptedException, VppInvocationException {
146 doReturn(TestHelperUtils.<DeleteVhostUserIfReply>createFutureException(retval)).when(api).deleteVhostUserIf(any(DeleteVhostUserIf.class));
149 private void whenDeleteVhostUserIfThenSuccess() throws ExecutionException, InterruptedException, VppInvocationException {
150 whenDeleteVhostUserIfThen();
153 private void whenDeleteVhostUserIfThenFailure() throws ExecutionException, InterruptedException, VppInvocationException {
154 whenDeleteVhostUserIfFailedThen(-1);
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);
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);
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);
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);
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);
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();
200 public void testWriteCurrentAttributes() throws Exception {
201 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
203 whenCreateVhostUserIfThenSuccess();
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()));
211 public void testWriteCurrentAttributesFailed() throws Exception {
212 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
214 whenVxlanAddDelTunnelThenFailure();
217 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
218 } catch (WriteFailedException.CreateFailedException e) {
219 assertTrue(e.getCause() instanceof VppBaseCallException);
220 verifyCreateVhostUserIfWasInvoked(vhostUser);
221 verifyZeroInteractions(mappingContext);
224 fail("WriteFailedException.CreateFailedException was expected");
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"));
233 whenModifyVhostUserIfThenSuccess();
235 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
236 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
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"));
245 whenModifyVhostUserIfThenFailure();
248 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
249 } catch (WriteFailedException.UpdateFailedException e) {
250 assertTrue(e.getCause() instanceof VppBaseCallException);
251 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
254 fail("WriteFailedException.UpdateFailedException was expected");
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"));
262 whenDeleteVhostUserIfThenSuccess();
264 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
265 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
266 verify(mappingContext).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
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"));
274 whenDeleteVhostUserIfThenFailure();
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")));
286 fail("WriteFailedException.DeleteFailedException was expected");