2 * Copyright (c) 2014 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.util;
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
21 * Utility class for ByteBuf's write methods.
23 public final class ByteBufWriteUtil {
24 public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
25 public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
27 private ByteBufWriteUtil() {
32 * Writes IPv4 address if not null, otherwise writes zeros to the
33 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
36 * IPv4 address to be written to the output.
38 * ByteBuf, where ipv4Address or zeros are written.
40 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
41 if (ipv4Address != null) {
42 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
49 * Writes IPv4 address if not null, otherwise writes zeros to the
50 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
53 * IPv4 address to be written to the output.
55 * ByteBuf, where ipv4Address or zeros are written.
57 public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
58 if (ipv4Address != null) {
59 output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
66 * Writes IPv4 prefix if not null, otherwise writes zeros to the
67 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
70 * IPv4 prefix value to be written to the output. Prefix is
71 * written in the last byte.
73 * ByteBuf, where ipv4Prefix or zeros are written.
75 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
76 if (ipv4Prefix != null) {
77 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
79 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
84 * Writes IPv6 address if not null, otherwise writes zeros to the
85 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
88 * IPv6 address to be written to the output.
90 * ByteBuf, where ipv6Address or zeros are written.
92 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
93 if (ipv6Address != null) {
94 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
96 output.writeZero(Ipv6Util.IPV6_LENGTH);
101 * Writes IPv6 address if not null, otherwise writes zeros to the
102 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
105 * IPv6 address to be written to the output.
107 * ByteBuf, where ipv6Address or zeros are written.
109 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
110 if (ipv6Address != null) {
111 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
113 output.writeZero(Ipv6Util.IPV6_LENGTH);
118 * Writes IPv6 prefix if not null, otherwise writes zeros to the
119 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
122 * IPv6 prefix to be written to the output. Prefix is written in
125 * ByteBuf, where ipv6Prefix or zeros are written.
127 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
128 if (ipv6Prefix != null) {
129 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
131 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
135 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
136 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
137 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
140 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
141 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
142 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
145 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
146 output.writeByte(prefixBits);
147 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
151 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
152 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
155 * Float32 value to be written to the output.
157 * ByteBuf, where value or zeros are written.
159 public static void writeFloat32(final Float32 value, final ByteBuf output) {
161 output.writeBytes(value.getValue());