Skip to content

Commit 5aa2b60

Browse files
committed
fixup
1 parent 8888b27 commit 5aa2b60

File tree

6 files changed

+110
-106
lines changed

6 files changed

+110
-106
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: 30 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,29 @@ public List<NetworkVO> getAllPersistentNetworksFromZone(long dataCenterId) {
429436
return search(sc, null);
430437
}
431438

439+
@Override
440+
public List<NetworkVO> listByNetworkDomains(List<String> uniqueNtwkDomains) {
441+
SearchCriteria<NetworkVO> sc = NetworkDomainSearch.create();
442+
sc.setParameters("networkDomains", uniqueNtwkDomains.toArray());
443+
return search(sc, null);
444+
}
445+
446+
@Override
447+
public List<NetworkVO> listByNetworkDomainsAndAccountIds(List<String> uniqueNtwkDomains, List<Long> accountIds) {
448+
SearchCriteria<NetworkVO> sc = NetworkDomainSearch.create();
449+
sc.setParameters("networkDomains", uniqueNtwkDomains.toArray());
450+
sc.setParameters("accounts", accountIds.toArray());
451+
return search(sc, null);
452+
}
453+
454+
@Override
455+
public List<NetworkVO> listByNetworkDomainsAndDomainIds(List<String> uniqueNtwkDomains, List<Long> domainIds) {
456+
SearchCriteria<NetworkVO> sc = NetworkDomainSearch.create();
457+
sc.setParameters("networkDomains", uniqueNtwkDomains.toArray());
458+
sc.setParameters("domains", domainIds.toArray());
459+
return search(sc, null);
460+
}
461+
432462
@Override
433463
public String getNextAvailableMacAddress(final long networkConfigId, Integer zoneMacIdentifier) {
434464
final SequenceFetcher fetch = SequenceFetcher.getInstance();

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23-
import java.util.Set;
2423

2524
import com.cloud.hypervisor.Hypervisor;
2625
import com.cloud.utils.Pair;
@@ -145,10 +144,6 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<
145144
*/
146145
List<String> listDistinctHostNames(long networkId, VirtualMachine.Type... types);
147146

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

154149
List<VMInstanceVO> listStartingWithNoHostId();

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
import java.util.HashMap;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.Set;
2827
import java.util.stream.Collectors;
2928

3029
import javax.annotation.PostConstruct;
3130
import javax.inject.Inject;
3231

33-
import org.apache.commons.collections.CollectionUtils;
3432
import org.apache.log4j.Logger;
3533
import org.springframework.stereotype.Component;
3634

@@ -296,8 +294,6 @@ protected void init() {
296294
DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName());
297295

298296
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);
301297
DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
302298
DistinctHostNameSearch.join("nicSearch", nicSearch, DistinctHostNameSearch.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
303299
DistinctHostNameSearch.done();
@@ -889,20 +885,6 @@ public List<String> listDistinctHostNames(long networkId, VirtualMachine.Type...
889885
return customSearch(sc, null);
890886
}
891887

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-
906888
@Override
907889
@DB
908890
public boolean remove(Long id) {

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

Lines changed: 59 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import javax.xml.parsers.DocumentBuilder;
5353
import javax.xml.parsers.ParserConfigurationException;
5454

55-
import com.cloud.network.vpc.Vpc;
5655
import org.apache.cloudstack.acl.ControlledEntity;
5756
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
5857
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -4430,106 +4429,83 @@ protected void verifyIfHypervisorSupportsRootdiskSizeOverride(HypervisorType hyp
44304429
}
44314430
}
44324431

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<>();
4432+
private List<NetworkVO> getNetworksWithSameNetworkDomainInDomains(List<NetworkVO> networkList, boolean checkSubDomains) {
4433+
List<String> uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
4434+
List<Long> domainIdList = new ArrayList<>();
44364435
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);
4436+
domainIdList.add(network.getDomainId());
44494437
}
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-
}
4438+
Set<Long> finalDomainIdList = new HashSet<>(domainIdList);
4439+
if (checkSubDomains) {
4440+
for (Long domainId : domainIdList) {
4441+
DomainVO domain = _domainDao.findById(domainId);
4442+
List<Long> childDomainIds = _domainDao.getDomainChildrenIds(domain.getPath());
4443+
finalDomainIdList.addAll(childDomainIds);
44674444
}
4468-
ntwkDomains.put(ntwkDomain, ntwkIds);
44694445
}
4470-
return ntwkDomains;
4446+
return _networkDao.listByNetworkDomainsAndDomainIds(uniqueNtwkDomains, finalDomainIdList.stream().collect(Collectors.toList()));
44714447
}
44724448

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<>();
4449+
private List<NetworkVO> getNetworksForCheckUniqueHostName(List<NetworkVO> networkList) {
4450+
List<String> uniqueNtwkDomains = networkList.stream().map(NetworkVO::getNetworkDomain).collect(Collectors.toList());
4451+
List<NetworkVO> finalNetworkList;
44764452
switch (VmDistinctHostNameScope.value()) {
44774453
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-
}
4454+
finalNetworkList = _networkDao.listByNetworkDomains(uniqueNtwkDomains);
4455+
break;
44834456
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-
}
4457+
finalNetworkList = getNetworksWithSameNetworkDomainInDomains(networkList, false);
44914458
break;
44924459
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-
}
4460+
finalNetworkList = getNetworksWithSameNetworkDomainInDomains(networkList, true);
45074461
break;
45084462
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-
}
4463+
List<Long> accountIds = networkList.stream().map(Network::getAccountId).collect(Collectors.toList());
4464+
finalNetworkList = _networkDao.listByNetworkDomainsAndAccountIds(uniqueNtwkDomains, accountIds);
45174465
break;
45184466
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-
}
4467+
Set<Long> vpcIds = networkList.stream().map(Network::getVpcId).filter(Objects::nonNull).collect(Collectors.toSet());
4468+
finalNetworkList = new ArrayList<>(networkList);
4469+
for (Long vpcId : vpcIds) {
4470+
finalNetworkList.addAll(_networkDao.listByVpc(vpcId));
45304471
}
4472+
break;
45314473
}
4474+
return finalNetworkList;
4475+
}
4476+
4477+
private Map<String, Set<Long>> getNetworkIdPerNetworkDomain(List<NetworkVO> networkList) {
4478+
Map<String, Set<Long>> ntwkDomains = new HashMap<>();
45324479

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

45354511
private String generateHostName(String uuidName) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,19 @@ 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+
}
272+
273+
@Override
274+
public List<NetworkVO> listByNetworkDomainsAndAccountIds(List<String> uniqueNtwkDomains, List<Long> accountIds) {
275+
return List.of();
276+
}
277+
278+
@Override
279+
public List<NetworkVO> listByNetworkDomainsAndDomainIds(List<String> uniqueNtwkDomains, List<Long> domainIds) {
280+
return List.of();
281+
}
267282
}

0 commit comments

Comments
 (0)