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

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


The following commit(s) were added to refs/heads/4.19 by this push:
     new 6c40a7bebbc deal with null return for create deployment plan for 
maintenance (#10518)
6c40a7bebbc is described below

commit 6c40a7bebbc4c9d4dd9c96769834ad4b9c6cb6c7
Author: dahn <[email protected]>
AuthorDate: Tue Mar 18 14:53:39 2025 +0100

    deal with null return for create deployment plan for maintenance (#10518)
    
    * deal with null return for create deployment plan for maintenance
    
    * deal with ide warnings
---
 .../com/cloud/resource/ResourceManagerImpl.java    | 219 +++++++++------------
 1 file changed, 92 insertions(+), 127 deletions(-)

diff --git a/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java 
b/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
index 5ba79c2d7bd..c05776ee6e1 100755
--- a/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
+++ b/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
@@ -67,9 +67,9 @@ import 
org.apache.cloudstack.framework.config.dao.ConfigurationDao;
 import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
 import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
 import org.apache.cloudstack.utils.identity.ManagementServerNode;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.ObjectUtils;
-import org.apache.commons.lang3.StringUtils;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -99,7 +99,6 @@ import com.cloud.capacity.CapacityVO;
 import com.cloud.capacity.dao.CapacityDao;
 import com.cloud.cluster.ClusterManager;
 import com.cloud.configuration.Config;
-import com.cloud.configuration.ConfigurationManager;
 import com.cloud.dc.ClusterDetailsDao;
 import com.cloud.dc.ClusterDetailsVO;
 import com.cloud.dc.ClusterVO;
@@ -131,7 +130,6 @@ import com.cloud.exception.DiscoveryException;
 import com.cloud.exception.InsufficientServerCapacityException;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.PermissionDeniedException;
-import com.cloud.exception.ResourceInUseException;
 import com.cloud.exception.ResourceUnavailableException;
 import com.cloud.gpu.GPU;
 import com.cloud.gpu.HostGpuGroupsVO;
@@ -169,7 +167,6 @@ import com.cloud.storage.StoragePoolHostVO;
 import com.cloud.storage.StoragePoolStatus;
 import com.cloud.storage.StorageService;
 import com.cloud.storage.VMTemplateVO;
-import com.cloud.storage.dao.DiskOfferingDao;
 import com.cloud.storage.dao.GuestOSCategoryDao;
 import com.cloud.storage.dao.StoragePoolHostDao;
 import com.cloud.storage.dao.VMTemplateDao;
@@ -201,6 +198,7 @@ import com.cloud.utils.net.NetUtils;
 import com.cloud.utils.ssh.SSHCmdHelper;
 import com.cloud.utils.ssh.SshException;
 import com.cloud.vm.UserVmManager;
+import com.cloud.utils.StringUtils;
 import com.cloud.vm.VMInstanceVO;
 import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VirtualMachine.State;
@@ -235,8 +233,6 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     @Inject
     private CapacityDao _capacityDao;
     @Inject
-    private DiskOfferingDao diskOfferingDao;
-    @Inject
     private ServiceOfferingDao serviceOfferingDao;
     @Inject
     private HostDao _hostDao;
@@ -295,8 +291,6 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     @Inject
     private VMTemplateDao _templateDao;
     @Inject
-    private ConfigurationManager _configMgr;
-    @Inject
     private ClusterVSMMapDao _clusterVSMMapDao;
     @Inject
     private UserVmDetailsDao userVmDetailsDao;
@@ -311,9 +305,9 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
     private final long _nodeId = ManagementServerNode.getManagementServerId();
 
-    private final HashMap<String, ResourceStateAdapter> _resourceStateAdapters 
= new HashMap<String, ResourceStateAdapter>();
+    private final HashMap<String, ResourceStateAdapter> _resourceStateAdapters 
= new HashMap<>();
 
-    private final HashMap<Integer, List<ResourceListener>> _lifeCycleListeners 
= new HashMap<Integer, List<ResourceListener>>();
+    private final HashMap<Integer, List<ResourceListener>> _lifeCycleListeners 
= new HashMap<>();
     private HypervisorType _defaultSystemVMHypervisor;
 
     private static final int ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION = 30; 
// seconds
@@ -323,11 +317,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     private SearchBuilder<HostGpuGroupsVO> _gpuAvailability;
 
     private void insertListener(final Integer event, final ResourceListener 
listener) {
-        List<ResourceListener> lst = _lifeCycleListeners.get(event);
-        if (lst == null) {
-            lst = new ArrayList<ResourceListener>();
-            _lifeCycleListeners.put(event, lst);
-        }
+        List<ResourceListener> lst = 
_lifeCycleListeners.computeIfAbsent(event, k -> new ArrayList<>());
 
         if (lst.contains(listener)) {
             throw new CloudRuntimeException("Duplicate resource lisener:" + 
listener.getClass().getSimpleName());
@@ -369,9 +359,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     @Override
     public void unregisterResourceEvent(final ResourceListener listener) {
         synchronized (_lifeCycleListeners) {
-            final Iterator it = _lifeCycleListeners.entrySet().iterator();
-            while (it.hasNext()) {
-                final Map.Entry<Integer, List<ResourceListener>> items = 
(Map.Entry<Integer, List<ResourceListener>>)it.next();
+            for (Map.Entry<Integer, List<ResourceListener>> items : 
_lifeCycleListeners.entrySet()) {
                 final List<ResourceListener> lst = items.getValue();
                 lst.remove(listener);
             }
@@ -380,7 +368,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
     protected void processResourceEvent(final Integer event, final Object... 
params) {
         final List<ResourceListener> lst = _lifeCycleListeners.get(event);
-        if (lst == null || lst.size() == 0) {
+        if (lst == null || lst.isEmpty()) {
             return;
         }
 
@@ -421,7 +409,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
     @DB
     @Override
-    public List<? extends Cluster> discoverCluster(final AddClusterCmd cmd) 
throws IllegalArgumentException, DiscoveryException, ResourceInUseException {
+    public List<? extends Cluster> discoverCluster(final AddClusterCmd cmd) 
throws IllegalArgumentException, DiscoveryException {
         final long dcId = cmd.getZoneId();
         final long podId = cmd.getPodId();
         final String clusterName = cmd.getClusterName();
@@ -430,10 +418,10 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         final String password = cmd.getPassword();
 
         if (url != null) {
-            url = URLDecoder.decode(url);
+            url = URLDecoder.decode(url, 
com.cloud.utils.StringUtils.getPreferredCharset());
         }
 
-        URI uri = null;
+        URI uri;
 
         // Check if the zone exists in the system
         final DataCenterVO zone = _dcDao.findById(dcId);
@@ -520,7 +508,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             discoverer.putParam(allParams);
         }
 
-        final List<ClusterVO> result = new ArrayList<ClusterVO>();
+        final List<ClusterVO> result = new ArrayList<>();
 
         ClusterVO cluster = new ClusterVO(dcId, podId, clusterName);
         cluster.setHypervisorType(hypervisorType.toString());
@@ -540,7 +528,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         result.add(cluster);
 
         if (clusterType == Cluster.ClusterType.CloudManaged) {
-            final Map<String, String> details = new HashMap<String, String>();
+            final Map<String, String> details = new HashMap<>();
             // should do this nicer perhaps ?
             if (hypervisorType == HypervisorType.Ovm3) {
                 final Map<String, String> allParams = cmd.getFullUrlParams();
@@ -578,8 +566,8 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                 throw new InvalidParameterValueException(url + " is not a 
valid uri");
             }
 
-            final List<HostVO> hosts = new ArrayList<HostVO>();
-            Map<? extends ServerResource, Map<String, String>> resources = 
null;
+            final List<HostVO> hosts = new ArrayList<>();
+            Map<? extends ServerResource, Map<String, String>> resources;
             resources = discoverer.find(dcId, podId, cluster.getId(), uri, 
username, password, null);
 
             if (resources != null) {
@@ -670,7 +658,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     private List<HostVO> discoverHostsFull(final Long dcId, final Long podId, 
Long clusterId, final String clusterName, String url, String username, String 
password,
             final String hypervisorType, final List<String> hostTags, final 
Map<String, String> params, final boolean deferAgentCreation) throws 
IllegalArgumentException, DiscoveryException,
             InvalidParameterValueException {
-        URI uri = null;
+        URI uri;
 
         // Check if the zone exists in the system
         final DataCenterVO zone = _dcDao.findById(dcId);
@@ -808,7 +796,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             throw new InvalidParameterValueException(url + " is not a valid 
uri");
         }
 
-        final List<HostVO> hosts = new ArrayList<HostVO>();
+        final List<HostVO> hosts = new ArrayList<>();
         s_logger.info("Trying to add a new host at " + url + " in data center 
" + dcId);
         boolean isHypervisorTypeSupported = false;
         for (final Discoverer discoverer : _discoverers) {
@@ -870,7 +858,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                         return null;
                     }
 
-                    HostVO host = null;
+                    HostVO host;
                     if (deferAgentCreation) {
                         host = (HostVO)createHostAndAgentDeferred(resource, 
entry.getValue(), true, hostTags, false);
                     } else {
@@ -964,7 +952,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                 _hostDao.remove(hostId);
                 if (clusterId != null) {
                     final List<HostVO> hosts = 
listAllHostsInCluster(clusterId);
-                    if (hosts.size() == 0) {
+                    if (hosts.isEmpty()) {
                         final ClusterVO cluster = 
_clusterDao.findById(clusterId);
                         cluster.setGuid(null);
                         _clusterDao.update(clusterId, cluster);
@@ -1089,7 +1077,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                     final Hypervisor.HypervisorType hypervisorType = 
cluster.getHypervisorType();
 
                     final List<HostVO> hosts = 
listAllHostsInCluster(cmd.getId());
-                    if (hosts.size() > 0) {
+                    if (!hosts.isEmpty()) {
                         if (s_logger.isDebugEnabled()) {
                             s_logger.debug("Cluster: " + cmd.getId() + " still 
has hosts, can't remove");
                         }
@@ -1099,7 +1087,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                     // don't allow to remove the cluster if it has non-removed 
storage
                     // pools
                     final List<StoragePoolVO> storagePools = 
_storagePoolDao.listPoolsByCluster(cmd.getId());
-                    if (storagePools.size() > 0) {
+                    if (!storagePools.isEmpty()) {
                         if (s_logger.isDebugEnabled()) {
                             s_logger.debug("Cluster: " + cmd.getId() + " still 
has storage pools, can't remove");
                         }
@@ -1167,36 +1155,26 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             }
         }
 
-        Cluster.ClusterType newClusterType = null;
+        Cluster.ClusterType newClusterType;
         if (clusterType != null && !clusterType.isEmpty()) {
             try {
                 newClusterType = Cluster.ClusterType.valueOf(clusterType);
             } catch (final IllegalArgumentException ex) {
                 throw new InvalidParameterValueException("Unable to resolve " 
+ clusterType + " to a supported type");
             }
-            if (newClusterType == null) {
-                s_logger.error("Unable to resolve " + clusterType + " to a 
valid supported cluster type");
-                throw new InvalidParameterValueException("Unable to resolve " 
+ clusterType + " to a supported type");
-            } else {
-                cluster.setClusterType(newClusterType);
-                doUpdate = true;
-            }
+            cluster.setClusterType(newClusterType);
+            doUpdate = true;
         }
 
-        Grouping.AllocationState newAllocationState = null;
+        Grouping.AllocationState newAllocationState;
         if (allocationState != null && !allocationState.isEmpty()) {
             try {
                 newAllocationState = 
Grouping.AllocationState.valueOf(allocationState);
             } catch (final IllegalArgumentException ex) {
                 throw new InvalidParameterValueException("Unable to resolve 
Allocation State '" + allocationState + "' to a supported state");
             }
-            if (newAllocationState == null) {
-                s_logger.error("Unable to resolve " + allocationState + " to a 
valid supported allocation State");
-                throw new InvalidParameterValueException("Unable to resolve " 
+ allocationState + " to a supported state");
-            } else {
-                cluster.setAllocationState(newAllocationState);
-                doUpdate = true;
-            }
+            cluster.setAllocationState(newAllocationState);
+            doUpdate = true;
         }
 
         Managed.ManagedState newManagedState = null;
@@ -1207,12 +1185,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             } catch (final IllegalArgumentException ex) {
                 throw new InvalidParameterValueException("Unable to resolve 
Managed State '" + managedstate + "' to a supported state");
             }
-            if (newManagedState == null) {
-                s_logger.error("Unable to resolve Managed State '" + 
managedstate + "' to a supported state");
-                throw new InvalidParameterValueException("Unable to resolve 
Managed State '" + managedstate + "' to a supported state");
-            } else {
-                doUpdate = true;
-            }
+            doUpdate = true;
         }
 
         if (doUpdate) {
@@ -1240,12 +1213,13 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                         }
                     }
                     final int retry = 40;
-                    boolean lsuccess = true;
+                    boolean lsuccess;
                     for (int i = 0; i < retry; i++) {
                         lsuccess = true;
                         try {
-                            Thread.sleep(5 * 1000);
-                        } catch (final Exception e) {
+                            Thread.currentThread().wait(5 * 1000);
+                        } catch (final InterruptedException e) {
+                            s_logger.debug("thread unexpectedly interupted 
during wait, while updating cluster");
                         }
                         hosts = listAllUpAndEnabledHosts(Host.Type.Routing, 
cluster.getId(), cluster.getPodId(), cluster.getDataCenterId());
                         for (final HostVO host : hosts) {
@@ -1254,12 +1228,12 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                                 break;
                             }
                         }
-                        if (lsuccess == true) {
+                        if (lsuccess) {
                             success = true;
                             break;
                         }
                     }
-                    if (success == false) {
+                    if (!success) {
                         throw new CloudRuntimeException("PrepareUnmanaged 
Failed due to some hosts are still in UP status after 5 Minutes, please try 
later ");
                     }
                 } finally {
@@ -1377,7 +1351,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         if (host.getType() == Host.Type.Routing) {
 
             final List<VMInstanceVO> vms = _vmDao.listByHostId(hostId);
-            if (vms.size() == 0) {
+            if (vms.isEmpty()) {
                 return true;
             }
 
@@ -1405,7 +1379,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                         String logMessage = String.format(
                                 "Unsupported 
host.maintenance.local.storage.strategy: %s. Please set a strategy according to 
the global settings description: "
                                         + "'Error', 'Migration', or 
'ForceStop'.",
-                                
HOST_MAINTENANCE_LOCAL_STRATEGY.value().toString());
+                                HOST_MAINTENANCE_LOCAL_STRATEGY.value());
                         s_logger.error(logMessage);
                         throw new CloudRuntimeException("There are active VMs 
using the host's local storage pool. Please stop all VMs on this host that use 
local storage.");
                     }
@@ -1461,13 +1435,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         ServiceOfferingVO offeringVO = 
serviceOfferingDao.findById(vm.getServiceOfferingId());
         final VirtualMachineProfile profile = new 
VirtualMachineProfileImpl(vm, null, offeringVO, null, null);
         plan.setMigrationPlan(true);
-        DeployDestination dest = null;
-        try {
-            dest = deploymentManager.planDeployment(profile, plan, new 
DeploymentPlanner.ExcludeList(), null);
-        } catch (InsufficientServerCapacityException e) {
-            throw new CloudRuntimeException(String.format("Maintenance failed, 
could not find deployment destination for VM [id=%s, name=%s].", vm.getId(), 
vm.getInstanceName()),
-                    e);
-        }
+        DeployDestination dest = getDeployDestination(vm, profile, plan);
         Host destHost = dest.getHost();
 
         try {
@@ -1479,6 +1447,20 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         }
     }
 
+    private DeployDestination getDeployDestination(VMInstanceVO vm, 
VirtualMachineProfile profile, DataCenterDeployment plan) {
+        DeployDestination dest;
+        try {
+            dest = deploymentManager.planDeployment(profile, plan, new 
DeploymentPlanner.ExcludeList(), null);
+        } catch (InsufficientServerCapacityException e) {
+            throw new CloudRuntimeException(String.format("Maintenance failed, 
could not find deployment destination for VM [id=%s, name=%s].", vm.getId(), 
vm.getInstanceName()),
+                    e);
+        }
+        if (dest == null) {
+            throw new CloudRuntimeException(String.format("Maintenance failed, 
could not find deployment destination for VM [id=%s, name=%s], using plan: 
%s.", vm.getId(), vm.getInstanceName(), plan));
+        }
+        return dest;
+    }
+
     @Override
     public boolean maintain(final long hostId) throws 
AgentUnavailableException {
         final Boolean result = propagateResourceEvent(hostId, 
ResourceState.Event.AdminAskMaintenance);
@@ -1526,15 +1508,15 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
         List<VMInstanceVO> migratingInVMs = _vmDao.findByHostInStates(hostId, 
State.Migrating);
 
-        if (migratingInVMs.size() > 0) {
+        if (!migratingInVMs.isEmpty()) {
             throw new CloudRuntimeException("Host contains incoming VMs 
migrating. Please wait for them to complete before putting to maintenance.");
         }
 
-        if (_vmDao.findByHostInStates(hostId, State.Starting, 
State.Stopping).size() > 0) {
+        if (!_vmDao.findByHostInStates(hostId, State.Starting, 
State.Stopping).isEmpty()) {
             throw new CloudRuntimeException("Host contains VMs in 
starting/stopping state. Please wait for them to complete before putting to 
maintenance.");
         }
 
-        if (_vmDao.findByHostInStates(hostId, State.Error, 
State.Unknown).size() > 0) {
+        if (!_vmDao.findByHostInStates(hostId, State.Error, 
State.Unknown).isEmpty()) {
             throw new CloudRuntimeException("Host contains VMs in 
error/unknown/shutdown state. Please fix errors to proceed.");
         }
 
@@ -1555,25 +1537,22 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         if(StringUtils.isBlank(HOST_MAINTENANCE_LOCAL_STRATEGY.value())) {
             return false;
         }
-        return 
HOST_MAINTENANCE_LOCAL_STRATEGY.value().toLowerCase().equals(WorkType.Migration.toString().toLowerCase());
+        return 
HOST_MAINTENANCE_LOCAL_STRATEGY.value().equalsIgnoreCase(WorkType.Migration.toString());
     }
 
     protected boolean isMaintenanceLocalStrategyForceStop() {
         if(StringUtils.isBlank(HOST_MAINTENANCE_LOCAL_STRATEGY.value())) {
             return false;
         }
-        return 
HOST_MAINTENANCE_LOCAL_STRATEGY.value().toLowerCase().equals(WorkType.ForceStop.toString().toLowerCase());
+        return 
HOST_MAINTENANCE_LOCAL_STRATEGY.value().equalsIgnoreCase(WorkType.ForceStop.toString());
     }
 
     /**
      * Returns true if the host.maintenance.local.storage.strategy is the 
Default: "Error", blank, empty, or null.
      */
     protected boolean isMaintenanceLocalStrategyDefault() {
-        if 
(StringUtils.isBlank(HOST_MAINTENANCE_LOCAL_STRATEGY.value().toString())
-                || 
HOST_MAINTENANCE_LOCAL_STRATEGY.value().toLowerCase().equals(State.Error.toString().toLowerCase()))
 {
-            return true;
-        }
-        return false;
+        return StringUtils.isBlank(HOST_MAINTENANCE_LOCAL_STRATEGY.value())
+                || 
HOST_MAINTENANCE_LOCAL_STRATEGY.value().equalsIgnoreCase(State.Error.toString());
     }
 
     /**
@@ -1640,7 +1619,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         HostVO host = _hostDao.findById(hostId);
 
         if (host == null || host.getRemoved() != null) {
-            throw new InvalidParameterValueException(String.format("Host 
[id=%s] does not exist", host.getId()));
+            throw new InvalidParameterValueException(String.format("Host 
[id=%s] does not exist", host == null ? "<unknown>" : host.getId()));
         }
 
         if (host.getResourceState() != ResourceState.Degraded) {
@@ -1725,7 +1704,6 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
      * Return true if host goes into Maintenance mode. There are various 
possibilities for VMs' states
      * on a host. We need to track the various VM states on each run and 
accordingly transit to the
      * appropriate state.
-     *
      * We change states as follows -
      * 1. If there are no VMs in running, migrating, starting, stopping, 
error, unknown states we can move
      *    to maintenance state. Note that there cannot be incoming migrations 
as the API Call prepare for
@@ -1899,7 +1877,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                 
guestOSDetail.setValue(String.valueOf(guestOSCategory.getId()));
                 _hostDetailsDao.update(guestOSDetail.getId(), guestOSDetail);
             } else {
-                final Map<String, String> detail = new HashMap<String, 
String>();
+                final Map<String, String> detail = new HashMap<>();
                 detail.put("guest.os.category.id", 
String.valueOf(guestOSCategory.getId()));
                 _hostDetailsDao.persist(hostId, detail);
             }
@@ -2049,9 +2027,9 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
     @Override
     public List<HypervisorType> getSupportedHypervisorTypes(final long zoneId, 
final boolean forVirtualRouter, final Long podId) {
-        final List<HypervisorType> hypervisorTypes = new 
ArrayList<HypervisorType>();
+        final List<HypervisorType> hypervisorTypes = new ArrayList<>();
 
-        List<ClusterVO> clustersForZone = new ArrayList<ClusterVO>();
+        List<ClusterVO> clustersForZone;
         if (podId != null) {
             clustersForZone = _clusterDao.listByPodId(podId);
         } else {
@@ -2060,7 +2038,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
         for (final ClusterVO cluster : clustersForZone) {
             final HypervisorType hType = cluster.getHypervisorType();
-            if (!forVirtualRouter || forVirtualRouter && hType != 
HypervisorType.BareMetal && hType != HypervisorType.Ovm) {
+            if (!forVirtualRouter || (hType != HypervisorType.BareMetal && 
hType != HypervisorType.Ovm)) {
                 hypervisorTypes.add(hType);
             }
         }
@@ -2096,7 +2074,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
         if (isValid) {
             final List<ClusterVO> clusters = 
_clusterDao.listByDcHyType(zoneId, defaultHyper.toString());
-            if (clusters.size() <= 0) {
+            if (clusters.isEmpty()) {
                 isValid = false;
             }
         }
@@ -2113,7 +2091,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         HypervisorType defaultHype = getDefaultHypervisor(zoneId);
         if (defaultHype == HypervisorType.None) {
             final List<HypervisorType> supportedHypes = 
getSupportedHypervisorTypes(zoneId, false, null);
-            if (supportedHypes.size() > 0) {
+            if (!supportedHypes.isEmpty()) {
                 Collections.shuffle(supportedHypes);
                 defaultHype = supportedHypes.get(0);
             }
@@ -2237,10 +2215,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         final String cidrNetmask = NetUtils.getCidrSubNet("255.255.255.255", 
cidrSize);
         final long cidrNetmaskNumeric = NetUtils.ip2Long(cidrNetmask);
         final long serverNetmaskNumeric = 
NetUtils.ip2Long(serverPrivateNetmask);
-        if (serverNetmaskNumeric > cidrNetmaskNumeric) {
-            return false;
-        }
-        return true;
+        return serverNetmaskNumeric <= cidrNetmaskNumeric;
     }
 
     private HostVO getNewHost(StartupCommand[] startupCommands) {
@@ -2254,11 +2229,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
         host = findHostByGuid(startupCommand.getGuidWithoutResource());
 
-        if (host != null) {
-            return host;
-        }
-
-        return null;
+        return host; // even when host == null!
     }
 
     protected HostVO createHostVO(final StartupCommand[] cmds, final 
ServerResource resource, final Map<String, String> details, List<String> 
hostTags,
@@ -2289,11 +2260,11 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             }
         }
 
-        long dcId = -1;
+        long dcId;
         DataCenterVO dc = _dcDao.findByName(dataCenter);
         if (dc == null) {
             try {
-                dcId = Long.parseLong(dataCenter);
+                dcId = Long.parseLong(dataCenter != null ? dataCenter : "-1");
                 dc = _dcDao.findById(dcId);
             } catch (final NumberFormatException e) {
                 s_logger.debug("Cannot parse " + dataCenter + " into Long.");
@@ -2307,7 +2278,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         HostPodVO p = _podDao.findByName(pod, dcId);
         if (p == null) {
             try {
-                final long podId = Long.parseLong(pod);
+                final long podId = Long.parseLong(pod != null ? pod : "-1");
                 p = _podDao.findById(podId);
             } catch (final NumberFormatException e) {
                 s_logger.debug("Cannot parse " + pod + " into Long.");
@@ -2326,9 +2297,9 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                 clusterId = Long.valueOf(cluster);
             } catch (final NumberFormatException e) {
                 if (podId != null) {
-                    ClusterVO c = _clusterDao.findBy(cluster, 
podId.longValue());
+                    ClusterVO c = _clusterDao.findBy(cluster, podId);
                     if (c == null) {
-                        c = new ClusterVO(dcId, podId.longValue(), cluster);
+                        c = new ClusterVO(dcId, podId, cluster);
                         c = _clusterDao.persist(c);
                     }
                     clusterId = c.getId();
@@ -2445,7 +2416,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         for (HostVO hostVO : hostVOs) {
             DetailVO hostDetailVO = _hostDetailsDao.findDetail(hostVO.getId(), 
name);
 
-            if (hostDetailVO == null || 
Boolean.parseBoolean(hostDetailVO.getValue()) == false) {
+            if (hostDetailVO == null || 
!Boolean.parseBoolean(hostDetailVO.getValue())) {
                 clusterSupportsResigning = false;
 
                 break;
@@ -2533,7 +2504,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             }
 
             if (s_logger.isDebugEnabled()) {
-                new Request(-1l, -1l, cmds, true, false).logD("Startup request 
from directly connected host: ", true);
+                new Request(-1L, -1L, cmds, true, false).logD("Startup request 
from directly connected host: ", true);
             }
 
             if (old) {
@@ -2603,7 +2574,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             }
 
             if (s_logger.isDebugEnabled()) {
-                new Request(-1l, -1l, cmds, true, false).logD("Startup request 
from directly connected host: ", true);
+                new Request(-1L, -1L, cmds, true, false).logD("Startup request 
from directly connected host: ", true);
             }
 
             if (old) {
@@ -2688,8 +2659,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
     @Override
     public Host createHostAndAgent(final Long hostId, final ServerResource 
resource, final Map<String, String> details, final boolean old, final 
List<String> hostTags, final boolean forRebalance) {
-        final Host host = createHostAndAgent(resource, details, old, hostTags, 
forRebalance);
-        return host;
+        return createHostAndAgent(resource, details, old, hostTags, 
forRebalance);
     }
 
     @Override
@@ -2699,8 +2669,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             throw new InvalidParameterValueException("Can't find zone with id 
" + zoneId);
         }
 
-        final Map<String, String> details = hostDetails;
-        final String guid = details.get("guid");
+        final String guid = hostDetails.get("guid");
         final List<HostVO> currentHosts = 
listAllUpAndEnabledHostsInOneZoneByType(hostType, zoneId);
         for (final HostVO currentHost : currentHosts) {
             if (currentHost.getGuid().equals(guid)) {
@@ -2716,7 +2685,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         return createHostVO(cmds, null, null, null, 
ResourceStateAdapter.Event.CREATE_HOST_VO_FOR_CONNECTED);
     }
 
-    private void checkIPConflicts(final HostPodVO pod, final DataCenterVO dc, 
final String serverPrivateIP, final String serverPrivateNetmask, final String 
serverPublicIP, final String serverPublicNetmask) {
+    private void checkIPConflicts(final HostPodVO pod, final DataCenterVO dc, 
final String serverPrivateIP, final String serverPublicIP) {
         // If the server's private IP is the same as is public IP, this host 
has
         // a host-only private network. Don't check for conflicts with the
         // private IP address table.
@@ -2745,7 +2714,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             // If the server's public IP address is already in the database,
             // return false
             final List<IPAddressVO> existingPublicIPs = 
_publicIPAddressDao.listByDcIdIpAddress(dc.getId(), serverPublicIP);
-            if (existingPublicIPs.size() > 0) {
+            if (!existingPublicIPs.isEmpty()) {
                 throw new IllegalArgumentException("The public ip address of 
the server (" + serverPublicIP + ") is already in use in zone: " + 
dc.getName());
             }
         }
@@ -2775,7 +2744,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
 
         final HostPodVO pod = _podDao.findById(host.getPodId());
         final DataCenterVO dc = _dcDao.findById(host.getDataCenterId());
-        checkIPConflicts(pod, dc, ssCmd.getPrivateIpAddress(), 
ssCmd.getPublicIpAddress(), ssCmd.getPublicIpAddress(), 
ssCmd.getPublicNetmask());
+        checkIPConflicts(pod, dc, ssCmd.getPrivateIpAddress(), 
ssCmd.getPublicIpAddress());
         host.setType(com.cloud.host.Host.Type.Routing);
         host.setDetails(details);
         host.setCaps(ssCmd.getCapabilities());
@@ -2812,8 +2781,8 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
                         throw new UnableDeleteHostException("Failed to set 
primary storage into maintenance mode");
                     }
                 } catch (final Exception e) {
-                    s_logger.debug("Failed to set primary storage into 
maintenance mode, due to: " + e.toString());
-                    throw new UnableDeleteHostException("Failed to set primary 
storage into maintenance mode, due to: " + e.toString());
+                    s_logger.debug("Failed to set primary storage into 
maintenance mode", e);
+                    throw new UnableDeleteHostException("Failed to set primary 
storage into maintenance mode, due to: " + e);
                 }
             }
 
@@ -2957,7 +2926,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
             if (result.getReturnCode() != 0) {
                 throw new CloudRuntimeException(String.format("Could not 
restart agent on %s due to: %s", host, result.getStdErr()));
             }
-            s_logger.debug("cloudstack-agent restart result: " + 
result.toString());
+            s_logger.debug("cloudstack-agent restart result: " + result);
         } catch (final SshException e) {
             throw new CloudRuntimeException("SSH to agent is enabled, but 
agent restart failed", e);
         }
@@ -2978,7 +2947,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     }
 
     @Override
-    public boolean executeUserRequest(final long hostId, final 
ResourceState.Event event) throws AgentUnavailableException {
+    public boolean executeUserRequest(final long hostId, final 
ResourceState.Event event) {
         if (event == ResourceState.Event.AdminAskMaintenance) {
             return doMaintain(hostId);
         } else if (event == ResourceState.Event.AdminCancelMaintenance) {
@@ -3292,7 +3261,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         // The search is not able to return list of enums, so getting
         // list of hypervisors as strings and then converting them to enum
         final List<String> hvs = _hostDao.customSearch(sc, null);
-        final List<HypervisorType> hypervisors = new 
ArrayList<HypervisorType>();
+        final List<HypervisorType> hypervisors = new ArrayList<>();
         for (final String hv : hvs) {
             hypervisors.add(HypervisorType.getType(hv));
         }
@@ -3317,7 +3286,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     public HostStats getHostStatistics(final long hostId) {
         final Answer answer = _agentMgr.easySend(hostId, new 
GetHostStatsCommand(_hostDao.findById(hostId).getGuid(), 
_hostDao.findById(hostId).getName(), hostId));
 
-        if (answer != null && answer instanceof UnsupportedAnswer) {
+        if (answer instanceof UnsupportedAnswer) {
             return null;
         }
 
@@ -3354,20 +3323,16 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     @Override
     public String getHostTags(final long hostId) {
         final List<String> hostTags = 
_hostTagsDao.getHostTags(hostId).parallelStream().map(HostTagVO::getTag).collect(Collectors.toList());
-        if (hostTags == null) {
-            return null;
-        } else {
-            return com.cloud.utils.StringUtils.listToCsvTags(hostTags);
-        }
+        return StringUtils.listToCsvTags(hostTags);
     }
 
     @Override
     public List<PodCluster> listByDataCenter(final long dcId) {
         final List<HostPodVO> pods = _podDao.listByDataCenterId(dcId);
-        final ArrayList<PodCluster> pcs = new ArrayList<PodCluster>();
+        final ArrayList<PodCluster> pcs = new ArrayList<>();
         for (final HostPodVO pod : pods) {
             final List<ClusterVO> clusters = 
_clusterDao.listByPodId(pod.getId());
-            if (clusters.size() == 0) {
+            if (clusters.isEmpty()) {
                 pcs.add(new PodCluster(pod, null));
             } else {
                 for (final ClusterVO cluster : clusters) {
@@ -3412,7 +3377,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     public boolean isHostGpuEnabled(final long hostId) {
         final SearchCriteria<HostGpuGroupsVO> sc = _gpuAvailability.create();
         sc.setParameters("hostId", hostId);
-        return _hostGpuGroupsDao.customSearch(sc, null).size() > 0 ? true : 
false;
+        return !_hostGpuGroupsDao.customSearch(sc, null).isEmpty();
     }
 
     @Override
@@ -3477,7 +3442,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
         // Update GPU group capacity
         final TransactionLegacy txn = TransactionLegacy.currentTxn();
         txn.start();
-        _hostGpuGroupsDao.persist(hostId, new 
ArrayList<String>(groupDetails.keySet()));
+        _hostGpuGroupsDao.persist(hostId, new 
ArrayList<>(groupDetails.keySet()));
         _vgpuTypesDao.persist(hostId, groupDetails);
         txn.commit();
     }
@@ -3485,7 +3450,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     @Override
     public HashMap<String, HashMap<String, VgpuTypesInfo>> 
getGPUStatistics(final HostVO host) {
         final Answer answer = _agentMgr.easySend(host.getId(), new 
GetGPUStatsCommand(host.getGuid(), host.getName()));
-        if (answer != null && answer instanceof UnsupportedAnswer) {
+        if (answer instanceof UnsupportedAnswer) {
             return null;
         }
         if (answer == null || !answer.getResult()) {
@@ -3526,7 +3491,7 @@ public class ResourceManagerImpl extends ManagerBase 
implements ResourceManager,
     @ActionEvent(eventType = EventTypes.EVENT_HOST_RESERVATION_RELEASE, 
eventDescription = "releasing host reservation", async = true)
     public boolean releaseHostReservation(final Long hostId) {
         try {
-            return Transaction.execute(new TransactionCallback<Boolean>() {
+            return Transaction.execute(new TransactionCallback<>() {
                 @Override
                 public Boolean doInTransaction(final TransactionStatus status) 
{
                     final PlannerHostReservationVO reservationEntry = 
_plannerHostReserveDao.findByHostId(hostId);


Reply via email to