fa2e4ac1f6d53b85cf0e7c8b69fb832b47ad5ed4
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / attributes / tunnel / identifier / PAddressPMulticastGroupUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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 package org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.util.Ipv4Util;
12 import org.opendaylight.protocol.util.Ipv6Util;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PAddressPMulticastGroup;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.bidir.pim.tree.BidirPimTreeBuilder;
16
17 final class PAddressPMulticastGroupUtil {
18     private PAddressPMulticastGroupUtil() {
19         // Hidden on purpose
20     }
21
22     static void serializeIpAddress(final IpAddress ipAddress, final ByteBuf byteBuf) {
23         if (ipAddress.getIpv4Address() != null) {
24             byteBuf.writeBytes(Ipv4Util.bytesForAddress(ipAddress.getIpv4Address()));
25         } else {
26             byteBuf.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address()));
27         }
28     }
29
30     static IpAddress parseIpAddress(final int ipLength, final ByteBuf buffer) {
31         if (ipLength == Ipv6Util.IPV6_LENGTH) {
32             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
33         } else if (ipLength == Ipv4Util.IP4_LENGTH) {
34             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
35         }
36         return null;
37     }
38
39     static void serializeSenderPMulticastGroup(final PAddressPMulticastGroup bidir, final ByteBuf byteBuf) {
40         serializeIpAddress(bidir.getPAddress(), byteBuf);
41         serializeIpAddress(bidir.getPMulticastGroup(), byteBuf);
42     }
43
44     static PAddressPMulticastGroup parseSenderPMulticastGroup(final ByteBuf buffer) {
45         final int ipLength = buffer.readableBytes() / 2;
46         final IpAddress pSenderAddress = parseIpAddress(ipLength, buffer);
47         final IpAddress pMulticastGroup = parseIpAddress(ipLength, buffer);
48         return new BidirPimTreeBuilder().setPAddress(pSenderAddress).setPMulticastGroup(pMulticastGroup).build();
49     }
50 }