Program: 107398
Project: 107398
Buggy source:
@Override @DB public synchronized StoragePoolVO preparePrimaryStorageForMaintenance(PreparePrimaryStorageForMaintenanceCmd cmd) throws ServerApiException {
  Long primaryStorageId=cmd.getId();
  Long userId=UserContext.current().getUserId();
  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 ResourceUnavailableException(msg);
    }
    primaryStorage=_storagePoolDao.findById(primaryStorageId);
    if (primaryStorage == null) {
      s_logger.warn("String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str");
    }
    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.Stopped) || vmInstance.getState().equals(State.Stopping)|| vmInstance.getState().equals(State.Starting)) {
        if (vmInstance.getType().equals(VirtualMachine.Type.ConsoleProxy)) {
          _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
          long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_PROXY_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_consoleProxyMgr.stopProxy(vmInstance.getId(),eventId)) {
            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) {
            long eventId1=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_PROXY_START,"String_Node_Str" + vmInstance.getId());
            _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
            if (_consoleProxyMgr.startProxy(vmInstance.getId(),eventId1) == 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)) {
          long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_VM_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_userVmMgr.stopVirtualMachine(userId,vmInstance.getId(),eventId)) {
            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)) {
          long eventId1=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_SSVM_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_secStorageMgr.stopSecStorageVm(vmInstance.getId(),eventId1)) {
            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) {
            long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_SSVM_START,"String_Node_Str" + vmInstance.getId());
            if (_secStorageMgr.startSecStorageVm(vmInstance.getId(),eventId) == 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)) {
          long eventId2=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_ROUTER_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_networkMgr.stopRouter(vmInstance.getId(),eventId2)) {
            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) {
            long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_PROXY_START,"String_Node_Str" + vmInstance.getId());
            if (_networkMgr.startRouter(vmInstance.getId(),eventId) == 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);
            }
          }
        }
      }
    }
    primaryStorage.setStatus(Status.Maintenance);
    _storagePoolDao.persist(primaryStorage);
    return _storagePoolDao.findById(primaryStorageId);
  }
 catch (  Exception e) {
    if (e instanceof ResourceUnavailableException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
    }
    if (e instanceof InvalidParameterValueException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
    }
    if (e instanceof CloudRuntimeException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
    }
    s_logger.error("String_Node_Str",e);
    throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_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().getUserId();
  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 ResourceUnavailableException(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");
          long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_PROXY_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_consoleProxyMgr.stopProxy(vmInstance.getId(),eventId)) {
            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) {
            long eventId1=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_PROXY_START,"String_Node_Str" + vmInstance.getId());
            _configMgr.updateConfiguration(userId,"String_Node_Str","String_Node_Str");
            if (_consoleProxyMgr.startProxy(vmInstance.getId(),eventId1) == 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)) {
          long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_VM_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_userVmMgr.stopVirtualMachine(userId,vmInstance.getId(),eventId)) {
            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)) {
          long eventId1=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_SSVM_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_secStorageMgr.stopSecStorageVm(vmInstance.getId(),eventId1)) {
            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) {
            long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_SSVM_START,"String_Node_Str" + vmInstance.getId());
            if (_secStorageMgr.startSecStorageVm(vmInstance.getId(),eventId) == 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)) {
          long eventId2=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_ROUTER_STOP,"String_Node_Str" + vmInstance.getId());
          if (!_networkMgr.stopRouter(vmInstance.getId(),eventId2)) {
            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) {
            long eventId=saveScheduledEvent(User.UID_SYSTEM,Account.ACCOUNT_ID_SYSTEM,EventTypes.EVENT_PROXY_START,"String_Node_Str" + vmInstance.getId());
            if (_networkMgr.startRouter(vmInstance.getId(),eventId) == 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);
            }
          }
        }
      }
    }
    primaryStorage.setStatus(Status.Maintenance);
    _storagePoolDao.persist(primaryStorage);
    return _storagePoolDao.findById(primaryStorageId);
  }
 catch (  Exception e) {
    if (e instanceof ResourceUnavailableException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
    }
    if (e instanceof InvalidParameterValueException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
    }
    if (e instanceof CloudRuntimeException) {
      s_logger.error("String_Node_Str",e);
      throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
    }
    s_logger.error("String_Node_Str",e);
    throw new ServerApiException(BaseCmd.PREPARE_STORAGE_MAINTENANCE_ERROR,e.getMessage());
  }
 finally {
    _storagePoolDao.releaseFromLockTable(primaryStorage.getId());
  }
}