Program: 106573
Project: 106573
Buggy source:
@Override @DB public SnapshotPolicyVO createPolicy(CreateSnapshotPolicyCmd cmd,Account policyOwner){
  Long volumeId=cmd.getVolumeId();
  VolumeVO volume=_volsDao.findById(cmd.getVolumeId());
  if (volume == null) {
    throw new InvalidParameterValueException("String_Node_Str" + volumeId);
  }
  _accountMgr.checkAccess(UserContext.current().getCaller(),null,true,volume);
  if (volume.getState() != Volume.State.Ready) {
    throw new InvalidParameterValueException("String_Node_Str" + volumeId + "String_Node_Str"+ Volume.State.Ready+ "String_Node_Str"+ volume.getState()+ "String_Node_Str");
  }
  if (volume.getTemplateId() != null) {
    VMTemplateVO template=_templateDao.findById(volume.getTemplateId());
    if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM) {
      throw new InvalidParameterValueException("String_Node_Str" + volumeId + "String_Node_Str");
    }
  }
  AccountVO owner=_accountDao.findById(volume.getAccountId());
  Long instanceId=volume.getInstanceId();
  if (instanceId != null) {
    if (_vmDao.findById(instanceId) == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  IntervalType intvType=DateUtil.IntervalType.getIntervalType(cmd.getIntervalType());
  if (intvType == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getIntervalType());
  }
  Type type=getSnapshotType(intvType);
  TimeZone timeZone=TimeZone.getTimeZone(cmd.getTimezone());
  String timezoneId=timeZone.getID();
  if (!timezoneId.equals(cmd.getTimezone())) {
    s_logger.warn("String_Node_Str" + timezoneId + "String_Node_Str"+ cmd.getTimezone());
  }
  try {
    DateUtil.getNextRunTime(intvType,cmd.getSchedule(),timezoneId,null);
  }
 catch (  Exception e) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getSchedule() + "String_Node_Str"+ cmd.getIntervalType());
  }
  if (cmd.getMaxSnaps() <= 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  int intervalMaxSnaps=type.getMax();
  if (cmd.getMaxSnaps() > intervalMaxSnaps) {
    throw new InvalidParameterValueException("String_Node_Str" + intervalMaxSnaps + "String_Node_Str"+ cmd.getIntervalType());
  }
  long accountLimit=_resourceLimitMgr.findCorrectResourceLimitForAccount(owner,ResourceType.snapshot);
  long domainLimit=_resourceLimitMgr.findCorrectResourceLimitForDomain(_domainMgr.getDomain(owner.getDomainId()),ResourceType.snapshot);
  int max=cmd.getMaxSnaps().intValue();
  if (owner.getType() != Account.ACCOUNT_TYPE_ADMIN && ((accountLimit != -1 && max > accountLimit) || (domainLimit != -1 && max > domainLimit))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  SnapshotPolicyVO policy=_snapshotPolicyDao.findOneByVolumeInterval(volumeId,intvType);
  if (policy == null) {
    policy=new SnapshotPolicyVO(volumeId,cmd.getSchedule(),timezoneId,intvType,cmd.getMaxSnaps());
    policy=_snapshotPolicyDao.persist(policy);
    _snapSchedMgr.scheduleNextSnapshotJob(policy);
  }
 else {
    try {
      policy=_snapshotPolicyDao.acquireInLockTable(policy.getId());
      policy.setSchedule(cmd.getSchedule());
      policy.setTimezone(timezoneId);
      policy.setInterval((short)intvType.ordinal());
      policy.setMaxSnaps(cmd.getMaxSnaps());
      policy.setActive(true);
      _snapshotPolicyDao.update(policy.getId(),policy);
    }
  finally {
      if (policy != null) {
        _snapshotPolicyDao.releaseFromLockTable(policy.getId());
      }
    }
  }
  return policy;
}

Fixed version (ground truth):
@Override @DB public SnapshotPolicyVO createPolicy(CreateSnapshotPolicyCmd cmd,Account policyOwner){
  Long volumeId=cmd.getVolumeId();
  VolumeVO volume=_volsDao.findById(cmd.getVolumeId());
  if (volume == null) {
    throw new InvalidParameterValueException("String_Node_Str" + volumeId);
  }
  _accountMgr.checkAccess(UserContext.current().getCaller(),null,true,volume);
  if (volume.getState() != Volume.State.Ready) {
    throw new InvalidParameterValueException("String_Node_Str" + volumeId + "String_Node_Str"+ Volume.State.Ready+ "String_Node_Str"+ volume.getState()+ "String_Node_Str");
  }
  if (volume.getTemplateId() != null) {
    VMTemplateVO template=_templateDao.findById(volume.getTemplateId());
    if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM) {
      throw new InvalidParameterValueException("String_Node_Str" + volumeId + "String_Node_Str");
    }
  }
  AccountVO owner=_accountDao.findById(volume.getAccountId());
  Long instanceId=volume.getInstanceId();
  if (instanceId != null) {
    if (_vmDao.findById(instanceId) == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  IntervalType intvType=DateUtil.IntervalType.getIntervalType(cmd.getIntervalType());
  if (intvType == null) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getIntervalType());
  }
  Type type=getSnapshotType(intvType);
  TimeZone timeZone=TimeZone.getTimeZone(cmd.getTimezone());
  String timezoneId=timeZone.getID();
  if (!timezoneId.equals(cmd.getTimezone())) {
    s_logger.warn("String_Node_Str" + timezoneId + "String_Node_Str"+ cmd.getTimezone());
  }
  try {
    DateUtil.getNextRunTime(intvType,cmd.getSchedule(),timezoneId,null);
  }
 catch (  Exception e) {
    throw new InvalidParameterValueException("String_Node_Str" + cmd.getSchedule() + "String_Node_Str"+ cmd.getIntervalType());
  }
  if (cmd.getMaxSnaps() <= 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  int intervalMaxSnaps=type.getMax();
  if (cmd.getMaxSnaps() > intervalMaxSnaps) {
    throw new InvalidParameterValueException("String_Node_Str" + intervalMaxSnaps + "String_Node_Str"+ cmd.getIntervalType());
  }
  long accountLimit=_resourceLimitMgr.findCorrectResourceLimitForAccount(owner,ResourceType.snapshot);
  long domainLimit=_resourceLimitMgr.findCorrectResourceLimitForDomain(_domainMgr.getDomain(owner.getDomainId()),ResourceType.snapshot);
  int max=cmd.getMaxSnaps().intValue();
  if (owner.getType() != Account.ACCOUNT_TYPE_ADMIN && ((accountLimit != -1 && max > accountLimit) || (domainLimit != -1 && max > domainLimit))) {
    String message="String_Node_Str";
    if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
      message="String_Node_Str";
    }
    throw new InvalidParameterValueException("String_Node_Str" + message + "String_Node_Str");
  }
  SnapshotPolicyVO policy=_snapshotPolicyDao.findOneByVolumeInterval(volumeId,intvType);
  if (policy == null) {
    policy=new SnapshotPolicyVO(volumeId,cmd.getSchedule(),timezoneId,intvType,cmd.getMaxSnaps());
    policy=_snapshotPolicyDao.persist(policy);
    _snapSchedMgr.scheduleNextSnapshotJob(policy);
  }
 else {
    try {
      policy=_snapshotPolicyDao.acquireInLockTable(policy.getId());
      policy.setSchedule(cmd.getSchedule());
      policy.setTimezone(timezoneId);
      policy.setInterval((short)intvType.ordinal());
      policy.setMaxSnaps(cmd.getMaxSnaps());
      policy.setActive(true);
      _snapshotPolicyDao.update(policy.getId(),policy);
    }
  finally {
      if (policy != null) {
        _snapshotPolicyDao.releaseFromLockTable(policy.getId());
      }
    }
  }
  return policy;
}