991cca391ad094770e879dee9d84bae6e8071905
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / ByteBufWriteUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.util;
10
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;
25
26 /**
27  * Utility class for ByteBuf's write methods.
28  */
29 public final class ByteBufWriteUtil {
30
31     public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
32
33     public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
34
35     public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
36
37     public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
38
39     public static final int ONE_BYTE_LENGTH = 1;
40
41     public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
42
43     public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
44
45     private ByteBufWriteUtil() {
46         throw new UnsupportedOperationException();
47     }
48
49     /**
50      * Writes 32-bit integer <code>value</code> if not null, otherwise writes
51      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
52      * increased by 4.
53      *
54      * @param value
55      *            Integer value to be written to the output.
56      * @param output
57      *            ByteBuf, where value or zeros are written.
58      */
59     public static void writeInt(final Integer value, final ByteBuf output) {
60         output.writeInt(value != null ? value : 0);
61     }
62
63     /**
64      * Writes 24-bit integer <code>value</code> if not null, otherwise writes
65      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
66      * increased by 3.
67      *
68      * @param value
69      *            Medium value to be written to the output.
70      * @param output
71      *            ByteBuf, where value or zeros are written.
72      */
73     public static void writeMedium(final Integer value, final ByteBuf output) {
74         output.writeMedium(value != null ? value : 0);
75     }
76
77     /**
78      * Writes 16-bit short <code>value</code> if not null, otherwise writes
79      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
80      * increased by 2.
81      *
82      * @param value
83      *            Short value to be written to the output.
84      * @param output
85      *            ByteBuf, where value or zeros are written.
86      */
87     public static void writeShort(final Short value, final ByteBuf output) {
88         output.writeShort(value != null ? value : 0);
89     }
90
91     /**
92      * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
93      * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
94      * 8.
95      *
96      * @param value
97      *            Long value to be written to the output.
98      * @param output
99      *            ByteBuf, where value or zeros are written.
100      */
101     public static void writeLong(final Long value, final ByteBuf output) {
102         output.writeLong(value != null ? value : 0L);
103     }
104
105     /**
106      * Writes boolean <code>value</code> if not null, otherwise writes zero to
107      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
108      *
109      * @param value
110      *            Boolean value to be written to the output.
111      * @param output
112      *            ByteBuf, where value or zero is written.
113      */
114     public static void writeBoolean(final Boolean value, final ByteBuf output) {
115         if (value != null) {
116             output.writeBoolean(value);
117         } else {
118             output.writeByte(0);
119         }
120     }
121
122     /**
123      * Writes unsigned byte <code>value</code> if not null, otherwise writes
124      * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
125      * increased by 1.
126      *
127      * @param value
128      *            Short value to be write to the output.
129      * @param output
130      *            ByteBuf, where value or zeros are written.
131      * @deprecated Use {@link #writeUnsignedByte(Uint8, ByteBuf)} instead.
132      */
133     @Deprecated(forRemoval = true)
134     public static void writeUnsignedByte(final Short value, final ByteBuf output) {
135         output.writeByte(value != null ? value : 0);
136     }
137
138     /**
139      * Writes unsigned byte <code>value</code> if not null, otherwise writes
140      * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
141      * increased by 1.
142      *
143      * @param value
144      *            Short value to be write to the output.
145      * @param output
146      *            ByteBuf, where value or zeros are written.
147      */
148     public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
149         output.writeByte(value != null ? value.byteValue() : 0);
150     }
151
152     /**
153      * Writes unsigned 16-bit short integer <code>value</code> if not null,
154      * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
155      * writerIndex is increased by 2.
156      *
157      * @param value
158      *            Integer value to be written to the output.
159      * @param output
160      *            ByteBuf, where value or zeros are written.
161      * @deprecated Use {@link #writeUnsignedShort(Uint16, ByteBuf)} instead.
162      */
163     @Deprecated(forRemoval = true)
164     public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
165         output.writeShort(value != null ? value.shortValue() : 0);
166     }
167
168     /**
169      * Writes unsigned 16-bit short integer <code>value</code> if not null,
170      * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
171      * writerIndex is increased by 2.
172      *
173      * @param value
174      *            Integer value to be written to the output.
175      * @param output
176      *            ByteBuf, where value or zeros are written.
177      */
178     public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
179         output.writeShort(value != null ? value.shortValue() : 0);
180     }
181
182     /**
183      * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
184      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
185      * increased by 4.
186      *
187      * @param value
188      *            Long value to be written to the output.
189      * @param output
190      *            ByteBuf, where value or zeros are written.
191      * @deprecated Use {@link #writeUnsignedInt(Uint32, ByteBuf)} instead.
192      */
193     @Deprecated(forRemoval = true)
194     public static void writeUnsignedInt(final Long value, final ByteBuf output) {
195         output.writeInt(value != null ? value.intValue() : 0);
196     }
197
198     /**
199      * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
200      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
201      * increased by 4.
202      *
203      * @param value
204      *            Long value to be written to the output.
205      * @param output
206      *            ByteBuf, where value or zeros are written.
207      */
208     public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
209         output.writeInt(value != null ? value.intValue() : 0);
210     }
211
212     /**
213      * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
214      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
215      * increased by 8.
216      *
217      * @param value
218      *            BigInteger value to be written to the output.
219      * @param output
220      *            ByteBuf, where value or zeros are written.
221      * @deprecated Use {@link #writeUnsignedLong(Uint64, ByteBuf)} instead.
222      */
223     @Deprecated(forRemoval = true)
224     public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
225         output.writeLong(value != null ? value.longValue() : 0L);
226     }
227
228     /**
229      * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
230      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
231      * increased by 8.
232      *
233      * @param value
234      *            BigInteger value to be written to the output.
235      * @param output
236      *            ByteBuf, where value or zeros are written.
237      */
238     public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
239         output.writeLong(value != null ? value.longValue() : 0L);
240     }
241
242     /**
243      * Writes IPv4 address if not null, otherwise writes zeros to the
244      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
245      *
246      * @param ipv4Address
247      *            IPv4 address to be written to the output.
248      * @param output
249      *            ByteBuf, where ipv4Address or zeros are written.
250      */
251     public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
252         if (ipv4Address != null) {
253             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
254         } else {
255             output.writeInt(0);
256         }
257     }
258
259     /**
260      * Writes IPv4 address if not null, otherwise writes zeros to the
261      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
262      *
263      * @param ipv4Address
264      *            IPv4 address to be written to the output.
265      * @param output
266      *            ByteBuf, where ipv4Address or zeros are written.
267      */
268     public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
269         if (ipv4Address != null) {
270             output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
271         } else {
272             output.writeInt(0);
273         }
274     }
275
276     /**
277      * Writes IPv4 prefix if not null, otherwise writes zeros to the
278      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
279      *
280      * @param ipv4Prefix
281      *            IPv4 prefix value to be written to the output. Prefix is
282      *            written in the last byte.
283      * @param output
284      *            ByteBuf, where ipv4Prefix or zeros are written.
285      */
286     public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
287         if (ipv4Prefix != null) {
288             output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
289         } else {
290             output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
291         }
292     }
293
294     /**
295      * Writes IPv6 address if not null, otherwise writes zeros to the
296      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
297      *
298      * @param ipv6Address
299      *            IPv6 address to be written to the output.
300      * @param output
301      *            ByteBuf, where ipv6Address or zeros are written.
302      */
303     public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
304         if (ipv6Address != null) {
305             output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
306         } else {
307             output.writeZero(Ipv6Util.IPV6_LENGTH);
308         }
309     }
310
311     /**
312      * Writes IPv6 address if not null, otherwise writes zeros to the
313      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
314      *
315      * @param ipv6Address
316      *            IPv6 address to be written to the output.
317      * @param output
318      *            ByteBuf, where ipv6Address or zeros are written.
319      */
320     public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
321         if (ipv6Address != null) {
322             output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
323         } else {
324             output.writeZero(Ipv6Util.IPV6_LENGTH);
325         }
326     }
327
328     /**
329      * Writes IPv6 prefix if not null, otherwise writes zeros to the
330      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
331      *
332      * @param ipv6Prefix
333      *            IPv6 prefix to be written to the output. Prefix is written in
334      *            the last byte.
335      * @param output
336      *            ByteBuf, where ipv6Prefix or zeros are written.
337      */
338     public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
339         if (ipv6Prefix != null) {
340             output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
341         } else {
342             output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
343         }
344     }
345
346     public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
347         final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
348         writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
349     }
350
351     public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
352         final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
353         writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
354     }
355
356     private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
357         output.writeByte(prefixBits);
358         output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
359     }
360
361     /**
362      * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
363      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
364      *
365      * @param value
366      *            Float32 value to be written to the output.
367      * @param output
368      *            ByteBuf, where value or zeros are written.
369      */
370     public static void writeFloat32(final Float32 value, final ByteBuf output) {
371         if (value != null) {
372             output.writeBytes(value.getValue());
373         } else {
374             output.writeInt(0);
375         }
376     }
377 }