HC2VPP-259: add support for mpls lookup 14/9514/3
authorMarek Gradzki <[email protected]>
Tue, 21 Nov 2017 12:08:44 +0000 (13:08 +0100)
committerMarek Gradzki <[email protected]>
Thu, 23 Nov 2017 12:17:34 +0000 (12:17 +0000)
Translates pop-and-lookup operation of
/hc2vpp-ietf-routing:routing/hc2vpp-ietf-mpl:mpls/
hc2vpp-ietf-mpls-static:static-lsps/static-lsp

to mpls_route_add_del call.

Configuring mpls lookup-type,
as defined in vpp-mpls yang module,
is required.

Other lookup types (ipv4/ipv6) are not supported yet.

Change-Id: If0580c6db69ee6c5f22a32bde031dbb6f852420f
Signed-off-by: Marek Gradzki <[email protected]>
mpls/impl/asciidoc/Readme.adoc
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/ImposeAndForwardWriter.java [new file with mode: 0644]
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/LspWriter.java [new file with mode: 0644]
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsLookupWriter.java [new file with mode: 0644]
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java
mpls/impl/src/main/java/io/fd/hc2vpp/mpls/StaticLspCustomizer.java
mpls/impl/src/test/java/io/fd/hc2vpp/mpls/ImposeAndForwardTest.java [moved from mpls/impl/src/test/java/io/fd/hc2vpp/mpls/StaticLspCustomizerTest.java with 97% similarity]
mpls/impl/src/test/java/io/fd/hc2vpp/mpls/PopAndMplsLookupTest.java [new file with mode: 0644]

index 064dd15..a88807b 100644 (file)
@@ -11,7 +11,7 @@ to
 
 sw_interface_set_mpls_enable.
 
-== Pushing MPLS labels
+== Label push
 
 Translates impose-and-forward operation of
 /hc2vpp-ietf-routing:routing/hc2vpp-ietf-mpl:mpls/
@@ -21,4 +21,15 @@ to ip_add_del_route call.
 
 For single outgoing-label, use simple-path out-segment.
 For multiple outgoing labels, use path-list out-segment
