mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Merge remote-tracking branch 'apache/4.17' into main
This commit is contained in:
commit
194b0b4610
@ -2016,7 +2016,7 @@ public class NetworkModelImpl extends ManagerBase implements NetworkModel, Confi
|
|||||||
usedIps.add(NetUtils.ip2Long(ip));
|
usedIps.add(NetUtils.ip2Long(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]), usedIps);
|
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]), usedIps, -1);
|
||||||
|
|
||||||
String gateway = network.getGateway();
|
String gateway = network.getGateway();
|
||||||
if ((gateway != null) && (allPossibleIps.contains(NetUtils.ip2Long(gateway))))
|
if ((gateway != null) && (allPossibleIps.contains(NetUtils.ip2Long(gateway))))
|
||||||
|
|||||||
@ -638,7 +638,7 @@ public class NetUtils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<Long> getAllIpsFromCidr(final String cidr, final long size, final Set<Long> usedIps) {
|
public static Set<Long> getAllIpsFromCidr(final String cidr, final long size, final Set<Long> usedIps, int maxIps) {
|
||||||
assert size < MAX_CIDR : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
|
assert size < MAX_CIDR : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
|
||||||
final Set<Long> result = new TreeSet<Long>();
|
final Set<Long> result = new TreeSet<Long>();
|
||||||
final long ip = ip2Long(cidr);
|
final long ip = ip2Long(cidr);
|
||||||
@ -650,11 +650,9 @@ public class NetUtils {
|
|||||||
|
|
||||||
end++;
|
end++;
|
||||||
end = (end << MAX_CIDR - size) - 2;
|
end = (end << MAX_CIDR - size) - 2;
|
||||||
int maxIps = 255; // get 255 ips as maximum
|
while (start <= end && (maxIps == -1 || result.size() < maxIps)) {
|
||||||
while (start <= end && maxIps > 0) {
|
|
||||||
if (!usedIps.contains(start)) {
|
if (!usedIps.contains(start)) {
|
||||||
result.add(start);
|
result.add(start);
|
||||||
maxIps--;
|
|
||||||
}
|
}
|
||||||
start++;
|
start++;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,8 +36,12 @@ import java.net.InetAddress;
|
|||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -58,6 +62,8 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
@PowerMockIgnore({"jdk.xml.internal.*", "javax.xml.parsers.*", "org.xml.sax.*", "org.w3c.dom.*"})
|
@PowerMockIgnore({"jdk.xml.internal.*", "javax.xml.parsers.*", "org.xml.sax.*", "org.w3c.dom.*"})
|
||||||
public class NetUtilsTest {
|
public class NetUtilsTest {
|
||||||
|
private static final String WIDE_SHARED_NET_CIDR_IP = "10.20.0.0";
|
||||||
|
private static final List<String> WIDE_SHARED_NET_USED_IPS = List.of("10.20.0.22", "10.20.1.22", "10.20.2.22");
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetRandomIpFromCidrWithSize24() throws Exception {
|
public void testGetRandomIpFromCidrWithSize24() throws Exception {
|
||||||
@ -750,6 +756,52 @@ public class NetUtilsTest {
|
|||||||
assertEquals("255.255.240.0", NetUtils.cidr2Netmask("169.254.240.0/20"));
|
assertEquals("255.255.240.0", NetUtils.cidr2Netmask("169.254.240.0/20"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void runTestGetAllIpsFromCidr(int cidrSize, int maxIps, boolean usedIpPresent, int resultSize) {
|
||||||
|
Set<Long> usedIps = new TreeSet<>();
|
||||||
|
if (usedIpPresent) {
|
||||||
|
for (String ip : WIDE_SHARED_NET_USED_IPS) {
|
||||||
|
usedIps.add(NetUtils.ip2Long(ip));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Set<Long> result = NetUtils.getAllIpsFromCidr(WIDE_SHARED_NET_CIDR_IP, cidrSize, usedIps, maxIps);
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(resultSize, result.size());
|
||||||
|
if (usedIpPresent) {
|
||||||
|
for (String ip : WIDE_SHARED_NET_USED_IPS) {
|
||||||
|
assertFalse(result.contains(NetUtils.ip2Long(ip)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllIpsFromCidrNoneUsedNoLimit() {
|
||||||
|
runTestGetAllIpsFromCidr(22, -1, false, 1022);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllIpsFromCidrNoneUsedLimit() {
|
||||||
|
runTestGetAllIpsFromCidr(22, 255, false, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllIpsFromCidrNoneUsedLessLimit() {
|
||||||
|
runTestGetAllIpsFromCidr(22, 10, false, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllIpsFromCidrUsedNoLimit() {
|
||||||
|
runTestGetAllIpsFromCidr(22, -1, true, 1022 - WIDE_SHARED_NET_USED_IPS.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAllIpsFromCidrUsedLimit() {
|
||||||
|
runTestGetAllIpsFromCidr(22, 50, true, 50);
|
||||||
|
List<String> usedIpsInRange = new ArrayList<>(WIDE_SHARED_NET_USED_IPS);
|
||||||
|
usedIpsInRange = usedIpsInRange.stream().filter(x -> x.startsWith("10.20.0.")).collect(Collectors.toList());
|
||||||
|
runTestGetAllIpsFromCidr(24, 255, true, 254 - usedIpsInRange.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getNetworkInterfaceTestReturnNullWhenStringIsNull() {
|
public void getNetworkInterfaceTestReturnNullWhenStringIsNull() {
|
||||||
NetworkInterface result = NetUtils.getNetworkInterface(null);
|
NetworkInterface result = NetUtils.getNetworkInterface(null);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user