691f47189e375297c291a12a52743723c82491dd
[bgpcep.git] / bgp / extensions / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / mcast / nlri / L3vpnMcastNlriSerializer.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.bgp.l3vpn.mcast.nlri;
10
11 import static org.opendaylight.protocol.util.Ipv6Util.IPV6_BITS_LENGTH;
12
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;
25
26 public final class L3vpnMcastNlriSerializer {
27     private L3vpnMcastNlriSerializer() {
28         throw new UnsupportedOperationException();
29     }
30
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();
35             if (addPath) {
36                 builder.setPathId(PathIdUtil.readPathId(nlri));
37             }
38             final int length = nlri.readUnsignedByte();
39
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)));
44             } else {
45                 builder.setPrefix(new IpPrefix(Ipv4Util.prefixForByteBuf(nlri)));
46             }
47             dests.add(builder.build());
48             int readed = initialLength - nlri.readableBytes();
49             while (readed % 8 != 0) {
50                 nlri.readByte();
51                 readed = initialLength - nlri.readableBytes();
52             }
53         }
54         return dests;
55     }
56
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);
66             } else {
67                 output.writeByte(IPV6_BITS_LENGTH);
68                 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), prefixBuf);
69             }
70             while (prefixBuf.readableBytes() % 8 != 0) {
71                 prefixBuf.writeZero(1);
72             }
73             output.writeBytes(prefixBuf);
74         }
75     }
76 }