From a73801b4a9ea4e04b0d3a6788fe28d215a798f05 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Thu, 29 Aug 2024 10:34:04 +0200 Subject: [PATCH] server: remove dedicated bgp peers and ipv4 subnets when delete an account or domain --- .../cloudstack/network/RoutedIpv4Manager.java | 8 ++++++ .../dao/DataCenterIpv4GuestSubnetDao.java | 2 ++ .../dao/DataCenterIpv4GuestSubnetDaoImpl.java | 14 ++++++++++ .../cloudstack/network/dao/BgpPeerDao.java | 3 ++ .../network/dao/BgpPeerDaoImpl.java | 14 ++++++++++ .../com/cloud/user/AccountManagerImpl.java | 9 ++++++ .../com/cloud/user/DomainManagerImpl.java | 9 ++++++ .../network/RoutedIpv4ManagerImpl.java | 28 +++++++++++++++++++ .../user/AccountManagetImplTestBase.java | 3 ++ .../com/cloud/user/DomainManagerImplTest.java | 3 ++ 10 files changed, 93 insertions(+) diff --git a/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java b/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java index b293349d736a..3e7dc7343a7d 100644 --- a/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java +++ b/api/src/main/java/org/apache/cloudstack/network/RoutedIpv4Manager.java @@ -188,4 +188,12 @@ public interface RoutedIpv4Manager extends PluggableService, Configurable { Vpc changeBgpPeersForVpc(ChangeBgpPeersForVpcCmd changeBgpPeersForVpcCmd); List getBgpPeerIdsForAccount(Account owner, long zoneIdd); + + void removeIpv4SubnetsForZoneByAccountId(long accountId); + + void removeIpv4SubnetsForZoneByDomainId(long domainId); + + void removeBgpPeersByAccountId(long accountId); + + void removeBgpPeersByDomainId(long domainId); } diff --git a/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDao.java b/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDao.java index 4898859eb715..87a98e05eef9 100644 --- a/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDao.java +++ b/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDao.java @@ -27,4 +27,6 @@ public interface DataCenterIpv4GuestSubnetDao extends GenericDao listByDataCenterId(long dcId); List listByDataCenterIdAndAccountId(long dcId, long accountId); List listByDataCenterIdAndDomainId(long dcId, long domainId); + List listByAccountId(long accountId); + List listByDomainId(long domainId); } diff --git a/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDaoImpl.java b/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDaoImpl.java index 5945b632f9b3..ee87135f8621 100644 --- a/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDaoImpl.java +++ b/engine/schema/src/main/java/org/apache/cloudstack/datacenter/dao/DataCenterIpv4GuestSubnetDaoImpl.java @@ -57,4 +57,18 @@ public List listByDataCenterIdAndDomainId(long dcId sc.and(sc.entity().getAccountId(), SearchCriteria.Op.NULL); return sc.list(); } + + @Override + public List listByAccountId(long accountId) { + QueryBuilder sc = QueryBuilder.create(DataCenterIpv4GuestSubnetVO.class); + sc.and(sc.entity().getAccountId(), SearchCriteria.Op.EQ, accountId); + return sc.list(); + } + + @Override + public List listByDomainId(long domainId) { + QueryBuilder sc = QueryBuilder.create(DataCenterIpv4GuestSubnetVO.class); + sc.and(sc.entity().getDomainId(), SearchCriteria.Op.EQ, domainId); + return sc.list(); + } } diff --git a/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDao.java b/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDao.java index 9c84aa48668a..8ca4c2d86da7 100644 --- a/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDao.java +++ b/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDao.java @@ -34,4 +34,7 @@ public interface BgpPeerDao extends GenericDao { BgpPeerVO persist(BgpPeerVO bgpPeerVO, Map details); List listAvailableBgpPeerIdsForAccount(long zoneId, long domainId, long accountId, boolean useSystemBgpPeers); + + int removeByAccountId(long accountId); + int removeByDomainId(long domainId); } diff --git a/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java b/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java index d459442a229d..0f95f7c3cd58 100644 --- a/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java +++ b/engine/schema/src/main/java/org/apache/cloudstack/network/dao/BgpPeerDaoImpl.java @@ -176,4 +176,18 @@ private List listBgpPeerIdsForAccount(long zoneId, long domainId, long acc throw new CloudRuntimeException("Caught: " + sql, e); } } + + @Override + public int removeByAccountId(long accountId) { + SearchCriteria sc = createSearchCriteria(); + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + return remove(sc); + } + + @Override + public int removeByDomainId(long domainId) { + SearchCriteria sc = createSearchCriteria(); + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + return remove(sc); + } } diff --git a/server/src/main/java/com/cloud/user/AccountManagerImpl.java b/server/src/main/java/com/cloud/user/AccountManagerImpl.java index 07d06fbd2f7e..6a9e15a58c70 100644 --- a/server/src/main/java/com/cloud/user/AccountManagerImpl.java +++ b/server/src/main/java/com/cloud/user/AccountManagerImpl.java @@ -73,6 +73,7 @@ import org.apache.cloudstack.framework.messagebus.MessageBus; import org.apache.cloudstack.framework.messagebus.PublishScope; import org.apache.cloudstack.managed.context.ManagedContextRunnable; +import org.apache.cloudstack.network.RoutedIpv4Manager; import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao; import org.apache.cloudstack.resourcedetail.UserDetailVO; import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao; @@ -320,6 +321,8 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M private IpAddressManager _ipAddrMgr; @Inject private RoleService roleService; + @Inject + private RoutedIpv4Manager routedIpv4Manager; @Inject private PasswordPolicy passwordPolicy; @@ -1067,6 +1070,12 @@ public int compare(NetworkVO network1, NetworkVO network2) { } } + // remove dedicated IPv4 subnets + routedIpv4Manager.removeIpv4SubnetsForZoneByAccountId(accountId); + + // remove dedicated BGP peers + routedIpv4Manager.removeBgpPeersByAccountId(accountId); + // release account specific guest vlans List maps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByAccount(accountId); for (AccountGuestVlanMapVO map : maps) { diff --git a/server/src/main/java/com/cloud/user/DomainManagerImpl.java b/server/src/main/java/com/cloud/user/DomainManagerImpl.java index 51705e63f3a9..4a81772d6d75 100644 --- a/server/src/main/java/com/cloud/user/DomainManagerImpl.java +++ b/server/src/main/java/com/cloud/user/DomainManagerImpl.java @@ -51,6 +51,7 @@ import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import org.apache.cloudstack.framework.messagebus.MessageBus; import org.apache.cloudstack.framework.messagebus.PublishScope; +import org.apache.cloudstack.network.RoutedIpv4Manager; import org.apache.cloudstack.region.RegionManager; import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao; import org.apache.commons.collections.CollectionUtils; @@ -161,6 +162,8 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom private ResourceLimitService resourceLimitService; @Inject private AffinityGroupDomainMapDao affinityGroupDomainMapDao; + @Inject + private RoutedIpv4Manager routedIpv4Manager; @Inject MessageBus _messageBus; @@ -393,6 +396,12 @@ private boolean cleanDomain(DomainVO domain, Boolean cleanup) { removeDomainWithNoAccountsForCleanupNetworksOrDedicatedResources(domain); } + // remove dedicated IPv4 subnets + routedIpv4Manager.removeIpv4SubnetsForZoneByDomainId(domain.getId()); + + // remove dedicated BGP peers + routedIpv4Manager.removeBgpPeersByDomainId(domain.getId()); + if (!_configMgr.releaseDomainSpecificVirtualRanges(domain.getId())) { CloudRuntimeException e = new CloudRuntimeException("Can't delete the domain yet because failed to release domain specific virtual ip ranges"); e.addProxyObject(domain.getUuid(), "domainId"); diff --git a/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java b/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java index 39612a8f11ea..678027fe33b0 100644 --- a/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java +++ b/server/src/main/java/org/apache/cloudstack/network/RoutedIpv4ManagerImpl.java @@ -1592,4 +1592,32 @@ private Vpc changeBgpPeersForVpcInternal(Vpc vpc, List bgpPeerIds) { return vpcDao.findById(vpc.getId()); } + + @Override + public void removeIpv4SubnetsForZoneByAccountId(long accountId) { + List existingSubnets = dataCenterIpv4GuestSubnetDao.listByAccountId(accountId); + for (DataCenterIpv4GuestSubnetVO subnet : existingSubnets) { + ipv4GuestSubnetNetworkMapDao.deleteByParentId(subnet.getId()); + dataCenterIpv4GuestSubnetDao.remove(subnet.getId()); + } + } + + @Override + public void removeIpv4SubnetsForZoneByDomainId(long domainId) { + List existingSubnets = dataCenterIpv4GuestSubnetDao.listByDomainId(domainId); + for (DataCenterIpv4GuestSubnetVO subnet : existingSubnets) { + ipv4GuestSubnetNetworkMapDao.deleteByParentId(subnet.getId()); + dataCenterIpv4GuestSubnetDao.remove(subnet.getId()); + } + } + + @Override + public void removeBgpPeersByAccountId(long accountId) { + bgpPeerDao.removeByAccountId(accountId); + } + + @Override + public void removeBgpPeersByDomainId(long domainId) { + bgpPeerDao.removeByDomainId(domainId); + } } diff --git a/server/src/test/java/com/cloud/user/AccountManagetImplTestBase.java b/server/src/test/java/com/cloud/user/AccountManagetImplTestBase.java index 7f9fa488471a..e97fddc02622 100644 --- a/server/src/test/java/com/cloud/user/AccountManagetImplTestBase.java +++ b/server/src/test/java/com/cloud/user/AccountManagetImplTestBase.java @@ -65,6 +65,7 @@ import org.apache.cloudstack.engine.service.api.OrchestrationService; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.framework.messagebus.MessageBus; +import org.apache.cloudstack.network.RoutedIpv4Manager; import org.apache.cloudstack.region.gslb.GlobalLoadBalancerRuleDao; import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao; import org.junit.After; @@ -203,6 +204,8 @@ public class AccountManagetImplTestBase { UsageEventDao _usageEventDao; @Mock AccountService _accountService; + @Mock + RoutedIpv4Manager routedIpv4Manager; @Before public void setup() { diff --git a/server/src/test/java/com/cloud/user/DomainManagerImplTest.java b/server/src/test/java/com/cloud/user/DomainManagerImplTest.java index 829f0c9cb200..39155986941e 100644 --- a/server/src/test/java/com/cloud/user/DomainManagerImplTest.java +++ b/server/src/test/java/com/cloud/user/DomainManagerImplTest.java @@ -49,6 +49,7 @@ import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import org.apache.cloudstack.framework.messagebus.MessageBus; import org.apache.cloudstack.framework.messagebus.PublishScope; +import org.apache.cloudstack.network.RoutedIpv4Manager; import org.apache.cloudstack.region.RegionManager; import org.junit.Assert; import org.junit.Before; @@ -108,6 +109,8 @@ public class DomainManagerImplTest { DomainDetailsDao _domainDetailsDao; @Mock AnnotationDao annotationDao; + @Mock + RoutedIpv4Manager routedIpv4Manager; @Spy @InjectMocks