HONEYCOMB-37: vxlan CRUD support
authorMarek Gradzki <[email protected]>
Mon, 9 May 2016 08:24:48 +0000 (10:24 +0200)
committerMarek Gradzki <[email protected]>
Mon, 9 May 2016 14:54:47 +0000 (14:54 +0000)
Change-Id: I4dad2a5766bd94ebdfdf6c302aa5054be5bc86ee
Signed-off-by: Marek Gradzki <[email protected]>
v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizer.java
v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizerTest.java [new file with mode: 0644]

index 2e53266..a4a840e 100644 (file)
@@ -59,7 +59,7 @@ public class VxlanCustomizer extends FutureJVppCustomizer implements ChildWriter
     @Override
     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id, @Nonnull final Vxlan dataAfter,
                                        @Nonnull final Context writeContext)
-        throws WriteFailedException.CreateFailedException {
+            throws WriteFailedException.CreateFailedException {
         try {
             createVxlanTunnel(id.firstKeyOf(Interface.class).getName(), dataAfter);
         } catch (VppApiInvocationException e) {
@@ -71,22 +71,26 @@ public class VxlanCustomizer extends FutureJVppCustomizer implements ChildWriter
     @Override
     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id, @Nonnull final Vxlan dataBefore,
                                         @Nonnull final Vxlan dataAfter, @Nonnull final Context writeContext)
-        throws WriteFailedException.UpdateFailedException {
+            throws WriteFailedException.UpdateFailedException {
 
-        // TODO handle update in a better way
-        try {
-            createVxlanTunnel(id.firstKeyOf(Interface.class).getName(), dataAfter);
-        } catch (VppApiInvocationException e) {
-            LOG.warn("Update of L2 failed", e);
-            throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
+        if (dataBefore.equals(dataAfter)) {
+            LOG.debug("dataBefore equals dataAfter, update will not be performed");
+            return;
         }
+        throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
+                new UnsupportedOperationException("Vxlan tunnel update is not supported"));
     }
 
     @Override
     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Vxlan> id, @Nonnull final Vxlan dataBefore,
-                                        @Nonnull final Context writeContext) {
-
-        // TODO handle delete
+                                        @Nonnull final Context writeContext)
+            throws WriteFailedException.DeleteFailedException {
+        try {
+            deleteVxlanTunnel(id.firstKeyOf(Interface.class).getName(), dataBefore);
+        } catch (VppApiInvocationException e) {
+            LOG.warn("Delete of Vxlan failed", e);
+            throw new WriteFailedException.DeleteFailedException(id, e);
+        }
     }
 
     private void createVxlanTunnel(final String swIfName, final Vxlan vxlan) throws VppApiInvocationException {
@@ -97,11 +101,11 @@ public class VxlanCustomizer extends FutureJVppCustomizer implements ChildWriter
 
         LOG.debug("Setting vxlan tunnel for interface: {}. Vxlan: {}", swIfName, vxlan);
         final CompletionStage<VxlanAddDelTunnelReply> vxlanAddDelTunnelReplyCompletionStage =
-            getFutureJVpp().vxlanAddDelTunnel(getVxlanTunnelRequest((byte) 1 /* is add */, srcAddress.getAddress(),
-                dstAddress.getAddress(), encapVrfId, -1, vni, (byte) 0 /* is IPV6 */));
+                getFutureJVpp().vxlanAddDelTunnel(getVxlanTunnelRequest((byte) 1 /* is add */, srcAddress.getAddress(),
+                        dstAddress.getAddress(), encapVrfId, -1, vni, (byte) 0 /* is IPV6 */));
 
         final VxlanAddDelTunnelReply reply =
-            V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture());
+                V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture());
         if (reply.retval < 0) {
             LOG.debug("Failed to set vxlan tunnel for interface: {}, vxlan: {}", swIfName, vxlan);
             throw new VppApiInvocationException("vxlanAddDelTunnel", reply.context, reply.retval);
@@ -112,6 +116,30 @@ public class VxlanCustomizer extends FutureJVppCustomizer implements ChildWriter
         }
     }
 
