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
9 package org.opendaylight.protocol.util;
11 import io.netty.buffer.ByteBuf;
12 import java.math.BigInteger;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
21 import org.opendaylight.yangtools.yang.common.Uint16;
22 import org.opendaylight.yangtools.yang.common.Uint32;
23 import org.opendaylight.yangtools.yang.common.Uint64;
24 import org.opendaylight.yangtools.yang.common.Uint8;
27 * Utility class for ByteBuf's write methods.
29 public final class ByteBufWriteUtil {
30 public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
31 public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
33 private ByteBufWriteUtil() {
34 throw new UnsupportedOperationException();
38 * Writes 32-bit integer <code>value</code> if not null, otherwise writes
39 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
43 * Integer value to be written to the output.
45 * ByteBuf, where value or zeros are written.
47 public static void writeInt(final Integer value, final ByteBuf output) {
48 output.writeInt(value != null ? value : 0);
52 * Writes 24-bit integer <code>value</code> if not null, otherwise writes
53 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
57 * Medium value to be written to the output.
59 * ByteBuf, where value or zeros are written.
61 public static void writeMedium(final Integer value, final ByteBuf output) {
62 output.writeMedium(value != null ? value : 0);
66 * Writes 16-bit short <code>value</code> if not null, otherwise writes
67 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
71 * Short value to be written to the output.
73 * ByteBuf, where value or zeros are written.
75 public static void writeShort(final Short value, final ByteBuf output) {
76 output.writeShort(value != null ? value : 0);
80 * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
81 * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
85 * Long value to be written to the output.
87 * ByteBuf, where value or zeros are written.
89 public static void writeLong(final Long value, final ByteBuf output) {
90 output.writeLong(value != null ? value : 0L);
94 * Writes boolean <code>value</code> if not null, otherwise writes zero to
95 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
98 * Boolean value to be written to the output.
100 * ByteBuf, where value or zero is written.
102 public static void writeBoolean(final Boolean value, final ByteBuf output) {
104 output.writeBoolean(value);
111 * Writes unsigned byte <code>value</code> if not null, otherwise writes
112 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
116 * Short value to be write to the output.
118 * ByteBuf, where value or zeros are written.
119 * @deprecated Use {@link #writeUnsignedByte(Uint8, ByteBuf)} instead.
121 @Deprecated(forRemoval = true)
122 public static void writeUnsignedByte(final Short value, final ByteBuf output) {
123 output.writeByte(value != null ? value : 0);
127 * Writes unsigned byte <code>value</code> if not null, otherwise writes
128 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
132 * Short value to be write to the output.
134 * ByteBuf, where value or zeros are written.
136 public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
137 output.writeByte(value != null ? value.byteValue() : 0);
141 * Writes unsigned 16-bit short integer <code>value</code> if not null,
142 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
143 * writerIndex is increased by 2.
146 * Integer value to be written to the output.
148 * ByteBuf, where value or zeros are written.
149 * @deprecated Use {@link #writeUnsignedShort(Uint16, ByteBuf)} instead.
151 @Deprecated(forRemoval = true)
152 public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
153 output.writeShort(value != null ? value.shortValue() : 0);
157 * Writes unsigned 16-bit short integer <code>value</code> if not null,
158 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
159 * writerIndex is increased by 2.
162 * Integer value to be written to the output.
164 * ByteBuf, where value or zeros are written.
166 public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
167 output.writeShort(value != null ? value.shortValue() : 0);
171 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
172 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
176 * Long value to be written to the output.
178 * ByteBuf, where value or zeros are written.
179 * @deprecated Use {@link #writeUnsignedInt(Uint32, ByteBuf)} instead.
181 @Deprecated(forRemoval = true)
182 public static void writeUnsignedInt(final Long value, final ByteBuf output) {
183 output.writeInt(value != null ? value.intValue() : 0);
187 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
188 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
192 * Long value to be written to the output.
194 * ByteBuf, where value or zeros are written.
196 public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
197 output.writeInt(value != null ? value.intValue() : 0);
201 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
202 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
206 * BigInteger value to be written to the output.
208 * ByteBuf, where value or zeros are written.
209 * @deprecated Use {@link #writeUnsignedLong(Uint64, ByteBuf)} instead.
211 @Deprecated(forRemoval = true)
212 public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
213 output.writeLong(value != null ? value.longValue() : 0L);
217 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
218 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
222 * BigInteger value to be written to the output.
224 * ByteBuf, where value or zeros are written.
226 public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
227 output.writeLong(value != null ? value.longValue() : 0L);
231 * Writes IPv4 address if not null, otherwise writes zeros to the
232 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
235 * IPv4 address to be written to the output.
237 * ByteBuf, where ipv4Address or zeros are written.
239 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
240 if (ipv4Address != null) {
241 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
248 * Writes IPv4 address if not null, otherwise writes zeros to the
249 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
252 * IPv4 address to be written to the output.
254 * ByteBuf, where ipv4Address or zeros are written.
256 public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
257 if (ipv4Address != null) {
258 output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
265 * Writes IPv4 prefix if not null, otherwise writes zeros to the
266 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
269 * IPv4 prefix value to be written to the output. Prefix is
270 * written in the last byte.
272 * ByteBuf, where ipv4Prefix or zeros are written.
274 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
275 if (ipv4Prefix != null) {
276 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
278 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
283 * Writes IPv6 address if not null, otherwise writes zeros to the
284 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
287 * IPv6 address to be written to the output.
289 * ByteBuf, where ipv6Address or zeros are written.
291 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
292 if (ipv6Address != null) {
293 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
295 output.writeZero(Ipv6Util.IPV6_LENGTH);
300 * Writes IPv6 address if not null, otherwise writes zeros to the
301 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
304 * IPv6 address to be written to the output.
306 * ByteBuf, where ipv6Address or zeros are written.
308 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
309 if (ipv6Address != null) {
310 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
312 output.writeZero(Ipv6Util.IPV6_LENGTH);
317 * Writes IPv6 prefix if not null, otherwise writes zeros to the
318 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
321 * IPv6 prefix to be written to the output. Prefix is written in
324 * ByteBuf, where ipv6Prefix or zeros are written.
326 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
327 if (ipv6Prefix != null) {
328 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
330 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
334 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
335 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
336 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
339 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
340 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
341 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
344 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
345 output.writeByte(prefixBits);
346 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
350 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
351 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
354 * Float32 value to be written to the output.
356 * ByteBuf, where value or zeros are written.
358 public static void writeFloat32(final Float32 value, final ByteBuf output) {
360 output.writeBytes(value.getValue());