Skip to content

Commit c9855aa

Browse files
committed
fixup
1 parent 8888b27 commit c9855aa

File tree

6 files changed

+108
-104
lines changed

6 files changed

+108
-104
lines changed

engine/schema/src/main/java/com/cloud/network/dao/NetworkDao.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public interface NetworkDao extends GenericDao<NetworkVO, Long>, StateDao<State,
4747

4848
int getOtherPersistentNetworksCount(long id, String broadcastURI, boolean isPersistent);
4949

50+
List<NetworkVO> listByNetworkDomains(List<String> uniqueNtwkDomains);
51+
52+
List<NetworkVO> listByNetworkDomainsAndAccountIds(List<String> uniqueNtwkDomains, List<Long> accountIds);
53+
54+
List<NetworkVO> listByNetworkDomainsAndDomainIds(List<String> uniqueNtwkDomains, List<Long> domainIds);
55+
5056
/**
5157
* Retrieves the next available mac address in this network configuration.
5258
*

engine/schema/src/main/java/com/cloud/network/dao/NetworkDaoImpl.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long>implements Ne
8686

8787
GenericSearchBuilder<NetworkVO, Long> GarbageCollectedSearch;
8888
SearchBuilder<NetworkVO> PrivateNetworkSearch;
89+
SearchBuilder<NetworkVO> NetworkDomainSearch;
8990

9091
@Inject
9192
ResourceTagDao _tagsDao;
@@ -198,6 +199,12 @@ protected void init() {
198199
PersistentNetworkSearch.join("persistent", persistentNtwkOffJoin, PersistentNetworkSearch.entity().getNetworkOfferingId(), persistentNtwkOffJoin.entity().getId(), JoinType.INNER);
199200
PersistentNetworkSearch.done();
200201

202+
NetworkDomainSearch = createSearchBuilder();
203+
NetworkDomainSearch.and("networkDomains", NetworkDomainSearch.entity().getNetworkDomain(), Op.IN);
204+
NetworkDomainSearch.and("accounts", NetworkDomainSearch.entity().getAccountId(), Op.IN);
205+
NetworkDomainSearch.and("domains", NetworkDomainSearch.entity().getDomainId(), Op.IN);
206+
NetworkDomainSearch.done();
207+
201208
PhysicalNetworkSearch = createSearchBuilder();
202209
PhysicalNetworkSearch.and("physicalNetworkId", PhysicalNetworkSearch.entity().getPhysicalNetworkId(), Op.EQ);
203210
PhysicalNetworkSearch.done();
@@ -429,6 +436,34 @@ public List<NetworkVO> getAllPersistentNetworksFromZone(long dataCenterId) {
429436
return search(sc, null);
430437
}
431438

439+
@Override
440+
public List<NetworkVO> listByNetworkDomains(List<String> uniqueNtwkDomains) {
441+
SearchBuilder<NetworkVO> NetworkDomainSearch = createSearchBuilder();
442+
NetworkDomainSearch.and("networkDomains", NetworkDomainSearch.entity().getNetworkDomain(), Op.IN);
443+
NetworkDomainSearch.and("accounts", NetworkDomainSearch.entity().getAccountId(), Op.IN);
444+
NetworkDomainSearch.and("domains", NetworkDomainSearch.entity().getDomainId(), Op.IN);
445+
NetworkDomainSearch.done();
446+
SearchCriteria<NetworkVO> sc = NetworkDomainSearch.create();
447+
sc.setParameters("networkDomains", uniqueNtwkDomains.toArray());
448+
return search(sc, null);
449+
}
450+
451+
@Override
452+
public List<NetworkVO> listByNetworkDomainsAndAccountIds(List<String> uniqueNtwkDomains, List<Long> accountIds) {
453+
SearchCriteria<NetworkVO> sc = NetworkDomainSearch.create();
454+
sc.setParameters("networkDomains", uniqueNtwkDomains.toArray());
455+
sc.setParameters("accounts", accountIds.toArray());
456+
return search(sc, null);
457+
}
458+
459+
@Override
460+
public List<NetworkVO> listByNetworkDomainsAndDomainIds(List<String> uniqueNtwkDomains, List<Long> domainIds) {
461+
SearchCriteria<NetworkVO> sc = NetworkDomainSearch.create();
462+
sc.setParameters("networkDomains", uniqueNtwkDomains.toArray());
463+
sc.setParameters("domains", domainIds.toArray());
464+
return search(sc, null);
465+
}
466+
432467
@Override
433468
public String getNextAvailableMacAddress(final long networkConfigId, Integer zoneMacIdentifier) {
434469
final SequenceFetcher fetch = SequenceFetcher.getInstance();

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<
145145
*/
146146
List<String> listDistinctHostNames(long networkId, VirtualMachine.Type... types);
147147

