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