-with single path (multiple path support is not implemented yet).
\ No newline at end of file
+with single path (multiple path support is not implemented yet).
+
+== Label pop
+
+Translates pop-and-lookup operation of
+/hc2vpp-ietf-routing:routing/hc2vpp-ietf-mpl:mpls/
+hc2vpp-ietf-mpls-static:static-lsps/static-lsp
+
+to mpls_route_add_del call.
+
+Configuring VPP specific lookup options
+defined in vpp-mpls yang module is required.
diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/ImposeAndForwardWriter.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/ImposeAndForwardWriter.java
new file mode 100644 (file)
index 0000000..b7ac967
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * 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.hc2vpp.common.translate.util.Ipv4Translator;
+import io.fd.hc2vpp.common.translate.util.NamingContext;
+import io.fd.honeycomb.translate.MappingContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
+import io.fd.vpp.jvpp.core.future.FutureJVppCore;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.StaticLspConfig.Operation;
+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.OutSegment;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.in.segment.type.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.out.segment.PathList;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.out.segment.SimplePath;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.out.segment.path.list.Paths;
+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.routing.types.rev170227.MplsLabel;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Translates {@link Operation#ImposeAndForward ImposeAndForward} operation to ip_add_del_route API.
+ *
+ * @see <a href="https://git.fd.io/vpp/tree/src/vnet/ip/ip.api">ip_add_del_route</a> definition
+ */
+final class ImposeAndForwardWriter implements LspWriter, Ipv4Translator {
+    private final FutureJVppCore vppApi;
+    private final NamingContext interfaceContext;
+
+    ImposeAndForwardWriter(@Nonnull final FutureJVppCore vppApi, @Nonnull final NamingContext interfaceContext) {
+        this.vppApi = vppApi;
+        this.interfaceContext = interfaceContext;
+    }
+
+    @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 IpAddDelRoute request = new IpAddDelRoute();
+        request.isAdd = booleanToByte(isAdd);
+        request.nextHopWeight = 1; // default value used in make test
+
+        final InSegment inSegment = config.getInSegment();
+        checkArgument(inSegment != null, "Configuring impose-and-forward, but in-segment is missing.");
+
+        checkArgument(inSegment.getType() instanceof IpPrefix, "Only ip-prefix type is supported, but %s given.",
+            inSegment.getType());
+        final Ipv4Prefix prefix = ((IpPrefix) inSegment.getType()).getIpPrefix().getIpv4Prefix();
+
+        // TODO(HC2VPP-264): add support for mpls + v6
+        request.dstAddressLength = extractPrefix(prefix);
+        request.dstAddress = ipv4AddressPrefixToArray(prefix);
+
+        final OutSegment outSegment = config.getOutSegment();
+        checkArgument(outSegment != null, "Configuring impose-and-forward, but out-segment is missing.");
+        translateOutSegment(outSegment, request, ctx);
+        getReplyForWrite(vppApi.ipAddDelRoute(request).toCompletableFuture(), id);
+    }
+
+    private void translateOutSegment(@Nonnull final OutSegment outSegment, @Nonnull final IpAddDelRoute request,
+                                     @Nonnull final MappingContext ctx) {
+        final String outgoingInterface;
+        if (outSegment instanceof SimplePath) {
+            final SimplePath path = (SimplePath) outSegment;
+            outgoingInterface = path.getOutgoingInterface();
+            final IpAddress nextHop = path.getNextHop();
+            checkArgument(nextHop != null, "Configuring impose-and-forward, but next-hop is missing.");
+            // TODO(HC2VPP-264): add support for mpls + v6
+            checkArgument(nextHop.getIpv4Address() != null, "Only IPv4 next-hop address is supported.");
+            request.nextHopAddress = ipv4AddressNoZoneToArray(nextHop.getIpv4Address().getValue());
+            request.nextHopNOutLabels = 1;
+            checkArgument(path.getOutgoingLabel() != null,
+                "Configuring impose-and-forward, but outgoing-label is missing.");
+            request.nextHopOutLabelStack = new int[] {path.getOutgoingLabel().getValue().intValue()};
+        } else if (outSegment instanceof PathList) {
+            final PathList pathList = (PathList) outSegment;
+            checkArgument(pathList.getPaths() != null && pathList.getPaths().size() == 1,
+                "Only single path is supported");
+            final Paths paths = pathList.getPaths().get(0);
+            outgoingInterface = paths.getOutgoingInterface();
+            // TODO(HC2VPP-264): add support for mpls + v6
+            final IpAddress nextHop = paths.getNextHop();
+            checkArgument(nextHop != null, "Configuring impose-and-forward, but next-hop is missing.");
+            checkArgument(nextHop.getIpv4Address() != null, "Only IPv4 next-hop address is supported.");
+            request.nextHopAddress = ipv4AddressNoZoneToArray(nextHop.getIpv4Address().getValue());
+            final List<MplsLabel> outgoingLabels = paths.getOutgoingLabels();
+            final int numberOfLabels = outgoingLabels.size();
+            checkArgument(numberOfLabels > 0 && numberOfLabels < MAX_LABELS,
+                "Number of labels (%s) not in range (0, %s].", numberOfLabels, MAX_LABELS, numberOfLabels);
+            request.nextHopNOutLabels = (byte) numberOfLabels;
+            request.nextHopOutLabelStack =
+                outgoingLabels.stream().mapToInt(label -> label.getValue().intValue()).toArray();
+        } else {
+            throw new IllegalArgumentException("Unsupported out-segment type: " + outSegment);
+        }
+
+        checkArgument(outgoingInterface != null, "Configuring impose-and-forward, but outgoing-interface is missing.");
+        request.nextHopSwIfIndex = interfaceContext.getIndex(outgoingInterface, ctx);
+        request.nextHopViaLabel = MPLS_LABEL_INVALID;
+    }
+}
diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/LspWriter.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/LspWriter.java
new file mode 100644 (file)
index 0000000..0e68f0c
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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 io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
+import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
+import io.fd.honeycomb.translate.MappingContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.routing.mpls._static.lsps.StaticLsp;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Translates MPLS operations to VPP API calls.
+ */
+interface LspWriter extends ByteDataTranslator, JvppReplyConsumer {
+    /**
+     * Maximum number of MPLS labels supported by VPP.
+     *
+     * @see <a href="https://git.fd.io/vpp/tree/src/vnet/ip/ip.api">ip_add_del_route</a> definition
+     * @see <a href="https://git.fd.io/vpp/tree/src/vnet/mpls/mpls.api">mpls_route_add_del</a> definition
+     */
+    int MAX_LABELS = 255;
+
+    /**
+     * Constant used by VPP to disable optional parameters of mpls label type.
+     */
+    int MPLS_LABEL_INVALID = 0x100000;
+
+    /**
+     * Translates {@link StaticLsp} to jVpp requests and writes the configuration to VPP.
+     *
+     * @param id    identifier of data being written
+     * @param data  lsp to be written
+     * @param ctx   persisted storage where mapping matadata are stored
+     * @param isAdd determines if the write is create or delete operation
+     * @throws WriteFailedException if write was unsuccessful
+     */
+    void write(@Nonnull final InstanceIdentifier<StaticLsp> id, @Nonnull final StaticLsp data,
+               @Nonnull final MappingContext ctx, final boolean isAdd) throws WriteFailedException;
+}
diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsLookupWriter.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsLookupWriter.java
new file mode 100644 (file)
index 0000000..c845c7a
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * 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#Mpls} 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 MplsLookupWriter implements LspWriter {
+    private static final byte MPLS_PROTOCOL = (byte) LookupType.Mpls.getIntValue();
+
+    private final FutureJVppCore vppApi;
+
+    MplsLookupWriter(@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 = MPLS_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 mpls 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) {
+        // MPLS lookup for the last label is not valid operation (there is no next label to lookup),
+        // so match only labels without EOS bit set:
+        request.mrEos = 0;
+        final Long mplsLookupInTable = vppLabelLookup.getLabelLookup().getMplsLookupInTable();
+        checkArgument(mplsLookupInTable != null,
+            "Configuring pop and mpls lookup, but MPLS lookup table was not given");
+        request.mrNextHopTableId = mplsLookupInTable.intValue();
+    }
+}
index 014fd26..918cc5c 100644 (file)
@@ -37,22 +37,24 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.Interface;
 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.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.StaticLspVppLookupAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.mpls.rev171120.vpp.label.lookup.attributes.LabelLookup;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 final class MplsWriterFactory implements WriterFactory {
     private static final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface>
-            IFC_ID =
-            InstanceIdentifier.create(Interfaces.class).child(
-                    org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class);
+        IFC_ID =
+        InstanceIdentifier.create(Interfaces.class).child(
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class);
 
     private static final InstanceIdentifier<Routing> ROUTING_ID = InstanceIdentifier.create(Routing.class);
     private static final InstanceIdentifier<Mpls> MPLS_ID = ROUTING_ID.augmentation(Routing1.class).child(Mpls.class);
     private static final InstanceIdentifier<Interface> INTERFACE_ID = MPLS_ID.child(Interface.class);
 
     private static final InstanceIdentifier<StaticLsp> STATIC_LSP_ID =
-            MPLS_ID.augmentation(Mpls1.class).child(StaticLsps.class).child(StaticLsp.class);
+        MPLS_ID.augmentation(Mpls1.class).child(StaticLsps.class).child(StaticLsp.class);
     private static final InstanceIdentifier<Config> CONFIG_ID = InstanceIdentifier.create(StaticLsp.class).child
-            (Config.class);
+        (Config.class);
 
     @Inject
     @Named("interface-context")
@@ -77,12 +79,12 @@ final class MplsWriterFactory implements WriterFactory {
         // /ietf-routing:routing/ietf-mpls:mpls/interface
         // First enable MPLS on interface, then configure it:
         registry.subtreeAddAfter(
-                ImmutableSet.of(
-                        CONFIG_ID,
-                        InstanceIdentifier.create(StaticLsp.class).child(Config.class).child(InSegment.class),
-                        InstanceIdentifier.create(StaticLsp.class).child(Config.class).child(Paths.class)
-                        ),
-                new GenericWriter<>(STATIC_LSP_ID, new StaticLspCustomizer(vppApi, ifcContext)),
-                INTERFACE_ID);
+            ImmutableSet
+                .of(CONFIG_ID, InstanceIdentifier.create(StaticLsp.class).child(Config.class).child(InSegment.class),
+                    InstanceIdentifier.create(StaticLsp.class).child(Config.class).child(Paths.class),
+                    InstanceIdentifier.create(StaticLsp.class).child(Config.class)
+                        .augmentation(StaticLspVppLookupAugmentation.class).child(LabelLookup.class)),
+            new GenericWriter<>(STATIC_LSP_ID, new StaticLspCustomizer(vppApi, ifcContext)),
+            INTERFACE_ID);
     }
 }
