Gbp coverage increased in ./neutron/mapper/infrastructure
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / Utils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.groupbasedpolicy.neutron.mapper.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.net.Inet4Address;
13 import java.net.InetAddress;
14
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;
21
22 import com.google.common.base.Preconditions;
23 import com.google.common.base.Strings;
24 import com.google.common.net.InetAddresses;
25
26 public class Utils {
27
28     private Utils() {
29         throw new UnsupportedOperationException("Cannot create an instance.");
30     }
31
32     /**
33      * This implementation does not use nameservice lookups (e.g. no DNS).
34      *
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
38      */
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));
46         }
47         return new IpPrefix(new Ipv6Prefix(cidr));
48     }
49
50     /**
51      * This implementation does not use nameservice lookups (e.g. no DNS).
52      *
53      * @param ipAddress - format must be valid for regex in {@link Ipv4Address} or
54      *        {@link Ipv6Address}
55      * @return the {@link IpAddress} having the given ipAddress string representation
56      * @throws IllegalArgumentException - if the argument is not a valid IP address string
57      */
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));
63         }
64         return new IpAddress(new Ipv6Address(ipAddress));
65     }
66
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('/', '_');
72         }
73         String ipPrefixIpv6 = ipPrefix.getIpv6Prefix().getValue();
74         return ipPrefixIpv6.replace('/', '_').replace(':', '.');
75     }
76
77     public static String getStringIpAddress(IpAddress ipAddress) {
78         if (ipAddress.getIpv4Address() != null) {
79             return ipAddress.getIpv4Address().getValue();
80         }
81         return ipAddress.getIpv6Address().getValue();
82     }
83
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]+)",
86                 "$1-$2-$3-$4-$5");
87     }
88
89 }