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 org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import com.google.common.net.InetAddresses;
31 import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
32 import io.fd.honeycomb.translate.vpp.util.NamingContext;
33 import io.fd.honeycomb.translate.write.WriteFailedException;
34 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeNextProtocol;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeVni;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpe;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpeBuilder;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.openvpp.jvpp.VppBaseCallException;
50 import org.openvpp.jvpp.core.dto.VxlanGpeAddDelTunnel;
51 import org.openvpp.jvpp.core.dto.VxlanGpeAddDelTunnelReply;
53 public class VxlanGpeCustomizerTest extends WriterCustomizerTest {
55 private static final byte ADD_VXLAN_GPE = 1;
56 private static final byte DEL_VXLAN_GPE = 0;
59 private DisabledInterfacesManager interfaceDisableContext;
61 private VxlanGpeCustomizer customizer;
62 private String ifaceName;
63 private InstanceIdentifier<VxlanGpe> id;
66 public void setUp() throws Exception {
67 InterfaceTypeTestUtils.setupWriteContext(writeContext,
68 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel.class);
69 NamingContext namingContext = new NamingContext("generateInterfaceName", "test-instance");
70 customizer = new VxlanGpeCustomizer(api, namingContext, interfaceDisableContext);
73 id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
74 .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
77 private void whenVxlanGpeAddDelTunnelThenSuccess() {
78 when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class)))
79 .thenReturn(future(new VxlanGpeAddDelTunnelReply()));
82 private void whenVxlanGpeAddDelTunnelThenFailure() {
83 doReturn(failedFuture()).when(api).vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
86 private VxlanGpeAddDelTunnel verifyVxlanGpeAddDelTunnelWasInvoked(final VxlanGpe vxlanGpe)
87 throws VppBaseCallException {
88 ArgumentCaptor<VxlanGpeAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(VxlanGpeAddDelTunnel.class);
89 verify(api).vxlanGpeAddDelTunnel(argumentCaptor.capture());
90 final VxlanGpeAddDelTunnel actual = argumentCaptor.getValue();
91 assertEquals(0, actual.isIpv6);
92 assertArrayEquals(InetAddresses.forString(vxlanGpe.getLocal().getIpv4Address().getValue()).getAddress(),
94 assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(),
96 assertEquals(vxlanGpe.getVni().getValue().intValue(), actual.vni);
97 assertEquals(vxlanGpe.getNextProtocol().getIntValue(), actual.protocol);
98 assertEquals(vxlanGpe.getEncapVrfId().intValue(), actual.encapVrfId);
99 assertEquals(vxlanGpe.getDecapVrfId().intValue(), actual.decapVrfId);
103 private void verifyVxlanGpeAddWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
104 final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
105 assertEquals(ADD_VXLAN_GPE, actual.isAdd);
108 private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
109 final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
110 assertEquals(DEL_VXLAN_GPE, actual.isAdd);
113 private static VxlanGpe generateVxlanGpe(long vni) {
114 final VxlanGpeBuilder builder = new VxlanGpeBuilder();
115 builder.setLocal(new IpAddress(new Ipv4Address("192.168.20.10")));
116 builder.setRemote(new IpAddress(new Ipv4Address("192.168.20.11")));
117 builder.setVni(new VxlanGpeVni(Long.valueOf(vni)));
118 builder.setNextProtocol(VxlanGpeNextProtocol.forValue(1));
119 builder.setEncapVrfId(Long.valueOf(123));
120 builder.setDecapVrfId(Long.valueOf(456));
121 return builder.build();
124 private static VxlanGpe generateVxlanGpe() {
125 return generateVxlanGpe(Long.valueOf(11));
129 public void testWriteCurrentAttributes() throws Exception {
130 final VxlanGpe vxlanGpe = generateVxlanGpe();
132 whenVxlanGpeAddDelTunnelThenSuccess();
133 noMappingDefined(mappingContext, ifaceName, "test-instance");
135 customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
136 verifyVxlanGpeAddWasInvoked(vxlanGpe);
137 verify(mappingContext).put(eq(mappingIid(ifaceName, "test-instance")), eq(
138 mapping(ifaceName, 0).get()));
142 public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
143 final VxlanGpe vxlanGpe = generateVxlanGpe();
144 final int ifaceId = 0;
146 whenVxlanGpeAddDelTunnelThenSuccess();
147 defineMapping(mappingContext, ifaceName, ifaceId, "test-instance");
149 customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
150 verifyVxlanGpeAddWasInvoked(vxlanGpe);
152 // Remove the first mapping before putting in the new one
153 verify(mappingContext).delete(eq(mappingIid(ifaceName, "test-instance")));
154 verify(mappingContext).put(eq(mappingIid(ifaceName, "test-instance")),
155 eq(mapping(ifaceName, ifaceId).get()));
159 public void testWriteCurrentAttributesFailed() throws Exception {
160 final VxlanGpe vxlanGpe = generateVxlanGpe();
162 whenVxlanGpeAddDelTunnelThenFailure();
165 customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
166 } catch (WriteFailedException.CreateFailedException e) {
167 assertTrue(e.getCause() instanceof VppBaseCallException);
168 verifyVxlanGpeAddWasInvoked(vxlanGpe);
169 // Mapping not stored due to failure
170 verify(mappingContext, times(0))
171 .put(eq(mappingIid(ifaceName, "test-instance")), eq(
172 mapping(ifaceName, 0).get()));
175 fail("WriteFailedException.CreateFailedException was expected");
179 public void testUpdateCurrentAttributes() throws Exception {
181 customizer.updateCurrentAttributes(id, generateVxlanGpe(10), generateVxlanGpe(11), writeContext);
182 } catch (WriteFailedException.UpdateFailedException e) {
183 assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
186 fail("WriteFailedException.UpdateFailedException was expected");
190 public void testDeleteCurrentAttributes() throws Exception {
191 final VxlanGpe vxlanGpe = generateVxlanGpe();
193 whenVxlanGpeAddDelTunnelThenSuccess();
194 defineMapping(mappingContext, ifaceName, 1, "test-instance");
196 customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
197 verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
198 verify(mappingContext).delete(eq(mappingIid(ifaceName, "test-instance")));
202 public void testDeleteCurrentAttributesaFailed() throws Exception {
203 final VxlanGpe vxlanGpe = generateVxlanGpe();
205 whenVxlanGpeAddDelTunnelThenFailure();
206 defineMapping(mappingContext, ifaceName, 1, "test-instance");
209 customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
210 } catch (WriteFailedException.DeleteFailedException e) {
211 assertTrue(e.getCause() instanceof VppBaseCallException);
212 verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
213 verify(mappingContext, times(0)).delete(eq(mappingIid(ifaceName, "test-instance")));
216 fail("WriteFailedException.DeleteFailedException was expected");