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