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.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.verifyZeroInteractions;
30 import static org.mockito.Mockito.when;
32 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
33 import io.fd.honeycomb.translate.vpp.util.NamingContext;
34 import io.fd.honeycomb.translate.write.WriteFailedException;
35 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUser;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VhostUserBuilder;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import io.fd.vpp.jvpp.VppBaseCallException;
47 import io.fd.vpp.jvpp.VppInvocationException;
48 import io.fd.vpp.jvpp.core.dto.CreateVhostUserIf;
49 import io.fd.vpp.jvpp.core.dto.CreateVhostUserIfReply;
50 import io.fd.vpp.jvpp.core.dto.DeleteVhostUserIf;
51 import io.fd.vpp.jvpp.core.dto.DeleteVhostUserIfReply;
52 import io.fd.vpp.jvpp.core.dto.ModifyVhostUserIf;
53 import io.fd.vpp.jvpp.core.dto.ModifyVhostUserIfReply;
55 public class VhostUserCustomizerTest extends WriterCustomizerTest implements Ipv4Translator {
57 private VhostUserCustomizer customizer;
58 private static final int IFACE_ID = 1;
59 private static final String IFACE_NAME = "eth0";
60 private static final InstanceIdentifier<VhostUser> ID =
61 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
62 .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
65 public void setUp() throws Exception {
66 InterfaceTypeTestUtils.setupWriteContext(writeContext,
67 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUser.class);
68 customizer = new VhostUserCustomizer(api, new NamingContext("generatedInterfaceName", "test-instance"));
71 private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser)
72 throws VppInvocationException {
73 ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
74 verify(api).createVhostUserIf(argumentCaptor.capture());
75 final CreateVhostUserIf actual = argumentCaptor.getValue();
76 assertEquals(0, actual.customDevInstance);
78 assertEquals(booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
79 assertEquals(0, actual.renumber);
80 assertEquals(0, actual.useCustomMac);
81 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
82 assertNotNull(actual.macAddress);
86 private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex)
87 throws VppInvocationException {
88 ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
89 verify(api).modifyVhostUserIf(argumentCaptor.capture());
90 final ModifyVhostUserIf actual = argumentCaptor.getValue();
91 assertEquals(0, actual.customDevInstance);
93 assertEquals(booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
94 assertEquals(0, actual.renumber);
95 assertEquals(swIfIndex, actual.swIfIndex);
96 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
100 private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) throws VppInvocationException {
101 ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
102 verify(api).deleteVhostUserIf(argumentCaptor.capture());
103 final DeleteVhostUserIf actual = argumentCaptor.getValue();
104 assertEquals(swIfIndex, actual.swIfIndex);
108 private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
109 VhostUserBuilder builder = new VhostUserBuilder();
110 builder.setRole(role);
111 builder.setSocket(socketName);
112 return builder.build();
116 public void testWriteCurrentAttributes() throws Exception {
117 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
119 when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(future(new CreateVhostUserIfReply()));
121 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
122 verifyCreateVhostUserIfWasInvoked(vhostUser);
123 verify(mappingContext).put(eq(mappingIid(IFACE_NAME, "test-instance")), eq(
124 mapping(IFACE_NAME, 0).get()));
128 public void testWriteCurrentAttributesFailed() throws Exception {
129 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
131 doReturn(failedFuture()).when(api).createVhostUserIf(any(CreateVhostUserIf.class));
134 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
135 } catch (WriteFailedException.CreateFailedException e) {
136 assertTrue(e.getCause() instanceof VppBaseCallException);
137 verifyCreateVhostUserIfWasInvoked(vhostUser);
138 verifyZeroInteractions(mappingContext);
141 fail("WriteFailedException.CreateFailedException was expected");
145 public void testUpdateCurrentAttributes() throws Exception {
146 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
147 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
148 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
150 when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(future(new ModifyVhostUserIfReply()));
152 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
153 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
157 public void testUpdateCurrentAttributesFailed() throws Exception {
158 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
159 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
160 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
162 doReturn(failedFuture()).when(api).modifyVhostUserIf(any(ModifyVhostUserIf.class));
165 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
166 } catch (WriteFailedException.UpdateFailedException e) {
167 assertTrue(e.getCause() instanceof VppBaseCallException);
168 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
171 fail("WriteFailedException.UpdateFailedException was expected");
175 public void testDeleteCurrentAttributes() throws Exception {
176 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
177 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
179 when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(future(new DeleteVhostUserIfReply()));
181 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
182 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
183 verify(mappingContext).delete(eq(mappingIid(IFACE_NAME, "test-instance")));
187 public void testDeleteCurrentAttributesFailed() throws Exception {
188 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
189 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
191 doReturn(failedFuture()).when(api).deleteVhostUserIf(any(DeleteVhostUserIf.class));
194 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
195 } catch (WriteFailedException.DeleteFailedException e) {
196 assertTrue(e.getCause() instanceof VppBaseCallException);
197 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
198 // Delete from context not invoked if delete from VPP failed
199 verify(mappingContext, times(0)).delete(eq(mappingIid(IFACE_NAME, "test-instance")));
200 verify(mappingContext).read(eq(mappingIid(IFACE_NAME, "test-instance")));
203 fail("WriteFailedException.DeleteFailedException was expected");