3064f0077810d65491f05138980b5ef0fa1aeced
[bgpcep.git] / bgp / inet / src / main / java / org / opendaylight / protocol / bgp / inet / codec / Ipv4NlriParser.java
1 /*
2  * Copyright (c) 2013, 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.inet.codec;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
15 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
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.PathIdUtil;
19 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
20 import org.opendaylight.protocol.util.ByteBufWriteUtil;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
26 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;
27 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;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
39
40 public final class Ipv4NlriParser implements NlriParser, NlriSerializer {
41
42     private static DestinationIpv4 prefixes(final ByteBuf nlri, final PeerSpecificParserConstraint constraints,
43             final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
44         final List<Ipv4Prefixes> prefixes = new ArrayList<>();
45         while (nlri.isReadable()) {
46             final Ipv4PrefixesBuilder prefixesBuilder = new Ipv4PrefixesBuilder();
47             if (MultiPathSupportUtil.isTableTypeSupported(constraints, new BgpTableTypeImpl(afi, safi))) {
48                 prefixesBuilder.setPathId(PathIdUtil.readPathId(nlri));
49             }
50             prefixesBuilder.setPrefix(Ipv4Util.prefixForByteBuf(nlri));
51             prefixes.add(prefixesBuilder.build());
52         }
53         return new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build();
54     }
55
56     @Override
57     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
58             final PeerSpecificParserConstraint constraint) {
59         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
60                 new DestinationIpv4CaseBuilder().setDestinationIpv4(prefixes(nlri, constraint,
61                         builder.getAfi(), builder.getSafi())).build()).build());
62     }
63
64     @Override
65     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
66             final PeerSpecificParserConstraint constraint) {
67         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
68                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
69                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
70                         .setDestinationIpv4(prefixes(nlri, constraint, builder.getAfi(), builder.getSafi()))
71                         .build()).build());
72     }
73
74     @Override
75     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
76         final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
77         final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
78         if (pathAttributes1 != null) {
79             final AdvertizedRoutes advertizedRoutes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
80             if (advertizedRoutes != null && advertizedRoutes.getDestinationType() instanceof DestinationIpv4Case) {
81                 final DestinationIpv4Case destinationIpv4Case =
82                         (DestinationIpv4Case) advertizedRoutes.getDestinationType();
83                 for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
84                     PathIdUtil.writePathId(ipv4Prefix.getPathId(), byteAggregator);
85                     ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);
86                 }
87             }
88         } else if (pathAttributes2 != null) {
89             final WithdrawnRoutes withdrawnRoutes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
90             if (withdrawnRoutes != null && withdrawnRoutes.getDestinationType() instanceof org.opendaylight.yang.gen
91                     .v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri
92                     .withdrawn.routes.destination.type.DestinationIpv4Case) {
93                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
94                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
95                         .DestinationIpv4Case destinationIpv4Case = (org.opendaylight.yang.gen.v1.urn.opendaylight
96                         .params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes
97                         .destination.type.DestinationIpv4Case) withdrawnRoutes.getDestinationType();
98                 for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
99                     PathIdUtil.writePathId(ipv4Prefix.getPathId(), byteAggregator);
100                     ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);
101                 }
102             }
103         }
104     }
105 }