Code clean up
[bgpcep.git] / bgp / extensions / inet / src / main / java / org / opendaylight / protocol / bgp / inet / codec / Ipv6NlriParser.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.Ipv6Util;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
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.DestinationIpv6Case;
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.DestinationIpv6CaseBuilder;
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 Ipv6NlriParser implements NlriParser, NlriSerializer {
41
42     private static DestinationIpv6 prefixes(final ByteBuf nlri, final PeerSpecificParserConstraint constraint,
43             final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
44         final List<Ipv6Prefixes> prefixes = new ArrayList<>();
45         final boolean supported = MultiPathSupportUtil.isTableTypeSupported(constraint,
46                 new BgpTableTypeImpl(afi, safi));
47         while (nlri.isReadable()) {
48             final Ipv6PrefixesBuilder prefixesBuilder = new Ipv6PrefixesBuilder();
49             if (supported) {
50                 prefixesBuilder.setPathId(PathIdUtil.readPathId(nlri));
51             }
52             prefixesBuilder.setPrefix(Ipv6Util.prefixForByteBuf(nlri));
53             prefixes.add(prefixesBuilder.build());
54         }
55         return new DestinationIpv6Builder().setIpv6Prefixes(prefixes).build();
56     }
57
58     @Override
59     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
60             final PeerSpecificParserConstraint constraint) {
61         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv6CaseBuilder()
62                 .setDestinationIpv6(prefixes(nlri, constraint, builder.getAfi(), builder.getSafi())).build()).build());
63     }
64
65     @Override
66     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
67             final PeerSpecificParserConstraint constraint) {
68         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn
69                 .opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes
70                 .destination.type.DestinationIpv6CaseBuilder().setDestinationIpv6(
71                 prefixes(nlri, constraint, builder.getAfi(), builder.getSafi())).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 DestinationIpv6Case) {
81                 final DestinationIpv6Case destinationIpv6Case =
82                         (DestinationIpv6Case) advertizedRoutes.getDestinationType();
83                 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
84                     PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
85                     ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.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.DestinationIpv6Case) {
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                         .DestinationIpv6Case destinationIpv6Case =
96                         (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
97                                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
98                                 .DestinationIpv6Case) withdrawnRoutes.getDestinationType();
99                 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
100                     PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
101                     ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
102                 }
103             }
104         }
105     }
106 }