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 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;
32 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
33 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
34 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
35 import io.fd.honeycomb.v3po.translate.write.WriteContext;
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;
60 public class VhostUserCustomizerTest {
63 private FutureJVpp api;
65 private WriteContext writeContext;
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);
76 public void setUp() throws Exception {
78 InterfaceTypeTestUtils.setupWriteContext(writeContext,
79 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
80 namingContext = new NamingContext("generatedInterfaceName");
81 // TODO create base class for tests using vppApi
82 customizer = new VhostUserCustomizer(api, namingContext);
85 private void whenCreateVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
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 reply.retval = retval;
91 when(replyFuture.get()).thenReturn(reply);
92 when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(replyCS);
95 private void whenCreateVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
96 whenCreateVhostUserIfThen(0);
99 private void whenVxlanAddDelTunnelThenFailure() throws ExecutionException, InterruptedException {
100 whenCreateVhostUserIfThen(-1);
103 private void whenModifyVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
104 final CompletionStage<ModifyVhostUserIfReply> replyCS = mock(CompletionStage.class);
105 final CompletableFuture<ModifyVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
106 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
107 final ModifyVhostUserIfReply reply = new ModifyVhostUserIfReply();
108 reply.retval = retval;
109 when(replyFuture.get()).thenReturn(reply);
110 when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(replyCS);
113 private void whenModifyVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
114 whenModifyVhostUserIfThen(0);
117 private void whenModifyVhostUserIfThenFailure() throws ExecutionException, InterruptedException {
118 whenModifyVhostUserIfThen(-1);
121 private void whenDeleteVhostUserIfThen(final int retval) throws ExecutionException, InterruptedException {
122 final CompletionStage<DeleteVhostUserIfReply> replyCS = mock(CompletionStage.class);
123 final CompletableFuture<DeleteVhostUserIfReply> replyFuture = mock(CompletableFuture.class);
124 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
125 final DeleteVhostUserIfReply reply = new DeleteVhostUserIfReply();
126 reply.retval = retval;
127 when(replyFuture.get()).thenReturn(reply);
128 when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(replyCS);
131 private void whenDeleteVhostUserIfThenSuccess() throws ExecutionException, InterruptedException {
132 whenDeleteVhostUserIfThen(0);
135 private void whenDeleteVhostUserIfThenFailure() throws ExecutionException, InterruptedException {
136 whenDeleteVhostUserIfThen(-1);
139 private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser) {
140 ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
141 verify(api).createVhostUserIf(argumentCaptor.capture());
142 final CreateVhostUserIf actual = argumentCaptor.getValue();
143 assertEquals(0, actual.customDevInstance);
145 assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
146 assertEquals(0, actual.renumber);
147 assertEquals(0, actual.useCustomMac);
148 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
149 assertNotNull(actual.macAddress);
153 private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex) {
154 ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
155 verify(api).modifyVhostUserIf(argumentCaptor.capture());
156 final ModifyVhostUserIf actual = argumentCaptor.getValue();
157 assertEquals(0, actual.customDevInstance);
159 assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
160 assertEquals(0, actual.renumber);
161 assertEquals(swIfIndex, actual.swIfIndex);
162 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
166 private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) {
167 ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
168 verify(api).deleteVhostUserIf(argumentCaptor.capture());
169 final DeleteVhostUserIf actual = argumentCaptor.getValue();
170 assertEquals(swIfIndex, actual.swIfIndex);
174 private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
175 VhostUserBuilder builder = new VhostUserBuilder();
176 builder.setRole(role);
177 builder.setSocket(socketName);
178 return builder.build();
182 public void testWriteCurrentAttributes() throws Exception {
183 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
185 whenCreateVhostUserIfThenSuccess();
187 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
188 verifyCreateVhostUserIfWasInvoked(vhostUser);
189 assertTrue(namingContext.containsIndex(IFACE_NAME));
193 public void testWriteCurrentAttributesFailed() throws Exception {
194 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
196 whenVxlanAddDelTunnelThenFailure();
199 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
200 } catch (WriteFailedException.CreateFailedException e) {
201 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
202 verifyCreateVhostUserIfWasInvoked(vhostUser);
203 assertFalse(namingContext.containsIndex(IFACE_NAME));
206 fail("WriteFailedException.CreateFailedException was expected");
210 public void testUpdateCurrentAttributes() throws Exception {
211 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
212 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
213 namingContext.addName(IFACE_ID, IFACE_NAME);
215 whenModifyVhostUserIfThenSuccess();
217 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
218 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
222 public void testUpdateCurrentAttributesNoUpdate() throws Exception {
223 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Server, "socketName");
224 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName");
225 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
226 verify(api, never()).modifyVhostUserIf(any(ModifyVhostUserIf.class));
230 public void testUpdateCurrentAttributesFailed() throws Exception {
231 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
232 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
233 namingContext.addName(IFACE_ID, IFACE_NAME);
235 whenModifyVhostUserIfThenFailure();
238 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
239 } catch (WriteFailedException.UpdateFailedException e) {
240 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
241 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
244 fail("WriteFailedException.UpdateFailedException was expected");
248 public void testDeleteCurrentAttributes() throws Exception {
249 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
250 namingContext.addName(IFACE_ID, IFACE_NAME);
252 whenDeleteVhostUserIfThenSuccess();
254 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
255 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
256 assertFalse(namingContext.containsIndex(IFACE_NAME));
260 public void testDeleteCurrentAttributesFailed() throws Exception {
261 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
262 namingContext.addName(IFACE_ID, IFACE_NAME);
264 whenDeleteVhostUserIfThenFailure();
267 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
268 } catch (WriteFailedException.DeleteFailedException e) {
269 assertEquals(VppApiInvocationException.class, e.getCause().getClass());
270 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
271 assertTrue(namingContext.containsIndex(IFACE_NAME));
274 fail("WriteFailedException.DeleteFailedException was expected");