Merge "Fix ODL itr rloc for supporting LISP CP over admin"
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / lisp / util / IpAddressUtil.java
1 /*
2  * Copyright (c) 2017 Cisco Systems. 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.util;
10
11 import com.google.common.net.InetAddresses;
12 import org.apache.commons.lang3.tuple.ImmutablePair;
13 import org.apache.commons.lang3.tuple.Pair;
14 import org.apache.commons.net.util.SubnetUtils;
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.exception.LispHelperArgumentException;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23
24 /**
25  * Created by Shakib Ahmed on 5/3/17.
26  */
27 public class IpAddressUtil {
28     private static final Logger LOG = LoggerFactory.getLogger(IpAddressUtil.class);
29
30     public static boolean isMetadataIp(Ipv4Address ipv4Address) {
31         return ipv4Address.getValue().equals(Constants.METADATA_IP);
32     }
33
34     public static Pair<Ipv4Address, Ipv4Address> getStartAndEndIp(Ipv4Prefix ipv4Prefix) {
35         SubnetUtils subnetUtils = new SubnetUtils(ipv4Prefix.getValue());
36         SubnetUtils.SubnetInfo prefixSubnetInfo = subnetUtils.getInfo();
37         Ipv4Address lowIp = new Ipv4Address(prefixSubnetInfo.getLowAddress());
38         Ipv4Address highIp = new Ipv4Address(prefixSubnetInfo.getHighAddress());
39         return new ImmutablePair<>(lowIp, highIp);
40     }
41
42     public static Pair<Ipv4Prefix, Ipv4Prefix> getSmallerSubnet(Ipv4Prefix ipv4Prefix) throws LispHelperArgumentException {
43         String cidrNotion = ipv4Prefix.getValue();
44
45         SubnetUtils subnetUtils = new SubnetUtils(cidrNotion);
46         String firstSubnet;
47         String secondSubnet;
48         int maskLen = Integer.valueOf(cidrNotion.split("/")[1]) + 1;
49
50         if (maskLen > 32) {
51             return new ImmutablePair<>(ipv4Prefix, ipv4Prefix);
52         }
53
54         SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
55         try {
56             int lowValue = InetAddresses.coerceToInteger(InetAddress.getByName(subnetInfo.getNetworkAddress()));
57             int highValue = InetAddresses.coerceToInteger(InetAddress.getByName(subnetInfo.getHighAddress())) + 1;
58             InetAddress middleAddress = InetAddresses.fromInteger(lowValue + (highValue - lowValue + 1) / 2);
59             String firstAddress = subnetInfo.getNetworkAddress();
60             String secondAddress = middleAddress.getHostAddress();
61             firstSubnet = firstAddress + "/" + maskLen;
62             secondSubnet = secondAddress + "/" + maskLen;
63         } catch (UnknownHostException e) {
64             LOG.warn("Failed to translate IP address " + cidrNotion+ " to smaller subnet");
65             throw new LispHelperArgumentException("Invalid argument for subnet " + cidrNotion);
66         }
67         return new ImmutablePair<>(new Ipv4Prefix(firstSubnet), new Ipv4Prefix(secondSubnet));
68     }
69
70     public static boolean ipInRange(Ipv4Address ip, String startIpStr, String endIpStr) {
71         String ipStr = ip.getValue();
72
73         int startLim = InetAddresses.coerceToInteger(InetAddresses.forString(startIpStr));
74         int endLim = InetAddresses.coerceToInteger(InetAddresses.forString(endIpStr));
75         int ipNum = InetAddresses.coerceToInteger(InetAddresses.forString(ipStr));
76
77         long startUnsigned = Integer.toUnsignedLong(startLim);
78         long endUnsigned = Integer.toUnsignedLong(endLim);
79         long ipNumUnsigned = Integer.toUnsignedLong(ipNum);
80
81         return (startUnsigned <= ipNumUnsigned) && (ipNumUnsigned <= endUnsigned);
82     }
83
84     public static String startIpOfSubnet(String cidrNotion) {
85         return new SubnetUtils(cidrNotion).getInfo().getLowAddress();
86     }
87
88     public static String endIpOfSubnet(String cidrNotion) {
89         return new SubnetUtils(cidrNotion).getInfo().getHighAddress();
90     }
91
92     public static int maskLen(String cidrNotion) {
93         return Integer.valueOf(cidrNotion.split("/")[1]);
94     }
95 }