HONEYCOMB-154: update revison of models that changed since 16.09
[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.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;
54
55 public class VhostUserCustomizerTest extends WriterCustomizerTest implements Ipv4Translator {
56
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);
63
64     @Override
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"));
69     }
70
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);
77
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);
83         return actual;
84     }
85
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);
92
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);
97         return actual;
98     }
99
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);
105         return actual;
106     }
107
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();
113     }
114
115     @Test
116     public void testWriteCurrentAttributes() throws Exception {
117         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Server, "socketName");
118
119         when(api.createVhostUserIf(any(CreateVhostUserIf.class))).thenReturn(future(new CreateVhostUserIfReply()));
120
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()));
125     }
126
127     @Test
128     public void testWriteCurrentAttributesFailed() throws Exception {
129         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
130
131         doReturn(failedFuture()).when(api).createVhostUserIf(any(CreateVhostUserIf.class));
132
133         try {
134             customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
135         } catch (WriteFailedException.CreateFailedException e) {
136             assertTrue(e.getCause() instanceof VppBaseCallException);
137             verifyCreateVhostUserIfWasInvoked(vhostUser);
138             verifyZeroInteractions(mappingContext);
139             return;
140         }
141         fail("WriteFailedException.CreateFailedException was expected");
142     }
143
144     @Test
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");
149
150         when(api.modifyVhostUserIf(any(ModifyVhostUserIf.class))).thenReturn(future(new ModifyVhostUserIfReply()));
151
152         customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
153         verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
154     }
155
156     @Test
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");
161
162         doReturn(failedFuture()).when(api).modifyVhostUserIf(any(ModifyVhostUserIf.class));
163
164         try {
165             customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
166         } catch (WriteFailedException.UpdateFailedException e) {
167             assertTrue(e.getCause() instanceof VppBaseCallException);
168             verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
169             return;
170         }
171         fail("WriteFailedException.UpdateFailedException was expected");
172     }
173
174     @Test
175     public void testDeleteCurrentAttributes() throws Exception {
176         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
177         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
178
179         when(api.deleteVhostUserIf(any(DeleteVhostUserIf.class))).thenReturn(future(new DeleteVhostUserIfReply()));
180
181         customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
182         verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
183         verify(mappingContext).delete(eq(mappingIid(IFACE_NAME, "test-instance")));
184     }
185
186     @Test
187     public void testDeleteCurrentAttributesFailed() throws Exception {
188         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
189         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, "test-instance");
190
191         doReturn(failedFuture()).when(api).deleteVhostUserIf(any(DeleteVhostUserIf.class));
192
193         try {
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")));
201             return;
202         }
203         fail("WriteFailedException.DeleteFailedException was expected");
204     }
205 }