Java Code Examples for org.apache.commons.net.util.SubnetUtils#getInfo()

The following examples show how to use org.apache.commons.net.util.SubnetUtils#getInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubnetUtilsTest.java    From peer-os with Apache License 2.0 6 votes vote down vote up
@Test
public void testIp()
{
    SubnetUtils utils = new SubnetUtils( "10.12.0.1/24" );
    final SubnetUtils.SubnetInfo info = utils.getInfo();
    assertEquals( "10.12.0.1", info.getAddress() );
    assertEquals( "10.12.0.0", info.getNetworkAddress() );
    assertEquals( "10.12.0.255", info.getBroadcastAddress() );
    assertEquals( 254, info.getAddressCount() );
    assertEquals( "10.12.0.1/24", info.getCidrSignature() );
    assertEquals( "10.12.0.1", info.getLowAddress() );
    assertEquals( "10.12.0.254", info.getHighAddress() );
    assertEquals( "255.255.255.0", info.getNetmask() );
    assertEquals( true, info.isInRange( "10.12.0.100" ) );
    assertEquals( false, info.isInRange( "10.11.0.1" ) );
}
 
Example 2
Source File: L3NetworkApiInterceptor.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void validate(APIAddIpRangeByNetworkCidrMsg msg) {
    try {
        SubnetUtils utils = new SubnetUtils(msg.getNetworkCidr());
        utils.setInclusiveHostCount(false);
        SubnetInfo subnet = utils.getInfo();
        if (subnet.getAddressCount() == 0) {
            throw new ApiMessageInterceptionException(argerr("%s is not an allowed network cidr, because it doesn't have usable ip range", msg.getNetworkCidr()));
        }

        if (msg.getGateway() != null && !(msg.getGateway().equals(subnet.getLowAddress()) || msg.getGateway().equals(subnet.getHighAddress()))) {
            throw new ApiMessageInterceptionException(argerr("%s is not the first or last address of the cidr %s", msg.getGateway(), msg.getNetworkCidr()));
        }
    } catch (IllegalArgumentException e) {
        throw new ApiMessageInterceptionException(argerr("%s is not a valid network cidr", msg.getNetworkCidr()));
    }

    if (msg.getIpRangeType() == null) {
        msg.setIpRangeType(IpRangeType.Normal.toString());
    }

    IpRangeInventory ipr = IpRangeInventory.fromMessage(msg);
    validate(ipr);
}
 
Example 3
Source File: K8sNetworkingUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains valid IP addresses of the given subnet.
 *
 * @param cidr CIDR
 * @return set of IP addresses
 */
public static Set<IpAddress> getSubnetIps(String cidr) {
    SubnetUtils utils = new SubnetUtils(cidr);
    utils.setInclusiveHostCount(false);
    SubnetUtils.SubnetInfo info = utils.getInfo();
    Set<String> allAddresses =
            new HashSet<>(Arrays.asList(info.getAllAddresses()));

    if (allAddresses.size() > 2) {
        allAddresses.remove(info.getLowAddress());
        allAddresses.remove(info.getHighAddress());
    }

    return allAddresses.stream()
            .map(IpAddress::valueOf).collect(Collectors.toSet());
}
 
