5e5422db776d3d243bb3d775d9c391fdd96e2d85
[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     public static final int AFI_BYTES_LENGTH = 2;
44
45     public static final int SAFI_BYTES_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      */
150     public static void writeUnsignedByte(final Short value, final ByteBuf output) {
151         if (value != null) {
152             output.writeByte(value);
153         } else {
154             output.writeZero(ONE_BYTE_LENGTH);
155         }
156     }
157
158     /**
159      * Writes unsigned 16-bit short integer <code>value</code> if not null,
160      * otherwise writes zeros to the <code>output</code> ByteBuf. ByteBuf's
161      * writerIndex is increased by 2.
162      *
163      * @param value
164      *            Integer value to be written to the output.
165      * @param output
166      *            ByteBuf, where value or zeros are written.
167      */
168     public static void writeUnsignedShort(final Integer value, final ByteBuf output) {
169         if (value != null) {
170             output.writeShort(value.shortValue());
171         } else {
172             output.writeZero(SHORT_BYTES_LENGTH);
173         }
174     }
175
176     /**
177      * Writes unsigned 32-bit integer <code>value</code> if not null, otherwise
178      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
179      * increased by 4.
180      *
181      * @param value
182      *            Long value to be written to the output.
183      * @param output
184      *            ByteBuf, where value or zeros are written.
185      */
186     public static void writeUnsignedInt(final Long value, final ByteBuf output) {
187         if (value != null) {
188             output.writeInt(value.intValue());
189         } else {
190             output.writeZero(INT_BYTES_LENGTH);
191         }
192     }
193
194     /**
195      * Writes unsigned 64-bit integer <code>value</code> if not null, otherwise
196      * writes zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
197      * increased by 8.
198      *
199      * @param value
200      *            BigInteger value to be written to the output.
201      * @param output
202      *            ByteBuf, where value or zeros are written.
203      */
204     public static void writeUnsignedLong(final BigInteger value, final ByteBuf output) {
205         if (value != null) {
206             output.writeLong(value.longValue());
207         } else {
208             output.writeZero(LONG_BYTES_LENGTH);
209         }
210     }
211
212     /**
213      * Writes IPv4 address if not null, otherwise writes zeros to the
214      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
215      *
216      * @param ipv4Address
217      *            IPv4 address to be written to the output.
218      * @param output
219      *            ByteBuf, where ipv4Address or zeros are written.
220      */
221     public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
222         if (ipv4Address != null) {
223             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
224         } else {
225             output.writeZero(Ipv4Util.IP4_LENGTH);
226         }
227     }
228
229     /**
230      * Writes IPv4 prefix if not null, otherwise writes zeros to the
231      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
232      *
233      * @param ipv4Prefix
234      *            IPv4 prefix value to be written to the output. Prefix is
235      *            written in the last byte.
236      * @param output
237      *            ByteBuf, where ipv4Prefix or zeros are written.
238      */
239     public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
240         if (ipv4Prefix != null) {
241             output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
242         } else {
243             output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
244         }
245     }
246
247     /**
248      * Writes IPv6 address if not null, otherwise writes zeros to the
249      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
250      *
251      * @param ipv6Address
252      *            IPv6 address to be written to the output.
253      * @param output
254      *            ByteBuf, where ipv6Address or zeros are written.
255      */
256     public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
257         if (ipv6Address != null) {
258             output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
259         } else {
260             output.writeZero(Ipv6Util.IPV6_LENGTH);
261         }
262     }
263
264     /**
265      * Writes IPv6 prefix if not null, otherwise writes zeros to the
266      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
267      *
268      * @param ipv6Prefix
269      *            IPv6 prefix to be written to the output. Prefix is written in
270      *            the last byte.
271      * @param output
272      *            ByteBuf, where ipv6Prefix or zeros are written.
273      */
274     public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
275         if (ipv6Prefix != null) {
276             output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
277         } else {
278             output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
279         }
280     }
281
282     public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
283         final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
284         writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
285     }
286
287     public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
288         final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
289         writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
290     }
291
292     private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
293         output.writeByte(prefixBits);
294         output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
295     }
296
297     /**
298      * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
299      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
300      *
301      * @param value
302      *            Float32 value to be written to the output.
303      * @param output
304      *            ByteBuf, where value or zeros are written.
305      */
306     public static void writeFloat32(final Float32 value, final ByteBuf output) {
307         if (value != null) {
308             output.writeBytes(value.getValue());
309         } else {
310             output.writeZero(FLOAT32_BYTES_LENGTH);
311         }
312     }
313
314     /**
315      * Writes BitSet's bits if not null, otherwise writes zeros to the
316      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by
317      * specified length.
318      *
319      * @param bitSet
320      *            BitSet values to be written to the output, starting from left most bit.
321      * @param outputLength
322      *            Number of bytes to be written, must be greater than 0.
323      * @param output
324      *            ByteBuf, where bitSet or zeros are written.
325      */
326     public static void writeBitSet(final BitSet bitSet, final int outputLength, final ByteBuf output) {
327         Preconditions.checkArgument(outputLength > 0);
328         if (bitSet != null) {
329             output.writeBytes(ByteArray.bitSetToBytes(bitSet, outputLength));
330         } else {
331             output.writeZero(outputLength);
332         }
333     }
334
335 }