Program: 105695
Project: 105695
Buggy source:
@Override @ActionEvent(eventType=EventTypes.EVENT_NIC_UPDATE,eventDescription="String_Node_Str",async=true) public UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd) throws InvalidParameterValueException, CloudRuntimeException {
  Long vmId=cmd.getVmId();
  Long nicId=cmd.getNicId();
  Account caller=CallContext.current().getCallingAccount();
  UserVmVO vmInstance=_vmDao.findById(vmId);
  if (vmInstance == null) {
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  NicVO nic=_nicDao.findById(nicId);
  if (nic == null) {
    throw new InvalidParameterValueException("String_Node_Str" + nicId);
  }
  NetworkVO network=_networkDao.findById(nic.getNetworkId());
  if (network == null) {
    throw new InvalidParameterValueException("String_Node_Str" + nic.getNetworkId());
  }
  _accountMgr.checkAccess(caller,null,true,vmInstance);
  DataCenterVO dc=_dcDao.findById(vmInstance.getDataCenterId());
  if (dc.getNetworkType() == DataCenter.NetworkType.Basic) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance.getDataCenterId() + "String_Node_Str");
  }
  Network existingdefaultnet=_networkModel.getDefaultNetworkForVm(vmId);
  if (nic.getInstanceId() != vmId) {
    throw new InvalidParameterValueException(nic + "String_Node_Str" + vmInstance);
  }
  if (nic.isDefaultNic()) {
    throw new CloudRuntimeException("String_Node_Str");
  }
  if ((vmInstance.getState() != State.Running) && (vmInstance.getState() != State.Stopped)) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str");
  }
  NicProfile existing=null;
  List<NicProfile> nicProfiles=_networkMgr.getNicProfiles(vmInstance);
  for (  NicProfile nicProfile : nicProfiles) {
    if (nicProfile.isDefaultNic() && nicProfile.getNetworkId() == existingdefaultnet.getId()) {
      existing=nicProfile;
      continue;
    }
  }
  if (existing == null) {
    s_logger.warn("String_Node_Str");
    throw new CloudRuntimeException("String_Node_Str");
  }
  Network oldDefaultNetwork=null;
  oldDefaultNetwork=_networkModel.getDefaultNetworkForVm(vmId);
  String oldNicIdString=Long.toString(_networkModel.getDefaultNic(vmId).getId());
  long oldNetworkOfferingId=-1L;
  if (oldDefaultNetwork != null) {
    oldNetworkOfferingId=oldDefaultNetwork.getNetworkOfferingId();
  }
  NicVO existingVO=_nicDao.findById(existing.id);
  Integer chosenID=nic.getDeviceId();
  Integer existingID=existing.getDeviceId();
  nic.setDefaultNic(true);
  nic.setDeviceId(existingID);
  existingVO.setDefaultNic(false);
  existingVO.setDeviceId(chosenID);
  nic=_nicDao.persist(nic);
  existingVO=_nicDao.persist(existingVO);
  Network newdefault=null;
  newdefault=_networkModel.getDefaultNetworkForVm(vmId);
  if (newdefault == null) {
    nic.setDefaultNic(false);
    nic.setDeviceId(chosenID);
    existingVO.setDefaultNic(true);
    existingVO.setDeviceId(existingID);
    nic=_nicDao.persist(nic);
    existingVO=_nicDao.persist(existingVO);
    newdefault=_networkModel.getDefaultNetworkForVm(vmId);
    if (newdefault.getId() == existingdefaultnet.getId()) {
      throw new CloudRuntimeException("String_Node_Str");
    }
    throw new CloudRuntimeException("String_Node_Str" + nic + "String_Node_Str");
  }
 else   if (newdefault.getId() == nic.getNetworkId()) {
    s_logger.debug("String_Node_Str" + network + "String_Node_Str"+ vmInstance);
    String nicIdString=Long.toString(nic.getId());
    long newNetworkOfferingId=network.getNetworkOfferingId();
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),oldNicIdString,oldNetworkOfferingId,null,1L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),nicIdString,newNetworkOfferingId,null,1L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),nicIdString,newNetworkOfferingId,null,0L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),oldNicIdString,oldNetworkOfferingId,null,0L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    return _vmDao.findById(vmInstance.getId());
  }
  throw new CloudRuntimeException("String_Node_Str" + newdefault.getId() + "String_Node_Str"+ nic.getNetworkId()+ "String_Node_Str");
}