148-
boolean hostNameExistsInDomainIds(String hostName, Set<Long> domainIdList);
149-
150-
boolean hostNameExistsInDomainIdsAccountIds(String hostName, Set<Long> accountIdList);
151-
152148
List<VMInstanceVO> findByHostInStates(Long hostId, State... states);
153149

154150
List<VMInstanceVO> listStartingWithNoHostId();

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,6 @@ protected void init() {
296296
DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName());
297297

298298
DistinctHostNameSearch.and("types", DistinctHostNameSearch.entity().getType(), SearchCriteria.Op.IN);
299-
DistinctHostNameSearch.and("accounts", DistinctHostNameSearch.entity().getAccountId(), SearchCriteria.Op.IN);
300-
DistinctHostNameSearch.and("domains", DistinctHostNameSearch.entity().getDomainId(), SearchCriteria.Op.IN);
301299
DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
302300
DistinctHostNameSearch.join("nicSearch", nicSearch, DistinctHostNameSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
303301
DistinctHostNameSearch.done();
@@ -889,20 +887,6 @@ public List<String> listDistinctHostNames(long networkId, VirtualMachine.Type...
889887
return customSearch(sc, null);
890888
}
891889

892-
public boolean hostNameExistsInDomainIds(String hostName, Set<Long> domainIdList) {
893-
SearchCriteria<String> sc = DistinctHostNameSearch.create();
894-
sc.setParameters("domains", domainIdList.toArray());
895-
896-
return CollectionUtils.isNotEmpty(customSearch(sc, null));
897-
}
898-
899-
public boolean hostNameExistsInDomainIdsAccountIds(String hostName, Set<Long> accountIdList) {
900-
SearchCriteria<String> sc = DistinctHostNameSearch.create();
901-
sc.setParameters("accounts", accountIdList.toArray());
902-
903-
return CollectionUtils.isNotEmpty(customSearch(sc, null));
904-
}
905-
906890
@Override
907891
@DB
908892
public boolean remove(Long id) {

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 62 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import javax.xml.parsers.ParserConfigurationException;
5454

5555
import com.cloud.network.vpc.Vpc;
56+
import com.cloud.user.OwnedBy;
5657
import org.apache.cloudstack.acl.ControlledEntity;
5758
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
5859
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -3095,7 +3096,7 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
30953096
for (Nic nic : nics) {
30963097
vmNtwks.add(_networkDao.findById(nic.getNetworkId()));
30973098
}
3098-
checkIfHostNameUniqueInNtwkDomain(hostName, vmNtwks);
3099+
checkIfHostNameIsUnique(hostName, vmNtwks);
30993100
}
31003101

31013102
List<NetworkVO> networks = nics.stream()
@@ -4317,7 +4318,7 @@ private UserVm getUncheckedUserVmResource(DataCenter zone, String hostName, Stri
43174318
throw new InvalidParameterValueException("There already exists a VM by the display name supplied");
43184319
}
43194320

4320-
checkIfHostNameUniqueInNtwkDomain(hostName, networkList);
4321+
checkIfHostNameIsUnique(hostName, networkList);
43214322

43224323
long userId = CallContext.current().getCallingUserId();
43234324
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
@@ -4430,106 +4431,83 @@ protected void verifyIfHypervisorSupportsRootdiskSizeOverride(HypervisorType hyp
44304431
}
44314432
}
44324433

