Remove ByteBufWriteUtil.writeInt()
[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 24-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 3.
35      *
36      * @param value
37      *            Medium value to be written to the output.
38      * @param output
39      *            ByteBuf, where value or zeros are written.
40      */
41     public static void writeMedium(final Integer value, final ByteBuf output) {
42         output.writeMedium(value != null ? value : 0);
43     }
44
45     /**
46      * Writes IPv4 address if not null, otherwise writes zeros to the
47      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
48      *
49      * @param ipv4Address
50      *            IPv4 address to be written to the output.
51      * @param output
52      *            ByteBuf, where ipv4Address or zeros are written.
53      */
54     public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) {
55         if (ipv4Address != null) {
56             output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address));
57         } else {
58             output.writeInt(0);
59         }
60     }
61
62     /**
63      * Writes IPv4 address if not null, otherwise writes zeros to the
64      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
65      *
66      * @param ipv4Address
67      *            IPv4 address to be written to the output.
68      * @param output
69      *            ByteBuf, where ipv4Address or zeros are written.
70      */
71     public static void writeIpv4Address(final Ipv4AddressNoZone ipv4Address, final ByteBuf output) {
72         if (ipv4Address != null) {
73             output.writeBytes(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBytes(ipv4Address));
74         } else {
75             output.writeInt(0);
76         }
77     }
78
79     /**
80      * Writes IPv4 prefix if not null, otherwise writes zeros to the
81      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 5.
82      *
83      * @param ipv4Prefix
84      *            IPv4 prefix value to be written to the output. Prefix is
85      *            written in the last byte.
86      * @param output
87      *            ByteBuf, where ipv4Prefix or zeros are written.
88      */
89     public static void writeIpv4Prefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
90         if (ipv4Prefix != null) {
91             output.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
92         } else {
93             output.writeZero(IPV4_PREFIX_BYTE_LENGTH);
94         }
95     }
96
97     /**
98      * Writes IPv6 address if not null, otherwise writes zeros to the
99      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
100      *
101      * @param ipv6Address
102      *            IPv6 address to be written to the output.
103      * @param output
104      *            ByteBuf, where ipv6Address or zeros are written.
105      */
106     public static void writeIpv6Address(final Ipv6Address ipv6Address, final ByteBuf output) {
107         if (ipv6Address != null) {
108             output.writeBytes(Ipv6Util.bytesForAddress(ipv6Address));
109         } else {
110             output.writeZero(Ipv6Util.IPV6_LENGTH);
111         }
112     }
113
114     /**
115      * Writes IPv6 address if not null, otherwise writes zeros to the
116      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 16.
117      *
118      * @param ipv6Address
119      *            IPv6 address to be written to the output.
120      * @param output
121      *            ByteBuf, where ipv6Address or zeros are written.
122      */
123     public static void writeIpv6Address(final Ipv6AddressNoZone ipv6Address, final ByteBuf output) {
124         if (ipv6Address != null) {
125             output.writeBytes(IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ipv6Address));
126         } else {
127             output.writeZero(Ipv6Util.IPV6_LENGTH);
128         }
129     }
130
131     /**
132      * Writes IPv6 prefix if not null, otherwise writes zeros to the
133      * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 17.
134      *
135      * @param ipv6Prefix
136      *            IPv6 prefix to be written to the output. Prefix is written in
137      *            the last byte.
138      * @param output
139      *            ByteBuf, where ipv6Prefix or zeros are written.
140      */
141     public static void writeIpv6Prefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
142         if (ipv6Prefix != null) {
143             output.writeBytes(Ipv6Util.bytesForPrefix(ipv6Prefix));
144         } else {
145             output.writeZero(IPV6_PREFIX_BYTE_LENGTH);
146         }
147     }
148
149     public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
150         final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
151         writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
152     }
153
154     public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
155         final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
156         writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
157     }
158
159     private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
160         output.writeByte(prefixBits);
161         output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
162     }
163
164     /**
165      * Writes Float32 <code>value</code> if not null, otherwise writes zeros to
166      * the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
167      *
168      * @param value
169      *            Float32 value to be written to the output.
170      * @param output
171      *            ByteBuf, where value or zeros are written.
172      */
173     public static void writeFloat32(final Float32 value, final ByteBuf output) {
174         if (value != null) {
175             output.writeBytes(value.getValue());
176         } else {
177             output.writeInt(0);
178         }
179     }
180 }