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_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.Ipv4Util;
20 import org.opendaylight.protocol.util.Ipv6Util;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestinationBuilder;
25 public final class L3vpnMcastNlriSerializer {
26 private L3vpnMcastNlriSerializer() {
27 throw new UnsupportedOperationException();
30 static List<L3vpnMcastDestination> extractDest(final ByteBuf nlri, final boolean addPath) {
31 List<L3vpnMcastDestination> dests = new ArrayList<>();
32 while (nlri.isReadable()) {
33 final L3vpnMcastDestinationBuilder builder = new L3vpnMcastDestinationBuilder();
35 builder.setPathId(PathIdUtil.readPathId(nlri));
37 final int length = nlri.readUnsignedByte();
38 builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
39 if (length == IPV6_LENGTH) {
40 builder.setPrefix(new IpPrefix(Ipv6Util.prefixForByteBuf(nlri)));
42 builder.setPrefix(new IpPrefix(Ipv4Util.prefixForByteBuf(nlri)));
44 dests.add(builder.build());
49 public static void serializeNlri(final List<L3vpnMcastDestination> destinationList, final ByteBuf output) {
50 for (final L3vpnMcastDestination dest : destinationList) {
51 PathIdUtil.writePathId(dest.getPathId(), output);
52 ByteBuf nlriOutput = Unpooled.buffer();
53 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriOutput);
54 final IpPrefix prefix = dest.getPrefix();
55 if (prefix.getIpv4Prefix() != null) {
56 output.writeByte(Ipv4Util.IP4_BITS_LENGTH);
57 nlriOutput.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
59 output.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
60 nlriOutput.writeBytes(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()));
62 while (nlriOutput.readableBytes() % 8 != 0) {
63 nlriOutput.writeZero(1);
65 output.writeBytes(nlriOutput);