4433-
private Map<String, Set<Long>> getNetworkIdPerNetworkDomain(List<? extends Network> networkList){
4434-
Map<String, Set<Long>> ntwkDomains = new HashMap<>();
4435-
Set<Long> vpcIds = new HashSet<>();
4434+
private List<NetworkVO> getNetworksWithSameNetworkDomainInDomains(List<NetworkVO> networkList, boolean checkSubDomains) {
4435+
List<String> uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
4436+
List<Long> domainIdList = new ArrayList<>();
44364437
for (Network network : networkList) {
4437-
String ntwkDomain = network.getNetworkDomain();
4438-
if (network.getVpcId() != null) {
4439-
vpcIds.add(network.getVpcId());
4440-
}
4441-
Set<Long> ntwkIds;
4442-
if (!ntwkDomains.containsKey(ntwkDomain)) {
4443-
ntwkIds = new HashSet<>();
4444-
} else {
4445-
ntwkIds = ntwkDomains.get(ntwkDomain);
4446-
}
4447-
ntwkIds.add(network.getId());
4448-
ntwkDomains.put(ntwkDomain, ntwkIds);
4438+
domainIdList.add(network.getDomainId());
44494439
}
4450-
4451-
for (Long vpcId : vpcIds) {
4452-
Vpc vpc = _vpcMgr.getActiveVpc(vpcId);
4453-
List<NetworkVO> networks = _networkDao.listByVpc(vpcId);
4454-
String ntwkDomain = vpc.getNetworkDomain();
4455-
4456-
Set<Long> ntwkIds;
4457-
if (!ntwkDomains.containsKey(ntwkDomain)) {
4458-
ntwkIds = new HashSet<>();
4459-
for (NetworkVO network : networks) {
4460-
ntwkIds.add(network.getId());
4461-
}
4462-
} else {
4463-
ntwkIds = ntwkDomains.get(ntwkDomain);
4464-
for (NetworkVO network : networks) {
4465-
ntwkIds.add(network.getId());
4466-
}
4440+
Set<Long> finalDomainIdList = new HashSet<>(domainIdList);
4441+
if (checkSubDomains) {
4442+
for (Long domainId : domainIdList) {
4443+
DomainVO domain = _domainDao.findById(domainId);
4444+
List<Long> childDomainIds = _domainDao.getDomainChildrenIds(domain.getPath());
4445+
finalDomainIdList.addAll(childDomainIds);
44674446
}
4468-
ntwkDomains.put(ntwkDomain, ntwkIds);
44694447
}
4470-
return ntwkDomains;
4448+
return _networkDao.listByNetworkDomainsAndDomainIds(uniqueNtwkDomains, finalDomainIdList.stream().collect(Collectors.toList()));
44714449
}
44724450

4473-
private void checkIfHostNameUniqueInNtwkDomain(String hostName, List<? extends Network> networkList) {
4474-
// Check that hostName is unique in the network domain
4475-
Set<Long> domainIdList = new HashSet<>();
4451+
private List<NetworkVO> getNetworksForCheckUniqueHostName(List<NetworkVO> networkList) {
4452+
List<String> uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
4453+
List<NetworkVO> finalNetworkList;
44764454
switch (VmDistinctHostNameScope.value()) {
44774455
case "global":
4478-
// Check that hostName is unique in the zone
4479-
VMInstanceVO vm = _vmInstanceDao.findVMByHostName(hostName);
4480-
if (vm != null) {
4481-
throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the zone");
4482-
}
4456+
finalNetworkList = _networkDao.listByNetworkDomains(uniqueNtwkDomains);
4457+
break;
44834458
case "domain":
4484-
// Check that hostName is unique in the domain
4485-
for (Network network : networkList) {
4486-
domainIdList.add(network.getDomainId());
4487-
}
4488-
if (_vmInstanceDao.hostNameExistsInDomainIds(hostName, domainIdList)) {
4489-
throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the domain");
4490-
}
4459+
finalNetworkList = getNetworksWithSameNetworkDomainInDomains(networkList, false);
44914460
break;
44924461
case "subdomain":
4493-
for (Network network : networkList) {
4494-
domainIdList.add(network.getDomainId());
4495-
}
4496-
Set<Long> finalDomainIdList = new HashSet<>();
4497-
for (Long domainId : domainIdList) {
4498-
finalDomainIdList.add(domainId);
4499-
DomainVO domain = _domainDao.findById(domainId);
4500-
List<Long> childDomainIds = _domainDao.getDomainChildrenIds(domain.getPath());
4501-
finalDomainIdList.addAll(childDomainIds);
4502-
}
4503-
4504-
if (_vmInstanceDao.hostNameExistsInDomainIds(hostName, finalDomainIdList)) {
4505-
throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the domain or subdomain");
4506-
}
4462+
finalNetworkList = getNetworksWithSameNetworkDomainInDomains(networkList, true);
45074463
break;
45084464
case "account":
4509-
// Check that hostName is unique in the account
4510-
Set<Long> accountIdList = new HashSet<>();
4511-
for (Network network : networkList) {
4512-
accountIdList.add(network.getAccountId());
4513-
}
4514-
if (_vmInstanceDao.hostNameExistsInDomainIdsAccountIds(hostName, accountIdList)) {
4515-
throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the account");
4516-
}
4465+
List<Long> accountIds = networkList.stream().map(Network::getAccountId).collect(Collectors.toList());
4466+
finalNetworkList = _networkDao.listByNetworkDomainsAndAccountIds(uniqueNtwkDomains, accountIds);
45174467
break;
45184468
default:
4519-
Map<String, Set<Long>> ntwkDomains = getNetworkIdPerNetworkDomain(networkList);
4520-
for (Entry<String, Set<Long>> ntwkDomain : ntwkDomains.entrySet()) {
4521-
for (Long ntwkId : ntwkDomain.getValue()) {
4522-
// * get all vms hostNames in the network
4523-
List<String> hostNames = _vmInstanceDao.listDistinctHostNames(ntwkId);
4524-
// * verify that there are no duplicates
4525-
if (hostNames.contains(hostName)) {
4526-
throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the network domain: " + ntwkDomain.getKey() + "; network="
4527-
+ ((_networkModel.getNetwork(ntwkId) != null) ? _networkModel.getNetwork(ntwkId).getName() : "<unknown>"));
4528-
}
4529-
}
4469+
Set<Long> vpcIds = networkList.stream().map(Network::getVpcId).filter(Objects::nonNull).collect(Collectors.toSet());
4470+
finalNetworkList = new ArrayList<>(networkList);
4471+
for (Long vpcId : vpcIds) {
4472+
finalNetworkList.addAll(_networkDao.listByVpc(vpcId));
45304473
}
4474+
break;
45314475
}
4476+
return finalNetworkList;
4477+
}
4478+
4479+
private Map<String, Set<Long>> getNetworkIdPerNetworkDomain(List<NetworkVO> networkList) {
4480+
Map<String, Set<Long>> ntwkDomains = new HashMap<>();
45324481

4482+
List<NetworkVO> updatedNetworkList = getNetworksForCheckUniqueHostName(networkList);
4483+
for (Network network : updatedNetworkList) {
4484+
String ntwkDomain = network.getNetworkDomain();
4485+
Set<Long> ntwkIds;
4486+
if (!ntwkDomains.containsKey(ntwkDomain)) {
4487+
ntwkIds = new HashSet<>();
4488+
} else {
4489+
ntwkIds = ntwkDomains.get(ntwkDomain);
4490+
}
4491+
ntwkIds.add(network.getId());
4492+
ntwkDomains.put(ntwkDomain, ntwkIds);
4493+
}
4494+
return ntwkDomains;
4495+
}
4496+
4497+
private void checkIfHostNameIsUnique(String hostName, List<NetworkVO> networkList) {
4498+
// Check that hostName is unique
4499+
Map<String, Set<Long>> ntwkDomains = getNetworkIdPerNetworkDomain(networkList);
4500+
for (Entry<String, Set<Long>> ntwkDomain : ntwkDomains.entrySet()) {
4501+
for (Long ntwkId : ntwkDomain.getValue()) {
4502+
// * get all vms hostNames in the network
4503+
List<String> hostNames = _vmInstanceDao.listDistinctHostNames(ntwkId);
4504+
// * verify that there are no duplicates
4505+
if (hostNames.contains(hostName)) {
4506+
throw new InvalidParameterValueException("The vm with hostName " + hostName + " already exists in the network domain: " + ntwkDomain.getKey() + "; network="
4507+
+ ((_networkModel.getNetwork(ntwkId) != null) ? _networkModel.getNetwork(ntwkId).getName() : "<unknown>"));
4508+
}
4509+
}
4510+
}
45334511
}
45344512

45354513
private String generateHostName(String uuidName) {

server/src/test/java/com/cloud/vpc/dao/MockNetworkDaoImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,9 @@ public List<NetworkVO> listByPhysicalNetworkPvlan(long physicalNetworkId, String
264264
public List<NetworkVO> getAllPersistentNetworksFromZone(long dataCenterId) {
265265
return null;
266266
}
267+
268+
@Override
269+
public List<NetworkVO> listByNetworkDomains(List<String> uniqueNtwkDomains) {
270+
return List.of();
271+
}
267272
}

0 commit comments

Comments
 (0)