import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/**
- * @author Kevin Wang
- */
public abstract class AbstractVpnNlriParser implements NlriParser, NlriSerializer {
private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnNlriParser.class);
protected abstract AdvertizedRoutes getAdvertizedRoutesByDestination(List<VpnDestination> dst);
- public static void serializeNlri(final List<VpnDestination> dests, final ByteBuf buffer) {
+ private static void serializeNlri(final List<VpnDestination> dests, final ByteBuf buffer) {
final ByteBuf nlriByteBuf = Unpooled.buffer();
for (final VpnDestination dest : dests) {
final List<LabelStack> labelStack = dest.getLabelStack();
final IpPrefix prefix = dest.getPrefix();
LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
- // Serialize the length field
- // Length field contains one Byte which represents the length of label stack and prefix in bits
- nlriByteBuf.writeByte(((LUNlriParser.LABEL_LENGTH * labelStack.size()) + LUNlriParser.getPrefixLength(prefix) + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
+ AbstractVpnNlriParser.serializeLengtField(prefix, labelStack, nlriByteBuf);
LUNlriParser.serializeLabelStackEntries(labelStack, nlriByteBuf);
RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null, "Ipv6 or Ipv4 prefix is missing.");
buffer.writeBytes(nlriByteBuf);
}
+ /**
+ * Serialize the length field Length field contains one Byte which represents the length of label stack and prefix in bits
+ * @param prefix ipPrefix
+ * @param labelStack list of labelStack
+ * @param nlriByteBuf ByteBuf
+ */
+ static void serializeLengtField(final IpPrefix prefix, final List<LabelStack> labelStack, final ByteBuf nlriByteBuf) {
+ final int prefixLenght = LUNlriParser.getPrefixLength(prefix);
+ int labelStackLenght = 0;
+ if(labelStack != null) {
+ labelStackLenght = LUNlriParser.LABEL_LENGTH * labelStack.size();
+ }
+ nlriByteBuf.writeByte((labelStackLenght + prefixLenght + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
+ }
+
+
private static List<VpnDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi) {
if (!nlri.isReadable()) {
return null;
package org.opendaylight.protocol.bgp.l3vpn;
import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableSet;
import io.netty.buffer.ByteBuf;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
+import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastIpv4RIBSupport;
import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
import org.opendaylight.protocol.util.ByteArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/**
- * @author Kevin Wang
- */
public abstract class AbstractVpnRIBSupport extends AbstractRIBSupport {
private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnRIBSupport.class);
private final NodeIdentifier nlriRoutesListNid;
private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
final ByteBuf buffer = Unpooled.buffer();
+ final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
+ .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
+ final ByteBuf nlriByteBuf = Unpooled.buffer();
+
+ for (final VpnDestination dest : Collections.singletonList(dests)) {
+ final IpPrefix prefix = dest.getPrefix();
+ LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
+ AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
+ RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
+ Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null, "Ipv6 or Ipv4 prefix is missing.");
+ LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
+ }
+ buffer.writeBytes(nlriByteBuf);
- final VpnDestination dest = extractVpnDestination(l3vpn);
- AbstractVpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
return new NodeIdentifierWithPredicates(routeQName(), this.routeKey, ByteArray.encodeBase64(buffer));
}
}
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
import java.util.Collection;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;
-import org.opendaylight.protocol.bgp.l3vpn.AbstractVpnNlriParser;
-import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
-import org.opendaylight.protocol.util.ByteArray;
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.Update;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
DestinationVpnIpv4Case UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV4_VPN)).build()).build();
+ private static final VpnRouteKey ROUTE_KEY = new VpnRouteKey("WAABAQIDBAECIgEW");
- private static final VpnRoute ROUTE;
- private static final VpnIpv4Routes ROUTES;
- private static final VpnRouteKey ROUTE_KEY;
-
- static {
- final BgpIpv4Activator act = new BgpIpv4Activator();
- final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
- act.start(context);
- final ByteBuf buffer = Unpooled.buffer();
- AbstractVpnNlriParser.serializeNlri(Collections.singletonList(IPV4_VPN), buffer);
- ROUTE_KEY = new VpnRouteKey(ByteArray.encodeBase64(buffer));
- ROUTE = new VpnRouteBuilder().setPathId(new PathId(NON_PATH_ID)).setAttributes(ATTRIBUTES).setPrefix(IPv4_PREFIX)
- .setLabelStack(LABEL_STACK).setRouteDistinguisher(DISTINGUISHER).setKey(ROUTE_KEY).build();
- ROUTES = new VpnIpv4RoutesBuilder().setVpnRoute(Collections.singletonList(ROUTE)).build();
- }
+ private static final VpnRoute ROUTE = new VpnRouteBuilder().setPathId(new PathId(NON_PATH_ID)).setAttributes(ATTRIBUTES).setPrefix(IPv4_PREFIX)
+ .setLabelStack(LABEL_STACK).setRouteDistinguisher(DISTINGUISHER).setKey(ROUTE_KEY).build();
+ private static final VpnIpv4Routes ROUTES = new VpnIpv4RoutesBuilder().setVpnRoute(Collections.singletonList(ROUTE)).build();
@Override
public void setUp() throws Exception {
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
import java.util.Collection;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;
-import org.opendaylight.protocol.bgp.l3vpn.AbstractVpnNlriParser;
-import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
-import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupportTest;
-import org.opendaylight.protocol.util.ByteArray;
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.Update;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
private static final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev160331.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
DestinationVpnIpv6Case UNREACH_NLRI = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev160331.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.
DestinationVpnIpv6CaseBuilder().setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV6_VPN)).build()).build();
-
- private static final VpnRoute ROUTE;
- private static final VpnIpv6Routes ROUTES;
- private static final VpnRouteKey ROUTE_KEY;
-
- static {
- final BgpIpv6Activator act = new BgpIpv6Activator();
- final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
- act.start(context);
- final ByteBuf buffer = Unpooled.buffer();
- AbstractVpnNlriParser.serializeNlri(Collections.singletonList(IPV6_VPN), buffer);
- ROUTE_KEY = new VpnRouteKey(ByteArray.encodeBase64(buffer));
- ROUTE = new VpnRouteBuilder().setPathId(new PathId(NON_PATH_ID)).setAttributes(ATTRIBUTES).setPrefix(IPv6_PREFIX)
- .setLabelStack(LABEL_STACK).setRouteDistinguisher(DISTINGUISHER).setKey(ROUTE_KEY).build();
- ROUTES = new VpnIpv6RoutesBuilder().setVpnRoute(Collections.singletonList(ROUTE)).build();
- }
+ private static final VpnRouteKey ROUTE_KEY = new VpnRouteKey("cAABAQIDBAECIAEjRVaJ");
+ private static final VpnRoute ROUTE = new VpnRouteBuilder().setPathId(new PathId(NON_PATH_ID)).setAttributes(ATTRIBUTES).setPrefix(IPv6_PREFIX)
+ .setLabelStack(LABEL_STACK).setRouteDistinguisher(DISTINGUISHER).setKey(ROUTE_KEY).build();
+ private static final VpnIpv6Routes ROUTES = new VpnIpv6RoutesBuilder().setVpnRoute(Collections.singletonList(ROUTE)).build();
@Override
public void setUp() throws Exception {
assertEquals(ROUTE, route);
}
-
@Test
public void testEmptyRoute() throws Exception {
final Routes empty = new VpnIpv6RoutesCaseBuilder().setVpnIpv6Routes(new VpnIpv6RoutesBuilder().setVpnRoute(Collections.emptyList()).build()).build();