Skip to content

Commit 405b996

Browse files
Henrique Satohsato03
authored andcommitted
Remove unused import & improve VR NIC validation
1 parent b4b2b20 commit 405b996

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

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
@@ -110,6 +110,8 @@ protected void init() {
110110
PeerRouterSearch.and("macAddress", PeerRouterSearch.entity().getMacAddress(), Op.EQ);
111111
PeerRouterSearch.and("vmType", PeerRouterSearch.entity().getVmType(), Op.EQ);
112112
PeerRouterSearch.done();
113+
114+
113115
}
114116

115117
@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

@@ -347,6 +355,14 @@ public List<DomainRouterJoinVO> getRouterByIdAndTrafficType(Long id, TrafficType
347355
return searchIncludingRemoved(sc, null, null, false);
348356
}
349357

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

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import javax.inject.Inject;
4848
import javax.naming.ConfigurationException;
4949

50+
import com.cloud.api.query.dao.DomainRouterJoinDao;
5051
import com.cloud.configuration.ConfigurationManager;
5152
import com.cloud.configuration.ConfigurationManagerImpl;
5253
import com.cloud.bgp.BGPService;
@@ -330,6 +331,8 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis
330331
Site2SiteVpnConnectionDao site2SiteVpnConnectionDao;
331332
@Inject
332333
Site2SiteCustomerGatewayDao site2SiteCustomerGatewayDao;
334+
@Inject
335+
protected DomainRouterJoinDao domainRouterJoinDao;
333336

334337
private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("VpcChecker"));
335338
private List<VpcProvider> vpcElements = null;
@@ -2199,9 +2202,8 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
21992202

22002203
try {
22012204
Account account = _accountMgr.getAccount(vpc.getAccountId());
2202-
int maxNetworks = VpcMaxNetworks.valueIn(vpc.getAccountId());
22032205

2204-
checkIfVpcNumberOfTiersIsNotExceeded(vpcId, maxNetworks, account);
2206+
checkIfVpcNumberOfTiersIsNotExceeded(vpcId, account);
22052207

22062208
checkIfVpcHasDomainRouterWithSufficientNicCapacity(vpc);
22072209

@@ -2227,20 +2229,23 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
22272229
}
22282230

22292231
protected boolean existsVpcDomainRouterWithSufficientNicCapacity(long vpcId) {
2230-
int countRouterDefaultNics = 2;
2231-
long countVpcNetworks = _ntwkDao.countVpcNetworks(vpcId);
22322232
DomainRouterVO vpcDomainRouter = routerDao.findOneByVpcId(vpcId);
22332233

22342234
if (vpcDomainRouter == null) {
22352235
return false;
22362236
}
22372237

2238-
int totalNicsAvailable = networkOrchestrationService.getVirtualMachineMaxNicsValue(vpcDomainRouter) - countRouterDefaultNics;
2238+
int countRouterDefaultNetworks = domainRouterJoinDao.countDefaultNetworksById(vpcDomainRouter.getId());
2239+
long countVpcNetworks = _ntwkDao.countVpcNetworks(vpcId);
2240+
2241+
int totalNicsAvailable = networkOrchestrationService.getVirtualMachineMaxNicsValue(vpcDomainRouter) - countRouterDefaultNetworks;
22392242

22402243
return totalNicsAvailable > countVpcNetworks;
22412244
}
22422245

