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