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.v3po.test.ContextTestUtils;
33 import io.fd.honeycomb.translate.v3po.util.NamingContext;
34 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
35 import io.fd.honeycomb.translate.write.WriteFailedException;
36 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUserBuilder;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.openvpp.jvpp.VppBaseCallException;
48 import org.openvpp.jvpp.VppInvocationException;
49 import org.openvpp.jvpp.core.dto.CreateVhostUserIf;
50 import org.openvpp.jvpp.core.dto.CreateVhostUserIfReply;
51 import org.openvpp.jvpp.core.dto.DeleteVhostUserIf;
52 import org.openvpp.jvpp.core.dto.DeleteVhostUserIfReply;
53 import org.openvpp.jvpp.core.dto.ModifyVhostUserIf;
54 import org.openvpp.jvpp.core.dto.ModifyVhostUserIfReply;
56 public class VhostUserCustomizerTest extends WriterCustomizerTest {
58 private VhostUserCustomizer customizer;
59 private static final int IFACE_ID = 1;
60 private static final String IFACE_NAME = "eth0";
61 private static final InstanceIdentifier<VhostUser> ID =
62 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
63 .augmentation(VppInterfaceAugmentation.class).child(VhostUser.class);
66 public void setUp() throws Exception {
67 InterfaceTypeTestUtils.setupWriteContext(writeContext,
68 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
69 customizer = new VhostUserCustomizer(api, new NamingContext("generatedInterfaceName", "test-instance"));
72 private CreateVhostUserIf verifyCreateVhostUserIfWasInvoked(final VhostUser vhostUser)
73 throws VppInvocationException {
74 ArgumentCaptor<CreateVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(CreateVhostUserIf.class);
75 verify(api).createVhostUserIf(argumentCaptor.capture());
76 final CreateVhostUserIf actual = argumentCaptor.getValue();
77 assertEquals(0, actual.customDevInstance);
79 assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
80 assertEquals(0, actual.renumber);
81 assertEquals(0, actual.useCustomMac);
82 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
83 assertNotNull(actual.macAddress);
87 private ModifyVhostUserIf verifyModifyVhostUserIfWasInvoked(final VhostUser vhostUser, final int swIfIndex)
88 throws VppInvocationException {
89 ArgumentCaptor<ModifyVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(ModifyVhostUserIf.class);
90 verify(api).modifyVhostUserIf(argumentCaptor.capture());
91 final ModifyVhostUserIf actual = argumentCaptor.getValue();
92 assertEquals(0, actual.customDevInstance);
94 assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
95 assertEquals(0, actual.renumber);
96 assertEquals(swIfIndex, actual.swIfIndex);
97 assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
101 private DeleteVhostUserIf verifyDeleteVhostUserIfWasInvoked(final int swIfIndex) throws VppInvocationException {
102 ArgumentCaptor<DeleteVhostUserIf> argumentCaptor = ArgumentCaptor.forClass(DeleteVhostUserIf.class);
103 verify(api).deleteVhostUserIf(argumentCaptor.capture());
104 final DeleteVhostUserIf actual = argumentCaptor.getValue();
105 assertEquals(swIfIndex, actual.swIfIndex);
109 private static VhostUser generateVhostUser(final VhostUserRole role, final String socketName) {
110 VhostUserBuilder builder = new VhostUserBuilder();
111 builder.setRole(role);
112 builder.setSocket(socketName);
113 return builder.build();
117 public void testWriteCurrentAttributes() throws Exception {
118 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
120 when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(future(new CreateVhostUserIfReply()));
122 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
123 verifyCreateVhostUserIfWasInvoked(vhostUser);
124 verify(mappingContext).put(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")), eq(
125 ContextTestUtils.getMapping(IFACE_NAME, 0).get()));
129 public void testWriteCurrentAttributesFailed() throws Exception {
130 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
132 doReturn(failedFuture()).when(api).createVhostUserIf(any(CreateVhostUserIf.class));
135 customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
136 } catch (WriteFailedException.CreateFailedException e) {
137 assertTrue(e.getCause() instanceof VppBaseCallException);
138 verifyCreateVhostUserIfWasInvoked(vhostUser);
139 verifyZeroInteractions(mappingContext);
142 fail("WriteFailedException.CreateFailedException was expected");
146 public void testUpdateCurrentAttributes() throws Exception {
147 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
148 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
149 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
151 when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(future(new ModifyVhostUserIfReply()));
153 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
154 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
158 public void testUpdateCurrentAttributesFailed() throws Exception {
159 final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
160 final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
161 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
163 doReturn(failedFuture()).when(api).modifyVhostUserIf(any(ModifyVhostUserIf.class));
166 customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
167 } catch (WriteFailedException.UpdateFailedException e) {
168 assertTrue(e.getCause() instanceof VppBaseCallException);
169 verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
172 fail("WriteFailedException.UpdateFailedException was expected");
176 public void testDeleteCurrentAttributes() throws Exception {
177 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
178 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
180 when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(future(new DeleteVhostUserIfReply()));
182 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
183 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
184 verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
188 public void testDeleteCurrentAttributesFailed() throws Exception {
189 final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
190 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
192 doReturn(failedFuture()).when(api).deleteVhostUserIf(any(DeleteVhostUserIf.class));
195 customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
196 } catch (WriteFailedException.DeleteFailedException e) {
197 assertTrue(e.getCause() instanceof VppBaseCallException);
198 verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
199 // Delete from context not invoked if delete from VPP failed
200 verify(mappingContext, times(0)).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
201 verify(mappingContext).read(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
204 fail("WriteFailedException.DeleteFailedException was expected");