Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion utils/src/main/java/com/cloud/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -90,7 +92,7 @@

/**
* Converts a List of tags to a comma separated list
* @param tags
* @param tagsList
* @return String containing a comma separated list of tags
*/

Expand Down Expand Up @@ -304,4 +306,92 @@

return mapResult;
}

/**
* Converts the comma separated numbers to ranges for any consecutive numbers in the input with numbers (and ranges)
* Eg: "198,200-203,299,300,301,303,304,305,306,307,308,311,197" to "197-198,200-203,299-301,303-308,311"
* @param inputNumbersAndRanges
* @return String containing a converted ranges for any consecutive numbers
*/
public static String numbersToRange(String inputNumbersAndRanges) {

Check warning on line 316 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L316

Added line #L316 was not covered by tests
Set<Integer> numberSet = new TreeSet<>();
for (String inputNumber : inputNumbersAndRanges.split(",")) {
inputNumber = inputNumber.trim();
if (inputNumber.contains("-")) {
String[] range = inputNumber.split("-");
if (range.length == 2 && range[0] != null && range[1] != null) {
int start = NumbersUtil.parseInt(range[0], 0);
int end = NumbersUtil.parseInt(range[1], 0);
for (int i = start; i <= end; i++) {
numberSet.add(i);
}
}
} else {
numberSet.add(NumbersUtil.parseInt(inputNumber, 0));
}
}

StringBuilder result = new StringBuilder();
if (!numberSet.isEmpty()) {
List<Integer> numbers = new ArrayList<>(numberSet);
int startNumber = numbers.get(0);
int endNumber = startNumber;

for (int i = 1; i < numbers.size(); i++) {
if (numbers.get(i) == endNumber + 1) {
endNumber = numbers.get(i);
} else {
appendRange(result, startNumber, endNumber);
startNumber = endNumber = numbers.get(i);
}
}
appendRange(result, startNumber, endNumber);
}

return result.toString();
}

Check warning on line 352 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L352

Added line #L352 was not covered by tests

private static void appendRange(StringBuilder sb, int startNumber, int endNumber) {

Check warning on line 354 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L354

Added line #L354 was not covered by tests
if (sb.length() > 0) {
sb.append(",");
}
if (startNumber == endNumber) {
sb.append(startNumber);
} else {
sb.append(startNumber).append("-").append(endNumber);
}
}

/**
* Converts the comma separated numbers and ranges to numbers
* Eg: "197-198,200-203,299-301,303-308,311" to "197,198,200,201,202,203,299,300,301,303,304,305,306,307,308,311"
* @param inputNumbersAndRanges
* @return String containing a converted numbers
*/
public static String rangeToNumbers(String inputNumbersAndRanges) {
Set<Integer> numberSet = new TreeSet<>();

Check warning on line 372 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L371-L372

Added lines #L371 - L372 were not covered by tests
for (String inputNumber : inputNumbersAndRanges.split(",")) {
inputNumber = inputNumber.trim();

Check warning on line 374 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L374

Added line #L374 was not covered by tests
if (inputNumber.contains("-")) {
String[] range = inputNumber.split("-");
int startNumber = Integer.parseInt(range[0]);
int endNumber = Integer.parseInt(range[1]);

Check warning on line 378 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L376-L378

Added lines #L376 - L378 were not covered by tests
for (int i = startNumber; i <= endNumber; i++) {
numberSet.add(i);

Check warning on line 380 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L380

Added line #L380 was not covered by tests
}
} else {
numberSet.add(Integer.parseInt(inputNumber));

Check warning on line 383 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L382-L383

Added lines #L382 - L383 were not covered by tests
}
}

StringBuilder result = new StringBuilder();

Check warning on line 387 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L387

Added line #L387 was not covered by tests
for (int number : numberSet) {
if (result.length() > 0) {
result.append(",");

Check warning on line 390 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L390

Added line #L390 was not covered by tests
}
result.append(number);
}

Check warning on line 393 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L392-L393

Added lines #L392 - L393 were not covered by tests

return result.toString();
}

Check warning on line 396 in utils/src/main/java/com/cloud/utils/StringUtils.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/com/cloud/utils/StringUtils.java#L395-L396

Added lines #L395 - L396 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,18 @@
}
}