index d29e866..b9e48cd 100644 (file)
@@ -19,59 +19,39 @@ package io.fd.hc2vpp.mpls;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.StaticLspConfig.Operation.ImposeAndForward;
+import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310.StaticLspConfig.Operation.PopAndLookup;
 
-import com.google.common.annotations.VisibleForTesting;
-import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
-import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
-import io.fd.hc2vpp.common.translate.util.Ipv4Translator;
-import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
 import io.fd.hc2vpp.common.translate.util.NamingContext;
 import io.fd.honeycomb.translate.MappingContext;
 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
 import io.fd.honeycomb.translate.write.WriteContext;
 import io.fd.honeycomb.translate.write.WriteFailedException;
-import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
-import java.util.List;
 import javax.annotation.Nonnull;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
 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.OutSegment;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.in.segment.type.IpPrefix;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.out.segment.PathList;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.out.segment.SimplePath;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls._static.rev170310._static.lsp_config.out.segment.path.list.Paths;
 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.StaticLspKey;
-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.VppLabelLookupAttributes;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-final class StaticLspCustomizer extends FutureJVppCustomizer implements
-    ListWriterCustomizer<StaticLsp, StaticLspKey>, JvppReplyConsumer, ByteDataTranslator, Ipv4Translator {
+/**
+ * Selects specific {@link LspWriter} based on {@link StaticLspConfig.Operation} and {@link LookupType}.
+ */
+final class StaticLspCustomizer implements ListWriterCustomizer<StaticLsp, StaticLspKey>, LspWriter {
     private static final Logger LOG = LoggerFactory.getLogger(StaticLspCustomizer.class);
-
-    /**
-     * Maximum number of MPLS labels supported by VPP. Value is based on type of next_hop_n_out_labels value which is
-     * u8.
-     */
-    private static final int MAX_LABELS = 255;
-
-    /**
-     * Constant used by VPP to disable optional parameters of mpls label type.
-     */
-    @VisibleForTesting
-    static final int MPLS_LABEL_INVALID = 0x100000;
-
-    private final NamingContext interfaceContext;
+    private final ImposeAndForwardWriter imposeAndForward;
+    private final MplsLookupWriter mplsLookup;
 
     StaticLspCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull NamingContext interfaceContext) {
-        super(vppApi);
-        this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
+        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);
     }
 
     @Override
