28bfdac9696c01cf934e9165140fe573b0d0e249
[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 package org.opendaylight.protocol.util;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
19
20 /**
21  * Utility class for ByteBuf's write methods.
22  */
23 public final class ByteBufWriteUtil {
24     public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
25     public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
26
27     private ByteBufWriteUtil() {
28         // Hidden on purpose
29     }
30
31     /**
32      * Writes 32-bit integer <code>value</code> if not null, otherwise writes
33      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
34      * increased by 4.
35      *
36      * @param value
37      *            Integer value to be written to the output.
38      * @param output
39      *            ByteBuf, where value or zeros are written.
40      */
41     public static void writeInt(final Integer value, final ByteBuf output) {
42         output.writeInt(value != null ? value : 0);
43     }
44
45     /**
46      * Writes 24-bit integer <code>value</code> if not null, otherwise writes
47      * zeros to the <code>output</code> ByteBuf. ByteBuf's writerIndex is
48      * increased by 3.
49      *
50      * @param value
51      *            Medium value to be written to the output.
52      * @param output
53      *            ByteBuf, where value or zeros are written.
54      */
55     public static void writeMedium(final Integer value, final ByteBuf output) {
56         output.writeMedium(value != null ? value : 0);
57     }
58
59     /**
60      * Writes IPv4 address if not null, otherwise writes zeros to the
61      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
62      *
63      * @param ipv4Address
64      *            IPv4 address to be written to the output.
65      * @param output
66      *            ByteBuf, where ipv4Address or zeros are written.
67      */
68     public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
69         if (ipv4Address != null) {
70             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
71         } else {
72             output.writeInt(0);
73         }
74     }
75
76     /**
77      * Writes IPv4 address if not null, otherwise writes zeros to the
78      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
79      *
80      * @param ipv4Address
81      *            IPv4 address to be written to the output.
82      * @param output
83      *            ByteBuf, where ipv4Address or zeros are written.
84      */
85     public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
86         if (ipv4Address != null) {
87             output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
88         } else {
89             output.writeInt(0);
90         }
91     }
92
93     /**
94      * Writes IPv4 prefix if not null, otherwise writes zeros to the
95      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
96      *
97      * @param ipv4Prefix
98      *            IPv4 prefix value to be written to the output. Prefix is
99      *            written in the last byte.
100      * @param output
101      *            ByteBuf, where ipv4Prefix or zeros are written.
102      */
103     public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
104         if (ipv4Prefix != null) {
105             output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
106         } else {
107             output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
108         }
109     }
110
111     /**
112      * Writes IPv6 address if not null, otherwise writes zeros to the
113      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
114      *
115      * @param ipv6Address
116      *            IPv6 address to be written to the output.
117      * @param output
118      *            ByteBuf, where ipv6Address or zeros are written.
119      */
120     public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
121         if (ipv6Address != null) {
122             output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
123         } else {
124             output.writeZero(Ipv6Util.IPV6_LENGTH);
125         }
126     }
127
128     /**
129      * Writes IPv6 address if not null, otherwise writes zeros to the
130      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
131      *
132      * @param ipv6Address
133      *            IPv6 address to be written to the output.
134      * @param output
135      *            ByteBuf, where ipv6Address or zeros are written.
136      */
137     public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
138         if (ipv6Address != null) {
139             output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
140         } else {
141             output.writeZero(Ipv6Util.IPV6_LENGTH);
142         }
143     }
144
145     /**
146      * Writes IPv6 prefix if not null, otherwise writes zeros to the
147      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
148      *
149      * @param ipv6Prefix
150      *            IPv6 prefix to be written to the output. Prefix is written in
151      *            the last byte.
152      * @param output
153      *            ByteBuf, where ipv6Prefix or zeros are written.
154      */
155     public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
156         if (ipv6Prefix != null) {
157             output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
158         } else {
159             output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
160         }
161     }
162
163     public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
164         final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
165         writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
166     }
167
168     public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
169         final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
170         writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
171     }
172
173     private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
174         output.writeByte(prefixBits);
175         output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
176     }
177
178     /**
179      * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
180      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
181      *
182      * @param value
183      *            Float32 value to be written to the output.
184      * @param output
185      *            ByteBuf, where value or zeros are written.
186      */
187     public static void writeFloat32(final Float32 value, final ByteBuf output) {
188         if (value != null) {
189             output.writeBytes(value.getValue());
190         } else {
191             output.writeInt(0);
192         }
193     }
194 }