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.translate.v3po.interfaces;
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyLong;
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;
35 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
36 import io.fd.honeycomb.translate.write.WriteContext;
37 import io.fd.honeycomb.translate.MappingContext;
38 import io.fd.honeycomb.translate.ModificationCache;
39 import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
40 import io.fd.honeycomb.translate.v3po.util.NamingContext;
41 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
42 import io.fd.honeycomb.translate.write.WriteFailedException;
43 import java.util.concurrent.CompletableFuture;
44 import java.util.concurrent.CompletionStage;
45 import java.util.concurrent.ExecutionException;
46 import java.util.concurrent.TimeUnit;
47 import java.util.concurrent.TimeoutException;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.openvpp.jvpp.VppBaseCallException;
61 import org.openvpp.jvpp.VppInvocationException;
62 import org.openvpp.jvpp.core.dto.CreateVhostUserIf;
63 import org.openvpp.jvpp.core.dto.CreateVhostUserIfReply;
64 import org.openvpp.jvpp.core.dto.DeleteVhostUserIf;
65 import org.openvpp.jvpp.core.dto.DeleteVhostUserIfReply;
66 import org.openvpp.jvpp.core.dto.ModifyVhostUserIf;
67 import org.openvpp.jvpp.core.dto.ModifyVhostUserIfReply;
68 import org.openvpp.jvpp.core.future.FutureJVppCore;
70 public class VhostUserCustomizerTest {
73 private FutureJVppCore api;
75 private WriteContext writeContext;
77 private MappingContext mappingContext;
79 private VhostUserCustomizer customizer;
80 private static final int IFACE_ID = 1;
81 private static final String IFACE_NAME = "eth0";
82 private static final InstanceIdentifier<VhostUser> ID =
83 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
84 .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
87 public void setUp() throws Exception {
89 InterfaceTypeTestUtils.setupWriteContext(writeContext,
90 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
91 final NamingContext namingContext = new NamingContext("generatedInterfaceName", "test-instance");
92 final ModificationCache toBeReturned = new ModificationCache();
93 doReturn(toBeReturned).when(writeContext).getModificationCache();
94 doReturn(mappingContext).when(writeContext).getMappingContext();
96 // TODO HONEYCOMB-116 create base class for tests using vppApi
97 customizer = new VhostUserCustomizer(api, namingContext);
100 private void whenCreateVhostUserIfThenSuccess()
101 throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
102 final CompletionStage<CreateVhostUserIfReply> replyCS = mock(CompletionStage.class);
103 final CompletableFuture<CreateVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
104 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
105 final CreateVhostUserIfReply reply = new CreateVhostUserIfReply();
106 when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
107 when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
111 * Failure response send
113 private void whenCreateVhostUserIfThenFailure()
114 throws ExecutionException, InterruptedException, VppInvocationException {
115 doReturn(TestHelperUtils.<CreateVhostUserIfReply>createFutureException()).when(api)
116 .createVhostUserIf(any(CreateVhostUserIf.class));
119 private void whenModifyVhostUserIfThenSuccess()
120 throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
121 final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
122 final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
123 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
124 final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
125 when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
126 when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
130 * Failure response send
132 private void whenModifyVhostUserIfThenFailure()
133 throws ExecutionException, InterruptedException, VppInvocationException {
134 doReturn(TestHelperUtils.<ModifyVhostUserIfReply>createFutureException()).when(api)
135 .modifyVhostUserIf(any(ModifyVhostUserIf.class));
138 private void whenDeleteVhostUserIfThenSuccess()
139 throws ExecutionException, InterruptedException, VppInvocationException, TimeoutException {
140 final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
141 final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
142 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
143 final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
144 when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
145 when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
149 * Failure response send
151 private void whenDeleteVhostUserIfThenFailure()
152 throws ExecutionException, InterruptedException, VppInvocationException {
153 doReturn(TestHelperUtils.<DeleteVhostUserIfReply>createFutureException()).when(api)
154 .deleteVhostUserIf(any(DeleteVhostUserIf.class));
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)
172 throws VppInvocationException {
173 ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
174 verify(api).modifyVhostUserIf(argumentCaptor.capture());
175 final ModifyVhostUserIf actual = argumentCaptor.getValue();
176 assertEquals(0, actual.customDevInstance);
178 assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
179 assertEquals(0, actual.renumber);
180 assertEquals(swIfIndex, actual.swIfIndex);
181 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
185 private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) throws VppInvocationException {
186 ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
187 verify(api).deleteVhostUserIf(argumentCaptor.capture());
188 final DeleteVhostUserIf actual = argumentCaptor.getValue();
189 assertEquals(swIfIndex, actual.swIfIndex);
193 private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
194 VhostUserBuilder builder = new VhostUserBuilder();
195 builder.setRole(role);
196 builder.setSocket(socketName);
197 return builder.build();
201 public void testWriteCurrentAttributes() throws Exception {
202 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
204 whenCreateVhostUserIfThenSuccess();
206 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
207 verifyCreateVhostUserIfWasInvoked(vhostUser);
208 verify(mappingContext).put(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")), eq(
209 ContextTestUtils.getMapping(IFACE_NAME, 0).get()));
213 public void testWriteCurrentAttributesFailed() throws Exception {
214 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
216 whenCreateVhostUserIfThenFailure();
219 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
220 } catch (WriteFailedException.CreateFailedException e) {
221 assertTrue(e.getCause() instanceof VppBaseCallException);
222 verifyCreateVhostUserIfWasInvoked(vhostUser);
223 verifyZeroInteractions(mappingContext);
226 fail("WriteFailedException.CreateFailedException was expected");
230 public void testUpdateCurrentAttributes() throws Exception {
231 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
232 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
233 doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
234 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
236 whenModifyVhostUserIfThenSuccess();
238 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
239 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
243 public void testUpdateCurrentAttributesFailed() throws Exception {
244 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
245 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
246 doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
247 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
249 whenModifyVhostUserIfThenFailure();
252 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
253 } catch (WriteFailedException.UpdateFailedException e) {
254 assertTrue(e.getCause() instanceof VppBaseCallException);
255 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
258 fail("WriteFailedException.UpdateFailedException was expected");
262 public void testDeleteCurrentAttributes() throws Exception {
263 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
264 doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
265 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
267 whenDeleteVhostUserIfThenSuccess();
269 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
270 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
271 verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
275 public void testDeleteCurrentAttributesFailed() throws Exception {
276 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
277 doReturn(ContextTestUtils.getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(
278 ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance"));
280 whenDeleteVhostUserIfThenFailure();
283 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
284 } catch (WriteFailedException.DeleteFailedException e) {
285 assertTrue(e.getCause() instanceof VppBaseCallException);
286 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
287 // Delete from context not invoked if delete from VPP failed
288 verify(mappingContext, times(0)).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
289 verify(mappingContext).read(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
292 fail("WriteFailedException.DeleteFailedException was expected");