2 * Copyright (c) 2014 Cisco Systems, Inc. and others. 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.groupbasedpolicy.neutron.mapper.util;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import java.net.Inet4Address;
13 import java.net.Inet6Address;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
17 import org.apache.commons.net.util.SubnetUtils;
18 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
26 import com.google.common.base.Strings;
27 import com.google.common.net.InetAddresses;
30 * @author Martin Sunal
37 * This implementation does not use nameservice lookups (e.g. no DNS).
39 * @param cidr - format must be valid for regex in {@link Ipv4Prefix} or {@link Ipv6Prefix}
40 * @return the {@link IpPrefix} having the given cidr string representation
41 * @throws IllegalArgumentException - if the argument is not a valid CIDR string
43 public static IpPrefix createIpPrefix(String cidr) {
44 checkArgument(!Strings.isNullOrEmpty(cidr), "Cannot be null or empty.");
45 String[] ipAndPrefix = cidr.split("/");
46 checkArgument(ipAndPrefix.length == 2, "Bad format.");
47 InetAddress ip = InetAddresses.forString(ipAndPrefix[0]);
48 if (ip instanceof Inet4Address) {
49 return new IpPrefix(new Ipv4Prefix(cidr));
51 return new IpPrefix(new Ipv6Prefix(cidr));
55 * This implementation does not use nameservice lookups (e.g. no DNS).
57 * @param ipAddress - format must be valid for regex in {@link Ipv4Address} or
59 * @return the {@link IpAddress} having the given ipAddress string representation
60 * @throws IllegalArgumentException - if the argument is not a valid IP address string
62 public static IpAddress createIpAddress(String ipAddress) {
63 checkArgument(!Strings.isNullOrEmpty(ipAddress), "Cannot be null or empty.");
64 InetAddress ip = InetAddresses.forString(ipAddress);
65 if (ip instanceof Inet4Address) {
66 return new IpAddress(new Ipv4Address(ipAddress));
68 return new IpAddress(new Ipv6Address(ipAddress));
71 public static String getStringIpPrefix(IpPrefix ipPrefix) {
72 if (ipPrefix.getIpv4Prefix() != null) {
73 return ipPrefix.getIpv4Prefix().getValue();
75 return ipPrefix.getIpv6Prefix().getValue();
78 public static String getStringIpAddress(IpAddress ipAddress) {
79 if (ipAddress.getIpv4Address() != null) {
80 return ipAddress.getIpv4Address().getValue();
82 return ipAddress.getIpv6Address().getValue();
85 public static boolean isHostInIpPrefix(IpAddress host, IpPrefix ipPrefix) {
86 String ipAddress = "";
88 if (host.getIpv4Address() != null) {
89 ipAddress = host.getIpv4Address().getValue();
92 ipAddress = host.getIpv6Address().getValue();
95 String cidr = getStringIpPrefix(ipPrefix);
99 SubnetUtils util = new SubnetUtils(cidr);
100 SubnetInfo info = util.getInfo();
101 return info.isInRange(ipAddress);
102 } catch (IllegalArgumentException e) {
107 if (ipVersion == 6) {
108 String[] parts = cidr.split("/");
110 int length = Integer.parseInt(parts[1]);
111 byte[] cidrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
112 byte[] ipBytes = ((Inet6Address) InetAddress.getByName(ipAddress)).getAddress();
114 for (i = 0; i < length; i++) { // offset is to ensure proper comparison
115 if ((((cidrBytes[i / 8]) & 0x000000FF) & (1 << (7 - (i % 8)))) != (((ipBytes[i / 8]) & 0x000000FF) & (1 << (7 - (i % 8))))) {
120 } catch (UnknownHostException e) {
127 public static String normalizeUuid(String string) {
128 return string.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)",