Skip to content

Commit e86f28b

Browse files
author
Henrique Sato
committed
Remove unused import & improve VR NIC validation
1 parent b9c4142 commit e86f28b

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ protected void init() {
107107
PeerRouterSearch.and("macAddress", PeerRouterSearch.entity().getMacAddress(), Op.EQ);
108108
PeerRouterSearch.and("vmType", PeerRouterSearch.entity().getVmType(), Op.EQ);
109109
PeerRouterSearch.done();
110+
111+
110112
}
111113

112114
@Override

server/src/main/java/com/cloud/api/query/dao/DomainRouterJoinDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public interface DomainRouterJoinDao extends GenericDao<DomainRouterJoinVO, Long
3737
List<DomainRouterJoinVO> searchByIds(Long... ids);
3838

3939
List<DomainRouterJoinVO> getRouterByIdAndTrafficType(Long id, Networks.TrafficType... trafficType);
40+
41+
int countDefaultNetworksById(long routerId);
4042
}

server/src/main/java/com/cloud/api/query/dao/DomainRouterJoinDaoImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import javax.inject.Inject;
2323

24+
import com.cloud.utils.db.GenericSearchBuilder;
2425
import org.apache.cloudstack.annotation.AnnotationService;
2526
import org.apache.cloudstack.annotation.dao.AnnotationDao;
2627
import org.apache.cloudstack.context.CallContext;
@@ -59,9 +60,9 @@ public class DomainRouterJoinDaoImpl extends GenericDaoBase<DomainRouterJoinVO,
5960
private AnnotationDao annotationDao;
6061

6162
private final SearchBuilder<DomainRouterJoinVO> vrSearch;
62-
6363
private final SearchBuilder<DomainRouterJoinVO> vrIdSearch;
6464
private final SearchBuilder<DomainRouterJoinVO> vrIdTrafficSearch;
65+
private final GenericSearchBuilder<DomainRouterJoinVO, Integer> defaultNetworksSearch;
6566