@@ -79,7 +59,7 @@ final class StaticLspCustomizer extends FutureJVppCustomizer implements
                                        @Nonnull final StaticLsp dataAfter,
                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
         LOG.debug("Adding MPLS LSP: {}", dataAfter);
-        imposeAndForward(id, validateConfig(dataAfter), writeContext, true);
+        write(id, dataAfter, writeContext.getMappingContext(), true);
         LOG.debug("MPLS LSP successfully configured: {}", dataAfter);
     }
 
@@ -88,82 +68,30 @@ final class StaticLspCustomizer extends FutureJVppCustomizer implements
                                         @Nonnull final StaticLsp dataBefore,
                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
         LOG.debug("Removing MPLS LSP: {}", dataBefore);
-        imposeAndForward(id, validateConfig(dataBefore), writeContext, false);
+        write(id, dataBefore, writeContext.getMappingContext(), false);
         LOG.debug("MPLS LSP successfully removed: {}", dataBefore);
     }
 
-    private static Config validateConfig(@Nonnull final StaticLsp data) {
+    @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();
         checkArgument(config != null, "Config node of static-lsp is missing.");
         final StaticLspConfig.Operation operation = config.getOperation();
-        checkArgument(ImposeAndForward.equals(operation),
-            "Only impose-and-forward operation is supported, but %s given.", operation);
-        return config;
-    }
-
-    private void imposeAndForward(@Nonnull final InstanceIdentifier<StaticLsp> id, @Nonnull final Config lspConfig,
-                                  @Nonnull final WriteContext writeContext, final boolean isAdd)
-        throws WriteFailedException {
-        final IpAddDelRoute request = new IpAddDelRoute();
-        request.isAdd = booleanToByte(isAdd);
-        request.nextHopWeight = 1; // default value used in make test
-
-        final InSegment inSegment = lspConfig.getInSegment();
-        checkArgument(inSegment != null, "Configuring impose-and-forward, but in-segment is missing.");
-
-        checkArgument(inSegment.getType() instanceof IpPrefix, "Only ip-prefix type is supported, but %s given.",
-            inSegment.getType());
-        final Ipv4Prefix prefix = ((IpPrefix) inSegment.getType()).getIpPrefix().getIpv4Prefix();
-
-        // TODO(HC2VPP-264): add support for mpls + v6
-        request.dstAddressLength = extractPrefix(prefix);
-        request.dstAddress = ipv4AddressPrefixToArray(prefix);
-
-        final OutSegment outSegment = lspConfig.getOutSegment();
-        checkArgument(outSegment != null, "Configuring impose-and-forward, but out-segment is missing.");
-        translateOutSegment(outSegment, request, writeContext.getMappingContext());
-        getReplyForWrite(getFutureJVpp().ipAddDelRoute(request).toCompletableFuture(), id);
-    }
-
-    private void translateOutSegment(@Nonnull final OutSegment outSegment, @Nonnull final IpAddDelRoute request,
-                                     @Nonnull final MappingContext mappingContext) {
-        String outgoingInterface = null;
-        if (outSegment instanceof SimplePath) {
-            final SimplePath path = (SimplePath) outSegment;
-            outgoingInterface = path.getOutgoingInterface();
-            final IpAddress nextHop = path.getNextHop();
-            checkArgument(nextHop != null, "Configuring impose-and-forward, but next-hop is missing.");
-            // TODO(HC2VPP-264): add support for mpls + v6
-            checkArgument(nextHop.getIpv4Address() != null, "Only IPv4 next-hop address is supported.");
-            request.nextHopAddress = ipv4AddressNoZoneToArray(nextHop.getIpv4Address().getValue());
-            request.nextHopNOutLabels = 1;
-            checkArgument(path.getOutgoingLabel() != null,
-                "Configuring impose-and-forward, but outgoing-label is missing.");
-            request.nextHopOutLabelStack = new int[] {path.getOutgoingLabel().getValue().intValue()};
-        } else if (outSegment instanceof PathList) {
-            final PathList pathList = (PathList) outSegment;
-            checkArgument(pathList.getPaths() != null && pathList.getPaths().size() == 1,
-                "Only single path is supported");
-            final Paths paths = pathList.getPaths().get(0);
-            outgoingInterface = paths.getOutgoingInterface();
-            // TODO(HC2VPP-264): add support for mpls + v6
-            final IpAddress nextHop = paths.getNextHop();
-            checkArgument(nextHop != null, "Configuring impose-and-forward, but next-hop is missing.");
-            checkArgument(nextHop.getIpv4Address() != null, "Only IPv4 next-hop address is supported.");
-            request.nextHopAddress = ipv4AddressNoZoneToArray(nextHop.getIpv4Address().getValue());
-            final List<MplsLabel> outgoingLabels = paths.getOutgoingLabels();
-            final int numberOfLabels = outgoingLabels.size();
-            checkArgument(numberOfLabels > 0 && numberOfLabels < MAX_LABELS,
-                "Number of labels (%s) not in range (0, %s].", numberOfLabels, MAX_LABELS, numberOfLabels);
-            request.nextHopNOutLabels = (byte) numberOfLabels;
-            request.nextHopOutLabelStack =
-                outgoingLabels.stream().mapToInt(label -> label.getValue().intValue()).toArray();
+        if (ImposeAndForward.equals(operation)) {
+            imposeAndForward.write(id, data, ctx, isAdd);
+        } else if (PopAndLookup.equals(operation)) {
+            final VppLabelLookupAttributes vppAttributes = config.getAugmentation(StaticLspVppLookupAugmentation.class);
+            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)) {
+                mplsLookup.write(id, data, ctx, isAdd);
+            } else {
+                throw new IllegalArgumentException("Unsupported lookup type: " + type);
+            }
         } else {
-            throw new IllegalArgumentException("Unsupported out-segment type: " + outSegment);
+            throw new IllegalArgumentException("Unsupported operation: " + operation);
         }
