2 * Copyright (c) 2017 Cisco Systems. 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.util;
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;
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
25 * Created by Shakib Ahmed on 5/3/17.
27 public class IpAddressUtil {
28 private static final Logger LOG = LoggerFactory.getLogger(IpAddressUtil.class);
30 public static boolean isMetadataIp(Ipv4Address ipv4Address) {
31 return ipv4Address.getValue().equals(Constants.METADATA_IP);
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);
42 public static Pair<Ipv4Prefix, Ipv4Prefix> getSmallerSubnet(Ipv4Prefix ipv4Prefix) throws LispHelperArgumentException {
43 String cidrNotion = ipv4Prefix.getValue();
45 SubnetUtils subnetUtils = new SubnetUtils(cidrNotion);
48 int maskLen = Integer.valueOf(cidrNotion.split("/")[1]) + 1;
51 return new ImmutablePair<>(ipv4Prefix, ipv4Prefix);
54 SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
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);
67 return new ImmutablePair<>(new Ipv4Prefix(firstSubnet), new Ipv4Prefix(secondSubnet));
70 public static boolean ipInRange(Ipv4Address ip, String startIpStr, String endIpStr) {
71 String ipStr = ip.getValue();
73 int startLim = InetAddresses.coerceToInteger(InetAddresses.forString(startIpStr));
74 int endLim = InetAddresses.coerceToInteger(InetAddresses.forString(endIpStr));
75 int ipNum = InetAddresses.coerceToInteger(InetAddresses.forString(ipStr));
77 long startUnsigned = Integer.toUnsignedLong(startLim);
78 long endUnsigned = Integer.toUnsignedLong(endLim);
79 long ipNumUnsigned = Integer.toUnsignedLong(ipNum);
81 return (startUnsigned <= ipNumUnsigned) && (ipNumUnsigned <= endUnsigned);
84 public static String startIpOfSubnet(String cidrNotion) {
85 return new SubnetUtils(cidrNotion).getInfo().getLowAddress();
88 public static String endIpOfSubnet(String cidrNotion) {
89 return new SubnetUtils(cidrNotion).getInfo().getHighAddress();
92 public static int maskLen(String cidrNotion) {
93 return Integer.valueOf(cidrNotion.split("/")[1]);