HC2VPP-259: add support for ipv4 lookup 25/9525/3
authorMarek Gradzki <[email protected]>
Wed, 22 Nov 2017 13:02:58 +0000 (14:02 +0100)
committerMarek Gradzki <[email protected]>
Thu, 23 Nov 2017 12:17:34 +0000 (12:17 +0000)
Change-Id: Ic6a2da08a8839bb42366b124737bb5243671815b
Signed-off-by: Marek Gradzki <[email protected]>
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/Ipv4LookupWriter.java [new file with mode: 0644]
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/StaticLspCustomizer.java
mpls/impl/src/test/java/io/fd/hc2vpp/mpls/PopAndIpv4LookupTest.java [new file with mode: 0644]

diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/Ipv4LookupWriter.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/Ipv4LookupWriter.java
new file mode 100644 (file)
index 0000000..7ac164a
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2017 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.hc2vpp.mpls;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import io.fd.honeycomb.translate.MappingContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import io.fd.vpp.jvpp.core.dto.MplsRouteAddDel;
+import io.fd.vpp.jvpp.core.future.FutureJVppCore;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.StaticLspConfig;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp.Config;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.InSegment;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.in.segment.type.MplsLabel;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.routing.mpls._static.lsps.StaticLsp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.LookupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.StaticLspVppLookupAugmentation;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Translates {@link StaticLspConfig.Operation#PopAndLookup} operation with {@link LookupType#Ipv4} to
+ * mpls_route_add_del API.
+ *
+ * @see <a href="https://git.fd.io/vpp/tree/src/vnet/mpls/mpls.api">mpls_route_add_del</a> definition
+ */
+final class Ipv4LookupWriter implements LspWriter {
+    private static final byte IPV4_PROTOCOL = (byte) LookupType.Ipv4.getIntValue();
+
+    private final FutureJVppCore vppApi;
+
+    Ipv4LookupWriter(@Nonnull final FutureJVppCore vppApi) {
+        this.vppApi = vppApi;
+    }
+
+    @Override
+    public void write(@Nonnull final InstanceIdentifier<StaticLsp> id, @Nonnull final StaticLsp data,
+                      @Nonnull final MappingContext ctx, final boolean isAdd) throws WriteFailedException {
+        final Config config = data.getConfig();
+        final MplsRouteAddDel request = new MplsRouteAddDel();
+
+        request.mrIsAdd = booleanToByte(isAdd);
+
+        translate(request, config);
+        translate(request, config.getAugmentation(StaticLspVppLookupAugmentation.class));
+
+        // default values based on inspecting VPP's CLI and make test code
+        request.mrClassifyTableIndex = -1;
+        request.mrNextHopProto = IPV4_PROTOCOL;
+        request.mrNextHopWeight = 1;
+        request.mrNextHop = new byte[0]; // no next hop since we POP
+        request.mrNextHopOutLabelStack = new int[0]; // no new labels
+        request.mrNextHopSwIfIndex = -1;
+        request.mrNextHopViaLabel = MPLS_LABEL_INVALID;
+
+        getReplyForWrite(vppApi.mplsRouteAddDel(request).toCompletableFuture(), id);
+    }
+
+    private void translate(@Nonnull final MplsRouteAddDel request, @Nonnull final Config config) {
+        final InSegment inSegment = config.getInSegment();
+        checkArgument(inSegment != null, "Configuring ipv4 pop-and-lookup, but in-segment is missing.");
+
+        checkArgument(inSegment.getType() instanceof MplsLabel, "Expecting mpls-label in-segment type, but %s given.",
+            inSegment.getType());
+        final Long label = ((MplsLabel) inSegment.getType()).getIncomingLabel().getValue();
+        request.mrLabel = label.intValue();
+    }
+
+    private void translate(@Nonnull final MplsRouteAddDel request,
+                           @Nonnull final StaticLspVppLookupAugmentation vppLabelLookup) {
+        // IPv4 lookup should only happen if there is no more labels to process, so setting the EOS bit:
+        request.mrEos = 1;
+        final Long lookupInTable = vppLabelLookup.getLabelLookup().getIp4LookupInTable();
+        checkArgument(lookupInTable != null,
+            "Configuring pop and ipv4 lookup, but ipv4 lookup table was not given");
+        request.mrNextHopTableId = lookupInTable.intValue();
+    }
+}
index b9e48cd..54653b9 100644 (file)
@@ -46,12 +46,14 @@ final class StaticLspCustomizer implements ListWriterCustomizer<StaticLsp, Stati
     private static final Logger LOG = LoggerFactory.getLogger(StaticLspCustomizer.class);
     private final ImposeAndForwardWriter imposeAndForward;
     private final MplsLookupWriter mplsLookup;
+    private final Ipv4LookupWriter ipv4Lookup;
 
     StaticLspCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull NamingContext interfaceContext) {
         checkNotNull(vppApi, "vppApi should not be null");
         checkNotNull(interfaceContext, "interfaceContext should not be null");
         this.imposeAndForward = new ImposeAndForwardWriter(vppApi, interfaceContext);
         this.mplsLookup = new MplsLookupWriter(vppApi);
+        this.ipv4Lookup = new Ipv4LookupWriter(vppApi);
     }
 
     @Override
@@ -85,7 +87,9 @@ final class StaticLspCustomizer implements ListWriterCustomizer<StaticLsp, Stati
             checkArgument(vppAttributes != null && vppAttributes.getLabelLookup() != null,
                 "Configuring pop-and-lookup operation but label-lookup leaf is missing");
             final LookupType type = vppAttributes.getLabelLookup().getType();