6667
protected DomainRouterJoinDaoImpl() {
6768

@@ -78,6 +79,13 @@ protected DomainRouterJoinDaoImpl() {
7879
vrIdTrafficSearch.and("trafficType", vrIdTrafficSearch.entity().getTrafficType(), SearchCriteria.Op.IN);
7980
vrIdTrafficSearch.done();
8081

82+
defaultNetworksSearch = createSearchBuilder(Integer.class);
83+
defaultNetworksSearch.select(null, SearchCriteria.Func.COUNT, defaultNetworksSearch.entity().getId());
84+
defaultNetworksSearch.and("id", defaultNetworksSearch.entity().getId(), SearchCriteria.Op.EQ);
85+
defaultNetworksSearch.and("network_name", defaultNetworksSearch.entity().getNetworkName(), SearchCriteria.Op.NULL);
86+
defaultNetworksSearch.and("removed", defaultNetworksSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
87+
defaultNetworksSearch.done();
88+
8189
_count = "select count(distinct id) from domain_router_view WHERE ";
8290
}
8391

@@ -349,6 +357,14 @@ public List<DomainRouterJoinVO> getRouterByIdAndTrafficType(Long id, TrafficType
349357
return searchIncludingRemoved(sc, null, null, false);
350358
}
351359

360+
@Override
361+
public int countDefaultNetworksById(long routerId) {
362+
SearchCriteria<Integer> sc = defaultNetworksSearch.create();
363+
sc.setParameters("id", routerId);
364+
final List<Integer> count = customSearch(sc, null);
365+
return count.get(0);
366+
}
367+
352368
@Override
353369
public List<DomainRouterJoinVO> newDomainRouterView(VirtualRouter vr) {
354370

server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import javax.inject.Inject;
4242
import javax.naming.ConfigurationException;
4343

44+
import com.cloud.api.query.dao.DomainRouterJoinDao;
4445
import com.cloud.configuration.ConfigurationManager;
4546
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
4647
import org.apache.cloudstack.alert.AlertService;
@@ -270,6 +271,8 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis
270271
protected NetworkHelper networkHelper;
271272
@Inject
272273
private VpcPrivateGatewayTransactionCallable vpcTxCallable;
274+
@Inject
275+
protected DomainRouterJoinDao domainRouterJoinDao;
273276

274277
private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("VpcChecker"));
275278
private List<VpcProvider> vpcElements = null;
@@ -1937,9 +1940,8 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
19371940

19381941
try {
19391942
Account account = _accountMgr.getAccount(vpc.getAccountId());
1940-
int maxNetworks = VpcMaxNetworks.valueIn(vpc.getAccountId());
19411943

1942-
checkIfVpcNumberOfTiersIsNotExceeded(vpcId, maxNetworks, account);
1944+
checkIfVpcNumberOfTiersIsNotExceeded(vpcId, account);
19431945

19441946
checkIfVpcHasDomainRouterWithSufficientNicCapacity(vpc);
19451947

@@ -1965,20 +1967,23 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
19651967
}
19661968

19671969
protected boolean existsVpcDomainRouterWithSufficientNicCapacity(long vpcId) {
1968-
int countRouterDefaultNics = 2;
1969-
long countVpcNetworks = _ntwkDao.countVpcNetworks(vpcId);
19701970
DomainRouterVO vpcDomainRouter = routerDao.findOneByVpcId(vpcId);
19711971

19721972
if (vpcDomainRouter == null) {
19731973
return false;
19741974
}
19751975

1976-
int totalNicsAvailable = networkOrchestrationService.getVirtualMachineMaxNicsValue(vpcDomainRouter) - countRouterDefaultNics;
1976+
int countRouterDefaultNetworks = domainRouterJoinDao.countDefaultNetworksById(vpcDomainRouter.getId());
1977+
long countVpcNetworks = _ntwkDao.countVpcNetworks(vpcId);
1978+
1979+
int totalNicsAvailable = networkOrchestrationService.getVirtualMachineMaxNicsValue(vpcDomainRouter) - countRouterDefaultNetworks;
19771980

19781981
return totalNicsAvailable > countVpcNetworks;
19791982
}
19801983

1981-
protected void checkIfVpcNumberOfTiersIsNotExceeded(long vpcId, int maxNetworks, Account account) {
1984+
protected void checkIfVpcNumberOfTiersIsNotExceeded(long vpcId, Account account) {
1985+
int maxNetworks = VpcMaxNetworks.valueIn(account.getId());
1986+
19821987
if (_ntwkDao.countVpcNetworks(vpcId) >= maxNetworks) {
19831988
logger.warn("Failed to create a new VPC Guest Network because the number of networks per VPC has reached its maximum capacity of {}. Increase it by modifying global or account config {}.", maxNetworks, VpcMaxNetworks);
19841989
throw new CloudRuntimeException(String.format("Number of networks per VPC cannot surpass [%s] for account [%s].", maxNetworks, account.getAccountName()));
@@ -1994,7 +1999,7 @@ protected void checkIfVpcHasDomainRouterWithSufficientNicCapacity(Vpc vpc) {
19941999

19952000
protected void checkIfNetworkCidrIsWithinVpcCidr(String cidr, Vpc vpc) {
19962001
if (!NetUtils.isNetworkAWithinNetworkB(cidr, vpc.getCidr())) {
1997-
throw new InvalidParameterValueException("Network cidr " + cidr + " is not within vpc " + vpc + " cidr");
2002+
throw new InvalidParameterValueException(String.format("Network CIDR %s is not within VPC %s CIDR", cidr, vpc));
19982003
}
19992004
}
20002005

@@ -2003,7 +2008,7 @@ protected void checkIfNetworkCidrNotCrossesOtherVpcNetworksCidr(String cidr, Vpc
20032008

20042009
for (final Network network : networks) {
20052010
if (NetUtils.isNetworkAWithinNetworkB(network.getCidr(), cidr) || NetUtils.isNetworkAWithinNetworkB(cidr, network.getCidr())) {
2006-
throw new InvalidParameterValueException("Network cidr " + cidr + " crosses other network cidr " + network + " belonging to the same vpc " + vpc);
2011+
throw new InvalidParameterValueException(String.format("Network CIDR %s crosses other network CIDR %s belonging to the same VPC %s.", cidr, network, vpc));
20072012
}
20082013
}
20092014
}

server/src/test/java/com/cloud/network/vpc/VpcManagerImplTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.cloud.agent.api.to.IpAddressTO;
2424
import com.cloud.agent.manager.Commands;
2525
import com.cloud.alert.AlertManager;
26+
import com.cloud.api.query.dao.DomainRouterJoinDao;
2627
import com.cloud.configuration.Resource;
2728
import com.cloud.dc.DataCenterVO;
2829
import com.cloud.dc.VlanVO;
@@ -86,7 +87,6 @@
8687
import org.mockito.junit.MockitoJUnitRunner;
8788
import org.springframework.test.util.ReflectionTestUtils;
8889

89-
import javax.management.InvalidApplicationException;
9090
import java.lang.reflect.Field;
9191
import java.util.ArrayList;
9292
import java.util.HashMap;
@@ -165,6 +165,8 @@ public class VpcManagerImplTest {
165165
DomainRouterVO domainRouterVOMock;
166166
@Mock
167167
Vpc vpcMock;
168+
@Mock
169+
DomainRouterJoinDao domainRouterJoinDaoMock;
168170

169171
public static final long ACCOUNT_ID = 1;
170172
private AccountVO account;
@@ -222,6 +224,7 @@ public void setup() throws NoSuchFieldException, IllegalAccessException {
222224
manager._firewallDao = firewallDao;
223225
manager._networkAclDao = networkACLDaoMock;
224226
manager.networkOrchestrationService = networkOrchestrationServiceMock;
227+
manager.domainRouterJoinDao = domainRouterJoinDaoMock;
225228
CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
226229
registerCallContext();
227230
overrideDefaultConfigValue(NetworkService.AllowUsersToSpecifyVRMtu, "_defaultValue", "false");
@@ -524,6 +527,8 @@ public void validateVpcPrivateGatewayTestAclFromDifferentVpcThrowsInvalidParamet
524527
public void existsVpcDomainRouterWithSufficientNicCapacityTestUnavailableRoutersReturnsFalse() {
525528
Mockito.when(networkDao.countVpcNetworks(vpcId)).thenReturn(7L);
526529
Mockito.when(routerDao.findOneByVpcId(vpcId)).thenReturn(domainRouterVOMock);
530+
Mockito.when(domainRouterVOMock.getId()).thenReturn(1L);
531+
Mockito.when(domainRouterJoinDaoMock.countDefaultNetworksById(1L)).thenReturn(2);
527532
Mockito.when(networkOrchestrationServiceMock.getVirtualMachineMaxNicsValue(domainRouterVOMock)).thenReturn(9);
528533

529534
boolean result = manager.existsVpcDomainRouterWithSufficientNicCapacity(vpcId);
@@ -535,6 +540,8 @@ public void existsVpcDomainRouterWithSufficientNicCapacityTestUnavailableRouters
535540
public void existsVpcDomainRouterWithSufficientNicCapacityTestAvailableRouterReturnsTrue() {
536541
Mockito.when(networkDao.countVpcNetworks(vpcId)).thenReturn(6L);
537542
Mockito.when(routerDao.findOneByVpcId(vpcId)).thenReturn(domainRouterVOMock);
543+
Mockito.when(domainRouterVOMock.getId()).thenReturn(1L);
544+
Mockito.when(domainRouterJoinDaoMock.countDefaultNetworksById(1L)).thenReturn(2);
538545
Mockito.when(networkOrchestrationServiceMock.getVirtualMachineMaxNicsValue(domainRouterVOMock)).thenReturn(9);
539546

540547
boolean result = manager.existsVpcDomainRouterWithSufficientNicCapacity(vpcId);
@@ -544,7 +551,6 @@ public void existsVpcDomainRouterWithSufficientNicCapacityTestAvailableRouterRet
544551

545552
@Test
546553
public void existsVpcDomainRouterWithSufficientNicCapacityTestNullRouterReturnsFalse() {
547-
Mockito.when(networkDao.countVpcNetworks(vpcId)).thenReturn(6L);
548554
Mockito.when(routerDao.findOneByVpcId(vpcId)).thenReturn(null);
549555

550556
boolean result = manager.existsVpcDomainRouterWithSufficientNicCapacity(vpcId);

0 commit comments

Comments
 (0)