Program: 107247
Project: 107247
Buggy source:
@Override @DB public synchronized StoragePoolVO preparePrimaryStorageForMaintenance(PreparePrimaryStorageForMaintenanceCmd cmd) throws ServerApiException {
  Long primaryStorageId=cmd.getId();
  Long userId=UserContext.current().getCallerUserId();
  boolean restart=true;
  StoragePoolVO primaryStorage=null;
  try {
    Transaction.currentTxn();
    primaryStorage=_storagePoolDao.acquireInLockTable(primaryStorageId);
    if (primaryStorage == null) {
      String msg="String_Node_Str";
      s_logger.error(msg);
      throw new ExecutionException(msg);
    }
    if (!primaryStorage.getStatus().equals(Status.Up) && !primaryStorage.getStatus().equals(Status.ErrorInMaintenance)) {
      throw new InvalidParameterValueException("String_Node_Str" + primaryStorageId + "String_Node_Str"+ primaryStorage.getStatus().toString());
    }
    primaryStorage.setStatus(Status.PrepareForMaintenance);
    _storagePoolDao.persist(primaryStorage);
    List<StoragePoolVO> upPools=_storagePoolDao.listPoolsByStatus(Status.Up);
    if (upPools == null || upPools.size() == 0) {
      restart=false;
    }
    List<VolumeVO> allVolumes=_volsDao.findByPoolId(primaryStorageId);
    for (    VolumeVO volume : allVolumes) {
      VMInstanceVO vmInstance=_vmInstanceDao.findById(volume.getInstanceId());
      if (vmInstance == null) {
        continue;
      }
      if (vmInstance.getState().equals(State.Running) || vmInstance.getState().equals(State.Starting)) {
        if (vmInstance.getType().equals(VirtualMachine.Type.ConsoleProxy)) {
          _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
          if (!_consoleProxyMgr.stopProxy(vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
 else           if (restart) {
            _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
            if (_consoleProxyMgr.startProxy(vmInstance.getId()) == null) {
              String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
              s_logger.warn(errorMsg);
              primaryStorage.setStatus(Status.ErrorInMaintenance);
              _storagePoolDao.persist(primaryStorage);
              throw new CloudRuntimeException(errorMsg);
            }
          }
        }
        if (vmInstance.getType().equals(VirtualMachine.Type.User)) {
          if (!_userVmMgr.stopVirtualMachine(userId,vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
        }
        if (vmInstance.getType().equals(VirtualMachine.Type.SecondaryStorageVm)) {
          if (!_secStorageMgr.stopSecStorageVm(vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
 else           if (restart) {
            if (_secStorageMgr.startSecStorageVm(vmInstance.getId()) == null) {
              String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
              s_logger.warn(errorMsg);
              primaryStorage.setStatus(Status.ErrorInMaintenance);
              _storagePoolDao.persist(primaryStorage);
              throw new CloudRuntimeException(errorMsg);
            }
          }
        }
        if (vmInstance.getType().equals(VirtualMachine.Type.DomainRouter)) {
          if (_routerMgr.stopRouterInternal(vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
 else           if (restart) {
            if (_routerMgr.stopRouterInternal(vmInstance.getId())) {
              String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
              s_logger.warn(errorMsg);
              primaryStorage.setStatus(Status.ErrorInMaintenance);
              _storagePoolDao.persist(primaryStorage);
              throw new CloudRuntimeException(errorMsg);
            }
          }
        }
      }
    }
    primaryStorage.setStatus(Status.Maintenance);
    _storagePoolDao.persist(primaryStorage);
    return _storagePoolDao.findById(primaryStorageId);
  }
 catch (  Exception e) {
    if (e instanceof ExecutionException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR,e.getMessage());
    }
    if (e instanceof InvalidParameterValueException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PARAM_ERROR,e.getMessage());
    }
    s_logger.error("String_Node_Str",e);
    throw new ServerApiException(BaseCmd.INTERNAL_ERROR,e.getMessage());
  }
 finally {
    _storagePoolDao.releaseFromLockTable(primaryStorage.getId());
  }
}

Fixed version (ground truth):
@Override @DB public synchronized StoragePoolVO preparePrimaryStorageForMaintenance(PreparePrimaryStorageForMaintenanceCmd cmd) throws ServerApiException {
  Long primaryStorageId=cmd.getId();
  Long userId=UserContext.current().getCallerUserId();
  boolean restart=true;
  StoragePoolVO primaryStorage=null;
  try {
    Transaction.currentTxn();
    primaryStorage=_storagePoolDao.acquireInLockTable(primaryStorageId);
    if (primaryStorage == null) {
      String msg="String_Node_Str";
      s_logger.error(msg);
      throw new ExecutionException(msg);
    }
    if (!primaryStorage.getStatus().equals(Status.Up) && !primaryStorage.getStatus().equals(Status.ErrorInMaintenance)) {
      throw new InvalidParameterValueException("String_Node_Str" + primaryStorageId + "String_Node_Str"+ primaryStorage.getStatus().toString());
    }
    primaryStorage.setStatus(Status.PrepareForMaintenance);
    _storagePoolDao.persist(primaryStorage);
    List<StoragePoolVO> upPools=_storagePoolDao.listPoolsByStatus(Status.Up);
    if (upPools == null || upPools.size() == 0) {
      restart=false;
    }
    List<VolumeVO> allVolumes=_volsDao.findByPoolId(primaryStorageId);
    for (    VolumeVO volume : allVolumes) {
      VMInstanceVO vmInstance=_vmInstanceDao.findById(volume.getInstanceId());
      if (vmInstance == null) {
        continue;
      }
      if (vmInstance.getState().equals(State.Running) || vmInstance.getState().equals(State.Starting)) {
        if (vmInstance.getType().equals(VirtualMachine.Type.ConsoleProxy)) {
          _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
          if (!_consoleProxyMgr.stopProxy(vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
 else           if (restart) {
            _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
            if (_consoleProxyMgr.startProxy(vmInstance.getId()) == null) {
              String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
              s_logger.warn(errorMsg);
              primaryStorage.setStatus(Status.ErrorInMaintenance);
              _storagePoolDao.persist(primaryStorage);
              throw new CloudRuntimeException(errorMsg);
            }
          }
        }
        if (vmInstance.getType().equals(VirtualMachine.Type.User)) {
          if (!_userVmMgr.stopVirtualMachine(userId,vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
        }
        if (vmInstance.getType().equals(VirtualMachine.Type.SecondaryStorageVm)) {
          if (!_secStorageMgr.stopSecStorageVm(vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
 else           if (restart) {
            if (_secStorageMgr.startSecStorageVm(vmInstance.getId()) == null) {
              String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
              s_logger.warn(errorMsg);
              primaryStorage.setStatus(Status.ErrorInMaintenance);
              _storagePoolDao.persist(primaryStorage);
              throw new CloudRuntimeException(errorMsg);
            }
          }
        }
        if (vmInstance.getType().equals(VirtualMachine.Type.DomainRouter)) {
          if (!_routerMgr.stopRouterInternal(vmInstance.getId())) {
            String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
            s_logger.warn(errorMsg);
            primaryStorage.setStatus(Status.ErrorInMaintenance);
            _storagePoolDao.persist(primaryStorage);
            throw new CloudRuntimeException(errorMsg);
          }
 else           if (restart) {
            if (!_routerMgr.stopRouterInternal(vmInstance.getId())) {
              String errorMsg="String_Node_Str" + vmInstance.getId() + "String_Node_Str";
              s_logger.warn(errorMsg);
              primaryStorage.setStatus(Status.ErrorInMaintenance);
              _storagePoolDao.persist(primaryStorage);
              throw new CloudRuntimeException(errorMsg);
            }
          }
        }
      }
    }
    primaryStorage.setStatus(Status.Maintenance);
    _storagePoolDao.persist(primaryStorage);
    return _storagePoolDao.findById(primaryStorageId);
  }
 catch (  Exception e) {
    if (e instanceof ExecutionException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR,e.getMessage());
    }
    if (e instanceof InvalidParameterValueException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PARAM_ERROR,e.getMessage());
    }
    s_logger.error("String_Node_Str",e);
    throw new ServerApiException(BaseCmd.INTERNAL_ERROR,e.getMessage());
  }
 finally {
    _storagePoolDao.releaseFromLockTable(primaryStorage.getId());
  }
}