77404aa45dde7be64b188663b593143620cee7df
[hc2vpp.git] /
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.v3po.translate.v3po.interfaces;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
20 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
21 import static java.util.Collections.singletonList;
22 import static org.junit.Assert.*;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.mockito.MockitoAnnotations.initMocks;
31
32 import com.google.common.base.Optional;
33 import com.google.common.net.InetAddresses;
34 import io.fd.honeycomb.v3po.translate.MappingContext;
35 import io.fd.honeycomb.v3po.translate.ModificationCache;
36 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
37 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
38 import io.fd.honeycomb.v3po.translate.write.WriteContext;
39 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
40 import java.util.concurrent.CompletableFuture;
41 import java.util.concurrent.CompletionStage;
42 import java.util.concurrent.ExecutionException;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Mock;
47 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
48 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeVni;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeNextProtocol;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpe;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpeBuilder;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.openvpp.jvpp.VppBaseCallException;
62 import org.openvpp.jvpp.VppInvocationException;
63 import org.openvpp.jvpp.dto.VxlanAddDelTunnel;
64 import org.openvpp.jvpp.dto.VxlanAddDelTunnelReply;
65 import org.openvpp.jvpp.dto.VxlanGpeAddDelTunnel;
66 import org.openvpp.jvpp.dto.VxlanGpeAddDelTunnelReply;
67 import org.openvpp.jvpp.future.FutureJVpp;
68
69 public class VxlanGpeCustomizerTest {
70
71     private static final byte ADD_VXLAN_GPE = 1;
72     private static final byte DEL_VXLAN_GPE = 0;
73
74     @Mock
75     private FutureJVpp api;
76     @Mock
77     private WriteContext writeContext;
78     @Mock
79     private MappingContext mappingContext;
80
81     private VxlanGpeCustomizer customizer;
82     private String ifaceName;
83     private InstanceIdentifier<VxlanGpe> id;
84
85     @Before
86     public void setUp() throws Exception {
87         initMocks(this);
88         InterfaceTypeTestUtils.setupWriteContext(writeContext,
89             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel.class);
90         // TODO create base class for tests using vppApi
91         NamingContext namingContext = new NamingContext("generateInterfaceNAme", "test-instance");
92         final ModificationCache toBeReturned = new ModificationCache();
93         doReturn(toBeReturned).when(writeContext).getModificationCache();
94         doReturn(mappingContext).when(writeContext).getMappingContext();
95
96         customizer = new VxlanGpeCustomizer(api, namingContext);
97
98         ifaceName = "eth0";
99         id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
100                         .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
101     }
102
103     private void whenVxlanGpeAddDelTunnelThen() throws ExecutionException, InterruptedException, VppBaseCallException {
104         final CompletionStage<VxlanGpeAddDelTunnelReply> replyCS = mock(CompletionStage.class);
105         final CompletableFuture<VxlanGpeAddDelTunnelReply> replyFuture = mock(CompletableFuture.class);
106         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
107         final VxlanGpeAddDelTunnelReply reply = new VxlanGpeAddDelTunnelReply();
108         when(replyFuture.get()).thenReturn(reply);
109         when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class))).thenReturn(replyCS);
110     }
111
112     private void whenVxlanGpeAddDelTunnelThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
113         whenVxlanGpeAddDelTunnelThen();
114     }
115
116     private void whenVxlanGpeAddDelTunnelThenFailure() throws ExecutionException, InterruptedException, VppBaseCallException {
117         whenVxlanGpeAddDelTunnelFailedThen(-1);
118     }
119
120     /**
121      * Failure response send
122      */
123     private void whenVxlanGpeAddDelTunnelFailedThen(final int retval) throws ExecutionException, InterruptedException, VppBaseCallException {
124         doReturn(TestHelperUtils.<VxlanAddDelTunnelReply>createFutureException(retval)).when(api).vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
125     }
126
127     private VxlanGpeAddDelTunnel verifyVxlanGpeAddDelTunnelWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
128         ArgumentCaptor<VxlanGpeAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(VxlanGpeAddDelTunnel.class);
129         verify(api).vxlanGpeAddDelTunnel(argumentCaptor.capture());
130         final VxlanGpeAddDelTunnel actual = argumentCaptor.getValue();
131         assertEquals(0, actual.isIpv6);
132         assertArrayEquals(InetAddresses.forString(vxlanGpe.getLocal().getIpv4Address().getValue()).getAddress(), actual.local);
133         assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(), actual.remote);
134         assertEquals(vxlanGpe.getVni().getValue().intValue(), actual.vni);
135         assertEquals(vxlanGpe.getNextProtocol().getIntValue(), actual.protocol);
136         assertEquals(vxlanGpe.getEncapVrfId().intValue(), actual.encapVrfId);
137         assertEquals(vxlanGpe.getDecapVrfId().intValue(), actual.decapVrfId);       
138         return actual;
139     }
140     private void verifyVxlanGpeAddWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
141         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
142         assertEquals(ADD_VXLAN_GPE, actual.isAdd);
143     }
144
145     private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
146         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
147         assertEquals(DEL_VXLAN_GPE, actual.isAdd);
148     }
149
150     private static VxlanGpe generateVxlanGpe(long vni) {
151         final VxlanGpeBuilder builder = new VxlanGpeBuilder();
152         builder.setLocal(new IpAddress(new Ipv4Address("192.168.20.10")));
153         builder.setRemote(new IpAddress(new Ipv4Address("192.168.20.11")));
154         builder.setVni(new VxlanGpeVni(Long.valueOf(vni)));
155         builder.setNextProtocol(VxlanGpeNextProtocol.forValue(1));
156         builder.setEncapVrfId(Long.valueOf(123));
157         builder.setDecapVrfId(Long.valueOf(456));   
158         return builder.build();
159     }
160
161     private static VxlanGpe generateVxlanGpe() {
162         return generateVxlanGpe(Long.valueOf(11));
163     }
164
165     @Test
166     public void testWriteCurrentAttributes() throws Exception {
167         final VxlanGpe vxlanGpe = generateVxlanGpe();
168
169         whenVxlanGpeAddDelTunnelThenSuccess();
170
171         doReturn(Optional.absent())
172             .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
173
174         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
175         verifyVxlanGpeAddWasInvoked(vxlanGpe);
176         verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get()));
177     }
178
179     @Test
180     public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
181         final VxlanGpe vxlanGpe = generateVxlanGpe();
182
183         whenVxlanGpeAddDelTunnelThenSuccess();
184         final Optional<Mapping> ifcMapping = getMapping(ifaceName, 0);
185
186         doReturn(Optional.of(new MappingsBuilder().setMapping(singletonList(ifcMapping.get())).build()))
187             .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
188
189         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
190         verifyVxlanGpeAddWasInvoked(vxlanGpe);
191
192         // Remove the first mapping before putting in the new one
193         verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance")));
194         verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(ifcMapping.get()));
195     }
196
197     @Test
198     public void testWriteCurrentAttributesFailed() throws Exception {
199         final VxlanGpe vxlanGpe = generateVxlanGpe();
200
201         whenVxlanGpeAddDelTunnelThenFailure();
202
203         try {
204             customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
205         } catch (WriteFailedException.CreateFailedException e) {
206             assertTrue(e.getCause() instanceof VppBaseCallException);
207             verifyVxlanGpeAddWasInvoked(vxlanGpe);
208             // Mapping not stored due to failure
209             verify(mappingContext, times(0)).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get()));
210             return;
211         }
212         fail("WriteFailedException.CreateFailedException was expected");
213     }
214
215     @Test
216     public void testUpdateCurrentAttributes() throws Exception {
217         try {
218             customizer.updateCurrentAttributes(id, generateVxlanGpe(10), generateVxlanGpe(11), writeContext);
219         } catch (WriteFailedException.UpdateFailedException e) {
220             assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
221             return;
222         }
223         fail("WriteFailedException.UpdateFailedException was expected");
224     }
225
226     @Test
227     public void testDeleteCurrentAttributes() throws Exception {
228         final VxlanGpe vxlanGpe = generateVxlanGpe();
229
230         whenVxlanGpeAddDelTunnelThenSuccess();
231         doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance"));
232
233         customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
234         verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
235         verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance")));
236     }
237
238     @Test
239     public void testDeleteCurrentAttributesaFailed() throws Exception {
240         final VxlanGpe vxlanGpe = generateVxlanGpe();
241
242         whenVxlanGpeAddDelTunnelThenFailure();
243         doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance"));
244
245         try {
246             customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
247         } catch (WriteFailedException.DeleteFailedException e) {
248             assertTrue(e.getCause() instanceof VppBaseCallException);
249             verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
250             verify(mappingContext, times(0)).delete(eq(getMappingIid(ifaceName, "test-instance")));
251             return;
252         }
253         fail("WriteFailedException.DeleteFailedException was expected");
254     }
255 }