Code clean up
[bgpcep.git] / bgp / 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_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.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;
24
25 public final class L3vpnMcastNlriSerializer {
26     private L3vpnMcastNlriSerializer() {
27         throw new UnsupportedOperationException();
28     }
29
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();
34             if (addPath) {
35                 builder.setPathId(PathIdUtil.readPathId(nlri));
36             }
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)));
41             } else {
42                 builder.setPrefix(new IpPrefix(Ipv4Util.prefixForByteBuf(nlri)));
43             }
44             dests.add(builder.build());
45         }
46         return dests;
47     }
48
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()));
58             } else {
59                 output.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
60                 nlriOutput.writeBytes(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()));
61             }
62             while (nlriOutput.readableBytes() % 8 != 0) {
63                 nlriOutput.writeZero(1);
64             }
65             output.writeBytes(nlriOutput);
66         }
67     }
68 }