Search in sources :

Example 6 with SubnetInfo

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();
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo) Nonnull(javax.annotation.Nonnull)

Example 7 with SubnetInfo

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);
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) IpMapKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo) IpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap) IntextIpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap) IpMapBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapBuilder)

Example 8 with SubnetInfo

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;
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Example 9 with SubnetInfo

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;
}
Also used : ArrayList(java.util.ArrayList) IpAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo)

Aggregations

SubnetInfo (org.apache.commons.net.util.SubnetUtils.SubnetInfo)9 SubnetUtils (org.apache.commons.net.util.SubnetUtils)8 IntextIpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap)2 IpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 IpAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress)1 IpMapping (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping)1 IpMappingKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey)1 IpMapBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapBuilder)1 IpMapKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey)1