2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
8 package org.opendaylight.bgp.concepts;
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;
23 * Utility class for IpAddress models type(like Originator Route Ip) serialization and parsing.
25 * @author Claudio D. Gasparini
27 public final class IpAddressUtil {
28 private static final int ZERO_BYTE = 1;
30 private IpAddressUtil() {
31 throw new UnsupportedOperationException();
35 * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
37 * @param buffer containing ip address
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));
47 throw new IllegalStateException("Unexpected size");
51 * Returns IpAddress from byte array containing ipAddress based on ByteArray length.
53 * @param buffer containing ip address
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));
63 throw new IllegalStateException("Unexpected size");
67 * Returns byte array containing IpAddress length and IpAddress.
69 * @param address containing ipv4 or ipv6 address
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()));
81 body.writeZero(ZERO_BYTE);
87 * Returns byte array containing IpAddress.
89 * @param address containing ipv4 or ipv6 address
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()));
99 body.writeZero(ZERO_BYTE);
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());