From 995340b2a5204f8f643b3c5a4d18620f02b795a0 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Fri, 23 Jun 2017 14:00:58 +0200 Subject: HC2VPP-174: add support for BGP IPv4/IPv6 unicast Tranlates BGP IPv4/IPv6 routes to VPP FIB. Not supported: - multiple paths (https://tools.ietf.org/html/rfc7911) - IPv6 SR Change-Id: I06f0e81dd44df6a2eb7a3fe95445041e8f4f7af9 Signed-off-by: Marek Gradzki --- bgp/asciidoc/Readme.adoc | 3 + bgp/inet/asciidoc/Readme.adoc | 3 + bgp/inet/pom.xml | 64 ++++++++++ .../java/io/fd/hc2vpp/bgp/inet/BgpInetModule.java | 43 +++++++ .../fd/hc2vpp/bgp/inet/InetRouteWriterFactory.java | 34 +++++ .../java/io/fd/hc2vpp/bgp/inet/Ipv4Writer.java | 111 ++++++++++++++++ .../java/io/fd/hc2vpp/bgp/inet/Ipv6Writer.java | 115 +++++++++++++++++ .../fd/hc2vpp/bgp/inet/RouteRequestProducer.java | 33 +++++ .../java/io/fd/hc2vpp/bgp/inet/Ipv4WriterTest.java | 137 ++++++++++++++++++++ .../java/io/fd/hc2vpp/bgp/inet/Ipv6WriterTest.java | 142 +++++++++++++++++++++ bgp/pom.xml | 56 ++++++++ pom.xml | 1 + vpp-integration/bgp-distribution/pom.xml | 6 + 13 files changed, 748 insertions(+) create mode 100644 bgp/asciidoc/Readme.adoc create mode 100644 bgp/inet/asciidoc/Readme.adoc create mode 100644 bgp/inet/pom.xml create mode 100644 bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/BgpInetModule.java create mode 100644 bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/InetRouteWriterFactory.java create mode 100644 bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv4Writer.java create mode 100644 bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv6Writer.java create mode 100644 bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/RouteRequestProducer.java create mode 100644 bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv4WriterTest.java create mode 100644 bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv6WriterTest.java create mode 100644 bgp/pom.xml diff --git a/bgp/asciidoc/Readme.adoc b/bgp/asciidoc/Readme.adoc new file mode 100644 index 000000000..27a929864 --- /dev/null +++ b/bgp/asciidoc/Readme.adoc @@ -0,0 +1,3 @@ += bgp-aggregator + +This is a Honeycomb plugin providing mapping code between BGP routes and VPP core APIs. \ No newline at end of file diff --git a/bgp/inet/asciidoc/Readme.adoc b/bgp/inet/asciidoc/Readme.adoc new file mode 100644 index 000000000..57e432485 --- /dev/null +++ b/bgp/inet/asciidoc/Readme.adoc @@ -0,0 +1,3 @@ += inet + +Provides mapping code between IPv4/IPv6 BGP routes and VPP core APIs. \ No newline at end of file diff --git a/bgp/inet/pom.xml b/bgp/inet/pom.xml new file mode 100644 index 000000000..a570d098b --- /dev/null +++ b/bgp/inet/pom.xml @@ -0,0 +1,64 @@ + + + + io.fd.hc2vpp.common + vpp-impl-parent + 1.17.07-SNAPSHOT + ../../vpp-common/vpp-impl-parent + + + 4.0.0 + io.fd.hc2vpp.bgp + bgp-inet + ${project.artifactId} + 1.17.07-SNAPSHOT + + + + + io.fd.honeycomb + bgp-translate-api + ${project.version} + + + + org.opendaylight.bgpcep + bgp-inet + ${bgpcep.version} + + + + io.fd.hc2vpp.common + vpp-translate-utils + + + + com.google.inject + guice + + + com.google.inject.extensions + guice-multibindings + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + io.fd.hc2vpp.common + vpp-translate-test + ${project.version} + test + + + \ No newline at end of file diff --git a/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/BgpInetModule.java b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/BgpInetModule.java new file mode 100644 index 000000000..58f2e84d8 --- /dev/null +++ b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/BgpInetModule.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.inet; + +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; + +/** + * BgpInetModule class instantiating BGP IPv4 and IPv6 route writers. + */ +public final class BgpInetModule extends AbstractModule { + + private static final Logger LOG = LoggerFactory.getLogger(BgpInetModule.class); + + @Override + protected void configure() { + LOG.info("Installing BGP inet module"); + + LOG.info("Injecting route writers"); + final Multibinder writerFactoryBinder = + Multibinder.newSetBinder(binder(), RouteWriterFactory.class); + writerFactoryBinder.addBinding().to(InetRouteWriterFactory.class); + + LOG.info("BgpInetModule successfully configured"); + } +} diff --git a/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/InetRouteWriterFactory.java b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/InetRouteWriterFactory.java new file mode 100644 index 000000000..821d4364d --- /dev/null +++ b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/InetRouteWriterFactory.java @@ -0,0 +1,34 @@ +/* + * 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.inet; + +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 InetRouteWriterFactory implements RouteWriterFactory { + @Inject + private FutureJVppCore vppApi; + + @Override + public void init(@Nonnull final RibWriter registry) { + registry.register(new Ipv4Writer(vppApi)); + registry.register(new Ipv6Writer(vppApi)); + } +} diff --git a/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv4Writer.java b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv4Writer.java new file mode 100644 index 000000000..ef1853ce8 --- /dev/null +++ b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv4Writer.java @@ -0,0 +1,111 @@ +/* + * 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.inet; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +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.IpAddDelRoute; +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.inet.types.rev130715.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4Routes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4Route; +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; + +final class Ipv4Writer implements RouteWriter, Ipv4Translator, JvppReplyConsumer, RouteRequestProducer { + + private static final Logger LOG = LoggerFactory.getLogger(Ipv4Writer.class); + + @SuppressWarnings("unchecked") + private static final InstanceIdentifier ID = InstanceIdentifier.create(BgpRib.class).child(Rib.class) + .child(LocRib.class).child(Tables.class).child((Class) Ipv4Routes.class) + .child(Ipv4Route.class); + + private final FutureJVppCore vppApi; + + Ipv4Writer(@Nonnull final FutureJVppCore vppApi) { + this.vppApi = checkNotNull(vppApi, "vppApi should not be null"); + } + + @Override + public void create(@Nonnull final InstanceIdentifier id, + @Nullable final Ipv4Route route) + throws WriteFailedException.CreateFailedException { + final IpAddDelRoute request = request(route, true); + LOG.debug("Translating id={}, route={} to {}", id, route, request); + getReplyForCreate(vppApi.ipAddDelRoute(request).toCompletableFuture(), id, route); + LOG.debug("VPP FIB updated successfully (added id={}).", id); + } + + @Override + public void delete(@Nonnull final InstanceIdentifier id, + @Nullable final Ipv4Route route) + throws WriteFailedException.DeleteFailedException { + LOG.debug("Removing id={}, route={}", id, route); + getReplyForDelete(vppApi.ipAddDelRoute(request(route, false)).toCompletableFuture(), id); + LOG.debug("VPP FIB updated successfully (removed id={}).", id); + } + + @Override + public void update(@Nonnull final InstanceIdentifier id, + @Nullable final Ipv4Route routeBefore, + @Nullable final Ipv4Route routeAfter) + throws WriteFailedException.UpdateFailedException { + throw new WriteFailedException.UpdateFailedException(id, routeBefore, routeAfter, + new UnsupportedOperationException("Operation not supported")); + } + + private IpAddDelRoute request(final Ipv4Route route, boolean isAdd) { + // TODO(HC2VPP-177): add support for request.nextHopWeight for multiple path case + + final CNextHop cNextHop = route.getAttributes().getCNextHop(); + checkArgument(cNextHop instanceof Ipv4NextHopCase, "only ipv4 next hop is supported, but was %s (route = %s)", + cNextHop, route); + + final IpAddDelRoute request = ipAddDelRoute(isAdd); + + final Ipv4Address nextHop = ((Ipv4NextHopCase) cNextHop).getIpv4NextHop().getGlobal(); + request.nextHopAddress = ipv4AddressNoZoneToArray(nextHop.getValue()); + + final Ipv4Prefix destinationAddress = route.getPrefix(); + request.dstAddress = ipv4AddressPrefixToArray(destinationAddress); + request.dstAddressLength = extractPrefix(destinationAddress); + + return request; + } + + @Nonnull + @Override + public InstanceIdentifier getManagedDataObjectType() { + return ID; + } +} diff --git a/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv6Writer.java b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv6Writer.java new file mode 100644 index 000000000..0e7e31de4 --- /dev/null +++ b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/Ipv6Writer.java @@ -0,0 +1,115 @@ +/* + * 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.inet; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.sun.istack.internal.Nullable; +import io.fd.hc2vpp.common.translate.util.Ipv6Translator; +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.IpAddDelRoute; +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.inet.types.rev130715.Ipv6Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.Ipv6Routes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.ipv6.routes.Ipv6Route; +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.Ipv6NextHopCase; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class Ipv6Writer implements RouteWriter, Ipv6Translator, JvppReplyConsumer, RouteRequestProducer { + + private static final Logger LOG = LoggerFactory.getLogger(Ipv6Writer.class); + + @SuppressWarnings("unchecked") + private static final InstanceIdentifier ID = InstanceIdentifier.create(BgpRib.class).child(Rib.class) + .child(LocRib.class).child(Tables.class).child((Class) Ipv6Routes.class) + .child(Ipv6Route.class); + + private final FutureJVppCore vppApi; + + Ipv6Writer(@Nonnull final FutureJVppCore vppApi) { + this.vppApi = checkNotNull(vppApi, "vppApi should not be null"); + } + + @Override + public void create(@Nonnull final InstanceIdentifier id, + @Nullable final Ipv6Route route) + throws WriteFailedException.CreateFailedException { + final IpAddDelRoute request = request(route, true); + LOG.debug("Translating id={}, route={} to {}", id, route, request); + getReplyForCreate(vppApi.ipAddDelRoute(request).toCompletableFuture(), id, route); + LOG.debug("VPP FIB updated successfully (added id={}).", id); + } + + @Override + public void delete(@Nonnull final InstanceIdentifier id, + @Nullable final Ipv6Route route) + throws WriteFailedException.DeleteFailedException { + LOG.debug("Removing id={}, route={}", id, route); + getReplyForDelete(vppApi.ipAddDelRoute(request(route, false)).toCompletableFuture(), id); + LOG.debug("VPP FIB updated successfully (removed id={}).", id); + } + + @Override + public void update(@Nonnull final InstanceIdentifier id, + @Nullable final Ipv6Route routeBefore, + @Nullable final Ipv6Route routeAfter) + throws WriteFailedException.UpdateFailedException { + throw new WriteFailedException.UpdateFailedException(id, routeBefore, routeAfter, + new UnsupportedOperationException("Operation not supported")); + } + + private IpAddDelRoute request(final Ipv6Route dataAfter, boolean isAdd) { + // TODO(HC2VPP-178): support of bgp-prefix-sid SR for IPv6 + // dataAfter.getAttributes().getBgpPrefixSid() + + // TODO(HC2VPP-177): add support for request.nextHopWeight for multiple path case + + final CNextHop cNextHop = dataAfter.getAttributes().getCNextHop(); + checkArgument(cNextHop instanceof Ipv6NextHopCase, "only ipv6 next hop is supported, but was %s (route = %s)", + cNextHop, dataAfter); + + final IpAddDelRoute request = ipAddDelRoute(isAdd); + request.isIpv6 = 1; + + final Ipv6Address nextHop = ((Ipv6NextHopCase) cNextHop).getIpv6NextHop().getGlobal(); + request.nextHopAddress = ipv6AddressNoZoneToArray(nextHop); + + final Ipv6Prefix destinationAddress = dataAfter.getPrefix(); + request.dstAddress = ipv6AddressPrefixToArray(destinationAddress); + request.dstAddressLength = extractPrefix(destinationAddress); + + return request; + } + + @Nonnull + @Override + public InstanceIdentifier getManagedDataObjectType() { + return ID; + } +} diff --git a/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/RouteRequestProducer.java b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/RouteRequestProducer.java new file mode 100644 index 000000000..2684ba5ec --- /dev/null +++ b/bgp/inet/src/main/java/io/fd/hc2vpp/bgp/inet/RouteRequestProducer.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.inet; + +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +import io.fd.vpp.jvpp.core.dto.IpAddDelRoute; + +interface RouteRequestProducer extends ByteDataTranslator { + int MPLS_LABEL_INVALID = 0x100000; + + default IpAddDelRoute ipAddDelRoute(boolean isAdd) { + final IpAddDelRoute request = new IpAddDelRoute(); + request.isAdd = booleanToByte(isAdd); + // we create recursive route and expect hc2vpp user to add route for next hop with interface specified + request.nextHopSwIfIndex = -1; + request.nextHopViaLabel = MPLS_LABEL_INVALID; + return request; + } +} diff --git a/bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv4WriterTest.java b/bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv4WriterTest.java new file mode 100644 index 000000000..0adfb480f --- /dev/null +++ b/bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv4WriterTest.java @@ -0,0 +1,137 @@ +/* + * 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.inet; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; +import static io.fd.hc2vpp.bgp.inet.RouteRequestProducer.MPLS_LABEL_INVALID; + +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.IpAddDelRoute; +import io.fd.vpp.jvpp.core.dto.IpAddDelRouteReply; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4Routes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4Route; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4RouteBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4RouteKey; +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.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.yangtools.yang.binding.InstanceIdentifier; + +public class Ipv4WriterTest implements FutureProducer, ByteDataTranslator { + private static final InstanceIdentifier 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 Ipv4Writer writer; + + @Before + public void setUp() { + initMocks(this); + writer = new Ipv4Writer(vppApi); + when(vppApi.ipAddDelRoute(any())).thenReturn(future(new IpAddDelRouteReply())); + } + + @SuppressWarnings("unchecked") + private static InstanceIdentifier id(final Ipv4Prefix destination, final PathId pathId) { + return TABLE_ID.child((Class) Ipv4Routes.class) + .child(Ipv4Route.class, new Ipv4RouteKey(pathId, destination)); + } + + private static Ipv4Route route(final Ipv4Prefix destination, final PathId pathId, + final Ipv4Address nextHopAddress) { + final Ipv4NextHopCase nextHop = + new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(nextHopAddress).build()) + .build(); + return new Ipv4RouteBuilder() + .setPrefix(destination) + .setPathId(pathId) + .setAttributes(new AttributesBuilder().setCNextHop(nextHop).build()) + .build(); + } + + @Test + public void testCreate() throws WriteFailedException.CreateFailedException { + final Ipv4Prefix destination = new Ipv4Prefix("1.2.3.4/24"); + final PathId pathId = new PathId(123L); + final Ipv4Address nextHopAddress = new Ipv4AddressNoZone("5.6.7.8"); + + writer.create( + id(destination, pathId), + route(destination, pathId, nextHopAddress) + ); + verifyRequest(true); + } + + @Test + public void testDelete() throws WriteFailedException.DeleteFailedException { + final Ipv4Prefix destination = new Ipv4Prefix("1.2.3.4/24"); + final PathId pathId = new PathId(456L); + final Ipv4Address nextHopAddress = new Ipv4AddressNoZone("5.6.7.8"); + + writer.delete( + id(destination, pathId), + route(destination, pathId, nextHopAddress) + ); + verifyRequest(false); + } + + @Test(expected = WriteFailedException.UpdateFailedException.class) + public void testUpdate() throws WriteFailedException.UpdateFailedException { + final Ipv4Prefix destination = new Ipv4Prefix("10.1.0.1/28"); + final PathId pathId = new PathId(456L); + + // update is not supported + writer.update(id(destination, pathId), mock(Ipv4Route.class), mock(Ipv4Route.class)); + } + + private void verifyRequest(boolean isAdd) { + final IpAddDelRoute request = new IpAddDelRoute(); + request.isAdd = booleanToByte(isAdd); + request.nextHopSwIfIndex = -1; + request.nextHopViaLabel = MPLS_LABEL_INVALID; + request.nextHopAddress = new byte[] {5, 6, 7, 8}; + request.dstAddress = new byte[] {1, 2, 3, 4}; + request.dstAddressLength = 24; + verify(vppApi).ipAddDelRoute(request); + } +} diff --git a/bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv6WriterTest.java b/bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv6WriterTest.java new file mode 100644 index 000000000..fa9554698 --- /dev/null +++ b/bgp/inet/src/test/java/io/fd/hc2vpp/bgp/inet/Ipv6WriterTest.java @@ -0,0 +1,142 @@ +/* + * 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.inet; + +import static io.fd.hc2vpp.bgp.inet.RouteRequestProducer.MPLS_LABEL_INVALID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +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.IpAddDelRoute; +import io.fd.vpp.jvpp.core.dto.IpAddDelRouteReply; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +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.Ipv6Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.Ipv6Routes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.ipv6.routes.Ipv6Route; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.ipv6.routes.Ipv6RouteBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.ipv6.routes.Ipv6RouteKey; +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.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.Ipv6AddressFamily; +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.Ipv6NextHopCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class Ipv6WriterTest implements FutureProducer, ByteDataTranslator { + private static final InstanceIdentifier TABLE_ID = InstanceIdentifier.create(BgpRib.class) + .child(Rib.class, new RibKey(new RibId("test-rib"))).child(LocRib.class) + .child(Tables.class, new TablesKey(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class)); + + @Mock + private FutureJVppCore vppApi; + private Ipv6Writer writer; + + @Before + public void setUp() { + initMocks(this); + writer = new Ipv6Writer(vppApi); + when(vppApi.ipAddDelRoute(any())).thenReturn(future(new IpAddDelRouteReply())); + } + + @SuppressWarnings("unchecked") + private static InstanceIdentifier id(final Ipv6Prefix destination, final PathId pathId) { + return TABLE_ID.child((Class) Ipv6Routes.class) + .child(Ipv6Route.class, new Ipv6RouteKey(pathId, destination)); + } + + private static Ipv6Route route(final Ipv6Prefix destination, final PathId pathId, + final Ipv6Address nextHopAddress) { + final Ipv6NextHopCase nextHop = + new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(nextHopAddress).build()) + .build(); + return new Ipv6RouteBuilder() + .setPrefix(destination) + .setPathId(pathId) + .setAttributes(new AttributesBuilder().setCNextHop(nextHop).build()) + .build(); + } + + @Test + public void testCreate() throws WriteFailedException.CreateFailedException { + final Ipv6Prefix destination = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:1/64"); + final PathId pathId = new PathId(123L); + final Ipv6Address nextHopAddress = new Ipv6AddressNoZone("2001:db8:a0b:12f0:0:0:0:2"); + + writer.create( + id(destination, pathId), + route(destination, pathId, nextHopAddress) + ); + verifyRequest(true); + } + + @Test + public void testDelete() throws WriteFailedException.DeleteFailedException { + final Ipv6Prefix destination = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:1/64"); + final PathId pathId = new PathId(456L); + final Ipv6Address nextHopAddress = new Ipv6AddressNoZone("2001:db8:a0b:12f0:0:0:0:2"); + + writer.delete( + id(destination, pathId), + route(destination, pathId, nextHopAddress) + ); + verifyRequest(false); + } + + @Test(expected = WriteFailedException.UpdateFailedException.class) + public void testUpdate() throws WriteFailedException.UpdateFailedException { + final Ipv6Prefix destination = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:1/64"); + final PathId pathId = new PathId(456L); + + // update is not supported + writer.update(id(destination, pathId), mock(Ipv6Route.class), mock(Ipv6Route.class)); + } + + private void verifyRequest(boolean isAdd) { + final IpAddDelRoute request = new IpAddDelRoute(); + request.isAdd = booleanToByte(isAdd); + request.isIpv6 = 1; + request.nextHopSwIfIndex = -1; + request.nextHopViaLabel = MPLS_LABEL_INVALID; + request.nextHopAddress = new byte[] { + 0x20, 0x01, 0x0d, (byte) 0xb8, 0x0a, 0x0b, 0x12, (byte) 0xf0, + 0, 0, 0, 0, 0, 0, 0, 2}; + request.dstAddress = new byte[] { + 0x20, 0x01, 0x0d, (byte) 0xb8, 0x0a, 0x0b, 0x12, (byte) 0xf0, + 0, 0, 0, 0, 0, 0, 0, 1}; + request.dstAddressLength = 64; + verify(vppApi).ipAddDelRoute(request); + } +} diff --git a/bgp/pom.xml b/bgp/pom.xml new file mode 100644 index 000000000..a534d2524 --- /dev/null +++ b/bgp/pom.xml @@ -0,0 +1,56 @@ + + + + + io.fd.hc2vpp.common + hc2vpp-parent + 1.17.07-SNAPSHOT + ../common/hc2vpp-parent + + + io.fd.hc2vpp.bgp + bgp-aggregator + 1.17.07-SNAPSHOT + ${project.artifactId} + pom + 4.0.0 + Aggregator for Hc2vpp BGP plugin + + + inet + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + org.apache.maven.plugins + maven-install-plugin + + true + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index fef706b91..bbf24cfab 100644 --- a/pom.xml +++ b/pom.xml @@ -53,5 +53,6 @@ vpp-management it interface-role + bgp \ No newline at end of file diff --git a/vpp-integration/bgp-distribution/pom.xml b/vpp-integration/bgp-distribution/pom.xml index 4e2945498..59892d4e4 100644 --- a/vpp-integration/bgp-distribution/pom.xml +++ b/vpp-integration/bgp-distribution/pom.xml @@ -33,6 +33,7 @@ io.fd.honeycomb.infra.bgp.distro.Main ${project.version} + io.fd.hc2vpp.bgp.inet.BgpInetModule @@ -60,6 +61,11 @@ + + io.fd.hc2vpp.bgp + bgp-inet + ${project.version} + io.fd.honeycomb bgp-distribution -- cgit 1.2.3-korg