Example usage for org.apache.commons.net.util SubnetUtils getInfo

List of usage examples for org.apache.commons.net.util SubnetUtils getInfo

Introduction

In this page you can find the example usage for org.apache.commons.net.util SubnetUtils getInfo.

Prototype

public final SubnetInfo getInfo() 

Source Link

Document

Return a SubnetInfo instance that contains subnet-specific statistics

Usage

From source file:org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnet.java

public boolean initDefaults() {
    if (enableDHCP == null) {
        enableDHCP = true;// w w  w . ja v  a2 s.c o  m
    }
    if (ipVersion == null) {
        ipVersion = IPV4_VERSION;
    }
    dnsNameservers = new ArrayList<>();
    if (hostRoutes == null) {
        hostRoutes = new ArrayList<>();
    }
    if (allocationPools == null) {
        allocationPools = new ArrayList<>();
        if (ipVersion == IPV4_VERSION) {
            try {
                SubnetUtils util = new SubnetUtils(cidr);
                SubnetInfo info = util.getInfo();
                if (gatewayIP == null || ("").equals(gatewayIP)) {
                    gatewayIP = info.getLowAddress();
                }
                if (allocationPools.size() < 1) {
                    NeutronSubnetIPAllocationPool source = new NeutronSubnetIPAllocationPool(
                            info.getLowAddress(), info.getHighAddress());
                    allocationPools = source.splitPool(gatewayIP);
                }
            } catch (IllegalArgumentException e) {
                LOGGER.warn("Failure in initDefault()", e);
                return false;
            }
        }
        if (ipVersion == IPV6_VERSION) {
            String[] parts = cidr.split("/");
            if (parts.length != 2) {
                return false;
            }
            try {
                int length = Integer.parseInt(parts[1]);
                BigInteger lowAddress_bi = NeutronSubnetIPAllocationPool.convertV6(parts[0]);
                String lowAddress = NeutronSubnetIPAllocationPool
                        .bigIntegerToIP(lowAddress_bi.add(BigInteger.ONE));
                BigInteger mask = BigInteger.ONE.shiftLeft(length).subtract(BigInteger.ONE);
                String highAddress = NeutronSubnetIPAllocationPool
                        .bigIntegerToIP(lowAddress_bi.add(mask).subtract(BigInteger.ONE));
                if (gatewayIP == null || ("").equals(gatewayIP)) {
                    gatewayIP = lowAddress;
                }
                if (allocationPools.size() < 1) {
                    NeutronSubnetIPAllocationPool source = new NeutronSubnetIPAllocationPool(lowAddress,
                            highAddress);
                    allocationPools = source.splitPoolV6(gatewayIP);
                }
            } catch (Exception e) {
                LOGGER.warn("Failure in initDefault()", e);
                return false;
            }
        }
    }
    return true;
}

From source file:org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnet.java