-
-        checkArgument(outgoingInterface != null, "Configuring impose-and-forward, but outgoing-interface is missing.");
-        request.nextHopSwIfIndex = interfaceContext.getIndex(outgoingInterface, mappingContext);
-        request.nextHopViaLabel = MPLS_LABEL_INVALID;
     }
 }
@@ -51,7 +51,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.types.rev170227.MplsLabel;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-public class StaticLspCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
+public class ImposeAndForwardTest extends WriterCustomizerTest implements ByteDataTranslator {
 
     private static final String IF_NAME = "local0";
     private static final int IF_INDEX = 123;
@@ -151,7 +151,7 @@ public class StaticLspCustomizerTest extends WriterCustomizerTest implements Byt
         request.dstAddress = new byte[] {1, 2, 3, 4};
         request.nextHopAddress = new byte[] {5, 6, 7, 8};
         request.nextHopNOutLabels = 1;
-        request.nextHopViaLabel = StaticLspCustomizer.MPLS_LABEL_INVALID;
+        request.nextHopViaLabel = LspWriter.MPLS_LABEL_INVALID;
         request.nextHopOutLabelStack = new int[] {111};
         return request;
     }
@@ -165,7 +165,7 @@ public class StaticLspCustomizerTest extends WriterCustomizerTest implements Byt
         request.dstAddress = new byte[] {10, 10, 24, 0};
         request.nextHopAddress = new byte[] {10, 10, 12, 2};
         request.nextHopNOutLabels = 2;