2243-
protected void checkIfVpcNumberOfTiersIsNotExceeded(long vpcId, int maxNetworks, Account account) {
2246+
protected void checkIfVpcNumberOfTiersIsNotExceeded(long vpcId, Account account) {
2247+
int maxNetworks = VpcMaxNetworks.valueIn(account.getId());
2248+
22442249
if (_ntwkDao.countVpcNetworks(vpcId) >= maxNetworks) {
22452250
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);
22462251
throw new CloudRuntimeException(String.format("Number of networks per VPC cannot surpass [%s] for account [%s].", maxNetworks, account.getAccountName()));
@@ -2256,7 +2261,7 @@ protected void checkIfVpcHasDomainRouterWithSufficientNicCapacity(Vpc vpc) {
22562261

22572262
protected void checkIfNetworkCidrIsWithinVpcCidr(String cidr, Vpc vpc) {
22582263
if (!NetUtils.isNetworkAWithinNetworkB(cidr, vpc.getCidr())) {
2259-
throw new InvalidParameterValueException("Network cidr " + cidr + " is not within vpc " + vpc + " cidr");
2264+
throw new InvalidParameterValueException(String.format("Network CIDR %s is not within VPC %s CIDR", cidr, vpc));
22602265
}
22612266
}
22622267

@@ -2265,7 +2270,7 @@ protected void checkIfNetworkCidrNotCrossesOtherVpcNetworksCidr(String cidr, Vpc
22652270

22662271
for (final Network network : networks) {
22672272
if (NetUtils.isNetworkAWithinNetworkB(network.getCidr(), cidr) || NetUtils.isNetworkAWithinNetworkB(cidr, network.getCidr())) {
2268-
throw new InvalidParameterValueException("Network cidr " + cidr + " crosses other network cidr " + network + " belonging to the same vpc " + vpc);
2273+
throw new InvalidParameterValueException(String.format("Network CIDR %s crosses other network CIDR %s belonging to the same VPC %s.", cidr, network, vpc));
22692274
}
22702275
}
22712276
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
import org.mockito.junit.MockitoJUnitRunner;
9393
import org.springframework.test.util.ReflectionTestUtils;
9494

95-
import javax.management.InvalidApplicationException;
9695
import java.lang.reflect.Field;
9796
import java.util.ArrayList;
9897
import java.util.Arrays;
@@ -253,6 +252,7 @@ public void setup() throws NoSuchFieldException, IllegalAccessException {
253252
manager._networkAclDao = networkACLDaoMock;
254253
manager.routedIpv4Manager = routedIpv4Manager;
255254
manager.networkOrchestrationService = networkOrchestrationServiceMock;
255+
manager.domainRouterJoinDao = domainRouterJoinDaoMock;
256256
CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
257257
registerCallContext();
258258
overrideDefaultConfigValue(NetworkService.AllowUsersToSpecifyVRMtu, "_defaultValue", "false");
@@ -613,6 +613,8 @@ public void validateVpcPrivateGatewayTestAclFromDifferentVpcThrowsInvalidParamet
613613
public void existsVpcDomainRouterWithSufficientNicCapacityTestUnavailableRoutersReturnsFalse() {
614614
Mockito.when(networkDao.countVpcNetworks(vpcId)).thenReturn(7L);
615615
Mockito.when(routerDao.findOneByVpcId(vpcId)).thenReturn(domainRouterVOMock);
616+
Mockito.when(domainRouterVOMock.getId()).thenReturn(1L);
617+
Mockito.when(domainRouterJoinDaoMock.countDefaultNetworksById(1L)).thenReturn(2);
616618
Mockito.when(networkOrchestrationServiceMock.getVirtualMachineMaxNicsValue(domainRouterVOMock)).thenReturn(9);
617619

618620
boolean result = manager.existsVpcDomainRouterWithSufficientNicCapacity(vpcId);
@@ -624,6 +626,8 @@ public void existsVpcDomainRouterWithSufficientNicCapacityTestUnavailableRouters
624626
public void existsVpcDomainRouterWithSufficientNicCapacityTestAvailableRouterReturnsTrue() {
625627
Mockito.when(networkDao.countVpcNetworks(vpcId)).thenReturn(6L);
626628
Mockito.when(routerDao.findOneByVpcId(vpcId)).thenReturn(domainRouterVOMock);
629+
Mockito.when(domainRouterVOMock.getId()).thenReturn(1L);
630+
Mockito.when(domainRouterJoinDaoMock.countDefaultNetworksById(1L)).thenReturn(2);
627631
Mockito.when(networkOrchestrationServiceMock.getVirtualMachineMaxNicsValue(domainRouterVOMock)).thenReturn(9);
628632

629633
boolean result = manager.existsVpcDomainRouterWithSufficientNicCapacity(vpcId);
@@ -633,7 +637,6 @@ public void existsVpcDomainRouterWithSufficientNicCapacityTestAvailableRouterRet
633637

634638
@Test
635639
public void existsVpcDomainRouterWithSufficientNicCapacityTestNullRouterReturnsFalse() {
636-
Mockito.when(networkDao.countVpcNetworks(vpcId)).thenReturn(6L);
637640
Mockito.when(routerDao.findOneByVpcId(vpcId)).thenReturn(null);
638641

639642
boolean result = manager.existsVpcDomainRouterWithSufficientNicCapacity(vpcId);
@@ -647,7 +650,7 @@ public void checkIfVpcNumberOfTiersIsNotExceededTestExceededTiersThrowCloudRunti
647650
AccountVO accountMock = Mockito.mock(AccountVO.class);
648651
Mockito.doReturn(5L).when(networkDao).countVpcNetworks(1L);
649652

650-
Assert.assertThrows(CloudRuntimeException.class, () -> manager.checkIfVpcNumberOfTiersIsNotExceeded(vpcId, maxNetworks, accountMock));
653+
Assert.assertThrows(CloudRuntimeException.class, () -> manager.checkIfVpcNumberOfTiersIsNotExceeded(vpcId, accountMock));
651654
}
652655

653656
@Test

0 commit comments

Comments
 (0)