0c1d2d72ff174686054c3860f537afdbf6537d7c
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / VhostUserCustomizerTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfaces;
18
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;
31
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;
55
56 public class VhostUserCustomizerTest extends WriterCustomizerTest {
57
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);
64
65     @Override
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"));
70     }
71
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);
78
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);
84         return actual;
85     }
86
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);
93
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);
98         return actual;
99     }
100
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);
106         return actual;
107     }
108
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();
114     }
115
116     @Test
117     public void testWriteCurrentAttributes() throws Exception {
118         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
119
120         when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(future(new CreateVhostUserIfReply()));
121
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()));
126     }
127
128     @Test
129     public void testWriteCurrentAttributesFailed() throws Exception {
130         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
131
132         doReturn(failedFuture()).when(api).createVhostUserIf(any(CreateVhostUserIf.class));
133
134         try {
135             customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
136         } catch (WriteFailedException.CreateFailedException e) {
137             assertTrue(e.getCause() instanceof VppBaseCallException);
138             verifyCreateVhostUserIfWasInvoked(vhostUser);
139             verifyZeroInteractions(mappingContext);
140             return;
141         }
142         fail("WriteFailedException.CreateFailedException was expected");
143     }
144
145     @Test
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");
150
151         when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(future(new ModifyVhostUserIfReply()));
152
153         customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
154         verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
155     }
156
157     @Test
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");
162
163         doReturn(failedFuture()).when(api).modifyVhostUserIf(any(ModifyVhostUserIf.class));
164
165         try {
166             customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
167         } catch (WriteFailedException.UpdateFailedException e) {
168             assertTrue(e.getCause() instanceof VppBaseCallException);
169             verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
170             return;
171         }
172         fail("WriteFailedException.UpdateFailedException was expected");
173     }
174
175     @Test
176     public void testDeleteCurrentAttributes() throws Exception {
177         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
178         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
179
180         when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(future(new DeleteVhostUserIfReply()));
181
182         customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
183         verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
184         verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(IFACE_NAME, "test-instance")));
185     }
186
187     @Test
188     public void testDeleteCurrentAttributesFailed() throws Exception {
189         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
190         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
191
192         doReturn(failedFuture()).when(api).deleteVhostUserIf(any(DeleteVhostUserIf.class));
193
194         try {
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")));
202             return;
203         }
204         fail("WriteFailedException.DeleteFailedException was expected");
205     }
206 }