Introduced neutron-mapper
[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.Inet6Address;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16
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;
25
26 import com.google.common.base.Strings;
27 import com.google.common.net.InetAddresses;
28
29 /**
30  * @author Martin Sunal
31  */
32 public class Utils {
33
34     private Utils() {}
35
36     /**
37      * This implementation does not use nameservice lookups (e.g. no DNS).
38      *
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
42      */
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));
50         }
51         return new IpPrefix(new Ipv6Prefix(cidr));
52     }
53
54     /**
55      * This implementation does not use nameservice lookups (e.g. no DNS).
56      *
57      * @param ipAddress - format must be valid for regex in {@link Ipv4Address} or
58      *        {@link Ipv6Address}
59      * @return the {@link IpAddress} having the given ipAddress string representation
60      * @throws IllegalArgumentException - if the argument is not a valid IP address string
61      */
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));
67         }
68         return new IpAddress(new Ipv6Address(ipAddress));
69     }
70
71     public static String getStringIpPrefix(IpPrefix ipPrefix) {
72         if (ipPrefix.getIpv4Prefix() != null) {
73             return ipPrefix.getIpv4Prefix().getValue();
74         }
75         return ipPrefix.getIpv6Prefix().getValue();
76     }
77
78     public static String getStringIpAddress(IpAddress ipAddress) {
79         if (ipAddress.getIpv4Address() != null) {
80             return ipAddress.getIpv4Address().getValue();
81         }
82         return ipAddress.getIpv6Address().getValue();
83     }
84
85     public static boolean isHostInIpPrefix(IpAddress host, IpPrefix ipPrefix) {
86         String ipAddress = "";
87         int ipVersion = 0;
88         if (host.getIpv4Address() != null) {
89             ipAddress = host.getIpv4Address().getValue();
90             ipVersion = 4;
91         } else {
92             ipAddress = host.getIpv6Address().getValue();
93             ipVersion = 6;
94         }
95         String cidr = getStringIpPrefix(ipPrefix);
96
97         if (ipVersion == 4) {
98             try {
99                 SubnetUtils util = new SubnetUtils(cidr);
100                 SubnetInfo info = util.getInfo();
101                 return info.isInRange(ipAddress);
102             } catch (IllegalArgumentException e) {
103                 return false;
104             }
105         }
106
107         if (ipVersion == 6) {
108             String[] parts = cidr.split("/");
109             try {
110                 int length = Integer.parseInt(parts[1]);
111                 byte[] cidrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
112                 byte[] ipBytes = ((Inet6Address) InetAddress.getByName(ipAddress)).getAddress();
113                 int i;
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))))) {
116                         return false;
117                     }
118                 }
119                 return true;
120             } catch (UnknownHostException e) {
121                 return false;
122             }
123         }
124         return false;
125     }
126
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]+)",
129                 "$1-$2-$3-$4-$5");
130     }
131
132 }