summaryrefslogtreecommitdiffstats
path: root/mpls/impl/src/main/java
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2017-11-16 12:56:19 +0100
committerMarek Gradzki <mgradzki@cisco.com>2017-11-20 10:45:07 +0100
commit1f80540e8e412e5a246e78b9a2d6d0802182b213 (patch)
tree9558f4bd6f0150555fb93d43536494bb2c406af4 /mpls/impl/src/main/java
parent2ab7e143739a403110bd95b977f5c24d76f4d7af (diff)
HC2VPP-258: add support for MPLS label push
Translates impose-and-forward operation of /hc2vpp-ietf-routing:routing/hc2vpp-ietf-mpl:mpls/ hc2vpp-ietf-mpls-static:static-lsps/static-lsp 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). See postman collection for examples. TODOs: - only IPv4 prefixes and next-hops are supported (HC2VPP-264). Change-Id: I318e722edbc1f7ffd3eff2f308fc73c2062283c0 Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'mpls/impl/src/main/java')
-rw-r--r--mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java31
-rw-r--r--mpls/impl/src/main/java/io/fd/hc2vpp/mpls/StaticLspCustomizer.java169
2 files changed, 197 insertions, 3 deletions
diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java
index 7d420e029..014fd2636 100644
--- a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java
+++ b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java
@@ -21,11 +21,18 @@ import com.google.inject.Inject;
import com.google.inject.name.Named;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.impl.write.GenericListWriter;
+import io.fd.honeycomb.translate.impl.write.GenericWriter;
import io.fd.honeycomb.translate.write.WriterFactory;
import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder;
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.interfaces.rev140508.Interfaces;
+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._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.out.segment.path.list.Paths;
+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.rev170702.Routing1;
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;
@@ -34,14 +41,19 @@ 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);
+ private static final InstanceIdentifier<Config> CONFIG_ID = InstanceIdentifier.create(StaticLsp.class).child
+ (Config.class);
+
@Inject
@Named("interface-context")
private NamingContext ifcContext;
@@ -59,5 +71,18 @@ final class MplsWriterFactory implements WriterFactory {
org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls._interface.Config.class)),
new GenericListWriter<>(INTERFACE_ID, new MplsInterfaceCustomizer(vppApi, ifcContext)),
IFC_ID);
+
+ // /ietf-routing:routing/ietf-mpls:mpls/ietf-mpls-static:static-lsps/static-lsp
+ // after
+ // /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);
}
}
diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/StaticLspCustomizer.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/StaticLspCustomizer.java
new file mode 100644
index 000000000..d29e86640
--- /dev/null
+++ b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/StaticLspCustomizer.java
@@ -0,0 +1,169 @@
+/*
+ * 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 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 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.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class StaticLspCustomizer extends FutureJVppCustomizer implements
+ ListWriterCustomizer<StaticLsp, StaticLspKey>, JvppReplyConsumer, ByteDataTranslator, Ipv4Translator {
+ 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;
+
+ StaticLspCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull NamingContext interfaceContext) {
+ super(vppApi);
+ this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
+ }
+
+ @Override
+ public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<StaticLsp> id,
+ @Nonnull final StaticLsp dataAfter,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ LOG.debug("Adding MPLS LSP: {}", dataAfter);
+ imposeAndForward(id, validateConfig(dataAfter), writeContext, true);
+ LOG.debug("MPLS LSP successfully configured: {}", dataAfter);
+ }
+
+ @Override
+ public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<StaticLsp> id,
+ @Nonnull final StaticLsp dataBefore,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ LOG.debug("Removing MPLS LSP: {}", dataBefore);
+ imposeAndForward(id, validateConfig(dataBefore), writeContext, false);
+ LOG.debug("MPLS LSP successfully removed: {}", dataBefore);
+ }
+
+ private static Config validateConfig(@Nonnull final StaticLsp data) {
+ 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();
+ } 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, mappingContext);
+ request.nextHopViaLabel = MPLS_LABEL_INVALID;
+ }
+}