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;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
18 import org.apache.commons.net.util.SubnetUtils;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.exception.LispArgumentException;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class IpAddressUtil {
26 private static final Logger LOG = LoggerFactory.getLogger(IpAddressUtil.class);
28 static boolean isMetadataIp(Ipv4Address ipv4Address) {
29 return ipv4Address.getValue().equals(Constants.METADATA_IP);
32 public static Pair<Ipv4Address, Ipv4Address> getStartAndEndIp(Ipv4Prefix ipv4Prefix) {
33 SubnetUtils subnetUtils = new SubnetUtils(ipv4Prefix.getValue());
34 SubnetUtils.SubnetInfo prefixSubnetInfo = subnetUtils.getInfo();
35 Ipv4Address lowIp = new Ipv4Address(prefixSubnetInfo.getLowAddress());
36 Ipv4Address highIp = new Ipv4Address(prefixSubnetInfo.getHighAddress());
37 return new ImmutablePair<>(lowIp, highIp);
40 public static Pair<Ipv4Prefix, Ipv4Prefix> getSmallerSubnet(Ipv4Prefix ipv4Prefix) throws LispArgumentException {
41 String cidrNotion = ipv4Prefix.getValue();
43 SubnetUtils subnetUtils = new SubnetUtils(cidrNotion);
46 int maskLen = Integer.valueOf(cidrNotion.split("/")[1]) + 1;
49 return new ImmutablePair<>(ipv4Prefix, ipv4Prefix);
52 SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
54 int lowValue = InetAddresses.coerceToInteger(InetAddress.getByName(subnetInfo.getNetworkAddress()));
55 int highValue = InetAddresses.coerceToInteger(InetAddress.getByName(subnetInfo.getHighAddress())) + 1;
56 InetAddress middleAddress = InetAddresses.fromInteger(lowValue + (highValue - lowValue + 1) / 2);
57 String firstAddress = subnetInfo.getNetworkAddress();
58 String secondAddress = middleAddress.getHostAddress();
59 firstSubnet = firstAddress + "/" + maskLen;
60 secondSubnet = secondAddress + "/" + maskLen;
61 } catch (UnknownHostException e) {
62 LOG.warn("Failed to translate IP address " + cidrNotion+ " to smaller subnet");
63 throw new LispArgumentException("Invalid argument for subnet " + cidrNotion);
65 return new ImmutablePair<>(new Ipv4Prefix(firstSubnet), new Ipv4Prefix(secondSubnet));
68 public static Ipv4Prefix toIpV4Prefix(Ipv4Address ipv4Address) {
69 return new Ipv4Prefix(ipv4Address.getValue() + "/32");
72 static boolean ipInRange(Ipv4Address ip, String startIpStr, String endIpStr) {
73 String ipStr = ip.getValue();
75 int startLim = InetAddresses.coerceToInteger(InetAddresses.forString(startIpStr));
76 int endLim = InetAddresses.coerceToInteger(InetAddresses.forString(endIpStr));
77 int ipNum = InetAddresses.coerceToInteger(InetAddresses.forString(ipStr));
79 long startUnsigned = Integer.toUnsignedLong(startLim);
80 long endUnsigned = Integer.toUnsignedLong(endLim);
81 long ipNumUnsigned = Integer.toUnsignedLong(ipNum);
83 return (startUnsigned <= ipNumUnsigned) && (ipNumUnsigned <= endUnsigned);
86 static String startIpOfSubnet(String cidrNotion) {
87 return new SubnetUtils(cidrNotion).getInfo().getLowAddress();
90 static String endIpOfSubnet(String cidrNotion) {
91 return new SubnetUtils(cidrNotion).getInfo().getHighAddress();
94 static int maskLen(String cidrNotion) {
95 return Integer.valueOf(cidrNotion.split("/")[1]);