public boolean isValidIP(String ipAddress) {
    if (ipVersion == IPV4_VERSION) {
        try {/*from   w  ww. j a  v a2  s  .  co m*/
            SubnetUtils util = new SubnetUtils(cidr);
            SubnetInfo info = util.getInfo();
            return info.isInRange(ipAddress);
        } catch (IllegalArgumentException e) {
            LOGGER.warn("Failure in isValidIP()", e);
            return false;
        }
    }

    if (ipVersion == IPV6_VERSION) {
        String[] parts = cidr.split("/");
        try {
            int length = Integer.parseInt(parts[1]);
            byte[] cidrBytes = InetAddress.getByName(parts[0]).getAddress();
            byte[] ipBytes = InetAddress.getByName(ipAddress).getAddress();
            int i;
            for (i = 0; i < length; i++) {
                if (((((int) cidrBytes[i / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK) & (1 << (IPV6_BYTE_OFFSET
                        - (i % IPV6_LENGTH_BYTES)))) != ((((int) ipBytes[i / IPV6_LENGTH_BYTES])
                                & IPV6_LSB_MASK) & (1 << (IPV6_BYTE_OFFSET - (i % IPV6_LENGTH_BYTES))))) {
                    return (false);
                }
            }
            return (true);
        } catch (UnknownHostException e) {
            LOGGER.warn("Failure in isValidIP()", e);
            return (false);
        }
    }
    return false;
}

From source file:org.opendaylight.neutron.spi.NeutronSubnet.java

public boolean isValidCIDR() {
    // fix for Bug 2290 - need to wrap the existing test as
    // IPv4 because SubnetUtils doesn't support IPv6
    if (ipVersion == IPV4_VERSION) {
        try {//w  w  w  .j av a2s  .c o m
            SubnetUtils util = new SubnetUtils(cidr);
            SubnetInfo info = util.getInfo();
            if (!info.getNetworkAddress().equals(info.getAddress())) {
                return false;
            }
        } catch (IllegalArgumentException e) {
            LOGGER.warn("Failure in isValidCIDR()", e);
            return false;
        }
        return true;
    }
    if (ipVersion == IPV6_VERSION) {
        // fix for Bug2290 - this is custom code because no classes
        // with ODL-friendly licenses have been found
        // extract address (in front of /) and length (after /)
        String[] parts = cidr.split("/");
        if (parts.length != 2) {
            return false;
        }
        try {
            int length = Integer.parseInt(parts[1]);
            //TODO?: limit check on length
            // convert to byte array
            byte[] addrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
            for (int index = length; index < IPV6_LENGTH; index++) {
                if (((((int) addrBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK)
                        & (1 << (IPV6_BYTE_OFFSET - (index % IPV6_LENGTH_BYTES)))) != 0) {
                    return (false);
                }
            }
            return (true);
        } catch (UnknownHostException e) {
            LOGGER.warn("Failure in isValidCIDR()", e);
            return (false);
        }
    }
    return false;
}

From source file:org.opendaylight.neutron.spi.NeutronSubnet.java

@Override
public void initDefaults() {
    if (enableDHCP == null) {
        enableDHCP = true;/*ww  w .  jav  a  2s  .  co m*/
    }
    if (ipVersion == null) {
        ipVersion = IPV4_VERSION;
    }
    if (dnsNameservers == null) {
        dnsNameservers = new ArrayList<String>();
    }
    if (hostRoutes == null) {
        hostRoutes = new ArrayList<NeutronRoute>();
    }
    if (allocationPools == null) {
        allocationPools = new ArrayList<NeutronSubnetIpAllocationPool>();
        if (ipVersion == IPV4_VERSION) {
            try {
                SubnetUtils util = new SubnetUtils(cidr);
                SubnetInfo info = util.getInfo();
                if (gatewayIp == null || ("").equals(gatewayIp)) {
                    gatewayIp = info.getLowAddress();
                }
                if (allocationPools.size() < 1) {
                    NeutronSubnetIpAllocationPool source = new NeutronSubnetIpAllocationPool(
                            info.getLowAddress(), info.getHighAddress());
                    allocationPools = source.splitPool(gatewayIp);
                }
            } catch (IllegalArgumentException e) {
                LOGGER.warn("Failure in initDefault()", e);
                return;
            }
        }
        if (ipVersion == IPV6_VERSION) {
            String[] parts = cidr.split("/");
            if (parts.length != 2) {
                return;
            }

            int length = Integer.parseInt(parts[1]);
            BigInteger lowAddressBi = NeutronSubnetIpAllocationPool.convertV6(parts[0]);
            String lowAddress = NeutronSubnetIpAllocationPool.bigIntegerToIp(lowAddressBi.add(BigInteger.ONE));
            BigInteger mask = BigInteger.ONE.shiftLeft(length).subtract(BigInteger.ONE);
            String highAddress = NeutronSubnetIpAllocationPool
                    .bigIntegerToIp(lowAddressBi.add(mask).subtract(BigInteger.ONE));
            if (gatewayIp == null || ("").equals(gatewayIp)) {
                gatewayIp = lowAddress;
            }
            if (allocationPools.size() < 1) {
                NeutronSubnetIpAllocationPool source = new NeutronSubnetIpAllocationPool(lowAddress,
                        highAddress);
                allocationPools = source.splitPoolV6(gatewayIp);
            }
        }
    }
}

From source file:org.opendaylight.neutron.spi.NeutronSubnet.java

public boolean isValidIp(String ipAddress) {
    if (ipVersion == IPV4_VERSION) {
        try {//w ww.  j a  va  2s.co m
            SubnetUtils util = new SubnetUtils(cidr);
            SubnetInfo info = util.getInfo();
            return info.isInRange(ipAddress);
        } catch (IllegalArgumentException e) {
            LOGGER.warn("Failure in isValidIp()", e);
            return false;
        }
    }

    if (ipVersion == IPV6_VERSION) {
        String[] parts = cidr.split("/");
        try {
            int length = Integer.parseInt(parts[1]);
            byte[] cidrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
            byte[] ipBytes = ((Inet6Address) InetAddress.getByName(ipAddress)).getAddress();
            for (int index = 0; index < length; index++) {
                if (((((int) cidrBytes[index / IPV6_LENGTH_BYTES]) & IPV6_LSB_MASK) & (1 << (IPV6_BYTE_OFFSET
                        - (index % IPV6_LENGTH_BYTES)))) != ((((int) ipBytes[index / IPV6_LENGTH_BYTES])
                                & IPV6_LSB_MASK) & (1 << (IPV6_BYTE_OFFSET - (index % IPV6_LENGTH_BYTES))))) {
                    return (false);
                }
            }
            return (true);
        } catch (UnknownHostException e) {
            LOGGER.warn("Failure in isValidIp()", e);
            return (false);
        }
    }
    return false;
}

From source file:org.opendaylight.opendove.odmc.OpenDoveGwIpv4.java

public static OpenDoveGwIpv4 assignEGWs(Object o, OpenDoveServiceAppliance target, String subnetCIDR,
        String subnetGatewayIP, String gwAddress, NeutronRouter router) {
    IfSBDoveGwIpv4CRUD gatewayIPDB = OpenDoveCRUDInterfaces.getIfSBDoveGwIpv4CRUD(o);
    SubnetUtils util = new SubnetUtils(subnetCIDR);
    SubnetInfo info = util.getInfo();
    OpenDoveGwIpv4 newGWIP = null;//from   w ww. j a  v a 2 s.  co m
    List<OpenDoveGwIpv4> gwIPs = gatewayIPDB.getGwIpv4Pool();
    Iterator<OpenDoveGwIpv4> ipIterator = gwIPs.iterator();
    boolean found = false;

    while (ipIterator.hasNext()) {
        OpenDoveGwIpv4 gwIP = ipIterator.next();

        if (gwIP.getGWUUID().equalsIgnoreCase(target.getUUID()) && info.isInRange(gwIP.getIP())
                && gwIP.getType().equalsIgnoreCase("external")) {
            found = true;
        }
    }

    if (!found) {
        newGWIP = new OpenDoveGwIpv4(gwAddress, OpenDoveSubnet.getIPMask(subnetCIDR), subnetGatewayIP,
                "external", target.getUUID(), 0, router);
        //TODO: this needs to be outside
        //newGWIP.setNeutronSubnet(neutronSubnet);
        gatewayIPDB.addGwIpv4(newGWIP.getUUID(), newGWIP);
        // Set the External IP for EGW, will be used by SNAT Pool Configuration
        target.setEGWExtIP(newGWIP);
    }

    return newGWIP;
}

From source file:org.opendaylight.opendove.odmc.OpenDoveServiceAppliance.java

public static OpenDoveServiceAppliance selectAssignedDGW(Object o, String cidr) {
    List<OpenDoveServiceAppliance> answer = new ArrayList<OpenDoveServiceAppliance>();
    IfOpenDoveServiceApplianceCRUD serviceApplianceDB = OpenDoveCRUDInterfaces.getIfDoveServiceApplianceCRUD(o);
    for (OpenDoveServiceAppliance oDSA : serviceApplianceDB.getAppliances()) {
        if (oDSA.get_isDGW() && oDSA.getDoveTunnel() != null && oDSA.getEGWExtIP() != null) {
            String ip = oDSA.getEGWExtIP().getIP();
            SubnetUtils util = new SubnetUtils(cidr);
            SubnetInfo info = util.getInfo();
            if (info.isInRange(ip)) {
                answer.add(oDSA);//from w  w w .  ja  va  2  s . c  o  m
            }
        }
    }
    if (answer.size() > 0) {
        Integer count = answer.size();
        int index = Math.abs(OpenDoveUtils.getNextInt()) % count;
        OpenDoveServiceAppliance target = answer.get(index);
        return target;
    }
    return null;
}

From source file:org.opendaylight.opendove.odmc.OpenDoveSubnet.java

public boolean containsAddress(String ip) {
    try {/*from   w  w  w.  j  a va 2s.c om*/
        SubnetUtils util = new SubnetUtils(subnet, mask);
        SubnetInfo info = util.getInfo();
        return info.isInRange(ip);
    } catch (Exception e) {
        return false;
    }
}

From source file:org.opendaylight.plugin2oc.neutron.SubnetHandler.java

boolean validGatewayIP(NeutronSubnet subnet, String ipAddress) {
    try {//from  ww  w  . j  a v  a 2 s .com
        SubnetUtils util = new SubnetUtils(subnet.getCidr());
        SubnetInfo info = util.getInfo();
        boolean inRange = info.isInRange(ipAddress);
        if (!inRange) {
            return false;
        } else {
            // ip available in allocation pool
            Iterator<NeutronSubnet_IPAllocationPool> i = subnet.getAllocationPools().iterator();
            while (i.hasNext()) {
                NeutronSubnet_IPAllocationPool pool = i.next();
                if (pool.contains(ipAddress)) {
                    return true;
                }
            }
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        LOGGER.error("Exception  :  " + e);
        return false;
    }
}

From source file:org.opendaylight.vpnservice.dhcpservice.DhcpPktHandler.java

private void setCommonOptions(DHCP pkt, DhcpInfo dhcpInfo) {
    pkt.setOptionInt(DHCPConstants.OPT_LEASE_TIME, dhcpMgr.getDhcpLeaseTime());
    if (dhcpMgr.getDhcpDefDomain() != null) {
        pkt.setOptionString(DHCPConstants.OPT_DOMAIN_NAME, dhcpMgr.getDhcpDefDomain());
    }/*  ww w  . j a va2 s .c o  m*/
    if (dhcpMgr.getDhcpLeaseTime() > 0) {
        pkt.setOptionInt(DHCPConstants.OPT_REBINDING_TIME, dhcpMgr.getDhcpRebindingTime());
        pkt.setOptionInt(DHCPConstants.OPT_RENEWAL_TIME, dhcpMgr.getDhcpRenewalTime());
    }
    SubnetUtils util = null;
    SubnetInfo info = null;
    util = new SubnetUtils(dhcpInfo.getCidr());
    info = util.getInfo();
    String gwIp = dhcpInfo.getGatewayIp();
    List<String> dnServers = dhcpInfo.getDnsServers();
    try {
        /*
         * setParameterListOptions may have initialized some of these
         * options to maintain order. If we can't fill them, unset to avoid
         * sending wrong information in reply.
         */
        if (gwIp != null) {
            pkt.setOptionInetAddr(DHCPConstants.OPT_SERVER_IDENTIFIER, gwIp);
            pkt.setOptionInetAddr(DHCPConstants.OPT_ROUTERS, gwIp);
        } else {
            pkt.unsetOption(DHCPConstants.OPT_SERVER_IDENTIFIER);
            pkt.unsetOption(DHCPConstants.OPT_ROUTERS);
        }
        if (info != null) {
            pkt.setOptionInetAddr(DHCPConstants.OPT_SUBNET_MASK, info.getNetmask());
            pkt.setOptionInetAddr(DHCPConstants.OPT_BROADCAST_ADDRESS, info.getBroadcastAddress());
        } else {
            pkt.unsetOption(DHCPConstants.OPT_SUBNET_MASK);
            pkt.unsetOption(DHCPConstants.OPT_BROADCAST_ADDRESS);
        }
        if ((dnServers != null) && (dnServers.size() > 0)) {
            pkt.setOptionStrAddrs(DHCPConstants.OPT_DOMAIN_NAME_SERVERS, dnServers);
        } else {
            pkt.unsetOption(DHCPConstants.OPT_DOMAIN_NAME_SERVERS);
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}