BUG-6647 Increase code coverage and clean up II
[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 com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.math.BigInteger;
14 import java.util.BitSet;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
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
22 /**
23  * Utility class for ByteBuf's write methods.
24  */
25 public final class ByteBufWriteUtil {
26
27     public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
28
29     public static final int MEDIUM_BYTES_LENGTH = 3;
30
31     public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
32
33     public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
34
35     public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
36
37     public static final int ONE_BYTE_LENGTH = 1;
38
39     public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
40
41     public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
42
43     private ByteBufWriteUtil() {
44         throw new UnsupportedOperationException();
45     }
46
47     /**
48      * Writes 32-bit integer <code>value</code> if not null, otherwise writes
49      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
50      * increased by 4.
51      *
52      * @param value
53      *            Integer value to be written to the output.
54      * @param output
55      *            ByteBuf, where value or zeros are written.
56      */
57     public static void writeInt(final Integer value, final ByteBuf output) {
58         if (value != null) {
59             output.writeInt(value);
60         } else {
61             output.writeZero(INT_BYTES_LENGTH);
62         }
63     }
64
65     /**
66      * Writes 24-bit integer <code>value</code> if not null, otherwise writes
67      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
68      * increased by 3.
69      *
70      * @param value
71      *            Medium value to be written to the output.
72      * @param output
73      *            ByteBuf, where value or zeros are written.
74      */
75     public static void writeMedium(final Integer value, final ByteBuf output) {
76         if (value != null) {
77             output.writeMedium(value);
78         } else {
79             output.writeZero(MEDIUM_BYTES_LENGTH);
80         }
81     }
82
83     /**
84      * Writes 16-bit short <code>value</code> if not null, otherwise writes
85      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
86      * increased by 2.
87      *
88      * @param value
89      *            Short value to be written to the output.
90      * @param output
91      *            ByteBuf, where value or zeros are written.
92      */
93     public static void writeShort(final Short value, final ByteBuf output) {
94         if (value != null) {
95             output.writeShort(value);
96         } else {
97             output.writeZero(SHORT_BYTES_LENGTH);
98         }
99     }
100
101     /**
102      * Writes 64-bit long <code>value</code> if not null, otherwise writes zeros
103      * to the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
104      * 8.
105      *
106      * @param value
107      *            Long value to be written to the output.
108      * @param output
109      *            ByteBuf, where value or zeros are written.
110      */
111     public static void writeLong(final Long value, final ByteBuf output) {
112         if (value != null) {
113             output.writeLong(value);
114         } else {
115             output.writeZero(LONG_BYTES_LENGTH);
116         }
117     }
118
119     /**
120      * Writes boolean <code>value</code> if not null, otherwise writes zero to
121      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 1.
122      *
123      * @param value
124      *            Boolean value to be written to the output.
125      * @param output
126      *            ByteBuf, where value or zero is written.
127      */
128     public static void writeBoolean(final Boolean value, final ByteBuf output) {
129         if (value != null) {
130             output.writeBoolean(value);
131         } else {
132             output.writeZero(ONE_BYTE_LENGTH);
133         }
134     }
135
136     /**
137      * Writes unsigned byte <code>value</code> if not null, otherwise writes
138      * zero to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
139      * increased by 1.
140      *
141      * @param value
142      *            Short value to be write to the output.
143      * @param output
144      *            ByteBuf, where value or zeros are written.
145      */
146     public static void writeUnsignedByte(final Short value, final ByteBuf output) {
147         if (value != null) {
148             output.writeByte(value);
149         } else {
150             output.writeZero(ONE_BYTE_LENGTH);
151         }
152     }
153
154     /**
155      * Writes unsigned 16-bit short integer <code>value</code> if not null,
156      * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
157      * writerIndex is increased by 2.
158      *
159      * @param value
160      *            Integer value to be written to the output.
161      * @param output
162      *            ByteBuf, where value or zeros are written.
163      */
164     public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
165         if (value != null) {
166             output.writeShort(value.shortValue());
167         } else {
168             output.writeZero(SHORT_BYTES_LENGTH);
169         }
170     }
171
172     /**
173      * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
174      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
175      * increased by 4.
176      *
177      * @param value
178      *            Long value to be written to the output.
179      * @param output
180      *            ByteBuf, where value or zeros are written.
181      */
182     public static void writeUnsignedInt(final Long value, final ByteBuf output) {
183         if (value != null) {
184             output.writeInt(value.intValue());
185         } else {
186             output.writeZero(INT_BYTES_LENGTH);
187         }
188     }
189
190     /**
191      * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
192      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
193      * increased by 8.
194      *
195      * @param value
196      *            BigInteger value to be written to the output.
197      * @param output
198      *            ByteBuf, where value or zeros are written.
199      */
200     public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
201         if (value != null) {
202             output.writeLong(value.longValue());
203         } else {
204             output.writeZero(LONG_BYTES_LENGTH);
205         }
206     }
207
208     /**
209      * Writes IPv4 address if not null, otherwise writes zeros to the
210      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
211      *
212      * @param ipv4Address
213      *            IPv4 address to be written to the output.
214      * @param output
215      *            ByteBuf, where ipv4Address or zeros are written.
216      */
217     public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
218         if (ipv4Address != null) {
219             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
220         } else {
221             output.writeZero(Ipv4Util.IP4_LENGTH);
222         }
223     }
224
225     /**
226      * Writes IPv4 prefix if not null, otherwise writes zeros to the
227      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
228      *
229      * @param ipv4Prefix
230      *            IPv4 prefix value to be written to the output. Prefix is
231      *            written in the last byte.
232      * @param output
233      *            ByteBuf, where ipv4Prefix or zeros are written.
234      */
235     public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
236         if (ipv4Prefix != null) {
237             output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
238         } else {
239             output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
240         }
241     }
242
243     /**
244      * Writes IPv6 address if not null, otherwise writes zeros to the
245      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
246      *
247      * @param ipv6Address
248      *            IPv6 address to be written to the output.
249      * @param output
250      *            ByteBuf, where ipv6Address or zeros are written.
251      */
252     public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
253         if (ipv6Address != null) {
254             output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
255         } else {
256             output.writeZero(Ipv6Util.IPV6_LENGTH);
257         }
258     }
259
260     /**
261      * Writes IPv6 prefix if not null, otherwise writes zeros to the
262      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
263      *
264      * @param ipv6Prefix
265      *            IPv6 prefix to be written to the output. Prefix is written in
266      *            the last byte.
267      * @param output
268      *            ByteBuf, where ipv6Prefix or zeros are written.
269      */
270     public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
271         if (ipv6Prefix != null) {
272             output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
273         } else {
274             output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
275         }
276     }
277
278     public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
279         final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
280         writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
281     }
282
283     public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
284         final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
285         writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
286     }
287
288     private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
289         output.writeByte(prefixBits);
290         output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
291     }
292
293     /**
294      * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
295      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
296      *
297      * @param value
298      *            Float32 value to be written to the output.
299      * @param output
300      *            ByteBuf, where value or zeros are written.
301      */
302     public static void writeFloat32(final Float32 value, final ByteBuf output) {
303         if (value != null) {
304             output.writeBytes(value.getValue());
305         } else {
306             output.writeZero(FLOAT32_BYTES_LENGTH);
307         }
308     }
309
310     /**
311      * Writes BitSet's bits if not null, otherwise writes zeros to the
312      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
313      * specified length.
314      *
315      * @param bitSet
316      *            BitSet values to be written to the output, starting from left most bit.
317      * @param outputLength
318      *            Number of bytes to be written, must be greater than 0.
319      * @param output
320      *            ByteBuf, where bitSet or zeros are written.
321      */
322     public static void writeBitSet(final BitSet bitSet, final int outputLength, final ByteBuf output) {
323         Preconditions.checkArgument(outputLength > 0);
324         if (bitSet != null) {
325             output.writeBytes(ByteArray.bitSetToBytes(bitSet, outputLength));
326         } else {
327             output.writeZero(outputLength);
328         }
329     }
330
331 }