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 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.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyZeroInteractions;
33 import static org.mockito.Mockito.when;
34 import static org.mockito.MockitoAnnotations.initMocks;
36 import io.fd.honeycomb.v3po.translate.MappingContext;
37 import io.fd.honeycomb.v3po.translate.ModificationCache;
38 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
39 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
40 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
41 import io.fd.honeycomb.v3po.translate.write.WriteContext;
42 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
43 import java.util.concurrent.CompletableFuture;
44 import java.util.concurrent.CompletionStage;
45 import java.util.concurrent.ExecutionException;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.ArgumentCaptor;
49 import org.mockito.Mock;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.openvpp.jvpp.VppBaseCallException;
59 import org.openvpp.jvpp.VppInvocationException;
60 import org.openvpp.jvpp.dto.CreateVhostUserIf;
61 import org.openvpp.jvpp.dto.CreateVhostUserIfReply;
62 import org.openvpp.jvpp.dto.DeleteVhostUserIf;
63 import org.openvpp.jvpp.dto.DeleteVhostUserIfReply;
64 import org.openvpp.jvpp.dto.ModifyVhostUserIf;
65 import org.openvpp.jvpp.dto.ModifyVhostUserIfReply;
66 import org.openvpp.jvpp.future.FutureJVpp;
68 public class VhostUserCustomizerTest {
71 private FutureJVpp api;
73 private WriteContext writeContext;
75 private MappingContext mappingContext;
77 private VhostUserCustomizer customizer;
78 private static final int IFACE_ID = 1;
79 private static final String IFACE_NAME = "eth0";
80 private static final InstanceIdentifier<VhostUser> ID =
81 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
82 .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
85 public void setUp() throws Exception {
87 InterfaceTypeTestUtils.setupWriteContext(writeContext,
88 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
89 final NamingContext namingContext = new NamingContext("generatedInterfaceName", "test-instance");
90 final ModificationCache toBeReturned = new ModificationCache();
91 doReturn(toBeReturned).when(writeContext).getModificationCache();
92 doReturn(mappingContext).when(writeContext).getMappingContext();
94 // TODO create base class for tests using vppApi
95 customizer = new VhostUserCustomizer(api, namingContext);
98 private void whenCreateVhostUserIfThenSuccess()
99 throws ExecutionException, InterruptedException, VppInvocationException {
100 final CompletionStage<CreateVhostUserIfReply> replyCS = mock(CompletionStage.class);
101 final CompletableFuture<CreateVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
102 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
103 final CreateVhostUserIfReply reply = new CreateVhostUserIfReply();
104 when(replyFuture.get()).thenReturn(reply);
105 when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
109 * Failure response send
111 private void whenCreateVhostUserIfThenFailure()
112 throws ExecutionException, InterruptedException, VppInvocationException {
113 doReturn(TestHelperUtils.<CreateVhostUserIfReply>createFutureException()).when(api)
114 .createVhostUserIf(any(CreateVhostUserIf.class));
117 private void whenModifyVhostUserIfThenSuccess()
118 throws ExecutionException, InterruptedException, VppInvocationException {
119 final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
120 final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
121 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
122 final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
123 when(replyFuture.get()).thenReturn(reply);
124 when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
128 * Failure response send
130 private void whenModifyVhostUserIfThenFailure()
131 throws ExecutionException, InterruptedException, VppInvocationException {
132 doReturn(TestHelperUtils.<ModifyVhostUserIfReply>createFutureException()).when(api)
133 .modifyVhostUserIf(any(ModifyVhostUserIf.class));
136 private void whenDeleteVhostUserIfThenSuccess()
137 throws ExecutionException, InterruptedException, VppInvocationException {
138 final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
139 final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
140 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
141 final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
142 when(replyFuture.get()).thenReturn(reply);
143 when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
147 * Failure response send
149 private void whenDeleteVhostUserIfThenFailure()
150 throws ExecutionException, InterruptedException, VppInvocationException {
151 doReturn(TestHelperUtils.<DeleteVhostUserIfReply>createFutureException()).when(api)
152 .deleteVhostUserIf(any(DeleteVhostUserIf.class));
155 private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser) throws VppInvocationException {
156 ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
157 verify(api).createVhostUserIf(argumentCaptor.capture());
158 final CreateVhostUserIf actual = argumentCaptor.getValue();
159 assertEquals(0, actual.customDevInstance);
161 assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
162 assertEquals(0, actual.renumber);
163 assertEquals(0, actual.useCustomMac);
164 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
165 assertNotNull(actual.macAddress);
169 private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex)
170 throws VppInvocationException {
171 ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
172 verify(api).modifyVhostUserIf(argumentCaptor.capture());
173 final ModifyVhostUserIf actual = argumentCaptor.getValue();
174 assertEquals(0, actual.customDevInstance);
176 assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
177 assertEquals(0, actual.renumber);
178 assertEquals(swIfIndex, actual.swIfIndex);
179 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
183 private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) throws VppInvocationException {
184 ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
185 verify(api).deleteVhostUserIf(argumentCaptor.capture());
186 final DeleteVhostUserIf actual = argumentCaptor.getValue();
187 assertEquals(swIfIndex, actual.swIfIndex);
191 private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
192 VhostUserBuilder builder = new VhostUserBuilder();
193 builder.setRole(role);
194 builder.setSocket(socketName);
195 return builder.build();
199 public void testWriteCurrentAttributes() throws Exception {
200 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
202 whenCreateVhostUserIfThenSuccess();
204 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
205 verifyCreateVhostUserIfWasInvoked(vhostUser);
206 verify(mappingContext).put(eq(getMappingIid(IFACE_NAME, "test-instance")), eq(getMapping(IFACE_NAME, 0).get()));
210 public void testWriteCurrentAttributesFailed() throws Exception {
211 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
213 whenCreateVhostUserIfThenFailure();
216 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
217 } catch (WriteFailedException.CreateFailedException e) {
218 assertTrue(e.getCause() instanceof VppBaseCallException);
219 verifyCreateVhostUserIfWasInvoked(vhostUser);
220 verifyZeroInteractions(mappingContext);
223 fail("WriteFailedException.CreateFailedException was expected");
227 public void testUpdateCurrentAttributes() throws Exception {
228 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
229 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
230 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
232 whenModifyVhostUserIfThenSuccess();
234 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
235 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
239 public void testUpdateCurrentAttributesFailed() throws Exception {
240 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
241 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
242 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
244 whenModifyVhostUserIfThenFailure();
247 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
248 } catch (WriteFailedException.UpdateFailedException e) {
249 assertTrue(e.getCause() instanceof VppBaseCallException);
250 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
253 fail("WriteFailedException.UpdateFailedException was expected");
257 public void testDeleteCurrentAttributes() throws Exception {
258 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
259 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
261 whenDeleteVhostUserIfThenSuccess();
263 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
264 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
265 verify(mappingContext).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
269 public void testDeleteCurrentAttributesFailed() throws Exception {
270 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
271 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
273 whenDeleteVhostUserIfThenFailure();
276 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
277 } catch (WriteFailedException.DeleteFailedException e) {
278 assertTrue(e.getCause() instanceof VppBaseCallException);
279 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
280 // Delete from context not invoked if delete from VPP failed
281 verify(mappingContext, times(0)).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
282 verify(mappingContext).read(eq(getMappingIid(IFACE_NAME, "test-instance")));
285 fail("WriteFailedException.DeleteFailedException was expected");