stephankruggg commented on code in PR #6899:
URL: https://github.com/apache/cloudstack/pull/6899#discussion_r1031726305


##########
engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java:
##########
@@ -70,22 +71,49 @@ public interface VirtualMachineManager extends Manager {
     ConfigKey<Boolean> VmConfigDriveForceHostCacheUse = new 
ConfigKey<>("Advanced", Boolean.class, "vm.configdrive.force.host.cache.use", 
"false",
             "If true, config drive is forced to create on the host cache 
storage. Currently only supported for KVM.", true, ConfigKey.Scope.Zone);
 
-    ConfigKey<Boolean> ResourceCountRunningVMsonly = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"resource.count.running.vms.only", "false",
+    ConfigKey<Boolean> ResourceCountRunningVMsonly = new 
ConfigKey<>("Advanced", Boolean.class, "resource.count.running.vms.only", 
"false",
             "Count the resources of only running VMs in resource limitation.", 
true);
 
-    ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"account.allow.expose.host.hostname",
+    ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new 
ConfigKey<>("Advanced", Boolean.class, "account.allow.expose.host.hostname",
             "false", "If set to true, it allows the hypervisor host name on 
which the VM is spawned on to be exposed to the VM", true, 
ConfigKey.Scope.Account);
 
-    ConfigKey<Boolean> AllowExposeHypervisorHostname = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"global.allow.expose.host.hostname",
+    ConfigKey<Boolean> AllowExposeHypervisorHostname = new 
ConfigKey<>("Advanced", Boolean.class, "global.allow.expose.host.hostname",
             "false", "If set to true, it allows the hypervisor host name on 
which the VM is spawned on to be exposed to the VM", true, 
ConfigKey.Scope.Global);
 
     ConfigKey<Boolean> AllowExposeDomainInMetadata = new 
ConfigKey<>("Advanced", Boolean.class, "metadata.allow.expose.domain",
             "false", "If set to true, it allows the VM's domain to be seen in 
metadata.", true, ConfigKey.Scope.Domain);
 
+    static final ConfigKey<Integer> VmServiceOfferingMaxCPUCores = new 
ConfigKey<>("Advanced",
+            Integer.class,
+            "vm.serviceoffering.cpu.cores.max",
+            "0",
+            "Maximum CPU cores for vm service offering. If 0 - no limitation",

Review Comment:
   ```suggestion
               "Maximum number of CPU cores for VM service offering. If 0 - no 
limitation",
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true

Review Comment:
   ```suggestion
        * Counts VR resources for the domain if global setting is true.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {

Review Comment:
   ```suggestion
               if (isIncrement) {
   ```



##########
server/src/main/java/com/cloud/network/router/NetworkHelperImpl.java:
##########
@@ -502,6 +510,14 @@ public DomainRouterVO deployRouter(final 
RouterDeploymentDefinition routerDeploy
         // failed both times, throw the exception up
         final List<HypervisorType> hypervisors = 
getHypervisors(routerDeploymentDefinition);
 
+        // Increment the resource count with router offering.
+        // If router can't be deployed or started, decrement the resources.
+        // If resource.count.running.vms is false then increment resource count

Review Comment:
   ```suggestion
           // If resource.count.running.vms is false, increment resource count.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of

Review Comment:
   ```suggestion
        * If the value is "all", counts all VR resources, otherwise get the 
difference between
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.

Review Comment:
   ```suggestion
        * Returns the service offering by the given configuration.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count 
with value %s and memory resource with value %s",cpuCount, memoryCount));

Review Comment:
   ```suggestion
                   s_logger.debug(String.format("Decrementing CPU resource 
count with value %s and memory resource with value %s.",cpuCount, memoryCount));
   ```



##########
engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java:
##########
@@ -70,22 +71,49 @@ public interface VirtualMachineManager extends Manager {
     ConfigKey<Boolean> VmConfigDriveForceHostCacheUse = new 
ConfigKey<>("Advanced", Boolean.class, "vm.configdrive.force.host.cache.use", 
"false",
             "If true, config drive is forced to create on the host cache 
storage. Currently only supported for KVM.", true, ConfigKey.Scope.Zone);
 
-    ConfigKey<Boolean> ResourceCountRunningVMsonly = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"resource.count.running.vms.only", "false",
+    ConfigKey<Boolean> ResourceCountRunningVMsonly = new 
ConfigKey<>("Advanced", Boolean.class, "resource.count.running.vms.only", 
"false",
             "Count the resources of only running VMs in resource limitation.", 
true);
 
-    ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"account.allow.expose.host.hostname",
+    ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new 
ConfigKey<>("Advanced", Boolean.class, "account.allow.expose.host.hostname",
             "false", "If set to true, it allows the hypervisor host name on 
which the VM is spawned on to be exposed to the VM", true, 
ConfigKey.Scope.Account);
 
-    ConfigKey<Boolean> AllowExposeHypervisorHostname = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"global.allow.expose.host.hostname",
+    ConfigKey<Boolean> AllowExposeHypervisorHostname = new 
ConfigKey<>("Advanced", Boolean.class, "global.allow.expose.host.hostname",
             "false", "If set to true, it allows the hypervisor host name on 
which the VM is spawned on to be exposed to the VM", true, 
ConfigKey.Scope.Global);
 
     ConfigKey<Boolean> AllowExposeDomainInMetadata = new 
ConfigKey<>("Advanced", Boolean.class, "metadata.allow.expose.domain",
             "false", "If set to true, it allows the VM's domain to be seen in 
metadata.", true, ConfigKey.Scope.Domain);
 
+    static final ConfigKey<Integer> VmServiceOfferingMaxCPUCores = new 
ConfigKey<>("Advanced",
+            Integer.class,
+            "vm.serviceoffering.cpu.cores.max",
+            "0",
+            "Maximum CPU cores for vm service offering. If 0 - no limitation",
+            true
+    );
+
+    static final ConfigKey<Integer> VmServiceOfferingMaxRAMSize = new 
ConfigKey<>("Advanced",
+            Integer.class,
+            "vm.serviceoffering.ram.size.max",
+            "0",
+            "Maximum RAM size in MB for vm service offering. If 0 - no 
limitation",

Review Comment:
   ```suggestion
               "Maximum RAM size in MB for VM service offering. If 0 - no 
limitation",
   ```



##########
engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java:
##########
@@ -70,22 +71,49 @@ public interface VirtualMachineManager extends Manager {
     ConfigKey<Boolean> VmConfigDriveForceHostCacheUse = new 
ConfigKey<>("Advanced", Boolean.class, "vm.configdrive.force.host.cache.use", 
"false",
             "If true, config drive is forced to create on the host cache 
storage. Currently only supported for KVM.", true, ConfigKey.Scope.Zone);
 
-    ConfigKey<Boolean> ResourceCountRunningVMsonly = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"resource.count.running.vms.only", "false",
+    ConfigKey<Boolean> ResourceCountRunningVMsonly = new 
ConfigKey<>("Advanced", Boolean.class, "resource.count.running.vms.only", 
"false",
             "Count the resources of only running VMs in resource limitation.", 
true);
 
-    ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"account.allow.expose.host.hostname",
+    ConfigKey<Boolean> AllowExposeHypervisorHostnameAccountLevel = new 
ConfigKey<>("Advanced", Boolean.class, "account.allow.expose.host.hostname",
             "false", "If set to true, it allows the hypervisor host name on 
which the VM is spawned on to be exposed to the VM", true, 
ConfigKey.Scope.Account);
 
-    ConfigKey<Boolean> AllowExposeHypervisorHostname = new 
ConfigKey<Boolean>("Advanced", Boolean.class, 
"global.allow.expose.host.hostname",
+    ConfigKey<Boolean> AllowExposeHypervisorHostname = new 
ConfigKey<>("Advanced", Boolean.class, "global.allow.expose.host.hostname",
             "false", "If set to true, it allows the hypervisor host name on 
which the VM is spawned on to be exposed to the VM", true, 
ConfigKey.Scope.Global);
 
     ConfigKey<Boolean> AllowExposeDomainInMetadata = new 
ConfigKey<>("Advanced", Boolean.class, "metadata.allow.expose.domain",
             "false", "If set to true, it allows the VM's domain to be seen in 
metadata.", true, ConfigKey.Scope.Domain);
 
+    static final ConfigKey<Integer> VmServiceOfferingMaxCPUCores = new 
ConfigKey<>("Advanced",
+            Integer.class,
+            "vm.serviceoffering.cpu.cores.max",
+            "0",
+            "Maximum CPU cores for vm service offering. If 0 - no limitation",
+            true
+    );
+
+    static final ConfigKey<Integer> VmServiceOfferingMaxRAMSize = new 
ConfigKey<>("Advanced",
+            Integer.class,
+            "vm.serviceoffering.ram.size.max",
+            "0",
+            "Maximum RAM size in MB for vm service offering. If 0 - no 
limitation",
+            true
+    );
+
+    ConfigKey<Boolean> ResourceCountRouters = new ConfigKey<>("Advanced", 
Boolean.class, "resource.count.routers",
+            "false","Count the CPU and memory resource count of virtual 
routers towards domain resource calculation",
+            true, ConfigKey.Scope.Domain);
+
+    ConfigKey<String> ResourceCountRoutersType = new ConfigKey<>("Advanced", 
String.class, "resource.count.routers.type", "all",
+            "Possible values are all and delta. If value is all then entire VR 
cpu and ram are counted else diff " +

Review Comment:
   ```suggestion
               "Possible values are all and delta. If the value is all then the 
entire VR CPU and RAM are counted, otherwise the difference " +
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));

Review Comment:
   ```suggestion
                   s_logger.debug(String.format("Incrementing the CPU count 
with value %s and RAM value with %s.", cpuCount, memoryCount));
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering

Review Comment:
   ```suggestion
        * current VR offering and default VR offering.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain

Review Comment:
   ```suggestion
        * Checks if resource count can be allocated to the account/domain.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram

Review Comment:
   ```suggestion
        * @return a Pair of CPU and RAM
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count 
with value %s and memory resource with value %s",cpuCount, memoryCount));
+            }
+        }
+
+        if(isIncrement) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        } else {
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        }
+    }
+
+    /**
+     * Function to increment the VR resource count
+     * If the global setting resource.count.router is true then the VR
+     * resource count will be considered as well
+     * If the global setting resource.count.router.type is "all" then
+     * all VR resource count will be considered else the diff between

Review Comment:
   ```suggestion
        * the total VR resource count will be considered, otherwise the 
difference between
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));

Review Comment:
   ```suggestion
               throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}.", ex.getMessage()));
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count 
with value %s and memory resource with value %s",cpuCount, memoryCount));
+            }
+        }
+
+        if(isIncrement) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        } else {
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        }
+    }
+
+    /**
+     * Function to increment the VR resource count
+     * If the global setting resource.count.router is true then the VR
+     * resource count will be considered as well

Review Comment:
   ```suggestion
        * resource count will be considered as well.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count 
with value %s and memory resource with value %s",cpuCount, memoryCount));
+            }
+        }
+
+        if(isIncrement) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        } else {
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        }
+    }
+
+    /**
+     * Function to increment the VR resource count
+     * If the global setting resource.count.router is true then the VR
+     * resource count will be considered as well
+     * If the global setting resource.count.router.type is "all" then
+     * all VR resource count will be considered else the diff between
+     * the current VR service offering and the default offering will
+     * be considered.
+     * During router deployment/destroy, we increment the resource
+     * count only if resource.count.running.vms is false else

Review Comment:
   ```suggestion
        * count only if resource.count.running.vms is false, otherwise
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count 
with value %s and memory resource with value %s",cpuCount, memoryCount));
+            }
+        }
+
+        if(isIncrement) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        } else {
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        }
+    }
+
+    /**
+     * Function to increment the VR resource count
+     * If the global setting resource.count.router is true then the VR
+     * resource count will be considered as well
+     * If the global setting resource.count.router.type is "all" then
+     * all VR resource count will be considered else the diff between
+     * the current VR service offering and the default offering will
+     * be considered.
+     * During router deployment/destroy, we increment the resource
+     * count only if resource.count.running.vms is false else
+     * we increment it during VR start/stop. Same applies for
+     * decrementing resource count.
+     *
+     * @param offering VR service offering
+     * @param owner account
+     * @param isDeployOrDestroy true if router is being deployed/destroyed
+     */
+    @Override
+    public void incrementVrResourceCount(ServiceOffering offering, Account 
owner, boolean isDeployOrDestroy) {
+        if (isDeployOrDestroy == 
Boolean.TRUE.equals(ResourceCountRunningVMsonly.value())) {
+            return;
+        }
+
+        final ServiceOffering defaultRouterOffering = 
getServiceOfferingByConfig("router.service.offering");
+        final Pair<Long, Long> cpuMemoryCount = 
resolveCpuAndMemoryCount(offering, defaultRouterOffering, owner);
+        calculateResourceCount(cpuMemoryCount, owner, true);
+    }
+
+    /**
+     * Function to decrement the VR resource count

Review Comment:
   ```suggestion
        * Decrements the VR resource count.
   ```



##########
server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java:
##########
@@ -1135,6 +1145,13 @@ private String validateConfigurationValue(final String 
name, String value, final
             return errMsg;
         }
 
+        if 
(VirtualMachineManager.ResourceCountRoutersType.key().equalsIgnoreCase(name)
+                && (!resourceCountRoutersTypeValues.contains(value) || 
isBlank(value))) {
+            final String msg = "Possible values are - delta or all";

Review Comment:
   ```suggestion
               final String msg = "Possible values are: delta or all.";
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -1398,15 +1405,152 @@ private void 
logBootModeParameters(Map<VirtualMachineProfile.Param, Object> para
         }
     }
 
+    /**
+     * Method to return the service offering by the given configuration.
+     *
+     * @param configName name of the config
+     * @return the service offering found or null if not found
+     */
+    public ServiceOffering getServiceOfferingByConfig(String configName) {
+        ServiceOffering defaultRouterOffering = null;
+        final String globalRouterOffering = configDao.getValue(configName);
+
+        if (globalRouterOffering != null) {
+            defaultRouterOffering = 
_serviceOfferingDao.findByUuid(globalRouterOffering);
+        }
+
+        if (defaultRouterOffering == null) {
+            defaultRouterOffering =  
_serviceOfferingDao.findByName(ServiceOffering.routerDefaultOffUniqueName);
+        }
+
+        return defaultRouterOffering;
+    }
+
+    /**
+     * Count VR resources for domain if global setting is true
+     * if value is "all" count all VR resources else get diff of
+     * current VR offering and default VR offering
+     *
+     * @param offering VR service offering
+     * @param defaultRouterOffering default VR service offering
+     * @param owner account
+     * @return a Pair of cpu and ram
+     */
+    private Pair<Long, Long> resolveCpuAndMemoryCount(ServiceOffering 
offering, ServiceOffering defaultRouterOffering, Account owner) {
+        Integer cpuCount = 0;
+        Integer memoryCount = 0;
+        if 
(COUNT_ALL_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            cpuCount = offering.getCpu();
+            memoryCount = offering.getRamSize();
+        } else if 
(COUNT_DELTA_VR_RESOURCES.equalsIgnoreCase(ResourceCountRoutersType.valueIn(owner.getDomainId())))
 {
+            // Default offering value can be greater than current offering 
value
+            if (offering.getCpu() >= defaultRouterOffering.getCpu()) {
+                cpuCount = offering.getCpu() - defaultRouterOffering.getCpu();
+            }
+            if (offering.getRamSize() >= defaultRouterOffering.getRamSize()) {
+                memoryCount = offering.getRamSize() - 
defaultRouterOffering.getRamSize();
+            }
+        }
+
+        return Pair.of(cpuCount.longValue(), memoryCount.longValue());
+    }
+
+    private void validateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner) {
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+        try {
+            if (cpuCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
cpuCount);
+            }
+            if (memoryCount > 0) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, memoryCount);
+            }
+        } catch (ResourceAllocationException ex) {
+            throw new CloudRuntimeException(String.format("Unable to 
deploy/start routers due to {}", ex.getMessage()));
+        }
+    }
+
+    /**
+     * Check if resource count can be allocated to account/domain
+     *
+     * @param cpuMemoryCount a Pair of cpu and ram
+     * @param owner the account
+     */
+    private void calculateResourceCount(Pair<Long, Long> cpuMemoryCount, 
Account owner, boolean isIncrement) {
+        validateResourceCount(cpuMemoryCount, owner);
+        final Long cpuCount = cpuMemoryCount.first();
+        final Long memoryCount = cpuMemoryCount.second();
+
+        // Increment the resource count
+        if (s_logger.isDebugEnabled()) {
+            if(isIncrement) {
+                s_logger.debug(String.format("Incrementing the CPU count with 
value %s and RAM value with %s", cpuCount, memoryCount));
+            } else {
+                s_logger.debug(String.format("Decrementing cpu resource count 
with value %s and memory resource with value %s",cpuCount, memoryCount));
+            }
+        }
+
+        if(isIncrement) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        } else {
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, cpuCount);
+            _resourceLimitMgr.decrementResourceCount(owner.getAccountId(), 
ResourceType.memory, memoryCount);
+        }
+    }
+
+    /**
+     * Function to increment the VR resource count

Review Comment:
   ```suggestion
        * Increments the VR resource count.
   ```



##########
engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java:
##########
@@ -2108,6 +2256,16 @@ private void advanceStop(final VMInstanceVO vm, final 
boolean cleanUpEvenIfUnabl
         }
     }
 
+    private void updateVrCountResourceBy(VirtualMachine.Type type, long 
domainId, ServiceOffering offering, Account owner, boolean decrement) {
+        if (VirtualMachine.Type.DomainRouter.equals(type) && 
Boolean.TRUE.equals(ResourceCountRouters.valueIn(domainId))) {
+            if(decrement) {

Review Comment:
   ```suggestion
               if (decrement) {
   ```



##########
server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java:
##########
@@ -1049,11 +1051,15 @@ public VolumeVO resizeVolume(ResizeVolumeCmd cmd) 
throws ResourceAllocationExcep
 
         HypervisorType hypervisorType = 
_volsDao.getHypervisorType(volume.getId());
         if (!SupportedHypervisorsForVolResize.contains(hypervisorType)) {
-            throw new InvalidParameterValueException("Hypervisor " + 
hypervisorType + " does not support volume resize");
+            throw new InvalidParameterValueException(String.format("Hypervisor 
%s does not support volume resize", hypervisorType));

Review Comment:
   ```suggestion
               throw new 
InvalidParameterValueException(String.format("Hypervisor %s does not support 
volume resize.", hypervisorType));
   ```



##########
server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java:
##########
@@ -1049,11 +1051,15 @@ public VolumeVO resizeVolume(ResizeVolumeCmd cmd) 
throws ResourceAllocationExcep
 
         HypervisorType hypervisorType = 
_volsDao.getHypervisorType(volume.getId());
         if (!SupportedHypervisorsForVolResize.contains(hypervisorType)) {
-            throw new InvalidParameterValueException("Hypervisor " + 
hypervisorType + " does not support volume resize");
+            throw new InvalidParameterValueException(String.format("Hypervisor 
%s does not support volume resize", hypervisorType));
+        }
+
+        if 
(!SupportedHypervisorsForRootDiskSizeOverride.contains(hypervisorType)) {
+            throw new InvalidParameterValueException(String.format("Hypervisor 
%s does not support  rootdisksize override", hypervisorType));

Review Comment:
   ```suggestion
               throw new 
InvalidParameterValueException(String.format("Hypervisor %s does not support 
rootdisksize override.", hypervisorType));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to