2 * Copyright (c) 2013, 2016 Cisco 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
9 package org.opendaylight.protocol.bgp.inet.codec;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
17 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
18 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
19 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
20 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
21 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
22 import org.opendaylight.protocol.util.ByteBufWriteUtil;
23 import org.opendaylight.protocol.util.Ipv6Util;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6Case;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
43 public final class Ipv6NlriParser implements NlriParser, NlriSerializer {
45 private static DestinationIpv6 prefixes(final ByteBuf nlri, final PeerSpecificParserConstraint constraint,
46 final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
47 final List<Ipv6Prefixes> prefixes = new ArrayList<>();
48 final boolean supported = MultiPathSupportUtil.isTableTypeSupported(constraint,
49 new BgpTableTypeImpl(afi, safi));
50 while (nlri.isReadable()) {
51 final Ipv6PrefixesBuilder prefixesBuilder = new Ipv6PrefixesBuilder();
53 prefixesBuilder.setPathId(PathIdUtil.readPathId(nlri));
55 prefixesBuilder.setPrefix(Ipv6Util.prefixForByteBuf(nlri));
56 prefixes.add(prefixesBuilder.build());
58 return new DestinationIpv6Builder().setIpv6Prefixes(prefixes).build();
62 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
63 parseNlri(nlri, builder, null);
67 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
68 parseNlri(nlri, builder, null);
72 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
73 final PeerSpecificParserConstraint constraint) {
74 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv6CaseBuilder()
75 .setDestinationIpv6(prefixes(nlri, constraint, builder.getAfi(), builder.getSafi())).build()).build());
79 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
80 final PeerSpecificParserConstraint constraint) {
81 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn
82 .opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes
83 .destination.type.DestinationIpv6CaseBuilder().setDestinationIpv6(
84 prefixes(nlri, constraint, builder.getAfi(), builder.getSafi())).build()).build());
88 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
89 Preconditions.checkArgument(attribute instanceof Attributes,
90 "Attribute parameter is not a PathAttribute object.");
91 final Attributes pathAttributes = (Attributes) attribute;
92 final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
93 final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
94 if (pathAttributes1 != null) {
95 final AdvertizedRoutes advertizedRoutes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
96 if (advertizedRoutes != null && advertizedRoutes.getDestinationType() instanceof DestinationIpv6Case) {
97 final DestinationIpv6Case destinationIpv6Case =
98 (DestinationIpv6Case) advertizedRoutes.getDestinationType();
99 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
100 PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
101 ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
104 } else if (pathAttributes2 != null) {
105 final WithdrawnRoutes withdrawnRoutes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
106 if (withdrawnRoutes != null && withdrawnRoutes.getDestinationType() instanceof org.opendaylight.yang.gen
107 .v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri
108 .withdrawn.routes.destination.type.DestinationIpv6Case) {
109 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
110 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
111 .DestinationIpv6Case destinationIpv6Case =
112 (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
113 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
114 .DestinationIpv6Case) withdrawnRoutes.getDestinationType();
115 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
116 PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
117 ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);