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
9 package org.opendaylight.protocol.bgp.l3vpn.mcast.nlri;
11 import static org.opendaylight.protocol.util.Ipv6Util.IPV6_BITS_LENGTH;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
18 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
19 import org.opendaylight.protocol.util.ByteBufWriteUtil;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.protocol.util.Ipv6Util;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestinationBuilder;
26 public final class L3vpnMcastNlriSerializer {
27 private L3vpnMcastNlriSerializer() {
28 throw new UnsupportedOperationException();
31 static List<L3vpnMcastDestination> extractDest(final ByteBuf nlri, final boolean addPath) {
32 List<L3vpnMcastDestination> dests = new ArrayList<>();
33 while (nlri.isReadable()) {
34 final L3vpnMcastDestinationBuilder builder = new L3vpnMcastDestinationBuilder();
36 builder.setPathId(PathIdUtil.readPathId(nlri));
38 final int length = nlri.readUnsignedByte();
40 final int initialLength = nlri.readableBytes();
41 builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
42 if (length == IPV6_BITS_LENGTH) {
43 builder.setPrefix(new IpPrefix(Ipv6Util.prefixForByteBuf(nlri)));
45 builder.setPrefix(new IpPrefix(Ipv4Util.prefixForByteBuf(nlri)));
47 dests.add(builder.build());
48 int readed = initialLength - nlri.readableBytes();
49 while (readed % 8 != 0) {
51 readed = initialLength - nlri.readableBytes();
57 public static void serializeNlri(final List<L3vpnMcastDestination> destinationList, final ByteBuf output) {
58 for (final L3vpnMcastDestination dest : destinationList) {
59 PathIdUtil.writePathId(dest.getPathId(), output);
60 ByteBuf prefixBuf = Unpooled.buffer();
61 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), prefixBuf);
62 final IpPrefix prefix = dest.getPrefix();
63 if (prefix.getIpv4Prefix() != null) {
64 output.writeByte(Ipv4Util.IP4_BITS_LENGTH);
65 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv4Prefix(), prefixBuf);
67 output.writeByte(IPV6_BITS_LENGTH);
68 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), prefixBuf);
70 while (prefixBuf.readableBytes() % 8 != 0) {
71 prefixBuf.writeZero(1);
73 output.writeBytes(prefixBuf);