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.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.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.fail;
25 import static org.mockito.Matchers.any;
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.v3po.translate.MappingContext;
36 import io.fd.honeycomb.v3po.translate.ModificationCache;
37 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
38 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
39 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
40 import io.fd.honeycomb.v3po.translate.write.WriteContext;
41 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
42 import java.util.concurrent.CompletableFuture;
43 import java.util.concurrent.CompletionStage;
44 import java.util.concurrent.ExecutionException;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.Mock;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.openvpp.jvpp.dto.CreateVhostUserIf;
58 import org.openvpp.jvpp.dto.CreateVhostUserIfReply;
59 import org.openvpp.jvpp.dto.DeleteVhostUserIf;
60 import org.openvpp.jvpp.dto.DeleteVhostUserIfReply;
61 import org.openvpp.jvpp.dto.ModifyVhostUserIf;
62 import org.openvpp.jvpp.dto.ModifyVhostUserIfReply;
63 import org.openvpp.jvpp.future.FutureJVpp;
65 public class VhostUserCustomizerTest {
68 private FutureJVpp api;
70 private WriteContext writeContext;
72 private MappingContext mappingContext;
74 private VhostUserCustomizer customizer;
75 private static final int IFACE_ID = 1;
76 private static final String IFACE_NAME = "eth0";
77 private static final InstanceIdentifier<VhostUser> ID =
78 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
79 .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
82 public void setUp() throws Exception {
84 InterfaceTypeTestUtils.setupWriteContext(writeContext,
85 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
86 final NamingContext namingContext = new NamingContext("generatedInterfaceName", "test-instance");
87 final ModificationCache toBeReturned = new ModificationCache();
88 doReturn(toBeReturned).when(writeContext).getModificationCache();
89 doReturn(mappingContext).when(writeContext).getMappingContext();
91 // TODO create base class for tests using vppApi
92 customizer = new VhostUserCustomizer(api, namingContext);
95 private void whenCreateVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
96 final CompletionStage<CreateVhostUserIfReply> replyCS = mock(CompletionStage.class);
97 final CompletableFuture<CreateVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
98 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
99 final CreateVhostUserIfReply reply = new CreateVhostUserIfReply();
100 reply.retval = retval;
101 when(replyFuture.get()).thenReturn(reply);
102 when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
105 private void whenCreateVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
106 whenCreateVhostUserIfThen(0);
109 private void whenVxlanAddDelTunnelThenFailure() throws ExecutionException, InterruptedException {
110 whenCreateVhostUserIfThen(-1);
113 private void whenModifyVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
114 final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
115 final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
116 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
117 final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
118 reply.retval = retval;
119 when(replyFuture.get()).thenReturn(reply);
120 when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
123 private void whenModifyVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
124 whenModifyVhostUserIfThen(0);
127 private void whenModifyVhostUserIfThenFailure() throws ExecutionException, InterruptedException {
128 whenModifyVhostUserIfThen(-1);
131 private void whenDeleteVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
132 final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
133 final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
134 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
135 final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
136 reply.retval = retval;
137 when(replyFuture.get()).thenReturn(reply);
138 when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
141 private void whenDeleteVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
142 whenDeleteVhostUserIfThen(0);
145 private void whenDeleteVhostUserIfThenFailure() throws ExecutionException, InterruptedException {
146 whenDeleteVhostUserIfThen(-1);
149 private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser) {
150 ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
151 verify(api).createVhostUserIf(argumentCaptor.capture());
152 final CreateVhostUserIf actual = argumentCaptor.getValue();
153 assertEquals(0, actual.customDevInstance);
155 assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
156 assertEquals(0, actual.renumber);
157 assertEquals(0, actual.useCustomMac);
158 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
159 assertNotNull(actual.macAddress);
163 private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex) {
164 ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
165 verify(api).modifyVhostUserIf(argumentCaptor.capture());
166 final ModifyVhostUserIf actual = argumentCaptor.getValue();
167 assertEquals(0, actual.customDevInstance);
169 assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
170 assertEquals(0, actual.renumber);
171 assertEquals(swIfIndex, actual.swIfIndex);
172 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
176 private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) {
177 ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
178 verify(api).deleteVhostUserIf(argumentCaptor.capture());
179 final DeleteVhostUserIf actual = argumentCaptor.getValue();
180 assertEquals(swIfIndex, actual.swIfIndex);
184 private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
185 VhostUserBuilder builder = new VhostUserBuilder();
186 builder.setRole(role);
187 builder.setSocket(socketName);
188 return builder.build();
192 public void testWriteCurrentAttributes() throws Exception {
193 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
195 whenCreateVhostUserIfThenSuccess();
197 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
198 verifyCreateVhostUserIfWasInvoked(vhostUser);
199 verify(mappingContext).put(eq(getMappingIid(IFACE_NAME, "test-instance")), eq(getMapping(IFACE_NAME, 0).get()));
203 public void testWriteCurrentAttributesFailed() throws Exception {
204 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
206 whenVxlanAddDelTunnelThenFailure();
209 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
210 } catch (WriteFailedException.CreateFailedException e) {
211 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
212 verifyCreateVhostUserIfWasInvoked(vhostUser);
213 verifyZeroInteractions(mappingContext);
216 fail("WriteFailedException.CreateFailedException was expected");
220 public void testUpdateCurrentAttributes() throws Exception {
221 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
222 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
223 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
225 whenModifyVhostUserIfThenSuccess();
227 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
228 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
232 public void testUpdateCurrentAttributesFailed() throws Exception {
233 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
234 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
235 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
237 whenModifyVhostUserIfThenFailure();
240 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
241 } catch (WriteFailedException.UpdateFailedException e) {
242 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
243 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
246 fail("WriteFailedException.UpdateFailedException was expected");
250 public void testDeleteCurrentAttributes() throws Exception {
251 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
252 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
254 whenDeleteVhostUserIfThenSuccess();
256 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
257 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
258 verify(mappingContext).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
262 public void testDeleteCurrentAttributesFailed() throws Exception {
263 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
264 doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
266 whenDeleteVhostUserIfThenFailure();
269 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
270 } catch (WriteFailedException.DeleteFailedException e) {
271 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
272 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
273 // Delete from context not invoked if delete from VPP failed
274 verify(mappingContext, times(0)).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
275 verify(mappingContext).read(eq(getMappingIid(IFACE_NAME, "test-instance")));
278 fail("WriteFailedException.DeleteFailedException was expected");