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