Skip to content

Commit 86fcb14

Browse files
committed
Merge remote-tracking branch 'origin/4.14'
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
2 parents 4f8b88b + 1efe6e2 commit 86fcb14

File tree

10 files changed

+187
-28
lines changed

10 files changed

+187
-28
lines changed

api/src/main/java/com/cloud/network/Networks.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URISyntaxException;
2121

2222
import com.cloud.utils.exception.CloudRuntimeException;
23+
import org.apache.commons.lang3.StringUtils;
2324

2425
/**
2526
* Network includes all of the enums used within networking.
@@ -253,20 +254,42 @@ public static URI fromString(String candidate) {
253254
Long.parseLong(candidate);
254255
return Vlan.toUri(candidate);
255256
} catch (NumberFormatException nfe) {
256-
if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(candidate)) {
257-
return Native.toUri(candidate);
258-
}
259-
try {
260-
URI uri = new URI(candidate);
261-
BroadcastDomainType tiep = getSchemeValue(uri);
262-
if (tiep.scheme != null && tiep.scheme.equals(uri.getScheme())) {
263-
return uri;
264-
} else {
265-
throw new CloudRuntimeException("string '" + candidate + "' has an unknown BroadcastDomainType.");
266-
}
267-
} catch (URISyntaxException e) {
268-
throw new CloudRuntimeException("string is not a broadcast URI: " + candidate);
257+
return getVlanUriWhenNumberFormatException(candidate);
258+
}
259+
}
260+
261+
/**
262+
* This method is called in case of NumberFormatException is thrown when parsing the String into long
263+
*/
264+
private static URI getVlanUriWhenNumberFormatException(String candidate) {
265+
if(StringUtils.isBlank(candidate)) {
266+
throw new CloudRuntimeException("Expected VLAN or VXLAN but got a null isolation method");
267+
}
268+
if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(candidate)) {
269+
return Native.toUri(candidate);
270+
}
271+
try {
272+
URI uri = new URI(candidate);
273+
BroadcastDomainType tiep = getSchemeValue(uri);
274+
if (tiep.scheme != null && tiep.scheme.equals(uri.getScheme())) {
275+
return uri;
276+
} else {
277+
throw new CloudRuntimeException("string '" + candidate + "' has an unknown BroadcastDomainType.");
269278
}
279+
} catch (URISyntaxException e) {
280+
throw new CloudRuntimeException("string is not a broadcast URI: " + candidate);
281+
}
282+
}
283+
284+
/**
285+
* Encodes a string into a BroadcastUri, according to the given BroadcastDomainType
286+
*/
287+
public static URI encodeStringIntoBroadcastUri(String candidate, BroadcastDomainType isolationMethod) {
288+
try{
289+
Long.parseLong(candidate);
290+
return isolationMethod.toUri(candidate);
291+
} catch (NumberFormatException nfe) {
292+
return getVlanUriWhenNumberFormatException(candidate);
270293
}
271294
}
272295
};

engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,18 +1082,23 @@ private AgentAttache handleConnectedAgent(final Link link, final StartupCommand[
10821082
ReadyCommand ready = null;
10831083
try {
10841084
final List<String> agentMSHostList = new ArrayList<>();
1085+
String lbAlgorithm = null;
10851086
if (startup != null && startup.length > 0) {
10861087
final String agentMSHosts = startup[0].getMsHostList();
10871088
if (!Strings.isNullOrEmpty(agentMSHosts)) {
1088-
agentMSHostList.addAll(Arrays.asList(agentMSHosts.split(",")));
1089+
String[] msHosts = agentMSHosts.split("@");
1090+
if (msHosts.length > 1) {
1091+
lbAlgorithm = msHosts[1];
1092+
}
1093+
agentMSHostList.addAll(Arrays.asList(msHosts[0].split(",")));
10891094
}
10901095
}
10911096

10921097
final HostVO host = _resourceMgr.createHostVOForConnectedAgent(startup);
10931098
if (host != null) {
10941099
ready = new ReadyCommand(host.getDataCenterId(), host.getId(), NumbersUtil.enableHumanReadableSizes);
10951100

1096-
if (!indirectAgentLB.compareManagementServerList(host.getId(), host.getDataCenterId(), agentMSHostList)) {
1101+
if (!indirectAgentLB.compareManagementServerList(host.getId(), host.getDataCenterId(), agentMSHostList, lbAlgorithm)) {
10971102
final List<String> newMSList = indirectAgentLB.getManagementServerList(host.getId(), host.getDataCenterId(), null);
10981103
ready.setMsHostList(newMSList);
10991104
ready.setLbAlgorithm(indirectAgentLB.getLBAlgorithmName());

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,7 @@ private Network createGuestNetwork(final long networkOfferingId, final String na
23592359
}
23602360

23612361
if (vlanSpecified) {
2362-
URI uri = BroadcastDomainType.fromString(vlanId);
2362+
URI uri = encodeVlanIdIntoBroadcastUri(vlanId, pNtwk);
23632363
// Aux: generate secondary URI for secondary VLAN ID (if provided) for performing checks
23642364
URI secondaryUri = isNotBlank(isolatedPvlan) ? BroadcastDomainType.fromString(isolatedPvlan) : null;
23652365
//don't allow to specify vlan tag used by physical network for dynamic vlan allocation
@@ -2513,7 +2513,7 @@ public Network doInTransaction(final TransactionStatus status) {
25132513
//Logical router's UUID provided as VLAN_ID
25142514
userNetwork.setVlanIdAsUUID(vlanIdFinal); //Set transient field
25152515
} else {
2516-
uri = BroadcastDomainType.fromString(vlanIdFinal);
2516+
uri = encodeVlanIdIntoBroadcastUri(vlanIdFinal, pNtwk);
25172517
}
25182518

25192519
if (_networksDao.listByPhysicalNetworkPvlan(physicalNetworkId, uri.toString()).size() > 0) {
@@ -2577,6 +2577,25 @@ public Network doInTransaction(final TransactionStatus status) {
25772577
return network;
25782578
}
25792579

2580+
/**
2581+
* Encodes VLAN/VXLAN ID into a Broadcast URI according to the isolation method from the Physical Network.
2582+
* @return Broadcast URI, e.g. 'vlan://vlan_ID' or 'vxlan://vlxan_ID'
2583+
*/
2584+
protected URI encodeVlanIdIntoBroadcastUri(String vlanId, PhysicalNetwork pNtwk) {
2585+
if (pNtwk == null) {
2586+
throw new InvalidParameterValueException(String.format("Failed to encode VLAN/VXLAN %s into a Broadcast URI. Physical Network cannot be null.", vlanId));
2587+
}
2588+
2589+
if(StringUtils.isNotBlank(pNtwk.getIsolationMethods().get(0))) {
2590+
String isolationMethod = pNtwk.getIsolationMethods().get(0).toLowerCase();
2591+
String vxlan = BroadcastDomainType.Vxlan.toString().toLowerCase();
2592+
if(isolationMethod.equals(vxlan)) {
2593+
return BroadcastDomainType.encodeStringIntoBroadcastUri(vlanId, BroadcastDomainType.Vxlan);
2594+
}
2595+
}
2596+
return BroadcastDomainType.fromString(vlanId);
2597+
}
2598+
25802599
/**
25812600
* Checks bypass VLAN id/range overlap check during network creation for guest networks
25822601
* @param bypassVlanOverlapCheck bypass VLAN id/range overlap check

engine/orchestration/src/test/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestratorTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import static org.mockito.Mockito.verify;
2323
import static org.mockito.Mockito.when;
2424

25+
import java.net.URI;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.HashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031

32+
import com.cloud.network.dao.PhysicalNetworkVO;
33+
import com.cloud.utils.exception.CloudRuntimeException;
3134
import org.apache.log4j.Logger;
3235
import org.junit.Assert;
3336
import org.junit.Before;
@@ -483,4 +486,74 @@ public void testDontReleaseNicWhenPreserveNicsSettingEnabled() {
483486
verify(testOrchastrator._nicDao, never()).remove(nicId);
484487
}
485488

489+
public void encodeVlanIdIntoBroadcastUriTestVxlan() {
490+
encodeVlanIdIntoBroadcastUriPrepareAndTest("123", "VXLAN", "vxlan", "vxlan://123");
491+
}
492+
493+
@Test
494+
public void encodeVlanIdIntoBroadcastUriTestVlan() {
495+
encodeVlanIdIntoBroadcastUriPrepareAndTest("123", "VLAN", "vlan", "vlan://123");
496+
}
497+
498+
@Test
499+
public void encodeVlanIdIntoBroadcastUriTestEmpty() {
500+
encodeVlanIdIntoBroadcastUriPrepareAndTest("123", "", "vlan", "vlan://123");
501+
}
502+
503+
@Test
504+
public void encodeVlanIdIntoBroadcastUriTestNull() {
505+
encodeVlanIdIntoBroadcastUriPrepareAndTest("123", null, "vlan", "vlan://123");
506+
}
507+
508+
@Test(expected = CloudRuntimeException.class)
509+
public void encodeVlanIdIntoBroadcastUriTestEmptyVlanId() {
510+
encodeVlanIdIntoBroadcastUriPrepareAndTest("", "vxlan", "vlan", "vlan://123");
511+
}
512+
513+
@Test(expected = CloudRuntimeException.class)
514+
public void encodeVlanIdIntoBroadcastUriTestNullVlanId() {
515+
encodeVlanIdIntoBroadcastUriPrepareAndTest(null, "vlan", "vlan", "vlan://123");
516+
}
517+
518+
@Test(expected = CloudRuntimeException.class)
519+
public void encodeVlanIdIntoBroadcastUriTestBlankVlanId() {
520+
encodeVlanIdIntoBroadcastUriPrepareAndTest(" ", "vlan", "vlan", "vlan://123");
521+
}
522+
523+
@Test
524+
public void encodeVlanIdIntoBroadcastUriTestNullVlanIdWithSchema() {
525+
encodeVlanIdIntoBroadcastUriPrepareAndTest("vlan://123", "vlan", "vlan", "vlan://123");
526+
}
527+
528+
@Test
529+
public void encodeVlanIdIntoBroadcastUriTestNullVlanIdWithSchemaIsolationVxlan() {
530+
encodeVlanIdIntoBroadcastUriPrepareAndTest("vlan://123", "vxlan", "vlan", "vlan://123");
531+
}
532+
533+
@Test
534+
public void encodeVlanIdIntoBroadcastUriTestNullVxlanIdWithSchema() {
535+
encodeVlanIdIntoBroadcastUriPrepareAndTest("vxlan://123", "vxlan", "vxlan", "vxlan://123");
536+
}
537+
538+
@Test
539+
public void encodeVlanIdIntoBroadcastUriTestNullVxlanIdWithSchemaIsolationVlan() {
540+
encodeVlanIdIntoBroadcastUriPrepareAndTest("vxlan://123", "vlan", "vxlan", "vxlan://123");
541+
}
542+
543+
@Test(expected = InvalidParameterValueException.class)
544+
public void encodeVlanIdIntoBroadcastUriTestNullNetwork() {
545+
URI resultUri = testOrchastrator.encodeVlanIdIntoBroadcastUri("vxlan://123", null);
546+
}
547+
548+
private void encodeVlanIdIntoBroadcastUriPrepareAndTest(String vlanId, String isolationMethod, String expectedIsolation, String expectedUri) {
549+
PhysicalNetworkVO physicalNetwork = new PhysicalNetworkVO();
550+
List<String> isolationMethods = new ArrayList<>();
551+
isolationMethods.add(isolationMethod);
552+
physicalNetwork.setIsolationMethods(isolationMethods);
553+
554+
URI resultUri = testOrchastrator.encodeVlanIdIntoBroadcastUri(vlanId, physicalNetwork);
555+
556+
Assert.assertEquals(expectedIsolation, resultUri.getScheme());
557+
Assert.assertEquals(expectedUri, resultUri.toString());
558+
}
486559
}

framework/agent-lb/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface IndirectAgentLB {
3737
* @param receivedMSHosts received management server list
3838
* @return true if mgmtHosts is up to date, false if not
3939
*/
40-
boolean compareManagementServerList(Long hostId, Long dcId, List<String> receivedMSHosts);
40+
boolean compareManagementServerList(Long hostId, Long dcId, List<String> receivedMSHosts, String lbAlgorithm);
4141

4242
/**
4343
* Returns the configure LB algorithm

plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@
318318
import com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo;
319319
import com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo;
320320
import com.vmware.vim25.VirtualIDEController;
321-
import com.vmware.vim25.VirtualMachineConfigSpec;
322321
import com.vmware.vim25.VirtualMachineBootOptions;
322+
import com.vmware.vim25.VirtualMachineConfigSpec;
323323
import com.vmware.vim25.VirtualMachineFileInfo;
324324
import com.vmware.vim25.VirtualMachineFileLayoutEx;
325325
import com.vmware.vim25.VirtualMachineFileLayoutExFileInfo;
@@ -7135,7 +7135,7 @@ private Answer execute(GetUnmanagedInstancesCommand cmd) {
71357135
VmwareHypervisorHost hyperHost = getHyperHost(context);
71367136

71377137
String vmName = cmd.getInstanceName();
7138-
List<VirtualMachineMO> vmMos = hyperHost.listVmsOnHyperHost(vmName);
7138+
List<VirtualMachineMO> vmMos = hyperHost.listVmsOnHyperHostWithHypervisorName(vmName);
71397139

71407140
for (VirtualMachineMO vmMo : vmMos) {
71417141
if (vmMo == null) {

server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ public List<String> getManagementServerList(final Long hostId, final Long dcId,
9898
}
9999

100100
@Override
101-
public boolean compareManagementServerList(final Long hostId, final Long dcId, final List<String> receivedMSHosts) {
101+
public boolean compareManagementServerList(final Long hostId, final Long dcId, final List<String> receivedMSHosts, final String lbAlgorithm) {
102102
if (receivedMSHosts == null || receivedMSHosts.size() < 1) {
103103
return false;
104104
}
105+
if (getLBAlgorithmName() != lbAlgorithm) {
106+
return false;
107+
}
105108
final List<String> expectedMSList = getManagementServerList(hostId, dcId, null);
106109
final org.apache.cloudstack.agent.lb.IndirectAgentLBAlgorithm algorithm = getAgentMSLBAlgorithm();
107110
return algorithm.compare(expectedMSList, receivedMSHosts);

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/ClusterMO.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424

25+
import org.apache.commons.collections.CollectionUtils;
2526
import org.apache.commons.lang.StringUtils;
2627
import org.apache.log4j.Logger;
2728

@@ -217,13 +218,13 @@ public ManagedObjectReference getHyperHostCluster() throws Exception {
217218
}
218219

219220
@Override
220-
public synchronized List<VirtualMachineMO> listVmsOnHyperHost(String vmName) throws Exception {
221+
public synchronized List<VirtualMachineMO> listVmsOnHyperHostWithHypervisorName(String vmName) throws Exception {
221222
List<VirtualMachineMO> vms = new ArrayList<>();
222223
List<ManagedObjectReference> hosts = _context.getVimClient().getDynamicProperty(_mor, "host");
223-
if (hosts != null && hosts.size() > 0) {
224+
if (CollectionUtils.isNotEmpty(hosts)) {
224225
for (ManagedObjectReference morHost : hosts) {
225226
HostMO hostMo = new HostMO(_context, morHost);
226-
vms.addAll(hostMo.listVmsOnHyperHost(vmName));
227+
vms.addAll(hostMo.listVmsOnHyperHostWithHypervisorName(vmName));
227228
}
228229
}
229230
return vms;

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/HostMO.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Collection;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.regex.Pattern;
2526

27+
import org.apache.commons.collections.CollectionUtils;
28+
import org.apache.commons.lang.StringUtils;
2629
import org.apache.log4j.Logger;
2730

2831
import com.google.gson.Gson;
@@ -497,10 +500,10 @@ public String getHostName() throws Exception {
497500
}
498501

499502
@Override
500-
public synchronized List<VirtualMachineMO> listVmsOnHyperHost(String vmName) throws Exception {
503+
public synchronized List<VirtualMachineMO> listVmsOnHyperHostWithHypervisorName(String vmName) throws Exception {
501504
List<VirtualMachineMO> vms = new ArrayList<>();
502-
if (vmName != null && !vmName.isEmpty()) {
503-
vms.add(findVmOnHyperHost(vmName));
505+
if (StringUtils.isNotEmpty(vmName)) {
506+
vms.add(findVmOnHyperHostWithHypervisorName(vmName));
504507
} else {
505508
loadVmCache();
506509
vms.addAll(_vmCache.values());
@@ -1209,4 +1212,36 @@ public boolean isUefiLegacySupported() throws Exception {
12091212
return false;
12101213
}
12111214

1215+
private synchronized VirtualMachineMO findVmOnHyperHostWithHypervisorName(String vmName) throws Exception {
1216+
if (s_logger.isDebugEnabled())
1217+
s_logger.debug("find VM hypervisor name: " + vmName + " on host");
1218+
1219+
VirtualMachineMO vmMo = getVmWithHypervisorName(_vmCache.values(), vmName);
1220+
if (vmMo != null) {
1221+
if (s_logger.isDebugEnabled())
1222+
s_logger.debug("VM hypervisor name: " + vmName + " found in host cache");
1223+
return vmMo;
1224+
}
1225+
1226+
s_logger.info("VM hypervisor name: " + vmName + " not found in host cache");
1227+
loadVmCache();
1228+
1229+
return getVmWithHypervisorName(_vmCache.values(), vmName);
1230+
}
1231+
1232+
private VirtualMachineMO getVmWithHypervisorName(Collection<VirtualMachineMO> vmList, String vmName) {
1233+
if (CollectionUtils.isNotEmpty(vmList)) {
1234+
for (VirtualMachineMO vm : vmList) {
1235+
try {
1236+
if (StringUtils.isNotEmpty(vm.getVmName()) && vm.getVmName().equals(vmName)) {
1237+
return vm;
1238+
}
1239+
} catch (Exception e) {
1240+
s_logger.debug("Failed to get VM name, ignoring exception", e);
1241+
}
1242+
}
1243+
}
1244+
return null;
1245+
}
1246+
12121247
}

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VmwareHypervisorHost.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface VmwareHypervisorHost {
5353

5454
String getHyperHostDefaultGateway() throws Exception;
5555

56-
List<VirtualMachineMO> listVmsOnHyperHost(String name) throws Exception;
56+
List<VirtualMachineMO> listVmsOnHyperHostWithHypervisorName(String name) throws Exception;
5757

5858
VirtualMachineMO findVmOnHyperHost(String name) throws Exception;
5959

0 commit comments

Comments
 (0)