2 * Copyright (c) 2018 AT&T Intellectual Property. 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.l3vpn.mcast.nlri;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
15 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
16 import org.opendaylight.protocol.util.Ipv4Util;
17 import org.opendaylight.protocol.util.Ipv6Util;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestinationBuilder;
22 public final class L3vpnMcastNlriSerializer {
23 private L3vpnMcastNlriSerializer() {
27 static List<L3vpnMcastDestination> extractDest(final ByteBuf nlri, final boolean addPath) {
28 List<L3vpnMcastDestination> dests = new ArrayList<>();
29 while (nlri.isReadable()) {
30 final L3vpnMcastDestinationBuilder builder = new L3vpnMcastDestinationBuilder();
32 builder.setPathId(PathIdUtil.readPathId(nlri));
34 final int length = nlri.readUnsignedByte();
36 final int initialLength = nlri.readableBytes();
37 builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
38 if (length == Ipv6Util.IPV6_BITS_LENGTH) {
39 builder.setPrefix(new IpPrefix(Ipv6Util.prefixForByteBuf(nlri)));
41 builder.setPrefix(new IpPrefix(Ipv4Util.prefixForByteBuf(nlri)));
43 dests.add(builder.build());
44 int readed = initialLength - nlri.readableBytes();
45 while (readed % 8 != 0) {
47 readed = initialLength - nlri.readableBytes();
53 public static void serializeNlri(final List<L3vpnMcastDestination> destinationList, final ByteBuf output) {
54 for (final L3vpnMcastDestination dest : destinationList) {
55 PathIdUtil.writePathId(dest.getPathId(), output);
56 ByteBuf prefixBuf = Unpooled.buffer();
57 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), prefixBuf);
58 final IpPrefix prefix = dest.getPrefix();
59 if (prefix.getIpv4Prefix() != null) {
60 output.writeByte(Ipv4Util.IP4_BITS_LENGTH);
61 Ipv4Util.writeMinimalPrefix(prefix.getIpv4Prefix(), prefixBuf);
63 output.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
64 Ipv6Util.writeMinimalPrefix(prefix.getIpv6Prefix(), prefixBuf);
66 // FIXME: remove this funky loop
67 while (prefixBuf.readableBytes() % 8 != 0) {
68 prefixBuf.writeByte(0);
70 output.writeBytes(prefixBuf);