2660b634f1696969bafd7aa231813d1794028b5b
[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 Pair<Ipv4Address, Ipv4Address> getStartAndEndIp(Ipv4Prefix ipv4Prefix) {
31         SubnetUtils subnetUtils = new SubnetUtils(ipv4Prefix.getValue());
32         SubnetUtils.SubnetInfo prefixSubnetInfo = subnetUtils.getInfo();
33         Ipv4Address lowIp = new Ipv4Address(prefixSubnetInfo.getLowAddress());
34         Ipv4Address highIp = new Ipv4Address(prefixSubnetInfo.getHighAddress());
35         return new ImmutablePair<>(lowIp, highIp);
36     }
37
38     public static Pair<Ipv4Prefix, Ipv4Prefix> getSmallerSubnet(Ipv4Prefix ipv4Prefix) throws LispHelperArgumentException {
39         String cidrNotion = ipv4Prefix.getValue();
40
41         SubnetUtils subnetUtils = new SubnetUtils(cidrNotion);
42         String firstSubnet;
43         String secondSubnet;
44         int maskLen = Integer.valueOf(cidrNotion.split("/")[1]) + 1;
45
46         if (maskLen > 32) {
47             return new ImmutablePair<>(ipv4Prefix, ipv4Prefix);
48         }
49
50         SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
51         try {
52             int lowValue = InetAddresses.coerceToInteger(InetAddress.getByName(subnetInfo.getNetworkAddress()));
53             int highValue = InetAddresses.coerceToInteger(InetAddress.getByName(subnetInfo.getHighAddress())) + 1;
54             InetAddress middleAddress = InetAddresses.fromInteger(lowValue + (highValue - lowValue + 1) / 2);
55             String firstAddress = subnetInfo.getNetworkAddress();
56             String secondAddress = middleAddress.getHostAddress();
57             firstSubnet = firstAddress + "/" + maskLen;
58             secondSubnet = secondAddress + "/" + maskLen;
59         } catch (UnknownHostException e) {
60             LOG.warn("Failed to translate IP address " + cidrNotion+ " to smaller subnet");
61             throw new LispHelperArgumentException("Invalid argument for subnet " + cidrNotion);
62         }
63         return new ImmutablePair<>(new Ipv4Prefix(firstSubnet), new Ipv4Prefix(secondSubnet));
64     }
65 }