
org.xlcloud.network.validatation.CIDRValidator Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012 AMG.lab, a Bull Group Company
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xlcloud.network.validatation;
import org.apache.commons.net.util.SubnetUtils;
import static org.xlcloud.validation.Validations.validateThat;
import org.xlcloud.rest.exception.DuplicatedEntityException;
import org.xlcloud.rest.exception.DuplicatedEntityException.EntityType;
import org.xlcloud.rest.exception.InternalErrorException.ErrorType;
import org.xlcloud.rest.exception.ValidationException.ValidationFailureType;
import org.xlcloud.rest.exception.ValidationException;
/**
* Validator for CIDR address
*
* @author mkaminsk, AMG.net
*/
public class CIDRValidator {
/**
* Validates if CIDR is has valid format and validates if 0 in mask are
* equal 0 in IpAddress
*/
public static void validateCIDR(String cidr) {
toSubnetUtils(cidr);
}
private static SubnetUtils toSubnetUtils(String cidr) {
validateThat(cidr).as("cidr").isNotBlank();
SubnetUtils utils = null;
try {
utils = new SubnetUtils(cidr);
} catch (IllegalArgumentException ex) {
throw new ValidationException("Invalid CIDR format",ValidationFailureType.CIDR_FORMAT);
}
int cidrAddress = ipAddressToInt(utils.getInfo().getAddress());
int lowestAddress = ipAddressToInt(utils.getInfo().getLowAddress());
if (cidrAddress > lowestAddress) {
throw new ValidationException("Ip address for CIDR has numbers for 0 mask",ValidationFailureType.CIDR_FORMAT);
}
return utils;
}
/**
* Validates if CIDR is has valid format and validates if 0 in mask are
* equal 0 in IpAddress
*
* @throws ValidationException
* , when at least one of CIDR has invalid format
* @throws DuplicatedEntityException
* , when CIDR addresses are overlaped
*/
public static void validateSeparableCIDR(String incoming, String external) {
SubnetUtils utils1 = toSubnetUtils(incoming);
SubnetUtils utils2 = toSubnetUtils(external);
String ipAddress1 = utils1.getInfo().getAddress();
String ipAddress2 = utils2.getInfo().getAddress();
String mask1 = utils1.getInfo().getNetmask();
String mask2 = utils2.getInfo().getNetmask();
int intMask1 = ipAddressToInt(mask1);
int intMask2 = ipAddressToInt(mask2);
int mask = (Integer.valueOf(incoming.split("/")[1]) < Integer.valueOf(external.split("/")[1])) ? (intMask1) : (intMask2);
if ((ipAddressToInt(ipAddress1) & mask) == (ipAddressToInt(ipAddress2) & mask)) {
throw new DuplicatedEntityException("CIDR address has overlap ranges", EntityType.CIDR_ADDRESS, incoming);
}
}
/**
* Convert IPs into ints (32 bits). E.g. 157.166.224.26 becomes 10011101
* 10100110 11100000 00011010
*
* @param ipaddress
* @return
*/
private static int ipAddressToInt(String addressIp) {
String[] ipaddress = addressIp.split("\\.");
return ((Integer.valueOf(ipaddress[0]) << 24) & 0xFF000000) | ((Integer.valueOf(ipaddress[1]) << 16) & 0xFF0000)
| ((Integer.valueOf(ipaddress[2]) << 8) & 0xFF00) | (Integer.valueOf(ipaddress[3]) & 0xFF);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy