Add INFO.yaml for GBP
[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
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15
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;
24
25 public class IpAddressUtil {
26     private static final Logger LOG = LoggerFactory.getLogger(IpAddressUtil.class);
27
28     static boolean isMetadataIp(Ipv4Address ipv4Address) {
29         return ipv4Address.getValue().equals(Constants.METADATA_IP);
30     }
31
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);
38     }
39
40     public static Pair<Ipv4Prefix, Ipv4Prefix> getSmallerSubnet(Ipv4Prefix ipv4Prefix) throws LispArgumentException {
41         String cidrNotion = ipv4Prefix.getValue();
42
43         SubnetUtils subnetUtils = new SubnetUtils(cidrNotion);
44         String firstSubnet;
45         String secondSubnet;
46         int maskLen = Integer.valueOf(cidrNotion.split("/")[1]) + 1;
47
48         if (maskLen > 32) {
49             return new ImmutablePair<>(ipv4Prefix, ipv4Prefix);
50         }
51
52         SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
53         try {
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);
64         }
65         return new ImmutablePair<>(new Ipv4Prefix(firstSubnet), new Ipv4Prefix(secondSubnet));
66     }
67
68     public static Ipv4Prefix toIpV4Prefix(Ipv4Address ipv4Address) {
69         return new Ipv4Prefix(ipv4Address.getValue() + "/32");
70     }
71
72     static boolean ipInRange(Ipv4Address ip, String startIpStr, String endIpStr) {
73         String ipStr = ip.getValue();
74
75         int startLim = InetAddresses.coerceToInteger(InetAddresses.forString(startIpStr));
76         int endLim = InetAddresses.coerceToInteger(InetAddresses.forString(endIpStr));
77         int ipNum = InetAddresses.coerceToInteger(InetAddresses.forString(ipStr));
78
79         long startUnsigned = Integer.toUnsignedLong(startLim);
80         long endUnsigned = Integer.toUnsignedLong(endLim);
81         long ipNumUnsigned = Integer.toUnsignedLong(ipNum);
82
83         return (startUnsigned <= ipNumUnsigned) && (ipNumUnsigned <= endUnsigned);
84     }
85
86     static String startIpOfSubnet(String cidrNotion) {
87         return new SubnetUtils(cidrNotion).getInfo().getLowAddress();
88     }
89
90     static String endIpOfSubnet(String cidrNotion) {
91         return new SubnetUtils(cidrNotion).getInfo().getHighAddress();
92     }
93
94     static int maskLen(String cidrNotion) {
95         return Integer.valueOf(cidrNotion.split("/")[1]);
96     }
97 }