-            if (LookupType.Mpls.equals(type)) {
+            if (LookupType.Ipv4.equals(type)) {
+                ipv4Lookup.write(id, data, ctx, isAdd);
+            } else if (LookupType.Mpls.equals(type)) {
                 mplsLookup.write(id, data, ctx, isAdd);
             } else {
                 throw new IllegalArgumentException("Unsupported lookup type: " + type);
diff --git a/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/PopAndIpv4LookupTest.java b/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/PopAndIpv4LookupTest.java
new file mode 100644 (file)
index 0000000..b76daaa
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2017 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.hc2vpp.mpls;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
+import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
+import io.fd.hc2vpp.common.translate.util.NamingContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import io.fd.vpp.jvpp.core.dto.MplsRouteAddDel;
+import io.fd.vpp.jvpp.core.dto.MplsRouteAddDelReply;
+import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.Mpls1;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.StaticLspConfig;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp.ConfigBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.InSegmentBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.in.segment.type.MplsLabelBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.routing.mpls.StaticLsps;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.routing.mpls._static.lsps.StaticLsp;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.routing.mpls._static.lsps.StaticLspBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.routing.mpls._static.lsps.StaticLspKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.Routing1;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.routing.Mpls;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.Routing;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.types.rev170227.MplsLabel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.LookupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.StaticLspVppLookupAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.StaticLspVppLookupAugmentationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.vpp.label.lookup.attributes.LabelLookupBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class PopAndIpv4LookupTest extends WriterCustomizerTest implements ByteDataTranslator {
+
+    private static final String IF_NAME = "local0";
+    private static final int IF_INDEX = 123;
+    private static final String LSP_NAME = "static-lsp0";
+
+    private static final InstanceIdentifier<StaticLsp> IID = InstanceIdentifier.create(Routing.class).augmentation
+        (Routing1.class).child(Mpls.class).augmentation(Mpls1.class).child(StaticLsps.class)
+        .child(StaticLsp.class, new StaticLspKey(LSP_NAME));
+
+    private static final StaticLsp POP_AND_IP4_LOOKUP = getStaticLsp();
+    private static final int IP4_TABLE_ID = 456;
+    private static final int LOCAL_LABEL = 104;
+
+    @Mock
+    private FutureJVppCoreFacade jvpp;
+    private StaticLspCustomizer customizer;
+
+    /**
+     * Equivalent of mpls local-label add eos 104 ip4-lookup-in-table 456
+     */
+    private static StaticLsp getStaticLsp() {
+        final StaticLsp data = new StaticLspBuilder()
+            .setName(LSP_NAME)
+            .setConfig(new ConfigBuilder()
+                .setInSegment(new InSegmentBuilder()
+                    .setType(new MplsLabelBuilder().setIncomingLabel(new MplsLabel((long) LOCAL_LABEL))
+                        .build())
+                    .build()
+                )
+                .setOperation(StaticLspConfig.Operation.PopAndLookup)
+                .addAugmentation(StaticLspVppLookupAugmentation.class,
+                    new StaticLspVppLookupAugmentationBuilder()
+                        .setLabelLookup(new LabelLookupBuilder()
+                            .setType(LookupType.Ipv4)
+                            .setIp4LookupInTable((long) IP4_TABLE_ID).build())
+                        .build())
+                .build())
+            .build();
+        return data;
+    }
+
+    @Override
+    public void setUpTest() {
+        final String ctxInstanceName = "test-ifc-context";
+        customizer = new StaticLspCustomizer(jvpp, new NamingContext("test-prefix", ctxInstanceName));
+        when(jvpp.mplsRouteAddDel(any())).thenReturn(future(new MplsRouteAddDelReply()));
+        defineMapping(mappingContext, IF_NAME, IF_INDEX, ctxInstanceName);
+    }
+
+    @Test
+    public void testWrite() throws WriteFailedException {
+        customizer.writeCurrentAttributes(IID, POP_AND_IP4_LOOKUP, writeContext);
+        verify(jvpp).mplsRouteAddDel(getRequestForSimpleLsp(true));
+    }
+
+    @Test
+    public void testDelete() throws WriteFailedException {
+        customizer.deleteCurrentAttributes(IID, POP_AND_IP4_LOOKUP, writeContext);
+        verify(jvpp).mplsRouteAddDel(getRequestForSimpleLsp(false));
+    }
+
+    private MplsRouteAddDel getRequestForSimpleLsp(final boolean add) {
+        MplsRouteAddDel request = new MplsRouteAddDel();
+        request.mrLabel = LOCAL_LABEL;
+        request.mrEos = 1;
+        request.mrNextHopTableId = IP4_TABLE_ID;
+        request.mrClassifyTableIndex = -1; // default value used in make test
+        request.mrIsAdd = booleanToByte(add);
+        request.mrNextHopProto = 0; // IPv4 data plane protocol index used by VPP
+        request.mrNextHopWeight = 1; // default value used in make test
+        request.mrNextHop = new byte[0]; // POP, so no next hop
+        request.mrNextHopSwIfIndex = -1; // this is what CLI is doing
+        request.mrNextHopViaLabel = LspWriter.MPLS_LABEL_INVALID; // default value used by make test
+        request.mrNextHopOutLabelStack = new int[0];
+        return request;
+    }
+}