Fixed version (ground truth):
@Override @ActionEvent(eventType=EventTypes.EVENT_NIC_UPDATE,eventDescription="String_Node_Str",async=true) public UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd) throws InvalidParameterValueException, CloudRuntimeException {
  Long vmId=cmd.getVmId();
  Long nicId=cmd.getNicId();
  Account caller=CallContext.current().getCallingAccount();
  UserVmVO vmInstance=_vmDao.findById(vmId);
  if (vmInstance == null) {
    throw new InvalidParameterValueException("String_Node_Str" + vmId);
  }
  NicVO nic=_nicDao.findById(nicId);
  if (nic == null) {
    throw new InvalidParameterValueException("String_Node_Str" + nicId);
  }
  NetworkVO network=_networkDao.findById(nic.getNetworkId());
  if (network == null) {
    throw new InvalidParameterValueException("String_Node_Str" + nic.getNetworkId());
  }
  _accountMgr.checkAccess(caller,null,true,vmInstance);
  DataCenterVO dc=_dcDao.findById(vmInstance.getDataCenterId());
  if (dc.getNetworkType() == DataCenter.NetworkType.Basic) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance.getDataCenterId() + "String_Node_Str");
  }
  Network existingdefaultnet=_networkModel.getDefaultNetworkForVm(vmId);
  if (nic.getInstanceId() != vmId) {
    throw new InvalidParameterValueException(nic + "String_Node_Str" + vmInstance);
  }
  if (nic.isDefaultNic()) {
    throw new CloudRuntimeException("String_Node_Str");
  }
  if ((vmInstance.getState() != State.Running) && (vmInstance.getState() != State.Stopped)) {
    throw new CloudRuntimeException("String_Node_Str" + vmInstance + "String_Node_Str");
  }
  NicProfile existing=null;
  List<NicProfile> nicProfiles=_networkMgr.getNicProfiles(vmInstance);
  for (  NicProfile nicProfile : nicProfiles) {
    if (nicProfile.isDefaultNic() && existingdefaultnet != null && nicProfile.getNetworkId() == existingdefaultnet.getId()) {
      existing=nicProfile;
    }
  }
  if (existing == null) {
    s_logger.warn("String_Node_Str");
    throw new CloudRuntimeException("String_Node_Str");
  }
  Network oldDefaultNetwork=null;
  oldDefaultNetwork=_networkModel.getDefaultNetworkForVm(vmId);
  String oldNicIdString=Long.toString(_networkModel.getDefaultNic(vmId).getId());
  long oldNetworkOfferingId=-1L;
  if (oldDefaultNetwork != null) {
    oldNetworkOfferingId=oldDefaultNetwork.getNetworkOfferingId();
  }
  NicVO existingVO=_nicDao.findById(existing.id);
  Integer chosenID=nic.getDeviceId();
  Integer existingID=existing.getDeviceId();
  nic.setDefaultNic(true);
  nic.setDeviceId(existingID);
  existingVO.setDefaultNic(false);
  existingVO.setDeviceId(chosenID);
  nic=_nicDao.persist(nic);
  existingVO=_nicDao.persist(existingVO);
  Network newdefault=null;
  newdefault=_networkModel.getDefaultNetworkForVm(vmId);
  if (newdefault == null) {
    nic.setDefaultNic(false);
    nic.setDeviceId(chosenID);
    existingVO.setDefaultNic(true);
    existingVO.setDeviceId(existingID);
    nic=_nicDao.persist(nic);
    _nicDao.persist(existingVO);
    newdefault=_networkModel.getDefaultNetworkForVm(vmId);
    if (newdefault.getId() == existingdefaultnet.getId()) {
      throw new CloudRuntimeException("String_Node_Str");
    }
    throw new CloudRuntimeException("String_Node_Str" + nic + "String_Node_Str");
  }
 else   if (newdefault.getId() == nic.getNetworkId()) {
    s_logger.debug("String_Node_Str" + network + "String_Node_Str"+ vmInstance);
    String nicIdString=Long.toString(nic.getId());
    long newNetworkOfferingId=network.getNetworkOfferingId();
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),oldNicIdString,oldNetworkOfferingId,null,1L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),nicIdString,newNetworkOfferingId,null,1L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),nicIdString,newNetworkOfferingId,null,0L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN,vmInstance.getAccountId(),vmInstance.getDataCenterId(),vmInstance.getId(),oldNicIdString,oldNetworkOfferingId,null,0L,VirtualMachine.class.getName(),vmInstance.getUuid(),vmInstance.isDisplay());
    return _vmDao.findById(vmInstance.getId());
  }
  throw new CloudRuntimeException("String_Node_Str" + newdefault.getId() + "String_Node_Str"+ nic.getNetworkId()+ "String_Node_Str");
}