This is an automated email from the ASF dual-hosted git repository.

shwstppr pushed a commit to branch 4.17
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.17 by this push:
     new 7ddebd3b2c server: fix error when dedicating guest vlan range for 
physical nw without vlan range (#6655)
7ddebd3b2c is described below

commit 7ddebd3b2c1e86d3494a114aa544fc6038f431ef
Author: Abhishek Kumar <[email protected]>
AuthorDate: Thu Aug 25 22:27:44 2022 +0530

    server: fix error when dedicating guest vlan range for physical nw without 
vlan range (#6655)
    
    Fixes #6648
    If any of the VLAN from the given range is not found in the database 
(cloud.op_dc_vnet_alloc) then an InvalidParameterValueException will be thrown.
    Also, refactors and fixes account check.
---
 .../java/com/cloud/network/NetworkServiceImpl.java | 27 ++++++++++++----------
 .../cloud/network/DedicateGuestVlanRangesTest.java | 20 ++++++++--------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java 
b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java
index a3cb967c8a..2aca1f7c64 100644
--- a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java
+++ b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java
@@ -370,6 +370,20 @@ public class NetworkServiceImpl extends ManagerBase 
implements NetworkService, C
 
     private Map<String, String> _configs;
 
+    private void 
verifyDedicatedGuestVlansWithExistingDatacenterVlans(PhysicalNetwork 
physicalNetwork, Account vlanOwner, int startVlan, int endVlan) {
+        for (int i = startVlan; i <= endVlan; i++) {
+            List<DataCenterVnetVO> dataCenterVnet = 
_dcVnetDao.findVnet(physicalNetwork.getDataCenterId(), physicalNetwork.getId(), 
Integer.toString(i));
+            if (CollectionUtils.isEmpty(dataCenterVnet)) {
+                throw new InvalidParameterValueException(String.format("Guest 
vlan %d from this range %d-%d is not present in the system for physical network 
ID: %s", i, startVlan, endVlan, physicalNetwork.getUuid()));
+            }
+            // Verify guest vlans in the range don't belong to a network of a 
different account
+            if (dataCenterVnet.get(0).getAccountId() != null && 
dataCenterVnet.get(0).getAccountId() != vlanOwner.getAccountId()) {
+                throw new InvalidParameterValueException("Guest vlan from this 
range " + dataCenterVnet.get(0).getVnet() + " is allocated to a different 
account."
+                        + " Can only dedicate a range which has no allocated 
vlans or has vlans allocated to the same account ");
+            }
+        }
+    }
+
     /* Get a list of IPs, classify them by service */
     protected Map<PublicIp, Set<Service>> getIpToServices(List<PublicIp> 
publicIps, boolean rulesRevoked, boolean includingFirewall) {
         Map<PublicIp, Set<Service>> ipToServices = new HashMap<PublicIp, 
Set<Service>>();
@@ -4073,18 +4087,7 @@ public class NetworkServiceImpl extends ManagerBase 
implements NetworkService, C
             }
         }
 
-        // Verify guest vlans in the range don't belong to a network of a 
different account
-        for (int i = startVlan; i <= endVlan; i++) {
-            List<DataCenterVnetVO> allocatedVlans = 
_dcVnetDao.listAllocatedVnetsInRange(physicalNetwork.getDataCenterId(), 
physicalNetwork.getId(), startVlan, endVlan);
-            if (allocatedVlans != null && !allocatedVlans.isEmpty()) {
-                for (DataCenterVnetVO allocatedVlan : allocatedVlans) {
-                    if (allocatedVlan.getAccountId() != 
vlanOwner.getAccountId()) {
-                        throw new InvalidParameterValueException("Guest vlan 
from this range " + allocatedVlan.getVnet() + " is allocated to a different 
account."
-                                + " Can only dedicate a range which has no 
allocated vlans or has vlans allocated to the same account ");
-                    }
-                }
-            }
-        }
+        verifyDedicatedGuestVlansWithExistingDatacenterVlans(physicalNetwork, 
vlanOwner, startVlan, endVlan);
 
         List<AccountGuestVlanMapVO> guestVlanMaps = 
_accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(physicalNetworkId);
         // Verify if vlan range is already dedicated
diff --git 
a/server/src/test/java/com/cloud/network/DedicateGuestVlanRangesTest.java 
b/server/src/test/java/com/cloud/network/DedicateGuestVlanRangesTest.java
index 8687465f3c..726f99e9a0 100644
--- a/server/src/test/java/com/cloud/network/DedicateGuestVlanRangesTest.java
+++ b/server/src/test/java/com/cloud/network/DedicateGuestVlanRangesTest.java
@@ -29,9 +29,10 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
-import com.cloud.user.User;
-import junit.framework.Assert;
-
+import 
org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd;
+import 
org.apache.cloudstack.api.command.admin.network.ListDedicatedGuestVlanRangesCmd;
+import 
org.apache.cloudstack.api.command.admin.network.ReleaseDedicatedGuestVlanRangeCmd;
+import org.apache.cloudstack.context.CallContext;
 import org.apache.log4j.Logger;
 import org.junit.After;
 import org.junit.Before;
@@ -39,11 +40,6 @@ import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
-import 
org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd;
-import 
org.apache.cloudstack.api.command.admin.network.ListDedicatedGuestVlanRangesCmd;
-import 
org.apache.cloudstack.api.command.admin.network.ReleaseDedicatedGuestVlanRangeCmd;
-import org.apache.cloudstack.context.CallContext;
-
 import com.cloud.dc.DataCenterVnetVO;
 import com.cloud.dc.dao.DataCenterVnetDao;
 import com.cloud.network.dao.AccountGuestVlanMapDao;
@@ -54,10 +50,13 @@ import com.cloud.projects.ProjectManager;
 import com.cloud.user.Account;
 import com.cloud.user.AccountManager;
 import com.cloud.user.AccountVO;
+import com.cloud.user.User;
 import com.cloud.user.UserVO;
 import com.cloud.user.dao.AccountDao;
 import com.cloud.utils.db.TransactionLegacy;
 
+import junit.framework.Assert;
+
 public class DedicateGuestVlanRangesTest {
 
     private static final Logger s_logger = 
Logger.getLogger(DedicateGuestVlanRangesTest.class);
@@ -275,7 +274,7 @@ public class DedicateGuestVlanRangesTest {
         DataCenterVnetVO dataCenter = new DataCenterVnetVO("2-5", 1L, 1L);
         dataCenter.setAccountId(1L);
         dataCenterList.add(dataCenter);
-        when(networkService._dcVnetDao.listAllocatedVnetsInRange(anyLong(), 
anyLong(), anyInt(), anyInt())).thenReturn(dataCenterList);
+        when(networkService._dcVnetDao.findVnet(anyLong(), anyLong(), 
anyString())).thenReturn(dataCenterList);
 
         try {
             networkService.dedicateGuestVlanRange(dedicateGuestVlanRangesCmd);
@@ -298,7 +297,8 @@ public class DedicateGuestVlanRangesTest {
 
         
when(networkService._physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetwork);
 
-        when(networkService._dcVnetDao.listAllocatedVnetsInRange(anyLong(), 
anyLong(), anyInt(), anyInt())).thenReturn(null);
+        DataCenterVnetVO dataCenterVnetVO = new DataCenterVnetVO("2-5", 1L, 
1L);
+        when(networkService._dcVnetDao.findVnet(anyLong(), anyLong(), 
anyString())).thenReturn(List.of(dataCenterVnetVO));
 
         List<AccountGuestVlanMapVO> guestVlanMaps = new 
ArrayList<AccountGuestVlanMapVO>();
         AccountGuestVlanMapVO accountGuestVlanMap = new 
AccountGuestVlanMapVO(1L, 1L);

Reply via email to