2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfaces;
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;
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;
73 public class VxlanGpeCustomizerTest {
75 private static final byte ADD_VXLAN_GPE = 1;
76 private static final byte DEL_VXLAN_GPE = 0;
79 private FutureJVppCore api;
81 private WriteContext writeContext;
83 private MappingContext mappingContext;
85 private DisabledInterfacesManager interfaceDisableContext;
87 private VxlanGpeCustomizer customizer;
88 private String ifaceName;
89 private InstanceIdentifier<VxlanGpe> id;
92 public void setUp() throws Exception {
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();
102 customizer = new VxlanGpeCustomizer(api, namingContext, interfaceDisableContext);
105 id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
106 .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
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);
120 * Failure response send
122 private void whenVxlanGpeAddDelTunnelThenFailure()
123 throws ExecutionException, InterruptedException, VppBaseCallException {
124 doReturn(TestHelperUtils.<VxlanAddDelTunnelReply>createFutureException()).when(api)
125 .vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
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(),
136 assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(),
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);
145 private void verifyVxlanGpeAddWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
146 final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
147 assertEquals(ADD_VXLAN_GPE, actual.isAdd);
150 private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
151 final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
152 assertEquals(DEL_VXLAN_GPE, actual.isAdd);
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();
166 private static VxlanGpe generateVxlanGpe() {
167 return generateVxlanGpe(Long.valueOf(11));
171 public void testWriteCurrentAttributes() throws Exception {
172 final VxlanGpe vxlanGpe = generateVxlanGpe();
174 whenVxlanGpeAddDelTunnelThenSuccess();
176 doReturn(Optional.absent())
177 .when(mappingContext).read(ContextTestUtils.getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
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()));
186 public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
187 final VxlanGpe vxlanGpe = generateVxlanGpe();
189 whenVxlanGpeAddDelTunnelThenSuccess();
190 final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(ifaceName, 0);
192 doReturn(Optional.of(new MappingsBuilder().setMapping(singletonList(ifcMapping.get())).build()))
193 .when(mappingContext).read(ContextTestUtils.getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
195 customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
196 verifyVxlanGpeAddWasInvoked(vxlanGpe);
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()));
204 public void testWriteCurrentAttributesFailed() throws Exception {
205 final VxlanGpe vxlanGpe = generateVxlanGpe();
207 whenVxlanGpeAddDelTunnelThenFailure();
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()));
220 fail("WriteFailedException.CreateFailedException was expected");
224 public void testUpdateCurrentAttributes() throws Exception {
226 customizer.updateCurrentAttributes(id, generateVxlanGpe(10), generateVxlanGpe(11), writeContext);
227 } catch (WriteFailedException.UpdateFailedException e) {
228 assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
231 fail("WriteFailedException.UpdateFailedException was expected");
235 public void testDeleteCurrentAttributes() throws Exception {
236 final VxlanGpe vxlanGpe = generateVxlanGpe();
238 whenVxlanGpeAddDelTunnelThenSuccess();
239 doReturn(ContextTestUtils.getMapping(ifaceName, 1)).when(mappingContext).read(
240 ContextTestUtils.getMappingIid(ifaceName, "test-instance"));
242 customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
243 verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
244 verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")));
248 public void testDeleteCurrentAttributesaFailed() throws Exception {
249 final VxlanGpe vxlanGpe = generateVxlanGpe();
251 whenVxlanGpeAddDelTunnelThenFailure();
252 doReturn(ContextTestUtils.getMapping(ifaceName, 1)).when(mappingContext).read(
253 ContextTestUtils.getMappingIid(ifaceName, "test-instance"));
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")));
263 fail("WriteFailedException.DeleteFailedException was expected");