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.v3po.translate.v3po.interfaces;
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.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.any;
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 import static org.mockito.MockitoAnnotations.initMocks;
35 import com.google.common.base.Optional;
36 import com.google.common.net.InetAddresses;
37 import io.fd.honeycomb.v3po.translate.MappingContext;
38 import io.fd.honeycomb.v3po.translate.ModificationCache;
39 import io.fd.honeycomb.v3po.translate.v3po.test.TestHelperUtils;
40 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
41 import io.fd.honeycomb.v3po.translate.write.WriteContext;
42 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
43 import java.util.concurrent.CompletableFuture;
44 import java.util.concurrent.CompletionStage;
45 import java.util.concurrent.ExecutionException;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.ArgumentCaptor;
49 import org.mockito.Mock;
50 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
51 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeNextProtocol;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeVni;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpe;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpeBuilder;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.openvpp.jvpp.VppBaseCallException;
65 import org.openvpp.jvpp.dto.VxlanAddDelTunnelReply;
66 import org.openvpp.jvpp.dto.VxlanGpeAddDelTunnel;
67 import org.openvpp.jvpp.dto.VxlanGpeAddDelTunnelReply;
68 import org.openvpp.jvpp.future.FutureJVpp;
70 public class VxlanGpeCustomizerTest {
72 private static final byte ADD_VXLAN_GPE = 1;
73 private static final byte DEL_VXLAN_GPE = 0;
76 private FutureJVpp api;
78 private WriteContext writeContext;
80 private MappingContext mappingContext;
82 private VxlanGpeCustomizer customizer;
83 private String ifaceName;
84 private InstanceIdentifier<VxlanGpe> id;
87 public void setUp() throws Exception {
89 InterfaceTypeTestUtils.setupWriteContext(writeContext,
90 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel.class);
91 // TODO create base class for tests using vppApi
92 NamingContext namingContext = new NamingContext("generateInterfaceNAme", "test-instance");
93 final ModificationCache toBeReturned = new ModificationCache();
94 doReturn(toBeReturned).when(writeContext).getModificationCache();
95 doReturn(mappingContext).when(writeContext).getMappingContext();
97 customizer = new VxlanGpeCustomizer(api, namingContext);
100 id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
101 .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
104 private void whenVxlanGpeAddDelTunnelThenSuccess()
105 throws ExecutionException, InterruptedException, VppBaseCallException {
106 final CompletionStage<VxlanGpeAddDelTunnelReply> replyCS = mock(CompletionStage.class);
107 final CompletableFuture<VxlanGpeAddDelTunnelReply> replyFuture = mock(CompletableFuture.class);
108 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
109 final VxlanGpeAddDelTunnelReply reply = new VxlanGpeAddDelTunnelReply();
110 when(replyFuture.get()).thenReturn(reply);
111 when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class))).thenReturn(replyCS);
115 * Failure response send
117 private void whenVxlanGpeAddDelTunnelThenFailure()
118 throws ExecutionException, InterruptedException, VppBaseCallException {
119 doReturn(TestHelperUtils.<VxlanAddDelTunnelReply>createFutureException()).when(api)
120 .vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
123 private VxlanGpeAddDelTunnel verifyVxlanGpeAddDelTunnelWasInvoked(final VxlanGpe vxlanGpe)
124 throws VppBaseCallException {
125 ArgumentCaptor<VxlanGpeAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(VxlanGpeAddDelTunnel.class);
126 verify(api).vxlanGpeAddDelTunnel(argumentCaptor.capture());
127 final VxlanGpeAddDelTunnel actual = argumentCaptor.getValue();
128 assertEquals(0, actual.isIpv6);
129 assertArrayEquals(InetAddresses.forString(vxlanGpe.getLocal().getIpv4Address().getValue()).getAddress(),
131 assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(),
133 assertEquals(vxlanGpe.getVni().getValue().intValue(), actual.vni);
134 assertEquals(vxlanGpe.getNextProtocol().getIntValue(), actual.protocol);
135 assertEquals(vxlanGpe.getEncapVrfId().intValue(), actual.encapVrfId);
136 assertEquals(vxlanGpe.getDecapVrfId().intValue(), actual.decapVrfId);
140 private void verifyVxlanGpeAddWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
141 final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
142 assertEquals(ADD_VXLAN_GPE, actual.isAdd);
145 private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
146 final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
147 assertEquals(DEL_VXLAN_GPE, actual.isAdd);
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();
161 private static VxlanGpe generateVxlanGpe() {
162 return generateVxlanGpe(Long.valueOf(11));
166 public void testWriteCurrentAttributes() throws Exception {
167 final VxlanGpe vxlanGpe = generateVxlanGpe();
169 whenVxlanGpeAddDelTunnelThenSuccess();
171 doReturn(Optional.absent())
172 .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
174 customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
175 verifyVxlanGpeAddWasInvoked(vxlanGpe);
176 verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get()));
180 public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
181 final VxlanGpe vxlanGpe = generateVxlanGpe();
183 whenVxlanGpeAddDelTunnelThenSuccess();
184 final Optional<Mapping> ifcMapping = getMapping(ifaceName, 0);
186 doReturn(Optional.of(new MappingsBuilder().setMapping(singletonList(ifcMapping.get())).build()))
187 .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
189 customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
190 verifyVxlanGpeAddWasInvoked(vxlanGpe);
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()));
198 public void testWriteCurrentAttributesFailed() throws Exception {
199 final VxlanGpe vxlanGpe = generateVxlanGpe();
201 whenVxlanGpeAddDelTunnelThenFailure();
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))
210 .put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get()));
213 fail("WriteFailedException.CreateFailedException was expected");
217 public void testUpdateCurrentAttributes() throws Exception {
219 customizer.updateCurrentAttributes(id, generateVxlanGpe(10), generateVxlanGpe(11), writeContext);
220 } catch (WriteFailedException.UpdateFailedException e) {
221 assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
224 fail("WriteFailedException.UpdateFailedException was expected");
228 public void testDeleteCurrentAttributes() throws Exception {
229 final VxlanGpe vxlanGpe = generateVxlanGpe();
231 whenVxlanGpeAddDelTunnelThenSuccess();
232 doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance"));
234 customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
235 verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
236 verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance")));
240 public void testDeleteCurrentAttributesaFailed() throws Exception {
241 final VxlanGpe vxlanGpe = generateVxlanGpe();
243 whenVxlanGpeAddDelTunnelThenFailure();
244 doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance"));
247 customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
248 } catch (WriteFailedException.DeleteFailedException e) {
249 assertTrue(e.getCause() instanceof VppBaseCallException);
250 verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
251 verify(mappingContext, times(0)).delete(eq(getMappingIid(ifaceName, "test-instance")));
254 fail("WriteFailedException.DeleteFailedException was expected");