81c36eb0db768a2eb0032d1ba463bd7b42ec3e42
[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 static final int ZERO_BYTE = 1;
29
30     private IpAddressUtil() {
31         throw new UnsupportedOperationException();
32     }
33
34     /**
35      * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
36      *
37      * @param buffer containing ip address
38      * @return IpAddress
39      */
40     public static @NonNull IpAddress addressForByteBuf(final ByteBuf buffer) {
41         final int ipLength = buffer.readUnsignedByte();
42         if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
43             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
44         } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
45             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
46         }
47         throw new IllegalStateException("Unexpected size");
48     }
49
50     /**
51      * Returns IpAddress from byte array containing ipAddress based on ByteArray length.
52      *
53      * @param buffer containing ip address
54      * @return IpAddress
55      */
56     public static @NonNull IpAddress addressForByteBufWOLength(final ByteBuf buffer) {
57         final int ipLength = buffer.readableBytes();
58         if (ipLength == Ipv6Util.IPV6_LENGTH) {
59             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
60         } else if (ipLength == Ipv4Util.IP4_LENGTH) {
61             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
62         }
63         throw new IllegalStateException("Unexpected size");
64     }
65
66     /**
67      * Returns byte array containing IpAddress length and IpAddress.
68      *
69      * @param address containing ipv4 or ipv6 address
70      * @return byte array
71      */
72     public static @NonNull ByteBuf bytesFor(final IpAddress address) {
73         final ByteBuf body = Unpooled.buffer();
74         if (address.getIpv4Address() != null) {
75             body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
76             body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
77         } else if (address.getIpv6Address() != null) {
78             body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
79             body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
80         } else {
81             body.writeZero(ZERO_BYTE);
82         }
83         return body;
84     }
85
86     /**
87      * Returns byte array containing IpAddress.
88      *
89      * @param address containing ipv4 or ipv6 address
90      * @return byte array
91      */
92     public static @NonNull ByteBuf bytesWOLengthFor(final IpAddress address) {
93         final ByteBuf body = Unpooled.buffer();
94         if (address.getIpv4Address() != null) {
95             body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
96         } else if (address.getIpv6Address() != null) {
97             body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
98         } else {
99             body.writeZero(ZERO_BYTE);
100         }
101         return body;
102     }
103
104     public static IpAddress extractIpAddress(final DataContainerNode<?> route, final NodeIdentifier rdNid) {
105         final NormalizedNode<?, ?> rdNode = NormalizedNodes.findNode(route, rdNid).orElse(null);
106         if (rdNode != null) {
107             return IpAddressBuilder.getDefaultInstance((String) rdNode.getValue());
108         }
109         return null;
110     }
111 }