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 32-bit integer <code>value</code> if not null, otherwise writes
33 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
37 * Integer value to be written to the output.
39 * ByteBuf, where value or zeros are written.
41 public static void writeInt(final Integer value, final ByteBuf output) {
42 output.writeInt(value != null ? value : 0);
46 * Writes 24-bit integer <code>value</code> if not null, otherwise writes
47 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
51 * Medium value to be written to the output.
53 * ByteBuf, where value or zeros are written.
55 public static void writeMedium(final Integer value, final ByteBuf output) {
56 output.writeMedium(value != null ? value : 0);
60 * Writes IPv4 address if not null, otherwise writes zeros to the
61 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
64 * IPv4 address to be written to the output.
66 * ByteBuf, where ipv4Address or zeros are written.
68 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
69 if (ipv4Address != null) {
70 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
77 * Writes IPv4 address if not null, otherwise writes zeros to the
78 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
81 * IPv4 address to be written to the output.
83 * ByteBuf, where ipv4Address or zeros are written.
85 public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
86 if (ipv4Address != null) {
87 output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
94 * Writes IPv4 prefix if not null, otherwise writes zeros to the
95 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
98 * IPv4 prefix value to be written to the output. Prefix is
99 * written in the last byte.
101 * ByteBuf, where ipv4Prefix or zeros are written.
103 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
104 if (ipv4Prefix != null) {
105 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
107 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
112 * Writes IPv6 address if not null, otherwise writes zeros to the
113 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
116 * IPv6 address to be written to the output.
118 * ByteBuf, where ipv6Address or zeros are written.
120 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
121 if (ipv6Address != null) {
122 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
124 output.writeZero(Ipv6Util.IPV6_LENGTH);
129 * Writes IPv6 address if not null, otherwise writes zeros to the
130 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
133 * IPv6 address to be written to the output.
135 * ByteBuf, where ipv6Address or zeros are written.
137 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
138 if (ipv6Address != null) {
139 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
141 output.writeZero(Ipv6Util.IPV6_LENGTH);
146 * Writes IPv6 prefix if not null, otherwise writes zeros to the
147 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
150 * IPv6 prefix to be written to the output. Prefix is written in
153 * ByteBuf, where ipv6Prefix or zeros are written.
155 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
156 if (ipv6Prefix != null) {
157 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
159 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
163 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
164 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
165 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
168 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
169 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
170 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
173 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
174 output.writeByte(prefixBits);
175 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
179 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
180 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
183 * Float32 value to be written to the output.
185 * ByteBuf, where value or zeros are written.
187 public static void writeFloat32(final Float32 value, final ByteBuf output) {
189 output.writeBytes(value.getValue());