Example 4
Source File: LoadBalancerApiInterceptor.java    From zstack with Apache License 2.0 5 votes vote down vote up
private void validate(APICreateLoadBalancerMsg msg) {
    List<String> useFor = Q.New(VipNetworkServicesRefVO.class).select(VipNetworkServicesRefVO_.serviceType).eq(VipNetworkServicesRefVO_.vipUuid, msg.getVipUuid()).listValues();
    if(useFor != null && !useFor.isEmpty()){
        VipUseForList useForList = new VipUseForList(useFor);
        if(!useForList.validateNewAdded(LoadBalancerConstants.LB_NETWORK_SERVICE_TYPE_STRING)){
            throw new ApiMessageInterceptionException(argerr("the vip[uuid:%s] has been occupied other network service entity[%s]", msg.getVipUuid(), useForList.toString()));
        }
    }

    /* the vip can not the first of the last ip of the cidr */
    VipVO vipVO = dbf.findByUuid(msg.getVipUuid(), VipVO.class);
    if (NetworkUtils.isIpv4Address(vipVO.getIp())) {
        AddressPoolVO addressPoolVO = dbf.findByUuid(vipVO.getIpRangeUuid(), AddressPoolVO.class);
        if (addressPoolVO == null) {
            return;
        }

        SubnetUtils utils = new SubnetUtils(addressPoolVO.getNetworkCidr());
        SubnetUtils.SubnetInfo subnet = utils.getInfo();
        String firstIp = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) - 1);
        String lastIp = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()) + 1);
        if (vipVO.getIp().equals(firstIp) || vipVO.getIp().equals(lastIp)) {
            throw new ApiMessageInterceptionException(argerr("loadbalancer vip [%s] can not the first of the last ip of the cidr", vipVO.getIp()));
        }
    }

}
 
Example 5
Source File: NetworkUtils.java    From zstack with Apache License 2.0 5 votes vote down vote up
public static boolean isCidrOverlap(String cidr1, String cidr2) {
    DebugUtils.Assert(isCidr(cidr1), String.format("%s is not a cidr", cidr1));
    DebugUtils.Assert(isCidr(cidr2), String.format("%s is not a cidr", cidr2));

    SubnetUtils su1 = new SubnetUtils(cidr1);
    SubnetUtils su2 = new SubnetUtils(cidr2);

    SubnetUtils.SubnetInfo info1 = su1.getInfo();
    SubnetUtils.SubnetInfo info2 = su2.getInfo();

    return isIpv4RangeOverlap(info1.getLowAddress(), info1.getHighAddress(), info2.getLowAddress(), info2.getHighAddress());
}
 
Example 6
Source File: IpRangeInventory.java    From zstack with Apache License 2.0 5 votes vote down vote up
public static IpRangeInventory fromMessage(APIAddIpRangeByNetworkCidrMsg msg) {
    SubnetUtils utils = new SubnetUtils(msg.getNetworkCidr());
    SubnetInfo subnet = utils.getInfo();

    IpRangeInventory ipr = new IpRangeInventory();
    ipr.setNetworkCidr(msg.getNetworkCidr());
    ipr.setName(msg.getName());
    ipr.setDescription(msg.getDescription());

    ipr.setIpRangeType(IpRangeType.valueOf(msg.getIpRangeType()));

    if (ipr.getIpRangeType() == IpRangeType.Normal) {
        String lowAddress = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()));
        String highAddress = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()));
        if (msg.getGateway() == null || msg.getGateway().equals(lowAddress)) {
            ipr.setGateway(lowAddress);
            ipr.setStartIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) + 1));
            ipr.setEndIp(subnet.getHighAddress());
        } else if (msg.getGateway().equals(highAddress)) {
            ipr.setGateway(highAddress);
            ipr.setStartIp(lowAddress);
            ipr.setEndIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()) - 1));
        }
    } else {
        ipr.setGateway(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress())));
        ipr.setStartIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) - 1));
        ipr.setEndIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()) + 1));
    }
    ipr.setNetmask(subnet.getNetmask());
    ipr.setPrefixLen(NetworkUtils.getPrefixLengthFromNetwork(subnet.getNetmask()));
    ipr.setL3NetworkUuid(msg.getL3NetworkUuid());
    ipr.setUuid(msg.getResourceUuid());
    ipr.setIpVersion(IPv6Constants.IPv4);
    return ipr;
}
 
Example 7
Source File: DefaultK8sNetwork.java    From onos with Apache License 2.0 5 votes vote down vote up
private IpAddress getGatewayIp(String cidr) {
    SubnetUtils utils = new SubnetUtils(cidr);
    utils.setInclusiveHostCount(false);
    SubnetUtils.SubnetInfo info = utils.getInfo();

    return IpAddress.valueOf(info.getLowAddress());
}