use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project netvirt by opendaylight.
the class DhcpPktHandler method convertToClasslessRouteOption.
@Nonnull
protected byte[] convertToClasslessRouteOption(String dest, String router) {
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
if (dest == null || router == null) {
return new byte[0];
}
// get prefix
Short prefix = null;
String[] parts = dest.split("/");
if (parts.length < 2) {
prefix = (short) 0;
} else {
prefix = Short.valueOf(parts[1]);
}
byteArray.write(prefix.byteValue());
SubnetUtils util = new SubnetUtils(dest);
SubnetInfo info = util.getInfo();
String strNetAddr = info.getNetworkAddress();
try {
byte[] netAddr = InetAddress.getByName(strNetAddr).getAddress();
// Strip any trailing 0s from netAddr
for (int i = 0; i < netAddr.length; i++) {
if (netAddr[i] != 0) {
byteArray.write(netAddr, i, 1);
}
}
byteArray.write(InetAddress.getByName(router).getAddress());
} catch (IOException e) {
return new byte[0];
}
return byteArray.toByteArray();
}
use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project netvirt by opendaylight.
the class NaptManager method registerMapping.
// 1. napt service functions
/**
* This method is used to inform this service of what external IP address to be used
* as mapping when requested one for the internal IP address given in the input.
*
* @param segmentId – segmentation in which the mapping to be used. Eg; routerid
* @param internal subnet prefix or ip address
* @param external subnet prefix or ip address
*/
public void registerMapping(long segmentId, IPAddress internal, IPAddress external) {
LOG.debug("registerMapping : called with segmentid {}, internalIp {}, prefix {}, externalIp {} " + "and prefix {} ", segmentId, internal.getIpAddress(), internal.getPrefixLength(), external.getIpAddress(), external.getPrefixLength());
// Create Pool per ExternalIp and not for all IPs in the subnet.
// Create new Pools during getExternalAddressMapping if exhausted.
String externalIpPool;
// subnet case
if (external.getPrefixLength() != 0 && external.getPrefixLength() != NatConstants.DEFAULT_PREFIX) {
String externalSubnet = external.getIpAddress() + "/" + external.getPrefixLength();
LOG.debug("registerMapping : externalSubnet is : {}", externalSubnet);
SubnetUtils subnetUtils = new SubnetUtils(externalSubnet);
SubnetInfo subnetInfo = subnetUtils.getInfo();
externalIpPool = subnetInfo.getLowAddress();
} else {
// ip case
externalIpPool = external.getIpAddress();
}
createNaptPortPool(externalIpPool);
// Store the ip to ip map in Operational DS
String internalIp = internal.getIpAddress();
if (internal.getPrefixLength() != 0) {
internalIp = internal.getIpAddress() + "/" + internal.getPrefixLength();
}
String externalIp = external.getIpAddress();
if (external.getPrefixLength() != 0) {
externalIp = external.getIpAddress() + "/" + external.getPrefixLength();
}
updateCounter(segmentId, externalIp, true);
// update the actual ip-map
IpMap ipm = new IpMapBuilder().setKey(new IpMapKey(internalIp)).setInternalIp(internalIp).setExternalIp(externalIp).build();
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, getIpMapIdentifier(segmentId, internalIp), ipm);
LOG.debug("registerMapping : registerMapping exit after updating DS with internalIP {}, externalIP {}", internalIp, externalIp);
}
use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project cloudbreak by hortonworks.
the class AwsResourceConnector method calculateSubnet.
private String calculateSubnet(String stackName, Vpc vpc, Iterable<String> subnetCidrs) {
SubnetInfo vpcInfo = new SubnetUtils(vpc.getCidrBlock()).getInfo();
String[] cidrParts = vpcInfo.getCidrSignature().split("/");
int netmask = Integer.parseInt(cidrParts[cidrParts.length - 1]);
int netmaskBits = CIDR_PREFIX - netmask;
if (netmaskBits <= 0) {
throw new CloudConnectorException("The selected VPC has to be in a bigger CIDR range than /24");
}
int numberOfSubnets = Double.valueOf(Math.pow(2, netmaskBits)).intValue();
int targetSubnet = 0;
if (stackName != null) {
byte[] b = stackName.getBytes(Charset.forName("UTF-8"));
for (byte ascii : b) {
targetSubnet += ascii;
}
}
targetSubnet = Long.valueOf(targetSubnet % numberOfSubnets).intValue();
String cidr = getSubnetCidrInRange(vpc, subnetCidrs, targetSubnet, numberOfSubnets);
if (cidr == null) {
cidr = getSubnetCidrInRange(vpc, subnetCidrs, 0, targetSubnet);
}
if (cidr == null) {
throw new CloudConnectorException("Cannot find non-overlapping CIDR range");
}
return cidr;
}
use of org.apache.commons.net.util.SubnetUtils.SubnetInfo in project genius by opendaylight.
the class VtepConfigSchemaListener method calculateAvailableIps.
/**
* Calculate available ips.
*
* @param subnetUtils
* the subnet cidr
* @param excludeIpFilter
* the exclude ip filter
* @param gatewayIp
* the gateway IP
* @return the list
*/
private List<IpAddress> calculateAvailableIps(SubnetUtils subnetUtils, String excludeIpFilter, IpAddress gatewayIp) {
List<IpAddress> lstAvailableIps = new ArrayList<>();
SubnetInfo subnetInfo = subnetUtils.getInfo();
String[] arrIpAddresses = subnetInfo.getAllAddresses();
for (String ipAddress : arrIpAddresses) {
lstAvailableIps.add(IpAddressBuilder.getDefaultInstance(ipAddress));
}
lstAvailableIps.remove(gatewayIp);
lstAvailableIps.removeAll(ItmUtils.getExcludeIpAddresses(excludeIpFilter, subnetInfo));
return lstAvailableIps;
}
Aggregations