diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2017-12-20 12:54:11 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-12-20 13:03:58 +0100 |
commit | 6bb01993fda9b927923ba351d5509ed741ad616e (patch) | |
tree | b91d34c63c552cf3923bde5cf9d55c4eaa52b2ef /bgp/bgp-prefix-sid | |
parent | 5b59e1bd3009a2d9a37c5df86bf34b3d3673f0ea (diff) |
HC2VPP-268: BGP prefix SID translation
Missing features (to be addressed in subsequent commits):
- non eos VPP FIB entry
- push label entry
Change-Id: I17ca7e3ce85ebb55aaa4438db34305ec30352798
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'bgp/bgp-prefix-sid')
6 files changed, 522 insertions, 0 deletions
diff --git a/bgp/bgp-prefix-sid/asciidoc/Readme.adoc b/bgp/bgp-prefix-sid/asciidoc/Readme.adoc new file mode 100644 index 000000000..1267be859 --- /dev/null +++ b/bgp/bgp-prefix-sid/asciidoc/Readme.adoc @@ -0,0 +1,3 @@ += bgp-prefix-sid + +This is a Honeycomb plugin providing mapping code between BGP Prefix SID routes and VPP core APIs.
\ No newline at end of file diff --git a/bgp/bgp-prefix-sid/pom.xml b/bgp/bgp-prefix-sid/pom.xml new file mode 100644 index 000000000..15587bdbd --- /dev/null +++ b/bgp/bgp-prefix-sid/pom.xml @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>io.fd.hc2vpp.common</groupId> + <artifactId>vpp-impl-parent</artifactId> + <version>1.18.01-SNAPSHOT</version> + <relativePath>../../vpp-common/vpp-impl-parent</relativePath> + </parent> + + <modelVersion>4.0.0</modelVersion> + <groupId>io.fd.hc2vpp.bgp</groupId> + <artifactId>bgp-prefix-sid</artifactId> + <name>${project.artifactId}</name> + <version>1.18.01-SNAPSHOT</version> + + <dependencies> + <!-- Honeycomb infrastructure --> + <dependency> + <groupId>io.fd.honeycomb</groupId> + <artifactId>bgp-translate-api</artifactId> + <version>${project.version}</version> + </dependency> + <!-- BGP api --> + <dependency> + <groupId>org.opendaylight.bgpcep</groupId> + <artifactId>bgp-labeled-unicast</artifactId> + <version>${odl.bgpcep.version}</version> + </dependency> + <!-- Translation --> + <dependency> + <groupId>io.fd.hc2vpp.common</groupId> + <artifactId>vpp-translate-utils</artifactId> + </dependency> + <!-- DI --> + <dependency> + <groupId>com.google.inject</groupId> + <artifactId>guice</artifactId> + </dependency> + <dependency> + <groupId>com.google.inject.extensions</groupId> + <artifactId>guice-multibindings</artifactId> + </dependency> + + <!-- test dependencies --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.fd.hc2vpp.common</groupId> + <artifactId>vpp-translate-test</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidModule.java b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidModule.java new file mode 100644 index 000000000..defc51996 --- /dev/null +++ b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidModule.java @@ -0,0 +1,43 @@ +/* + * 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.bgp.prefix.sid; + +import com.google.inject.AbstractModule; +import com.google.inject.multibindings.Multibinder; +import io.fd.honeycomb.translate.bgp.RouteWriterFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Initializes route writers for BGP Prefix SID. + */ +public final class BgpPrefixSidModule extends AbstractModule { + + private static final Logger LOG = LoggerFactory.getLogger(BgpPrefixSidModule.class); + + @Override + protected void configure() { + LOG.info("Installing BGP Prefix SID module"); + + LOG.info("Injecting route writers"); + final Multibinder<RouteWriterFactory> writerFactoryBinder = + Multibinder.newSetBinder(binder(), RouteWriterFactory.class); + writerFactoryBinder.addBinding().to(BgpPrefixSidWriterFactory.class); + + LOG.info("BgpPrefixSidModule successfully configured"); + } +} diff --git a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java new file mode 100644 index 000000000..081a63e9a --- /dev/null +++ b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriter.java @@ -0,0 +1,214 @@ +/* + * 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.bgp.prefix.sid; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.annotations.VisibleForTesting; +import com.sun.istack.internal.Nullable; +import io.fd.hc2vpp.common.translate.util.Ipv4Translator; +import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; +import io.fd.honeycomb.translate.bgp.RouteWriter; +import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.vpp.jvpp.core.dto.MplsRouteAddDel; +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.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabelIndexTlv; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.LabeledUnicastRoutes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.OriginatorSrgbTlv; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRoute; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.originator.srgb.tlv.SrgbValue; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSid; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Programs VPP according to draft-ietf-idr-bgp-prefix-sid. + * + * @see <a href="https://tools.ietf.org/html/draft-ietf-idr-bgp-prefix-sid-07#section-4.1">Receiving BGP-Prefix-SID + * attribute</a> + */ +final class BgpPrefixSidMplsWriter implements RouteWriter<LabeledUnicastRoute>, Ipv4Translator, JvppReplyConsumer { + + /** + * Constant used by VPP to disable optional parameters of mpls label type. + */ + @VisibleForTesting + static final int MPLS_LABEL_INVALID = 0x100000; + + private static final Logger LOG = LoggerFactory.getLogger(BgpPrefixSidMplsWriter.class); + + @SuppressWarnings("unchecked") + private static final InstanceIdentifier<LabeledUnicastRoute> ID = + InstanceIdentifier.create(BgpRib.class).child(Rib.class) + .child(LocRib.class).child(Tables.class).child((Class) LabeledUnicastRoutes.class) + .child(LabeledUnicastRoute.class); + + private final FutureJVppCore vppApi; + + BgpPrefixSidMplsWriter(@Nonnull final FutureJVppCore vppApi) { + this.vppApi = checkNotNull(vppApi, "vppApi should not be null"); + } + + @Override + public void create(@Nonnull final InstanceIdentifier<LabeledUnicastRoute> id, + @Nullable final LabeledUnicastRoute route) + throws WriteFailedException.CreateFailedException { + final MplsRouteAddDel request = request(route, true); + LOG.debug("Translating id={}, route={} to {}", id, route, request); + getReplyForCreate(vppApi.mplsRouteAddDel(request).toCompletableFuture(), id, route); + + + // TODO(HC2VPP-268): except for SWAP EOS label entry, we should also create: + // 1) SWAP NON-EOS label + // 2) Push label to handle situations when non MPLS packet goes in and its destination is equals to + // the prefix that is being announced (in the example from the draft, it is BGP-Prefix-SID originator loopback): + // https://tools.ietf.org/html/draft-ietf-spring-segment-routing-msdc-06#section-4.2.2 + + LOG.debug("VPP FIB updated successfully (added id={}).", id); + } + + @Override + public void delete(@Nonnull final InstanceIdentifier<LabeledUnicastRoute> id, + @Nullable final LabeledUnicastRoute route) + throws WriteFailedException.DeleteFailedException { + LOG.debug("Removing id={}, route={}", id, route); + getReplyForDelete(vppApi.mplsRouteAddDel(request(route, false)).toCompletableFuture(), id); + LOG.debug("VPP FIB updated successfully (removed id={}).", id); + } + + @Override + public void update(@Nonnull final InstanceIdentifier<LabeledUnicastRoute> id, + @Nullable final LabeledUnicastRoute routeBefore, + @Nullable final LabeledUnicastRoute routeAfter) + throws WriteFailedException.UpdateFailedException { + throw new WriteFailedException.UpdateFailedException(id, routeBefore, routeAfter, + new UnsupportedOperationException("Operation not supported")); + } + + private MplsRouteAddDel request(final LabeledUnicastRoute route, boolean isAdd) { + final MplsRouteAddDel request = mplsRouteAddDel(isAdd); + + + translate(route.getAttributes().getCNextHop(), request); + translate(route.getAttributes().getBgpPrefixSid(), request); + translate(route.getLabelStack(), request); + + request.mrEos = 1; + return request; + } + + private MplsRouteAddDel mplsRouteAddDel(final boolean isAdd) { + final MplsRouteAddDel request = new MplsRouteAddDel(); + request.mrIsAdd = booleanToByte(isAdd); + + // default values based on inspecting VPP's CLI and make test code + request.mrClassifyTableIndex = -1; + request.mrNextHopWeight = 1; + request.mrNextHopViaLabel = MPLS_LABEL_INVALID; + return request; + } + + private void translate(@Nonnull final CNextHop cNextHop, @Nonnull final MplsRouteAddDel request) { + checkArgument(cNextHop instanceof Ipv4NextHopCase, + "only ipv4 next hop is supported, but was %s (cNextHop = %s)", cNextHop, cNextHop); + final Ipv4Address nextHop = ((Ipv4NextHopCase) cNextHop).getIpv4NextHop().getGlobal(); + request.mrNextHop = ipv4AddressNoZoneToArray(nextHop.getValue()); + + // We create recursive route. In order to make everything work, + // operator needs to manually map next hop address to proper interface. + // Either via CLI or HC. + // + // VPP can't recursively resolve a route that has out labels via a route that does not have out labels. + // Implicit null label is trick to get around it (no more labels will be added to the package). + // CLI example: + // + // ip route add <next-hop-ip> via <next-hop-ifc> out-labels 3 + request.mrNextHopSwIfIndex = -1; + } + + private void translate(@Nonnull final BgpPrefixSid bgpPrefixSid, @Nonnull final MplsRouteAddDel request) { + Long labelIndex = null; + OriginatorSrgbTlv originatorSrgb = null; + for (BgpPrefixSidTlvs entry : bgpPrefixSid.getBgpPrefixSidTlvs()) { + final BgpPrefixSidTlv tlv = entry.getBgpPrefixSidTlv(); + if (tlv instanceof LabelIndexTlv) { + if (labelIndex != null) { + LOG.warn(" More than one label-index-tlv encountered while parsing bgp-prefix-sid-tlvs: %s." + + "Ignoring all but %s", bgpPrefixSid, labelIndex); + } else { + labelIndex = ((LabelIndexTlv) tlv).getLabelIndexTlv(); + } + } else if (tlv instanceof OriginatorSrgbTlv) { + if (originatorSrgb != null) { + LOG.warn("More than one originator-srgb-tlv encountered while parsing bgp-prefix-sid-tlvs: %s." + + "Ignoring all but %s", bgpPrefixSid, originatorSrgb); + } else { + originatorSrgb = (OriginatorSrgbTlv) tlv; + } + } + } + + // TODO(HC2VPP-272): add support for dynamic (random) label (RFC3107) + + checkArgument(labelIndex != null, "Missing label-index-tlv"); + // TODO(HC2VPP-272): the originator-srgb-tlv is optional, make SRGB range configurable via netconf (requires writeConfig) + checkArgument(originatorSrgb != null, "Missing originator-srgb-tlv"); + // TODO(HC2VPP-272): add support for more than one SRGB + checkArgument(originatorSrgb.getSrgbValue().size() == 1, + "Only one SRGB range is currently supported, but more than one was defined: %s", originatorSrgb); + // Compute local label based on labelIndex value: + final SrgbValue srgbValue = originatorSrgb.getSrgbValue().get(0); + final long srgbStart = srgbValue.getBase().getValue(); + final long localLabel = srgbStart + labelIndex; + final long srgbEnd = srgbStart + srgbValue.getRange().getValue(); + checkArgument(localLabel <= srgbEnd && localLabel >= srgbStart); + request.mrLabel = (int) localLabel; + } + + private void translate(@Nonnull final List<LabelStack> labelStack, @Nonnull final MplsRouteAddDel request) { + final int labelCount = labelStack.size(); + checkArgument(labelCount == 1, "Single label expected, but labelStack.size()==%s", labelCount); + final int label = labelStack.get(0).getLabelValue().getValue().intValue(); + + // TODO(HC2VPP-271): add support for special labels, e.g. implicit null (for PHP). + + // swap one label to another + request.mrNextHopOutLabelStack = new int[] {label}; + request.mrNextHopNOutLabels = 1; + } + + // TODO(HC2VPP-268): add test which checks if ID is serializable + @Nonnull + @Override + public InstanceIdentifier<LabeledUnicastRoute> getManagedDataObjectType() { + return ID; + } +} diff --git a/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidWriterFactory.java b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidWriterFactory.java new file mode 100644 index 000000000..48960651e --- /dev/null +++ b/bgp/bgp-prefix-sid/src/main/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidWriterFactory.java @@ -0,0 +1,33 @@ +/* + * 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.bgp.prefix.sid; + +import com.google.inject.Inject; +import io.fd.honeycomb.translate.bgp.RibWriter; +import io.fd.honeycomb.translate.bgp.RouteWriterFactory; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import javax.annotation.Nonnull; + +final class BgpPrefixSidWriterFactory implements RouteWriterFactory { + @Inject + private FutureJVppCore vppApi; + + @Override + public void init(@Nonnull final RibWriter registry) { + registry.register(new BgpPrefixSidMplsWriter(vppApi)); + } +} diff --git a/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java b/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java new file mode 100644 index 000000000..36df877c1 --- /dev/null +++ b/bgp/bgp-prefix-sid/src/test/java/io/fd/hc2vpp/bgp/prefix/sid/BgpPrefixSidMplsWriterTest.java @@ -0,0 +1,165 @@ +/* + * 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.bgp.prefix.sid; + +import static io.fd.hc2vpp.bgp.prefix.sid.BgpPrefixSidMplsWriter.MPLS_LABEL_INVALID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import com.google.common.collect.Lists; +import io.fd.hc2vpp.common.test.util.FutureProducer; +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +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.FutureJVppCore; +import java.util.Collections; +import org.junit.Before; +import org.junit.Test; +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.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabeledUnicastRoutes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.Srgb; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRoute; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRouteBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.LabeledUnicastRouteKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.list.labeled.unicast.route.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.bgp.prefix.sid.tlv.LuOriginatorSrgbTlvBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.originator.srgb.tlv.SrgbValueBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.bgp.prefix.sid.tlv.LuLabelIndexTlvBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSid; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSidBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class BgpPrefixSidMplsWriterTest implements FutureProducer, ByteDataTranslator { + private static final InstanceIdentifier<Tables> TABLE_ID = InstanceIdentifier.create(BgpRib.class) + .child(Rib.class, new RibKey(new RibId("test-rib"))).child(LocRib.class) + .child(Tables.class, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)); + + @Mock + private FutureJVppCore vppApi; + private BgpPrefixSidMplsWriter writer; + + @SuppressWarnings("unchecked") + private static InstanceIdentifier<LabeledUnicastRoute> id(final PathId pathId, final String routeKey) { + return TABLE_ID.child((Class) LabeledUnicastRoutes.class) + .child(LabeledUnicastRoute.class, new LabeledUnicastRouteKey(pathId, routeKey)); + } + + private static LabeledUnicastRoute route(final String routeKey, final PathId pathId, + final Ipv4Address nextHopAddress, + final BgpPrefixSid bgpPrefixSid) { + final Ipv4NextHopCase nextHop = + new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(nextHopAddress).build()) + .build(); + return new LabeledUnicastRouteBuilder() + .setKey(new LabeledUnicastRouteKey(pathId, routeKey)) + .setPathId(pathId) + .setAttributes(new AttributesBuilder() + .setCNextHop(nextHop) + .setBgpPrefixSid(bgpPrefixSid) + .build()) + .setLabelStack( + Collections.singletonList(new LabelStackBuilder().setLabelValue(new MplsLabel(16101L)).build())) + .build(); + } + + private static BgpPrefixSidTlvs labelIndexTlv(final long label) { + return new BgpPrefixSidTlvsBuilder() + .setBgpPrefixSidTlv(new LuLabelIndexTlvBuilder() + .setLabelIndexTlv(label) + .build()) + .build(); + } + + @Before + public void setUp() { + initMocks(this); + writer = new BgpPrefixSidMplsWriter(vppApi); + when(vppApi.mplsRouteAddDel(any())).thenReturn(future(new MplsRouteAddDelReply())); + } + + private BgpPrefixSidTlvs originatorSrgbTlv(final long base, final long range) { + return new BgpPrefixSidTlvsBuilder() + .setBgpPrefixSidTlv(new LuOriginatorSrgbTlvBuilder() + .setSrgbValue(Collections.singletonList(new SrgbValueBuilder() + .setBase(new Srgb(base)) + .setRange(new Srgb(range)) + .build())) + .build()) + .build(); + } + + @Test + public void testCreate() throws WriteFailedException.CreateFailedException { + final String routeKey = "route-key"; + final PathId pathId = new PathId(123L); + final Ipv4Address nextHopAddress = new Ipv4AddressNoZone("5.6.7.8"); + + final BgpPrefixSid bgpPrefixSid = new BgpPrefixSidBuilder() + .setBgpPrefixSidTlvs( + Lists.newArrayList( + labelIndexTlv(102L), + originatorSrgbTlv(16000, 800) + )) + .build(); + writer.create( + id(pathId, routeKey), + route(routeKey, pathId, nextHopAddress, bgpPrefixSid) + ); + verifyRequest(true); + } + + private void verifyRequest(boolean isAdd) { + final MplsRouteAddDel request = new MplsRouteAddDel(); + request.mrIsAdd = booleanToByte(isAdd); + request.mrClassifyTableIndex = -1; + request.mrNextHopWeight = 1; + request.mrNextHopViaLabel = MPLS_LABEL_INVALID; + + request.mrNextHop = new byte[] {5, 6, 7, 8}; + request.mrNextHopSwIfIndex = -1; + + request.mrLabel = 16102; + + request.mrNextHopOutLabelStack = new int[] {16101}; + request.mrNextHopNOutLabels = 1; + + request.mrEos = 1; + verify(vppApi).mplsRouteAddDel(request); + } +} |