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.InetAddress;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
22 import com.google.common.base.Preconditions;
23 import com.google.common.base.Strings;
24 import com.google.common.net.InetAddresses;
29 throw new UnsupportedOperationException("Cannot create an instance.");
33 * This implementation does not use nameservice lookups (e.g. no DNS).
35 * @param cidr - format must be valid for regex in {@link Ipv4Prefix} or {@link Ipv6Prefix}
36 * @return the {@link IpPrefix} having the given cidr string representation
37 * @throws IllegalArgumentException - if the argument is not a valid CIDR string
39 public static IpPrefix createIpPrefix(String cidr) {
40 checkArgument(!Strings.isNullOrEmpty(cidr), "Cannot be null or empty.");
41 String[] ipAndPrefix = cidr.split("/");
42 checkArgument(ipAndPrefix.length == 2, "Bad format.");
43 InetAddress ip = InetAddresses.forString(ipAndPrefix[0]);
44 if (ip instanceof Inet4Address) {
45 return new IpPrefix(new Ipv4Prefix(cidr));
47 return new IpPrefix(new Ipv6Prefix(cidr));
51 * This implementation does not use nameservice lookups (e.g. no DNS).
53 * @param ipAddress - format must be valid for regex in {@link Ipv4Address} or
55 * @return the {@link IpAddress} having the given ipAddress string representation
56 * @throws IllegalArgumentException - if the argument is not a valid IP address string
58 public static IpAddress createIpAddress(String ipAddress) {
59 checkArgument(!Strings.isNullOrEmpty(ipAddress), "Cannot be null or empty.");
60 InetAddress ip = InetAddresses.forString(ipAddress);
61 if (ip instanceof Inet4Address) {
62 return new IpAddress(new Ipv4Address(ipAddress));
64 return new IpAddress(new Ipv6Address(ipAddress));
67 public static String getStringIpPrefix(IpPrefix ipPrefix) {
68 Preconditions.checkNotNull(ipPrefix);
69 if (ipPrefix.getIpv4Prefix() != null) {
70 String ipPrefixIpv4 = ipPrefix.getIpv4Prefix().getValue();
71 return ipPrefixIpv4.replace('/', '_');
73 String ipPrefixIpv6 = ipPrefix.getIpv6Prefix().getValue();
74 return ipPrefixIpv6.replace('/', '_').replace(':', '.');
77 public static String getStringIpAddress(IpAddress ipAddress) {
78 if (ipAddress.getIpv4Address() != null) {
79 return ipAddress.getIpv4Address().getValue();
81 return ipAddress.getIpv6Address().getValue();
84 public static String normalizeUuid(String string) {
85 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]+)",