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
8 package org.opendaylight.protocol.bgp.inet.codec;
10 import io.netty.buffer.ByteBuf;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
14 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
15 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
16 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
18 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
19 import org.opendaylight.protocol.util.Ipv4Util;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4Case;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
38 public final class Ipv4NlriParser implements NlriParser, NlriSerializer {
40 private static DestinationIpv4 prefixes(final ByteBuf nlri, final PeerSpecificParserConstraint constraints,
41 final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
42 final List<Ipv4Prefixes> prefixes = new ArrayList<>();
43 while (nlri.isReadable()) {
44 final Ipv4PrefixesBuilder prefixesBuilder = new Ipv4PrefixesBuilder();
45 if (MultiPathSupportUtil.isTableTypeSupported(constraints, new BgpTableTypeImpl(afi, safi))) {
46 prefixesBuilder.setPathId(PathIdUtil.readPathId(nlri));
48 prefixesBuilder.setPrefix(Ipv4Util.prefixForByteBuf(nlri));
49 prefixes.add(prefixesBuilder.build());
51 return new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build();
55 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
56 final PeerSpecificParserConstraint constraint) {
57 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
58 new DestinationIpv4CaseBuilder().setDestinationIpv4(prefixes(nlri, constraint,
59 builder.getAfi(), builder.getSafi())).build()).build());
63 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
64 final PeerSpecificParserConstraint constraint) {
65 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
66 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
67 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
68 .setDestinationIpv4(prefixes(nlri, constraint, builder.getAfi(), builder.getSafi()))
73 public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
74 final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
75 final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
76 if (pathAttributes1 != null) {
77 final AdvertizedRoutes advertizedRoutes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
78 if (advertizedRoutes != null && advertizedRoutes.getDestinationType() instanceof DestinationIpv4Case) {
79 final DestinationIpv4Case destinationIpv4Case =
80 (DestinationIpv4Case) advertizedRoutes.getDestinationType();
81 for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
82 PathIdUtil.writePathId(ipv4Prefix.getPathId(), byteAggregator);
83 Ipv4Util.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);
86 } else if (pathAttributes2 != null) {
87 final WithdrawnRoutes withdrawnRoutes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
88 if (withdrawnRoutes != null && withdrawnRoutes.getDestinationType() instanceof org.opendaylight.yang.gen
89 .v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri
90 .withdrawn.routes.destination.type.DestinationIpv4Case) {
91 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
92 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
93 .DestinationIpv4Case destinationIpv4Case = (org.opendaylight.yang.gen.v1.urn.opendaylight
94 .params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes
95 .destination.type.DestinationIpv4Case) withdrawnRoutes.getDestinationType();
96 for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
97 PathIdUtil.writePathId(ipv4Prefix.getPathId(), byteAggregator);
98 Ipv4Util.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);