public static String composeCloudNetworkName(String prefix, String vlanId, String svlanId, Integer networkRateMbps, String vSwitchName) {
public static String composeCloudNetworkName(String prefix, String vlanId, String svlanId, Integer networkRateMbps, String vSwitchName, VirtualSwitchType vSwitchType) {
StringBuffer sb = new StringBuffer(prefix);
if (vlanId == null || UNTAGGED_VLAN_NAME.equalsIgnoreCase(vlanId)) {
sb.append(".untagged");
} else {
if (vSwitchType != VirtualSwitchType.StandardVirtualSwitch && StringUtils.containsAny(vlanId, ",-")) {
vlanId = com.cloud.utils.StringUtils.numbersToRange(vlanId);
}
sb.append(".").append(vlanId);
if (svlanId != null) {
sb.append(".").append("s" + svlanId);
}

}

if (networkRateMbps != null && networkRateMbps.intValue() > 0)
Expand All @@ -293,7 +295,12 @@
sb.append(".").append(VersioningContants.PORTGROUP_NAMING_VERSION);
sb.append("-").append(vSwitchName);

return sb.toString();
String networkName = sb.toString();
if (networkName.length() > 80) {
// the maximum limit for a vSwitch name is 80 chars, applies to both standard and distributed virtual switches.
s_logger.warn(String.format("The network name: %s for the vSwitch %s of type %s, exceeds 80 chars", networkName, vSwitchName, vSwitchType));

Check warning on line 301 in vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java

View check run for this annotation

Codecov / codecov/patch

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java#L301

Added line #L301 was not covered by tests
}
return networkName;
}

public static Map<String, String> getValidatedVsmCredentials(VmwareContext context) throws Exception {
Expand Down Expand Up @@ -595,7 +602,7 @@
if (vlanId != null) {
vlanId = vlanId.replace("vlan://", "");
}
networkName = composeCloudNetworkName(namePrefix, vlanId, secondaryvlanId, networkRateMbps, physicalNetwork);
networkName = composeCloudNetworkName(namePrefix, vlanId, secondaryvlanId, networkRateMbps, physicalNetwork, vSwitchType);

Check warning on line 605 in vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java

View check run for this annotation

Codecov / codecov/patch

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java#L605

Added line #L605 was not covered by tests

if (vlanId != null && !UNTAGGED_VLAN_NAME.equalsIgnoreCase(vlanId) && !StringUtils.containsAny(vlanId, ",-")) {
createGCTag = true;
Expand Down Expand Up @@ -1167,8 +1174,9 @@
if (vlanId == null && vlanRange != null && !vlanRange.isEmpty()) {
s_logger.debug("Creating dvSwitch port vlan-trunk spec with range: " + vlanRange);
VmwareDistributedVirtualSwitchTrunkVlanSpec trunkVlanSpec = new VmwareDistributedVirtualSwitchTrunkVlanSpec();
for (final String vlanRangePart : vlanRange.split(",")) {
if (vlanRangePart == null || vlanRange.isEmpty()) {
String vlanRangeUpdated = com.cloud.utils.StringUtils.numbersToRange(vlanRange);
for (final String vlanRangePart : vlanRangeUpdated.split(",")) {
if (vlanRangePart == null || vlanRangePart.isEmpty()) {
continue;
}
final NumericRange numericRange = new NumericRange();
Expand Down Expand Up @@ -1320,7 +1328,7 @@
// No doubt about this, depending on vid=null to avoid lots of code below
vid = null;
} else {
networkName = composeCloudNetworkName(namePrefix, vlanId, null, networkRateMbps, vSwitchName);
networkName = composeCloudNetworkName(namePrefix, vlanId, null, networkRateMbps, vSwitchName, VirtualSwitchType.StandardVirtualSwitch);

Check warning on line 1331 in vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java

View check run for this annotation

Codecov / codecov/patch

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java#L1331

Added line #L1331 was not covered by tests

if (vlanId != null && !UNTAGGED_VLAN_NAME.equalsIgnoreCase(vlanId)) {
createGCTag = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public void testComposeCloudNetworkNameTaggedVlanPublicTraffic() throws Exceptio
networkRateMbps = 200;
prefix = "cloud.public";
vSwitchName = "vSwitch0";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName);
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName, VirtualSwitchType.StandardVirtualSwitch);
assertEquals("cloud.public.100.200.1-vSwitch0", cloudNetworkName);
}

Expand All @@ -567,7 +567,7 @@ public void testComposeCloudNetworkNameUnTaggedVlanStorageTraffic() throws Excep
networkRateMbps = null;
prefix = "cloud.storage";
vSwitchName = "vSwitch1";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName);
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName, VirtualSwitchType.StandardVirtualSwitch);
assertEquals("cloud.storage.untagged.0.1-vSwitch1", cloudNetworkName);
}

Expand All @@ -578,10 +578,60 @@ public void testComposeCloudNetworkNameUnTaggedVlanGuestTraffic() throws Excepti
networkRateMbps = 512;
prefix = "cloud.guest";
vSwitchName = "vSwitch2";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName);
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, svlanId, networkRateMbps, vSwitchName, VirtualSwitchType.StandardVirtualSwitch);
assertEquals("cloud.guest.400.s123.512.1-vSwitch2", cloudNetworkName);
}

