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