Reduce ByteBuf.writeZero() usage
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / bgp / concepts / IpAddressUtil.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.bgp.concepts;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.protocol.util.Ipv4Util;
14 import org.opendaylight.protocol.util.Ipv6Util;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
21
22 /**
23  * Utility class for IpAddress models type(like Originator Route Ip) serialization and parsing.
24  *
25  * @author Claudio D. Gasparini
26  */
27 public final class IpAddressUtil {
28     private IpAddressUtil() {
29         throw new UnsupportedOperationException();
30     }
31
32     /**
33      * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
34      *
35      * @param buffer containing ip address
36      * @return IpAddress
37      */
38     public static @NonNull IpAddress addressForByteBuf(final ByteBuf buffer) {
39         final int ipLength = buffer.readUnsignedByte();
40         if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
41             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
42         } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
43             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
44         }
45         throw new IllegalStateException("Unexpected size");
46     }
47
48     /**
49      * Returns IpAddress from byte array containing ipAddress based on ByteArray length.
50      *
51      * @param buffer containing ip address
52      * @return IpAddress
53      */
54     public static @NonNull IpAddress addressForByteBufWOLength(final ByteBuf buffer) {
55         final int ipLength = buffer.readableBytes();
56         if (ipLength == Ipv6Util.IPV6_LENGTH) {
57             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
58         } else if (ipLength == Ipv4Util.IP4_LENGTH) {
59             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
60         }
61         throw new IllegalStateException("Unexpected size");
62     }
63
64     /**
65      * Returns byte array containing IpAddress length and IpAddress.
66      *
67      * @param address containing ipv4 or ipv6 address
68      * @return byte array
69      */
70     public static @NonNull ByteBuf bytesFor(final IpAddress address) {
71         final ByteBuf body = Unpooled.buffer();
72         if (address.getIpv4Address() != null) {
73             body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
74             body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
75         } else if (address.getIpv6Address() != null) {
76             body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
77             body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
78         } else {
79             body.writeByte(0);
80         }
81         return body;
82     }
83
84     /**
85      * Returns byte array containing IpAddress.
86      *
87      * @param address containing ipv4 or ipv6 address
88      * @return byte array
89      */
90     public static @NonNull ByteBuf bytesWOLengthFor(final IpAddress address) {
91         final ByteBuf body = Unpooled.buffer();
92         if (address.getIpv4Address() != null) {
93             body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
94         } else if (address.getIpv6Address() != null) {
95             body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
96         } else {
97             body.writeByte(0);
98         }
99         return body;
100     }
101
102     public static IpAddress extractIpAddress(final DataContainerNode<?> route, final NodeIdentifier rdNid) {
103         final NormalizedNode<?, ?> rdNode = NormalizedNodes.findNode(route, rdNid).orElse(null);
104         if (rdNode != null) {
105             return IpAddressBuilder.getDefaultInstance((String) rdNode.getValue());
106         }
107         return null;
108     }
109 }