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 IpAddressUtil() {
29 throw new UnsupportedOperationException();
33 * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
35 * @param buffer containing ip address
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));
45 throw new IllegalStateException("Unexpected size");
49 * Returns IpAddress from byte array containing ipAddress based on ByteArray length.
51 * @param buffer containing ip address
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));
61 throw new IllegalStateException("Unexpected size");
65 * Returns byte array containing IpAddress length and IpAddress.
67 * @param address containing ipv4 or ipv6 address
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()));
85 * Returns byte array containing IpAddress.
87 * @param address containing ipv4 or ipv6 address
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()));
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());