2 * Copyright (c) 2016 Cisco Systems, Inc. and others. 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
8 package org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier;
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;
17 final class PAddressPMulticastGroupUtil {
18 private PAddressPMulticastGroupUtil() {
22 static void serializeIpAddress(final IpAddress ipAddress, final ByteBuf byteBuf) {
23 if (ipAddress.getIpv4Address() != null) {
24 byteBuf.writeBytes(Ipv4Util.bytesForAddress(ipAddress.getIpv4Address()));
26 byteBuf.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address()));
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));
39 static void serializeSenderPMulticastGroup(final PAddressPMulticastGroup bidir, final ByteBuf byteBuf) {
40 serializeIpAddress(bidir.getPAddress(), byteBuf);
41 serializeIpAddress(bidir.getPMulticastGroup(), byteBuf);
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();