2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.l3vpn.unicast;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.List;
14 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
15 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
16 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
17 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public abstract class AbstractVpnNlriParser implements NlriParser, NlriSerializer {
34 private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnNlriParser.class);
36 protected abstract List<VpnDestination> getWithdrawnVpnDestination(DestinationType dstType);
38 protected abstract List<VpnDestination> getAdvertizedVpnDestination(DestinationType dstType);
40 protected abstract WithdrawnRoutes getWithdrawnRoutesByDestination(List<VpnDestination> dst);
42 protected abstract AdvertizedRoutes getAdvertizedRoutesByDestination(List<VpnDestination> dst);
44 private static void serializeNlri(final List<VpnDestination> dests,
45 final boolean isWithdrawnRoute, final ByteBuf buffer) {
46 final ByteBuf nlriByteBuf = Unpooled.buffer();
47 for (final VpnDestination dest : dests) {
48 final List<LabelStack> labelStack = dest.getLabelStack();
49 final IpPrefix prefix = dest.getPrefix();
50 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
51 AbstractVpnNlriParser.serializeLengtField(prefix, labelStack, nlriByteBuf);
52 LUNlriParser.serializeLabelStackEntries(labelStack, isWithdrawnRoute, nlriByteBuf);
53 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
54 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
55 "Ipv6 or Ipv4 prefix is missing.");
56 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
58 buffer.writeBytes(nlriByteBuf);
62 * Serialize the length field Length field contains one Byte which represents the length of label stack and prefix
65 * @param prefix ipPrefix
66 * @param labelStack list of labelStack
67 * @param nlriByteBuf ByteBuf
69 static void serializeLengtField(final IpPrefix prefix, final List<LabelStack> labelStack,
70 final ByteBuf nlriByteBuf) {
71 final int prefixLenght = LUNlriParser.getPrefixLength(prefix);
72 int labelStackLenght = 0;
73 if (labelStack != null) {
74 labelStackLenght = LUNlriParser.LABEL_LENGTH * labelStack.size();
76 nlriByteBuf.writeByte((labelStackLenght + prefixLenght + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
80 public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
81 final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
82 final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
83 List<VpnDestination> vpnDst = null;
84 boolean isWithdrawnRoute = false;
85 if (pathAttributes1 != null) {
86 final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
88 vpnDst = getAdvertizedVpnDestination(routes.getDestinationType());
90 } else if (pathAttributes2 != null) {
91 final WithdrawnRoutes routes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
93 vpnDst = getWithdrawnVpnDestination(routes.getDestinationType());
94 isWithdrawnRoute = true;
98 serializeNlri(vpnDst, isWithdrawnRoute, byteAggregator);
103 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
104 final PeerSpecificParserConstraint constraint) {
105 if (!nlri.isReadable()) {
108 final List<VpnDestination> dst = VpnDestinationUtil.parseNlri(nlri, constraint,
109 builder.getAfi(), builder.getSafi());
110 builder.setWithdrawnRoutes(getWithdrawnRoutesByDestination(dst));
114 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
115 final PeerSpecificParserConstraint constraint) {
116 if (!nlri.isReadable()) {
119 final List<VpnDestination> dst = VpnDestinationUtil.parseNlri(nlri, constraint,
120 builder.getAfi(), builder.getSafi());
121 builder.setAdvertizedRoutes(getAdvertizedRoutesByDestination(dst));