2ad831f9f50d6ee9f45ef7d58efd17c3aef84d85
[hc2vpp.git] /
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.v3po.translate.v3po.interfaces;
18
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;
34
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;
69
70 public class VxlanGpeCustomizerTest {
71
72     private static final byte ADD_VXLAN_GPE = 1;
73     private static final byte DEL_VXLAN_GPE = 0;
74
75     @Mock
76     private FutureJVpp api;
77     @Mock
78     private WriteContext writeContext;
79     @Mock
80     private MappingContext mappingContext;
81
82     private VxlanGpeCustomizer customizer;
83     private String ifaceName;
84     private InstanceIdentifier<VxlanGpe> id;
85
86     @Before
87     public void setUp() throws Exception {
88         initMocks(this);
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();
96
97         customizer = new VxlanGpeCustomizer(api, namingContext);
98
99         ifaceName = "eth0";
100         id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
101                         .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
102     }
103
104     private void whenVxlanGpeAddDelTunnelThen() throws ExecutionException, InterruptedException, VppBaseCallException {
105         final CompletionStage<VxlanGpeAddDelTunnelReply> replyCS = mock(CompletionStage.class);
106         final CompletableFuture<VxlanGpeAddDelTunnelReply> replyFuture = mock(CompletableFuture.class);
107         when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
108         final VxlanGpeAddDelTunnelReply reply = new VxlanGpeAddDelTunnelReply();
109         when(replyFuture.get()).thenReturn(reply);
110         when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class))).thenReturn(replyCS);
111     }
112
113     private void whenVxlanGpeAddDelTunnelThenSuccess() throws ExecutionException, InterruptedException, VppBaseCallException {
114         whenVxlanGpeAddDelTunnelThen();
115     }
116
117     private void whenVxlanGpeAddDelTunnelThenFailure() throws ExecutionException, InterruptedException, VppBaseCallException {
118         whenVxlanGpeAddDelTunnelFailedThen(-1);
119     }
120
121     /**
122      * Failure response send
123      */
124     private void whenVxlanGpeAddDelTunnelFailedThen(final int retval) throws ExecutionException, InterruptedException, VppBaseCallException {
125         doReturn(TestHelperUtils.<VxlanAddDelTunnelReply>createFutureException(retval)).when(api).vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
126     }
127
128     private VxlanGpeAddDelTunnel verifyVxlanGpeAddDelTunnelWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
129         ArgumentCaptor<VxlanGpeAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(VxlanGpeAddDelTunnel.class);
130         verify(api).vxlanGpeAddDelTunnel(argumentCaptor.capture());
131         final VxlanGpeAddDelTunnel actual = argumentCaptor.getValue();
132         assertEquals(0, actual.isIpv6);
133         assertArrayEquals(InetAddresses.forString(vxlanGpe.getLocal().getIpv4Address().getValue()).getAddress(), actual.local);
134         assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(), actual.remote);
135         assertEquals(vxlanGpe.getVni().getValue().intValue(), actual.vni);
136         assertEquals(vxlanGpe.getNextProtocol().getIntValue(), actual.protocol);
137         assertEquals(vxlanGpe.getEncapVrfId().intValue(), actual.encapVrfId);
138         assertEquals(vxlanGpe.getDecapVrfId().intValue(), actual.decapVrfId);
139         return actual;
140     }
141     private void verifyVxlanGpeAddWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
142         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
143         assertEquals(ADD_VXLAN_GPE, actual.isAdd);
144     }
145
146     private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
147         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
148         assertEquals(DEL_VXLAN_GPE, actual.isAdd);
149     }
150
151     private static VxlanGpe generateVxlanGpe(long vni) {
152         final VxlanGpeBuilder builder = new VxlanGpeBuilder();
153         builder.setLocal(new IpAddress(new Ipv4Address("192.168.20.10")));
154         builder.setRemote(new IpAddress(new Ipv4Address("192.168.20.11")));
155         builder.setVni(new VxlanGpeVni(Long.valueOf(vni)));
156         builder.setNextProtocol(VxlanGpeNextProtocol.forValue(1));
157         builder.setEncapVrfId(Long.valueOf(123));
158         builder.setDecapVrfId(Long.valueOf(456));
159         return builder.build();
160     }
161
162     private static VxlanGpe generateVxlanGpe() {
163         return generateVxlanGpe(Long.valueOf(11));
164     }
165
166     @Test
167     public void testWriteCurrentAttributes() throws Exception {
168         final VxlanGpe vxlanGpe = generateVxlanGpe();
169
170         whenVxlanGpeAddDelTunnelThenSuccess();
171
172         doReturn(Optional.absent())
173             .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
174
175         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
176         verifyVxlanGpeAddWasInvoked(vxlanGpe);
177         verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get()));
178     }
179
180     @Test
181     public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
182         final VxlanGpe vxlanGpe = generateVxlanGpe();
183
184         whenVxlanGpeAddDelTunnelThenSuccess();
185         final Optional<Mapping> ifcMapping = getMapping(ifaceName, 0);
186
187         doReturn(Optional.of(new MappingsBuilder().setMapping(singletonList(ifcMapping.get())).build()))
188             .when(mappingContext).read(getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
189
190         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
191         verifyVxlanGpeAddWasInvoked(vxlanGpe);
192
193         // Remove the first mapping before putting in the new one
194         verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance")));
195         verify(mappingContext).put(eq(getMappingIid(ifaceName, "test-instance")), eq(ifcMapping.get()));
196     }
197
198     @Test
199     public void testWriteCurrentAttributesFailed() throws Exception {
200         final VxlanGpe vxlanGpe = generateVxlanGpe();
201
202         whenVxlanGpeAddDelTunnelThenFailure();
203
204         try {
205             customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
206         } catch (WriteFailedException.CreateFailedException e) {
207             assertTrue(e.getCause() instanceof VppBaseCallException);
208             verifyVxlanGpeAddWasInvoked(vxlanGpe);
209             // Mapping not stored due to failure
210             verify(mappingContext, times(0)).put(eq(getMappingIid(ifaceName, "test-instance")), eq(getMapping(ifaceName, 0).get()));
211             return;
212         }
213         fail("WriteFailedException.CreateFailedException was expected");
214     }
215
216     @Test
217     public void testUpdateCurrentAttributes() throws Exception {
218         try {
219             customizer.updateCurrentAttributes(id, generateVxlanGpe(10), generateVxlanGpe(11), writeContext);
220         } catch (WriteFailedException.UpdateFailedException e) {
221             assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
222             return;
223         }
224         fail("WriteFailedException.UpdateFailedException was expected");
225     }
226
227     @Test
228     public void testDeleteCurrentAttributes() throws Exception {
229         final VxlanGpe vxlanGpe = generateVxlanGpe();
230
231         whenVxlanGpeAddDelTunnelThenSuccess();
232         doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance"));
233
234         customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
235         verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
236         verify(mappingContext).delete(eq(getMappingIid(ifaceName, "test-instance")));
237     }
238
239     @Test
240     public void testDeleteCurrentAttributesaFailed() throws Exception {
241         final VxlanGpe vxlanGpe = generateVxlanGpe();
242
243         whenVxlanGpeAddDelTunnelThenFailure();
244         doReturn(getMapping(ifaceName, 1)).when(mappingContext).read(getMappingIid(ifaceName, "test-instance"));
245
246         try {
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")));
252             return;
253         }
254         fail("WriteFailedException.DeleteFailedException was expected");
255     }
256 }