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 24-bit integer <code>value</code> if not null, otherwise writes
33 * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
37 * Medium value to be written to the output.
39 * ByteBuf, where value or zeros are written.
41 public static void writeMedium(final Integer value, final ByteBuf output) {
42 output.writeMedium(value != null ? value : 0);
46 * Writes IPv4 address if not null, otherwise writes zeros to the
47 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
50 * IPv4 address to be written to the output.
52 * ByteBuf, where ipv4Address or zeros are written.
54 public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
55 if (ipv4Address != null) {
56 output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
63 * Writes IPv4 address if not null, otherwise writes zeros to the
64 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
67 * IPv4 address to be written to the output.
69 * ByteBuf, where ipv4Address or zeros are written.
71 public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
72 if (ipv4Address != null) {
73 output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
80 * Writes IPv4 prefix if not null, otherwise writes zeros to the
81 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
84 * IPv4 prefix value to be written to the output. Prefix is
85 * written in the last byte.
87 * ByteBuf, where ipv4Prefix or zeros are written.
89 public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
90 if (ipv4Prefix != null) {
91 output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
93 output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
98 * Writes IPv6 address if not null, otherwise writes zeros to the
99 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
102 * IPv6 address to be written to the output.
104 * ByteBuf, where ipv6Address or zeros are written.
106 public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
107 if (ipv6Address != null) {
108 output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
110 output.writeZero(Ipv6Util.IPV6_LENGTH);
115 * Writes IPv6 address if not null, otherwise writes zeros to the
116 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
119 * IPv6 address to be written to the output.
121 * ByteBuf, where ipv6Address or zeros are written.
123 public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
124 if (ipv6Address != null) {
125 output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
127 output.writeZero(Ipv6Util.IPV6_LENGTH);
132 * Writes IPv6 prefix if not null, otherwise writes zeros to the
133 * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
136 * IPv6 prefix to be written to the output. Prefix is written in
139 * ByteBuf, where ipv6Prefix or zeros are written.
141 public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
142 if (ipv6Prefix != null) {
143 output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
145 output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
149 public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
150 final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
151 writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
154 public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
155 final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
156 writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
159 private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
160 output.writeByte(prefixBits);
161 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
165 * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
166 * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
169 * Float32 value to be written to the output.
171 * ByteBuf, where value or zeros are written.
173 public static void writeFloat32(final Float32 value, final ByteBuf output) {
175 output.writeBytes(value.getValue());