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.IpAddressNoZone;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZoneBuilder;
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() {
33 * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
35 * @param buffer containing ip address
36 * @return IpAddressNoZone
38 public static @NonNull IpAddressNoZone addressForByteBuf(final ByteBuf buffer) {
39 final int ipLength = buffer.readUnsignedByte();
40 if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
41 return new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer));
42 } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
43 return new IpAddressNoZone(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
52 * @return IpAddressNoZone
54 public static @NonNull IpAddressNoZone addressForByteBufWOLength(final ByteBuf buffer) {
55 final int ipLength = buffer.readableBytes();
56 if (ipLength == Ipv6Util.IPV6_LENGTH) {
57 return new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer));
58 } else if (ipLength == Ipv4Util.IP4_LENGTH) {
59 return new IpAddressNoZone(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 IpAddressNoZone address) {
71 final ByteBuf body = Unpooled.buffer();
72 if (address.getIpv4AddressNoZone() != null) {
73 body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
74 body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4AddressNoZone()));
75 } else if (address.getIpv6AddressNoZone() != null) {
76 body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
77 body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6AddressNoZone()));
85 * Returns byte array containing IpAddress.
87 * @param address containing ipv4 or ipv6 address
90 public static @NonNull ByteBuf bytesWOLengthFor(final IpAddressNoZone address) {
91 final ByteBuf body = Unpooled.buffer();
92 if (address.getIpv4AddressNoZone() != null) {
93 body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4AddressNoZone()));
94 } else if (address.getIpv6AddressNoZone() != null) {
95 body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6AddressNoZone()));
102 public static IpAddressNoZone extractIpAddress(final DataContainerNode<?> route, final NodeIdentifier rdNid) {
103 final NormalizedNode<?, ?> rdNode = NormalizedNodes.findNode(route, rdNid).orElse(null);
104 if (rdNode != null) {
105 return IpAddressNoZoneBuilder.getDefaultInstance((String) rdNode.getValue());