-        request.nextHopViaLabel = StaticLspCustomizer.MPLS_LABEL_INVALID;
+        request.nextHopViaLabel = LspWriter.MPLS_LABEL_INVALID;
         request.nextHopOutLabelStack = new int[] {102, 104};
         return request;
     }
diff --git a/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/PopAndMplsLookupTest.java b/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/PopAndMplsLookupTest.java
new file mode 100644 (file)
index 0000000..ef97109
--- /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 PopAndMplsLookupTest 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_MPLS_LOOKUP = getStaticLsp();
+    private static final int MPLS_TABLE_ID = 456;
+    private static final int LOCAL_LABEL = 102;
+
+    @Mock
+    private FutureJVppCoreFacade jvpp;
+    private StaticLspCustomizer customizer;
+
+    /**
+     * Equivalent of mpls local-label add non-eos 102 mpls-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.Mpls)
+                            .setMplsLookupInTable((long) MPLS_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_MPLS_LOOKUP, writeContext);
+        verify(jvpp).mplsRouteAddDel(getRequestForSimpleLsp(true));
+    }
+
+    @Test
+    public void testDelete() throws WriteFailedException {
+        customizer.deleteCurrentAttributes(IID, POP_AND_MPLS_LOOKUP, writeContext);
+        verify(jvpp).mplsRouteAddDel(getRequestForSimpleLsp(false));
+    }
+
+    private MplsRouteAddDel getRequestForSimpleLsp(final boolean add) {
+        MplsRouteAddDel request = new MplsRouteAddDel();
+        request.mrLabel = LOCAL_LABEL;
+        request.mrEos = 0;
+        request.mrNextHopTableId = MPLS_TABLE_ID;
+        request.mrClassifyTableIndex = -1; // default value used in make test
+        request.mrIsAdd = booleanToByte(add);
+        request.mrNextHopProto = 2; // MPLS 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;
+    }
+}