0792a0e1e9190d8954194a005273d029f9af98d6
[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 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;
29
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.rev161214.VppInterfaceAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VxlanGpeNextProtocol;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VxlanGpeVni;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VxlanGpe;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.VxlanGpeBuilder;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import io.fd.vpp.jvpp.VppBaseCallException;
50 import io.fd.vpp.jvpp.core.dto.VxlanGpeAddDelTunnel;
51 import io.fd.vpp.jvpp.core.dto.VxlanGpeAddDelTunnelReply;
52
53 public class VxlanGpeCustomizerTest extends WriterCustomizerTest {
54
55     private static final byte ADD_VXLAN_GPE = 1;
56     private static final byte DEL_VXLAN_GPE = 0;
57
58     @Mock
59     private DisabledInterfacesManager interfaceDisableContext;
60
61     private VxlanGpeCustomizer customizer;
62     private String ifaceName;
63     private InstanceIdentifier<VxlanGpe> id;
64
65     @Override
66     public void setUp() throws Exception {
67         InterfaceTypeTestUtils.setupWriteContext(writeContext,
68             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VxlanGpeTunnel.class);
69         NamingContext namingContext = new NamingContext("generateInterfaceName", "test-instance");
70         customizer = new VxlanGpeCustomizer(api, namingContext, interfaceDisableContext);
71
72         ifaceName = "elth0";
73         id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
74             .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
75     }
76
77     private void whenVxlanGpeAddDelTunnelThenSuccess() {
78         when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class)))
79             .thenReturn(future(new VxlanGpeAddDelTunnelReply()));
80     }
81
82     private void whenVxlanGpeAddDelTunnelThenFailure() {
83         doReturn(failedFuture()).when(api).vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
84     }
85
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(),
93             actual.local);
94         assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(),
95             actual.remote);
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);
100         return actual;
101     }
102
103     private void verifyVxlanGpeAddWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
104         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
105         assertEquals(ADD_VXLAN_GPE, actual.isAdd);
106     }
107
108     private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
109         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
110         assertEquals(DEL_VXLAN_GPE, actual.isAdd);
111     }
112
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();
122     }
123
124     private static VxlanGpe generateVxlanGpe() {
125         return generateVxlanGpe(Long.valueOf(11));
126     }
127
128     @Test
129     public void testWriteCurrentAttributes() throws Exception {
130         final VxlanGpe vxlanGpe = generateVxlanGpe();
131
132         whenVxlanGpeAddDelTunnelThenSuccess();
133         noMappingDefined(mappingContext, ifaceName, "test-instance");
134
135         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
136         verifyVxlanGpeAddWasInvoked(vxlanGpe);
137         verify(mappingContext).put(eq(mappingIid(ifaceName, "test-instance")), eq(
138             mapping(ifaceName, 0).get()));
139     }
140
141     @Test
142     public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
143         final VxlanGpe vxlanGpe = generateVxlanGpe();
144         final int ifaceId = 0;
145
146         whenVxlanGpeAddDelTunnelThenSuccess();
147         defineMapping(mappingContext, ifaceName, ifaceId, "test-instance");
148
149         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
150         verifyVxlanGpeAddWasInvoked(vxlanGpe);
151
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()));
156     }
157
158     @Test
159     public void testWriteCurrentAttributesFailed() throws Exception {
160         final VxlanGpe vxlanGpe = generateVxlanGpe();
161
162         whenVxlanGpeAddDelTunnelThenFailure();
163
164         try {
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()));
173             return;
174         }
175         fail("WriteFailedException.CreateFailedException was expected");
176     }
177
178     @Test
179     public void testUpdateCurrentAttributes() throws Exception {
180         try {
181             customizer.updateCurrentAttributes(id, generateVxlanGpe(10), generateVxlanGpe(11), writeContext);
182         } catch (WriteFailedException.UpdateFailedException e) {
183             assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
184             return;
185         }
186         fail("WriteFailedException.UpdateFailedException was expected");
187     }
188
189     @Test
190     public void testDeleteCurrentAttributes() throws Exception {
191         final VxlanGpe vxlanGpe = generateVxlanGpe();
192
193         whenVxlanGpeAddDelTunnelThenSuccess();
194         defineMapping(mappingContext, ifaceName, 1, "test-instance");
195
196         customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
197         verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
198         verify(mappingContext).delete(eq(mappingIid(ifaceName, "test-instance")));
199     }
200
201     @Test
202     public void testDeleteCurrentAttributesaFailed() throws Exception {
203         final VxlanGpe vxlanGpe = generateVxlanGpe();
204
205         whenVxlanGpeAddDelTunnelThenFailure();
206         defineMapping(mappingContext, ifaceName, 1, "test-instance");
207
208         try {
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")));
214             return;
215         }
216         fail("WriteFailedException.DeleteFailedException was expected");
217     }
218 }