6a205886dd0696f7b6545b4258a9260d0019856b
[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 MEDIUM_BYTES_LENGTH = 3;
34
35     public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
36
37     public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
38
39     public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
40
41     public static final int ONE_BYTE_LENGTH = 1;
42
43     public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
44
45     public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
46
47     private ByteBufWriteUtil() {
48         throw new UnsupportedOperationException();
49     }
50
51     /**
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
54      * increased by 4.
55      *
56      * @param value
57      *            Integer value to be written to the output.
58      * @param output
59      *            ByteBuf, where value or zeros are written.
60      */
61     public static void writeInt(final Integer value, final ByteBuf output) {
62         if (value != null) {
63             output.writeInt(value);
64         } else {
65             output.writeZero(INT_BYTES_LENGTH);
66         }
67     }
68
69     /**
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
72      * increased by 3.
73      *
74      * @param value
75      *            Medium value to be written to the output.
76      * @param output
77      *            ByteBuf, where value or zeros are written.
78      */
79     public static void writeMedium(final Integer value, final ByteBuf output) {
80         if (value != null) {
81             output.writeMedium(value);
82         } else {
83             output.writeZero(MEDIUM_BYTES_LENGTH);
84         }
85     }
86
87     /**
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
90      * increased by 2.
91      *
92      * @param value
93      *            Short value to be written to the output.
94      * @param output
95      *            ByteBuf, where value or zeros are written.
96      */
97     public static void writeShort(final Short value, final ByteBuf output) {
98         if (value != null) {
99             output.writeShort(value);
100         } else {
101             output.writeZero(SHORT_BYTES_LENGTH);
102         }
103     }
104
105     /**
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
108      * 8.
109      *
110      * @param value
111      *            Long value to be written to the output.
112      * @param output
113      *            ByteBuf, where value or zeros are written.
114      */
115     public static void writeLong(final Long value, final ByteBuf output) {
116         if (value != null) {
117             output.writeLong(value);
118         } else {
119             output.writeZero(LONG_BYTES_LENGTH);
120         }
121     }
122
123     /**
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.
126      *
127      * @param value
128      *            Boolean value to be written to the output.
129      * @param output
130      *            ByteBuf, where value or zero is written.
131      */
132     public static void writeBoolean(final Boolean value, final ByteBuf output) {
133         if (value != null) {
134             output.writeBoolean(value);
135         } else {
136             output.writeZero(ONE_BYTE_LENGTH);
137         }
138     }
139
140     /**
141      * Writes unsigned byte <code>value</code> if not null, otherwise writes
142      * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
143      * increased by 1.
144      *
145      * @param value
146      *            Short value to be write to the output.
147      * @param output
148      *            ByteBuf, where value or zeros are written.
149      * @deprecated Use {@link #writeUnsignedByte(Uint8, ByteBuf)} instead.
150      */
151     @Deprecated(forRemoval = true)
152     public static void writeUnsignedByte(final Short value, final ByteBuf output) {
153         if (value != null) {
154             output.writeByte(value);
155         } else {
156             output.writeZero(ONE_BYTE_LENGTH);
157         }
158     }
159
160     /**
161      * Writes unsigned byte <code>value</code> if not null, otherwise writes
162      * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
163      * increased by 1.
164      *
165      * @param value
166      *            Short value to be write to the output.
167      * @param output
168      *            ByteBuf, where value or zeros are written.
169      */
170     public static void writeUnsignedByte(final Uint8 value, final ByteBuf output) {
171         if (value != null) {
172             output.writeByte(value.byteValue());
173         } else {
174             output.writeZero(ONE_BYTE_LENGTH);
175         }
176     }
177
178     /**
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.
182      *
183      * @param value
184      *            Integer value to be written to the output.
185      * @param output
186      *            ByteBuf, where value or zeros are written.
187      * @deprecated Use {@link #writeUnsignedShort(Uint16, ByteBuf)} instead.
188      */
189     @Deprecated(forRemoval = true)
190     public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
191         if (value != null) {
192             output.writeShort(value.shortValue());
193         } else {
194             output.writeZero(SHORT_BYTES_LENGTH);
195         }
196     }
197
198     /**
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.
202      *
203      * @param value
204      *            Integer value to be written to the output.
205      * @param output
206      *            ByteBuf, where value or zeros are written.
207      */
208     public static void writeUnsignedShort(final Uint16 value, final ByteBuf output) {
209         if (value != null) {
210             output.writeShort(value.shortValue());
211         } else {
212             output.writeZero(SHORT_BYTES_LENGTH);
213         }
214     }
215
216     /**
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
219      * increased by 4.
220      *
221      * @param value
222      *            Long value to be written to the output.
223      * @param output
224      *            ByteBuf, where value or zeros are written.
225      * @deprecated Use {@link #writeUnsignedInt(Uint32, ByteBuf)} instead.
226      */
227     @Deprecated(forRemoval = true)
228     public static void writeUnsignedInt(final Long value, final ByteBuf output) {
229         if (value != null) {
230             output.writeInt(value.intValue());
231         } else {
232             output.writeZero(INT_BYTES_LENGTH);
233         }
234     }
235
236     /**
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
239      * increased by 4.
240      *
241      * @param value
242      *            Long value to be written to the output.
243      * @param output
244      *            ByteBuf, where value or zeros are written.
245      */
246     public static void writeUnsignedInt(final Uint32 value, final ByteBuf output) {
247         if (value != null) {
248             output.writeInt(value.intValue());
249         } else {
250             output.writeZero(INT_BYTES_LENGTH);
251         }
252     }
253
254     /**
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
257      * increased by 8.
258      *
259      * @param value
260      *            BigInteger value to be written to the output.
261      * @param output
262      *            ByteBuf, where value or zeros are written.
263      * @deprecated Use {@link #writeUnsignedLong(Uint64, ByteBuf)} instead.
264      */
265     @Deprecated(forRemoval = true)
266     public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
267         if (value != null) {
268             output.writeLong(value.longValue());
269         } else {
270             output.writeZero(LONG_BYTES_LENGTH);
271         }
272     }
273
274     /**
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
277      * increased by 8.
278      *
279      * @param value
280      *            BigInteger value to be written to the output.
281      * @param output
282      *            ByteBuf, where value or zeros are written.
283      */
284     public static void writeUnsignedLong(final Uint64 value, final ByteBuf output) {
285         if (value != null) {
286             output.writeLong(value.longValue());
287         } else {
288             output.writeZero(LONG_BYTES_LENGTH);
289         }
290     }
291
292     /**
293      * Writes IPv4 address if not null, otherwise writes zeros to the
294      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
295      *
296      * @param ipv4Address
297      *            IPv4 address to be written to the output.
298      * @param output
299      *            ByteBuf, where ipv4Address or zeros are written.
300      */
301     public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
302         if (ipv4Address != null) {
303             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
304         } else {
305             output.writeZero(Ipv4Util.IP4_LENGTH);
306         }
307     }
308
309     /**
310      * Writes IPv4 address if not null, otherwise writes zeros to the
311      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
312      *
313      * @param ipv4Address
314      *            IPv4 address to be written to the output.
315      * @param output
316      *            ByteBuf, where ipv4Address or zeros are written.
317      */
318     public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
319         if (ipv4Address != null) {
320             output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
321         } else {
322             output.writeZero(Ipv4Util.IP4_LENGTH);
323         }
324     }
325
326     /**
327      * Writes IPv4 prefix if not null, otherwise writes zeros to the
328      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
329      *
330      * @param ipv4Prefix
331      *            IPv4 prefix value to be written to the output. Prefix is
332      *            written in the last byte.
333      * @param output
334      *            ByteBuf, where ipv4Prefix or zeros are written.
335      */
336     public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
337         if (ipv4Prefix != null) {
338             output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
339         } else {
340             output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
341         }
342     }
343
344     /**
345      * Writes IPv6 address if not null, otherwise writes zeros to the
346      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
347      *
348      * @param ipv6Address
349      *            IPv6 address to be written to the output.
350      * @param output
351      *            ByteBuf, where ipv6Address or zeros are written.
352      */
353     public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
354         if (ipv6Address != null) {
355             output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
356         } else {
357             output.writeZero(Ipv6Util.IPV6_LENGTH);
358         }
359     }
360
361     /**
362      * Writes IPv6 address if not null, otherwise writes zeros to the
363      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
364      *
365      * @param ipv6Address
366      *            IPv6 address to be written to the output.
367      * @param output
368      *            ByteBuf, where ipv6Address or zeros are written.
369      */
370     public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
371         if (ipv6Address != null) {
372             output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
373         } else {
374             output.writeZero(Ipv6Util.IPV6_LENGTH);
375         }
376     }
377
378     /**
379      * Writes IPv6 prefix if not null, otherwise writes zeros to the
380      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
381      *
382      * @param ipv6Prefix
383      *            IPv6 prefix to be written to the output. Prefix is written in
384      *            the last byte.
385      * @param output
386      *            ByteBuf, where ipv6Prefix or zeros are written.
387      */
388     public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
389         if (ipv6Prefix != null) {
390             output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
391         } else {
392             output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
393         }
394     }
395
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]);
399     }
400
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]);
404     }
405
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)));
409     }
410
411     /**
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.
414      *
415      * @param value
416      *            Float32 value to be written to the output.
417      * @param output
418      *            ByteBuf, where value or zeros are written.
419      */
420     public static void writeFloat32(final Float32 value, final ByteBuf output) {
421         if (value != null) {
422             output.writeBytes(value.getValue());
423         } else {
424             output.writeZero(FLOAT32_BYTES_LENGTH);
425         }
426     }
427 }