@Test
public void testComposeCloudNetworkNameVlanRangeGuestTrafficDvSwitch() {
vlanId = "400-500";
networkRateMbps = 512;
prefix = "cloud.guest";
vSwitchName = "dvSwitch0";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, null, networkRateMbps, vSwitchName, VirtualSwitchType.VMwareDistributedVirtualSwitch);
assertEquals("cloud.guest.400-500.512.1-dvSwitch0", cloudNetworkName);
}

@Test
public void testComposeCloudNetworkNameVlanNumbersGuestTrafficDvSwitch() {
vlanId = "3001,3002,3003,3004,3005,3006,3007,3008,3009,3010,3011,3012,3013,3014,3015,3016,3017,3018,3019,3020";
networkRateMbps = 512;
prefix = "cloud.guest";
vSwitchName = "dvSwitch0";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, null, networkRateMbps, vSwitchName, VirtualSwitchType.VMwareDistributedVirtualSwitch);
assertEquals("cloud.guest.3001-3020.512.1-dvSwitch0", cloudNetworkName);
}

@Test
public void testComposeCloudNetworkNameVlanNumbersAndRangeGuestTrafficDvSwitch() {
vlanId = "3001,3004-3006,3007,3008,3009,3010,3011,3012,3013,3014,3015,3016,3017,3018,3020";
networkRateMbps = 512;
prefix = "cloud.guest";
vSwitchName = "dvSwitch0";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, null, networkRateMbps, vSwitchName, VirtualSwitchType.VMwareDistributedVirtualSwitch);
assertEquals("cloud.guest.3001,3004-3018,3020.512.1-dvSwitch0", cloudNetworkName);
}

@Test
public void testComposeCloudNetworkNameUnorderedVlanNumbersAndRangeGuestTrafficDvSwitch() {
vlanId = "3018,3020,3011,3012,3004-3006,3007,3001,3008,3009,3010,3013,3014,3015,3016,3017";
networkRateMbps = 512;
prefix = "cloud.guest";
vSwitchName = "dvSwitch0";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, null, networkRateMbps, vSwitchName, VirtualSwitchType.VMwareDistributedVirtualSwitch);
assertEquals("cloud.guest.3001,3004-3018,3020.512.1-dvSwitch0", cloudNetworkName);
}

@Test
public void testComposeCloudNetworkNameOverlappingVlanNumbersAndRangeGuestTrafficDvSwitch() {
vlanId = "3018,3020,3011,3012,3004-3006,3007,3001,3008,3009,3010,3013,3014,3015,3016,3017,3005-3008";
networkRateMbps = 512;
prefix = "cloud.guest";
vSwitchName = "dvSwitch0";
String cloudNetworkName = HypervisorHostHelper.composeCloudNetworkName(prefix, vlanId, null, networkRateMbps, vSwitchName, VirtualSwitchType.VMwareDistributedVirtualSwitch);
assertEquals("cloud.guest.3001,3004-3018,3020.512.1-dvSwitch0", cloudNetworkName);
}

@Test
public void testOvfDomRewriter() {
final String ovfString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
Expand Down
Loading