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