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;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
16 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
19 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestinationBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
40 public abstract class AbstractVpnNlriParser implements NlriParser, NlriSerializer {
41 private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnNlriParser.class);
43 protected abstract List<VpnDestination> getWithdrawnVpnDestination(DestinationType dstType);
45 protected abstract List<VpnDestination> getAdvertizedVpnDestination(DestinationType dstType);
47 protected abstract WithdrawnRoutes getWithdrawnRoutesByDestination(List<VpnDestination> dst);
49 protected abstract AdvertizedRoutes getAdvertizedRoutesByDestination(List<VpnDestination> dst);
51 public static void serializeNlri(final List<VpnDestination> dests, final ByteBuf buffer) {
52 final ByteBuf nlriByteBuf = Unpooled.buffer();
53 for (final VpnDestination dest : dests) {
54 final List<LabelStack> labelStack = dest.getLabelStack();
55 final IpPrefix prefix = dest.getPrefix();
56 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
57 // Serialize the length field
58 // Length field contains one Byte which represents the length of label stack and prefix in bits
59 nlriByteBuf.writeByte(((LUNlriParser.LABEL_LENGTH * labelStack.size()) + LUNlriParser.getPrefixLength(prefix) + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
60 LUNlriParser.serializeLabelStackEntries(labelStack, nlriByteBuf);
61 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
62 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null, "Ipv6 or Ipv4 prefix is missing.");
63 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
65 buffer.writeBytes(nlriByteBuf);
68 private static List<VpnDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi) {
69 if (!nlri.isReadable()) {
72 final List<VpnDestination> dests = new ArrayList<>();
74 while (nlri.isReadable()) {
75 final VpnDestinationBuilder builder = new VpnDestinationBuilder();
76 final short length = nlri.readUnsignedByte();
77 builder.setLabelStack(LUNlriParser.parseLabel(nlri));
78 final int labelNum = builder.getLabelStack().size();
79 final int prefixLen = length - (LUNlriParser.LABEL_LENGTH * Byte.SIZE * labelNum) - (RouteDistinguisherUtil.RD_LENGTH * Byte.SIZE);
80 builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
81 Preconditions.checkState(prefixLen > 0, "A valid VPN IP prefix is required.");
82 builder.setPrefix(LUNlriParser.parseIpPrefix(nlri, prefixLen, afi));
83 dests.add(builder.build());
89 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
90 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a Attributes object");
91 final Attributes pathAttributes = (Attributes) attribute;
92 final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
93 final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
94 List<VpnDestination> vpnDst = null;
95 if (pathAttributes1 != null) {
96 final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
98 vpnDst = getAdvertizedVpnDestination(routes.getDestinationType());
100 } else if (pathAttributes2 != null) {
101 final WithdrawnRoutes routes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
102 if (routes != null) {
103 vpnDst = getWithdrawnVpnDestination(routes.getDestinationType());
106 if (vpnDst != null) {
107 serializeNlri(vpnDst, byteAggregator);
112 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
113 if (!nlri.isReadable()) {
116 final List<VpnDestination> dst = parseNlri(nlri, builder.getAfi());
117 builder.setWithdrawnRoutes(getWithdrawnRoutesByDestination(dst));
121 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
122 if (!nlri.isReadable()) {
125 final List<VpnDestination> dst = parseNlri(nlri, builder.getAfi());
126 builder.setAdvertizedRoutes(getAdvertizedRoutesByDestination(dst));