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 {
31 public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
33 public static final int MEDIUM_BYTES_LENGTH = 3;
35 public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
37 public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
39 public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
41 public static final int ONE_BYTE_LENGTH = 1;
43 public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
45 public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
47 private ByteBufWriteUtil() {
48 throw new UnsupportedOperationException();
52 * Writes 32-bit integer <code>value</code> if not null, otherwise writes
53 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
57 * Integer value to be written to the output.
59 * ByteBuf, where value or zeros are written.
61 public static void writeInt(final Integer value, final ByteBuf output) {
63 output.writeInt(value);
65 output.writeZero(INT_BYTES_LENGTH);
70 * Writes 24-bit integer <code>value</code> if not null, otherwise writes
71 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
75 * Medium value to be written to the output.
77 * ByteBuf, where value or zeros are written.
79 public static void writeMedium(final Integer value, final ByteBuf output) {
81 output.writeMedium(value);
83 output.writeZero(MEDIUM_BYTES_LENGTH);
88 * Writes 16-bit short <code>value</code> if not null, otherwise writes
89 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
93 * Short value to be written to the output.
95 * ByteBuf, where value or zeros are written.
97 public static void writeShort(final Short value, final ByteBuf output) {
99 output.writeShort(value);
101 output.writeZero(SHORT_BYTES_LENGTH);
106 * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
107 * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
111 * Long value to be written to the output.
113 * ByteBuf, where value or zeros are written.
115 public static void writeLong(final Long value, final ByteBuf output) {
117 output.writeLong(value);
119 output.writeZero(LONG_BYTES_LENGTH);
124 * Writes boolean <code>value</code> if not null, otherwise writes zero to
125 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
128 * Boolean value to be written to the output.
130 * ByteBuf, where value or zero is written.
132 public static void writeBoolean(final Boolean value, final ByteBuf output) {
134 output.writeBoolean(value);
136 output.writeZero(ONE_BYTE_LENGTH);
141 * Writes unsigned byte <code>value</code> if not null, otherwise writes
142 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
146 * Short value to be write to the output.
148 * ByteBuf, where value or zeros are written.
149 * @deprecated Use {@link #writeUnsignedByte(Uint8, ByteBuf)} instead.
151 @Deprecated(forRemoval = true)
152 public static void writeUnsignedByte(final Short value, final ByteBuf output) {
154 output.writeByte(value);
156 output.writeZero(ONE_BYTE_LENGTH);
161 * Writes unsigned byte <code>value</code> if not null, otherwise writes
162 * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
166 * Short value to be write to the output.
168 * ByteBuf, where value or zeros are written.
170 public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
172 output.writeByte(value.byteValue());
174 output.writeZero(ONE_BYTE_LENGTH);
179 * Writes unsigned 16-bit short integer <code>value</code> if not null,
180 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
181 * writerIndex is increased by 2.
184 * Integer value to be written to the output.
186 * ByteBuf, where value or zeros are written.
187 * @deprecated Use {@link #writeUnsignedShort(Uint16, ByteBuf)} instead.
189 @Deprecated(forRemoval = true)
190 public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
192 output.writeShort(value.shortValue());
194 output.writeZero(SHORT_BYTES_LENGTH);
199 * Writes unsigned 16-bit short integer <code>value</code> if not null,
200 * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
201 * writerIndex is increased by 2.
204 * Integer value to be written to the output.
206 * ByteBuf, where value or zeros are written.
208 public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
210 output.writeShort(value.shortValue());
212 output.writeZero(SHORT_BYTES_LENGTH);
217 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
218 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
222 * Long value to be written to the output.
224 * ByteBuf, where value or zeros are written.
225 * @deprecated Use {@link #writeUnsignedInt(Uint32, ByteBuf)} instead.
227 @Deprecated(forRemoval = true)
228 public static void writeUnsignedInt(final Long value, final ByteBuf output) {
230 output.writeInt(value.intValue());
232 output.writeZero(INT_BYTES_LENGTH);
237 * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
238 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
242 * Long value to be written to the output.
244 * ByteBuf, where value or zeros are written.
246 public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
248 output.writeInt(value.intValue());
250 output.writeZero(INT_BYTES_LENGTH);
255 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
256 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
260 * BigInteger value to be written to the output.
262 * ByteBuf, where value or zeros are written.
263 * @deprecated Use {@link #writeUnsignedLong(Uint64, ByteBuf)} instead.
265 @Deprecated(forRemoval = true)
266 public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
268 output.writeLong(value.longValue());
270 output.writeZero(LONG_BYTES_LENGTH);
275 * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
276 * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
280 * BigInteger value to be written to the output.
282 * ByteBuf, where value or zeros are written.
284 public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
286 output.writeLong(value.longValue());
288 output.writeZero(LONG_BYTES_LENGTH);
293 * Writes IPv4 address if not null, otherwise writes zeros to the
294 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
297 * IPv4 address to be written to the output.
299 * ByteBuf, where ipv4Address or zeros are written.
301 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
302 if (ipv4Address != null) {
303 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
305 output.writeZero(Ipv4Util.IP4_LENGTH);
310 * Writes IPv4 address if not null, otherwise writes zeros to the
311 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
314 * IPv4 address to be written to the output.
316 * ByteBuf, where ipv4Address or zeros are written.
318 public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
319 if (ipv4Address != null) {
320 output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
322 output.writeZero(Ipv4Util.IP4_LENGTH);
327 * Writes IPv4 prefix if not null, otherwise writes zeros to the
328 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
331 * IPv4 prefix value to be written to the output. Prefix is
332 * written in the last byte.
334 * ByteBuf, where ipv4Prefix or zeros are written.
336 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
337 if (ipv4Prefix != null) {
338 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
340 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
345 * Writes IPv6 address if not null, otherwise writes zeros to the
346 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
349 * IPv6 address to be written to the output.
351 * ByteBuf, where ipv6Address or zeros are written.
353 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
354 if (ipv6Address != null) {
355 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
357 output.writeZero(Ipv6Util.IPV6_LENGTH);
362 * Writes IPv6 address if not null, otherwise writes zeros to the
363 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
366 * IPv6 address to be written to the output.
368 * ByteBuf, where ipv6Address or zeros are written.
370 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
371 if (ipv6Address != null) {
372 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
374 output.writeZero(Ipv6Util.IPV6_LENGTH);
379 * Writes IPv6 prefix if not null, otherwise writes zeros to the
380 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
383 * IPv6 prefix to be written to the output. Prefix is written in
386 * ByteBuf, where ipv6Prefix or zeros are written.
388 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
389 if (ipv6Prefix != null) {
390 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
392 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
396 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
397 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
398 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
401 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
402 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
403 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
406 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
407 output.writeByte(prefixBits);
408 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
412 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
413 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
416 * Float32 value to be written to the output.
418 * ByteBuf, where value or zeros are written.
420 public static void writeFloat32(final Float32 value, final ByteBuf output) {
422 output.writeBytes(value.getValue());
424 output.writeZero(FLOAT32_BYTES_LENGTH);