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;
29 import com.google.common.net.InetAddresses;
30 import io.fd.honeycomb.translate.vpp.util.NamingContext;
31 import io.fd.honeycomb.translate.write.WriteFailedException;
32 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Gre;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.GreBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import io.fd.vpp.jvpp.VppBaseCallException;
45 import io.fd.vpp.jvpp.VppInvocationException;
46 import io.fd.vpp.jvpp.core.dto.GreAddDelTunnel;
47 import io.fd.vpp.jvpp.core.dto.GreAddDelTunnelReply;
49 public class GreCustomizerTest extends WriterCustomizerTest {
51 private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
52 private final String IFACE_NAME = "eth0";
53 private final int IFACE_ID = 1;
54 private InstanceIdentifier<Gre> id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
55 .augmentation(VppInterfaceAugmentation.class).child(Gre.class);
56 private static final byte ADD_GRE = 1;
57 private static final byte DEL_GRE = 0;
59 private GreCustomizer customizer;
62 public void setUp() throws Exception {
63 InterfaceTypeTestUtils.setupWriteContext(writeContext,
64 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.GreTunnel.class);
65 customizer = new GreCustomizer(api, new NamingContext("generateInterfaceNAme", IFC_TEST_INSTANCE));
68 private void whenGreAddDelTunnelThenSuccess() {
69 final GreAddDelTunnelReply reply = new GreAddDelTunnelReply();
70 reply.swIfIndex = IFACE_ID;
71 doReturn(future(reply)).when(api).greAddDelTunnel(any(GreAddDelTunnel.class));
74 private void whenGreAddDelTunnelThenFailure() {
75 doReturn(failedFuture()).when(api).greAddDelTunnel(any(GreAddDelTunnel.class));
78 private GreAddDelTunnel verifyGreAddDelTunnelWasInvoked(final Gre gre) throws VppInvocationException {
79 ArgumentCaptor<GreAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(GreAddDelTunnel.class);
80 verify(api).greAddDelTunnel(argumentCaptor.capture());
81 final GreAddDelTunnel actual = argumentCaptor.getValue();
82 assertEquals(0, actual.isIpv6);
83 assertArrayEquals(InetAddresses.forString(gre.getSrc().getIpv4Address().getValue()).getAddress(),
85 assertArrayEquals(InetAddresses.forString(gre.getDst().getIpv4Address().getValue()).getAddress(),
87 assertEquals(gre.getOuterFibId().intValue(), actual.outerFibId);
91 private void verifyGreAddWasInvoked(final Gre gre) throws VppInvocationException {
92 final GreAddDelTunnel actual = verifyGreAddDelTunnelWasInvoked(gre);
93 assertEquals(ADD_GRE, actual.isAdd);
96 private void verifyGreDeleteWasInvoked(final Gre gre) throws VppInvocationException {
97 final GreAddDelTunnel actual = verifyGreAddDelTunnelWasInvoked(gre);
98 assertEquals(DEL_GRE, actual.isAdd);
101 private static Gre generateGre() {
102 final GreBuilder builder = new GreBuilder();
103 builder.setSrc(new IpAddress(new Ipv4Address("192.168.20.10")));
104 builder.setDst(new IpAddress(new Ipv4Address("192.168.20.11")));
105 builder.setOuterFibId(Long.valueOf(123));
106 return builder.build();
110 public void testWriteCurrentAttributes() throws Exception {
111 final Gre gre = generateGre();
113 whenGreAddDelTunnelThenSuccess();
115 noMappingDefined(mappingContext, IFACE_NAME, IFC_TEST_INSTANCE);
117 customizer.writeCurrentAttributes(id, gre, writeContext);
118 verifyGreAddWasInvoked(gre);
119 verify(mappingContext).put(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)),
120 eq(mapping(IFACE_NAME, IFACE_ID).get()));
124 public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
125 final Gre gre = generateGre();
127 whenGreAddDelTunnelThenSuccess();
128 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_TEST_INSTANCE);
130 customizer.writeCurrentAttributes(id, gre, writeContext);
131 verifyGreAddWasInvoked(gre);
133 // Remove the first mapping before putting in the new one
134 verify(mappingContext).delete(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)));
135 verify(mappingContext).put(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)), eq(mapping(IFACE_NAME, IFACE_ID).get()));
139 public void testWriteCurrentAttributesFailed() throws Exception {
140 final Gre gre = generateGre();
142 whenGreAddDelTunnelThenFailure();
145 customizer.writeCurrentAttributes(id, gre, writeContext);
146 } catch (WriteFailedException.CreateFailedException e) {
147 assertTrue(e.getCause() instanceof VppBaseCallException);
148 verifyGreAddWasInvoked(gre);
149 // Mapping not stored due to failure
150 verify(mappingContext, times(0)).put(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)), eq(mapping(
151 IFACE_NAME, 0).get()));
154 fail("WriteFailedException.CreateFailedException was expected");
158 public void testUpdateCurrentAttributes() throws Exception {
160 customizer.updateCurrentAttributes(id, generateGre(), generateGre(), writeContext);
161 } catch (WriteFailedException.UpdateFailedException e) {
162 assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
165 fail("WriteFailedException.UpdateFailedException was expected");
169 public void testDeleteCurrentAttributes() throws Exception {
170 final Gre gre = generateGre();
172 whenGreAddDelTunnelThenSuccess();
173 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_TEST_INSTANCE);
175 customizer.deleteCurrentAttributes(id, gre, writeContext);
176 verifyGreDeleteWasInvoked(gre);
177 verify(mappingContext).delete(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)));
181 public void testDeleteCurrentAttributesaFailed() throws Exception {
182 final Gre gre = generateGre();
184 whenGreAddDelTunnelThenFailure();
185 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_TEST_INSTANCE);
188 customizer.deleteCurrentAttributes(id, gre, writeContext);
189 } catch (WriteFailedException.DeleteFailedException e) {
190 assertTrue(e.getCause() instanceof VppBaseCallException);
191 verifyGreDeleteWasInvoked(gre);
192 verify(mappingContext, times(0)).delete(eq(mappingIid(IFACE_NAME, IFC_TEST_INSTANCE)));
195 fail("WriteFailedException.DeleteFailedException was expected");