Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / bgp / extensions / 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 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.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;
37
38 public final class Ipv4NlriParser implements NlriParser, NlriSerializer {
39
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));
47             }
48             prefixesBuilder.setPrefix(Ipv4Util.prefixForByteBuf(nlri));
49             prefixes.add(prefixesBuilder.build());
50         }
51         return new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build();
52     }
53
54     @Override
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());
60     }
61
62     @Override
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()))
69                         .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 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);
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.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);
99                 }
100             }
101         }
102     }
103 }