Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[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 package org.opendaylight.protocol.bgp.inet.codec;
9
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.Ipv6Util;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
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.DestinationIpv6Case;
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.DestinationIpv6CaseBuilder;
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;
37
38 public final class Ipv6NlriParser implements NlriParser, NlriSerializer {
39
40     private static DestinationIpv6 prefixes(final ByteBuf nlri, final PeerSpecificParserConstraint constraint,
41             final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
42         final List<Ipv6Prefixes> prefixes = new ArrayList<>();
43         final boolean supported = MultiPathSupportUtil.isTableTypeSupported(constraint,
44                 new BgpTableTypeImpl(afi, safi));
45         while (nlri.isReadable()) {
46             final Ipv6PrefixesBuilder prefixesBuilder = new Ipv6PrefixesBuilder();
47             if (supported) {
48                 prefixesBuilder.setPathId(PathIdUtil.readPathId(nlri));
49             }
50             prefixesBuilder.setPrefix(Ipv6Util.prefixForByteBuf(nlri));
51             prefixes.add(prefixesBuilder.build());
52         }
53         return new DestinationIpv6Builder().setIpv6Prefixes(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(new DestinationIpv6CaseBuilder()
60                 .setDestinationIpv6(prefixes(nlri, constraint, builder.getAfi(), builder.getSafi())).build()).build());
61     }
62
63     @Override
64     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
65             final PeerSpecificParserConstraint constraint) {
66         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn
67                 .opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes
68                 .destination.type.DestinationIpv6CaseBuilder().setDestinationIpv6(
69                 prefixes(nlri, constraint, builder.getAfi(), builder.getSafi())).build()).build());
70     }
71
72     @Override
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 DestinationIpv6Case) {
79                 final DestinationIpv6Case destinationIpv6Case =
80                         (DestinationIpv6Case) advertizedRoutes.getDestinationType();
81                 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
82                     PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
83                     Ipv6Util.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
84                 }
85             }
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.DestinationIpv6Case) {
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                         .DestinationIpv6Case destinationIpv6Case =
94                         (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
95                                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
96                                 .DestinationIpv6Case) withdrawnRoutes.getDestinationType();
97                 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
98                     PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
99                     Ipv6Util.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
100                 }
101             }
102         }
103     }
104 }