+    private void deleteVxlanTunnel(final String swIfName, final Vxlan vxlan) throws VppApiInvocationException {
+        final InetAddress srcAddress = InetAddresses.forString(vxlan.getSrc().getValue());
+        final InetAddress dstAddress = InetAddresses.forString(vxlan.getDst().getValue());
+        int encapVrfId = vxlan.getEncapVrfId().intValue();
+        int vni = vxlan.getVni().getValue().intValue();
+
+        LOG.debug("Deleting vxlan tunnel for interface: {}. Vxlan: {}", swIfName, vxlan);
+        final CompletionStage<VxlanAddDelTunnelReply> vxlanAddDelTunnelReplyCompletionStage =
+                getFutureJVpp().vxlanAddDelTunnel(getVxlanTunnelRequest((byte) 0 /* is add */, srcAddress.getAddress(),
+                        dstAddress.getAddress(), encapVrfId, -1, vni, (byte) 0 /* is IPV6 */));
+
+        final VxlanAddDelTunnelReply reply =
+                V3poUtils.getReply(vxlanAddDelTunnelReplyCompletionStage.toCompletableFuture());
+        if (reply.retval < 0) {
+            LOG.debug("Failed to delete vxlan tunnel for interface: {}, vxlan: {}", swIfName, vxlan);
+            throw new VppApiInvocationException("vxlanAddDelTunnel", reply.context, reply.retval);
+        } else {
+            LOG.debug("Vxlan tunnel deleted successfully for: {}, vxlan: {}", swIfName, vxlan);
+            // Remove interface to our interface context
+            interfaceContext.removeName(swIfName);
+        }
+    }
+
+
     private VxlanAddDelTunnel getVxlanTunnelRequest(final byte isAdd, final byte[] srcAddr, final byte[] dstAddr,
                                                     final int encapVrfId,
                                                     final int decapNextIndex, final int vni, final byte isIpv6) {
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/VxlanCustomizerTest.java
new file mode 100644 (file)
index 0000000..2f1a9e6
--- /dev/null
@@ -0,0 +1,210 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.v3po.translate.v3po.interfaces;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+import com.google.common.net.InetAddresses;
+import io.fd.honeycomb.v3po.translate.Context;
+import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
+import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
+import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.concurrent.ExecutionException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanVni;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Vxlan;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.openvpp.jvpp.dto.VxlanAddDelTunnel;
+import org.openvpp.jvpp.dto.VxlanAddDelTunnelReply;
+import org.openvpp.jvpp.future.FutureJVpp;
+
+public class VxlanCustomizerTest {
+
+    private static final byte ADD_VXLAN = 1;
+    private static final byte DEL_VXLAN = 0;
+
+    @Mock
+    private FutureJVpp api;
+    @Mock
+    private Context ctx;
+
+    private VxlanCustomizer customizer;
+    private NamingContext namingContext;
+    private String ifaceName;
+    private InstanceIdentifier<Vxlan> id;
+
+    @Before
+    public void setUp() throws Exception {
+        initMocks(this);
+        // TODO create base class for tests using vppApi
+        namingContext = new NamingContext("generateInterfaceNAme");
+        customizer = new VxlanCustomizer(api, namingContext);
+
+        ifaceName = "eth0";
+        id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
+                        .augmentation(VppInterfaceAugmentation.class).child(Vxlan.class);
+    }
+
+    private void whenVxlanAddDelTunnelThen(final int retval) throws ExecutionException, InterruptedException {
+        final CompletionStage<VxlanAddDelTunnelReply> replyCS = mock(CompletionStage.class);
+        final CompletableFuture<VxlanAddDelTunnelReply> replyFuture = mock(CompletableFuture.class);
+        when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
+        final VxlanAddDelTunnelReply reply = new VxlanAddDelTunnelReply();
+        reply.retval = retval;
+        when(replyFuture.get()).thenReturn(reply);
+        when(api.vxlanAddDelTunnel(any(VxlanAddDelTunnel.class))).thenReturn(replyCS);
+    }
+
+    private void whenVxlanAddDelTunnelThenSuccess() throws ExecutionException, InterruptedException {
+        whenVxlanAddDelTunnelThen(0);
+    }
+
+    private void whenVxlanAddDelTunnelThenFailure() throws ExecutionException, InterruptedException {
+        whenVxlanAddDelTunnelThen(-1);
+    }
+
+    private VxlanAddDelTunnel verifyVxlanAddDelTunnelWasInvoked(final Vxlan vxlan) {
+        ArgumentCaptor<VxlanAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(VxlanAddDelTunnel.class);
+        verify(api).vxlanAddDelTunnel(argumentCaptor.capture());
+        final VxlanAddDelTunnel actual = argumentCaptor.getValue();
+        assertEquals(0, actual.isIpv6);
+        assertEquals(-1, actual.decapNextIndex);
+        assertArrayEquals(InetAddresses.forString(vxlan.getSrc().getValue()).getAddress(), actual.srcAddress);
+        assertArrayEquals(InetAddresses.forString(vxlan.getDst().getValue()).getAddress(), actual.dstAddress);
+        assertEquals(vxlan.getEncapVrfId().intValue(), actual.encapVrfId);
+        assertEquals(vxlan.getVni().getValue().intValue(), actual.vni);
+        return actual;
+    }
+    private void verifyVxlanAddWasInvoked(final Vxlan vxlan) {
+        final VxlanAddDelTunnel actual = verifyVxlanAddDelTunnelWasInvoked(vxlan);
+        assertEquals(ADD_VXLAN, actual.isAdd);
+    }
+
+    private void verifyVxlanDeleteWasInvoked(final Vxlan vxlan) {
+        final VxlanAddDelTunnel actual = verifyVxlanAddDelTunnelWasInvoked(vxlan);
+        assertEquals(DEL_VXLAN, actual.isAdd);
+    }
+
+    private static Vxlan generateVxlan(long vni) {
+        final VxlanBuilder builder = new VxlanBuilder();
+        builder.setSrc(new Ipv4Address("192.168.20.10"));
+        builder.setDst(new Ipv4Address("192.168.20.11"));
+        builder.setEncapVrfId(Long.valueOf(123));
+        builder.setVni(new VxlanVni(Long.valueOf(vni)));
+        return builder.build();
+    }
+
+    private static Vxlan generateVxlan() {
+        return generateVxlan(Long.valueOf(11));
+    }
+
+    @Test
+    public void testWriteCurrentAttributes() throws Exception {
+        final Vxlan vxlan = generateVxlan();
+
+        whenVxlanAddDelTunnelThenSuccess();
+
+        customizer.writeCurrentAttributes(id, vxlan, ctx);
+        verifyVxlanAddWasInvoked(vxlan);
+        assertTrue(namingContext.containsIndex(ifaceName));
+    }
+
+    @Test
+    public void testWriteCurrentAttributesFailed() throws Exception {
+        final Vxlan vxlan = generateVxlan();
+
+        whenVxlanAddDelTunnelThenFailure();
+
+        try {
+            customizer.writeCurrentAttributes(id, vxlan, ctx);
+        } catch (WriteFailedException.CreateFailedException e) {
+            assertEquals(VppApiInvocationException.class, e.getCause().getClass());
+            verifyVxlanAddWasInvoked(vxlan);
+            assertFalse(namingContext.containsIndex(ifaceName));
+            return;
+        }
+        fail("WriteFailedException.CreateFailedException was expected");
+    }
+
+    @Test
+    public void testUpdateCurrentAttributes() throws Exception {
+        try {
+            customizer.updateCurrentAttributes(id, generateVxlan(10), generateVxlan(11), ctx);
+        } catch (WriteFailedException.UpdateFailedException e) {
+            assertEquals(UnsupportedOperationException.class, e.getCause().getClass());
+            return;
+        }
+        fail("WriteFailedException.UpdateFailedException was expected");
+    }
+
+    @Test
+    public void testUpdateCurrentAttributesNoUpdate() throws Exception {
+        customizer.updateCurrentAttributes(id, generateVxlan(), generateVxlan(), ctx);
+        verify(api, never()).vxlanAddDelTunnel(any(VxlanAddDelTunnel.class));
+    }
+
+    @Test
+    public void testDeleteCurrentAttributes() throws Exception {
+        final Vxlan vxlan = generateVxlan();
+
+        whenVxlanAddDelTunnelThenSuccess();
+        namingContext.addName(1, ifaceName);
+
+        customizer.deleteCurrentAttributes(id, vxlan, ctx);
+        verifyVxlanDeleteWasInvoked(vxlan);
+        assertFalse(namingContext.containsIndex(ifaceName));
+    }
+
+    @Test
+    public void testDeleteCurrentAttributesaFailed() throws Exception {
+        final Vxlan vxlan = generateVxlan();
+
+        whenVxlanAddDelTunnelThenFailure();
+        namingContext.addName(1, ifaceName);
+
+        try {
+            customizer.deleteCurrentAttributes(id, vxlan, ctx);
+        } catch (WriteFailedException.DeleteFailedException e) {
+            assertEquals(VppApiInvocationException.class, e.getCause().getClass());
+            verifyVxlanDeleteWasInvoked(vxlan);
+            assertTrue(namingContext.containsIndex(ifaceName));
+            return;
+        }
+        fail("WriteFailedException.DeleteFailedException was expected");
+    }
+}
\ No newline at end of file