Program: 89186
Project: 89186
Buggy source:
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_NETWORK_UPDATE,eventDescription="String_Node_Str",async=true) public Network updateGuestNetwork(long networkId,String name,String displayText,Account callerAccount,User callerUser,String domainSuffix,Long networkOfferingId){
  boolean restartNetwork=false;
  NetworkVO network=_networksDao.findById(networkId);
  if (network == null) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId + "String_Node_Str");
  }
  if (network.getState() == Network.State.Destroy) {
    throw new InvalidParameterValueException("String_Node_Str" + Network.State.Destroy);
  }
  NetworkOffering offering=_networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
  if (offering.isSystemOnly()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (network.getTrafficType() != Networks.TrafficType.Guest) {
    throw new InvalidParameterValueException("String_Node_Str" + TrafficType.Guest);
  }
  _accountMgr.checkAccess(callerAccount,null,true,network);
  if (name != null) {
    network.setName(name);
  }
  if (displayText != null) {
    network.setDisplayText(displayText);
  }
  if ((networkOfferingId != null || domainSuffix != null) && network.getGuestType() != GuestType.Isolated) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  boolean networkOfferingChanged=false;
  long oldNetworkOfferingId=network.getNetworkOfferingId();
  if (networkOfferingId != null) {
    NetworkOfferingVO networkOffering=_networkOfferingDao.findById(networkOfferingId);
    if (networkOffering == null || networkOffering.isSystemOnly()) {
      throw new InvalidParameterValueException("String_Node_Str" + networkOfferingId);
    }
    if (networkOffering.getState() != NetworkOffering.State.Enabled) {
      throw new InvalidParameterValueException("String_Node_Str" + networkOffering + "String_Node_Str"+ NetworkOffering.State.Enabled+ "String_Node_Str");
    }
    if (networkOfferingId != oldNetworkOfferingId) {
      if (network.isSpecifiedCidr() && networkOfferingIsConfiguredForExternalNetworking(networkOfferingId)) {
        throw new InvalidParameterValueException("String_Node_Str" + networkOffering + "String_Node_Str");
      }
      if (!canUpgrade(network,oldNetworkOfferingId,networkOfferingId)) {
        throw new InvalidParameterValueException("String_Node_Str" + oldNetworkOfferingId + "String_Node_Str"+ networkOfferingId+ "String_Node_Str");
      }
      restartNetwork=true;
      networkOfferingChanged=true;
    }
  }
  Map<String,String> newSvcProviders=new HashMap<String,String>();
  if (networkOfferingChanged) {
    newSvcProviders=finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId),network.getPhysicalNetworkId());
  }
  if (domainSuffix != null) {
    if (!NetUtils.verifyDomainName(domainSuffix)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
    }
    long offeringId=oldNetworkOfferingId;
    if (networkOfferingId != null) {
      offeringId=networkOfferingId;
    }
    Map<Network.Capability,String> dnsCapabilities=getNetworkOfferingServiceCapabilities(_configMgr.getNetworkOffering(offeringId),Service.Dns);
    String isUpdateDnsSupported=dnsCapabilities.get(Capability.AllowDnsSuffixModification);
    if (isUpdateDnsSupported == null || !Boolean.valueOf(isUpdateDnsSupported)) {
      throw new InvalidParameterValueException("String_Node_Str" + networkOfferingId);
    }
    network.setNetworkDomain(domainSuffix);
    restartNetwork=true;
  }
  ReservationContext context=new ReservationContextImpl(null,null,callerUser,callerAccount);
  boolean validStateToShutdown=(network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup || network.getState() == Network.State.Allocated);
  if (restartNetwork) {
    if (validStateToShutdown) {
      s_logger.debug("String_Node_Str" + networkId + "String_Node_Str");
      if (!shutdownNetworkElementsAndResources(context,true,network)) {
        s_logger.warn("String_Node_Str" + network);
        throw new CloudRuntimeException("String_Node_Str" + network);
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str" + network + "String_Node_Str"+ network.getState());
    }
  }
  network=_networksDao.findById(networkId);
  boolean validStateToImplement=(network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup || network.getState() == Network.State.Allocated);
  if (restartNetwork && !validStateToImplement) {
    throw new CloudRuntimeException("String_Node_Str" + network + "String_Node_Str"+ network.getState());
  }
  if (networkOfferingId != null) {
    if (networkOfferingChanged) {
      Transaction txn=Transaction.currentTxn();
      txn.start();
      network.setNetworkOfferingId(networkOfferingId);
      _networksDao.update(networkId,network,newSvcProviders);
      List<NicVO> nics=_nicDao.listByNetworkId(networkId);
      for (      NicVO nic : nics) {
        long vmId=nic.getInstanceId();
        VMInstanceVO vm=_vmDao.findById(vmId);
        if (vm == null) {
          s_logger.error("String_Node_Str" + nic.getId() + "String_Node_Str"+ vmId);
          continue;
        }
        long isDefault=(nic.isDefaultNic()) ? 1 : 0;
        UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vm.getAccountId(),vm.getDataCenterIdToDeployIn(),vm.getId(),null,oldNetworkOfferingId,null,0L);
        _usageEventDao.persist(usageEvent);
        usageEvent=new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN,vm.getAccountId(),vm.getDataCenterIdToDeployIn(),vm.getId(),vm.getHostName(),networkOfferingId,null,isDefault);
        _usageEventDao.persist(usageEvent);
      }
      txn.commit();
    }
 else {
      network.setNetworkOfferingId(networkOfferingId);
      _networksDao.update(networkId,network,finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId),network.getPhysicalNetworkId()));
    }
  }
 else {
    _networksDao.update(networkId,network);
  }
  if (restartNetwork) {
    if (network.getState() != Network.State.Allocated) {
      DeployDestination dest=new DeployDestination(_dcDao.findById(network.getDataCenterId()),null,null,null);
      s_logger.debug("String_Node_Str" + network + "String_Node_Str");
      try {
        implementNetworkElementsAndResources(dest,context,network,_networkOfferingDao.findById(network.getNetworkOfferingId()));
      }
 catch (      Exception ex) {
        s_logger.warn("String_Node_Str" + network + "String_Node_Str",ex);
        throw new CloudRuntimeException("String_Node_Str" + network + "String_Node_Str");
      }
    }
  }
  return getNetwork(network.getId());
}

Fixed version (ground truth):
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_NETWORK_UPDATE,eventDescription="String_Node_Str",async=true) public Network updateGuestNetwork(long networkId,String name,String displayText,Account callerAccount,User callerUser,String domainSuffix,Long networkOfferingId){
  boolean restartNetwork=false;
  NetworkVO network=_networksDao.findById(networkId);
  if (network == null) {
    throw new InvalidParameterValueException("String_Node_Str" + networkId + "String_Node_Str");
  }
  if (network.getState() == Network.State.Destroy) {
    throw new InvalidParameterValueException("String_Node_Str" + Network.State.Destroy);
  }
  NetworkOffering offering=_networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId());
  if (offering.isSystemOnly()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (network.getTrafficType() != Networks.TrafficType.Guest) {
    throw new InvalidParameterValueException("String_Node_Str" + TrafficType.Guest);
  }
  _accountMgr.checkAccess(callerAccount,null,true,network);
  if (name != null) {
    network.setName(name);
  }
  if (displayText != null) {
    network.setDisplayText(displayText);
  }
  if ((networkOfferingId != null || domainSuffix != null) && network.getGuestType() != GuestType.Isolated) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  boolean networkOfferingChanged=false;
  long oldNetworkOfferingId=network.getNetworkOfferingId();
  if (networkOfferingId != null) {
    NetworkOfferingVO networkOffering=_networkOfferingDao.findById(networkOfferingId);
    if (networkOffering == null || networkOffering.isSystemOnly()) {
      throw new InvalidParameterValueException("String_Node_Str" + networkOfferingId);
    }
    if (networkOffering.getState() != NetworkOffering.State.Enabled) {
      throw new InvalidParameterValueException("String_Node_Str" + networkOffering + "String_Node_Str"+ NetworkOffering.State.Enabled+ "String_Node_Str");
    }
    if (networkOfferingId != oldNetworkOfferingId) {
      if (network.isSpecifiedCidr() && networkOfferingIsConfiguredForExternalNetworking(networkOfferingId)) {
        throw new InvalidParameterValueException("String_Node_Str" + networkOffering + "String_Node_Str");
      }
      if (!canUpgrade(network,oldNetworkOfferingId,networkOfferingId)) {
        throw new InvalidParameterValueException("String_Node_Str" + oldNetworkOfferingId + "String_Node_Str"+ networkOfferingId+ "String_Node_Str");
      }
      restartNetwork=true;
      networkOfferingChanged=true;
    }
  }
  Map<String,String> newSvcProviders=new HashMap<String,String>();
  if (networkOfferingChanged) {
    newSvcProviders=finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId),network.getPhysicalNetworkId());
  }
  if (domainSuffix != null) {
    if (!NetUtils.verifyDomainName(domainSuffix)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str");
    }
    long offeringId=oldNetworkOfferingId;
    if (networkOfferingId != null) {
      offeringId=networkOfferingId;
    }
    Map<Network.Capability,String> dnsCapabilities=getNetworkOfferingServiceCapabilities(_configMgr.getNetworkOffering(offeringId),Service.Dns);
    String isUpdateDnsSupported=dnsCapabilities.get(Capability.AllowDnsSuffixModification);
    if (isUpdateDnsSupported == null || !Boolean.valueOf(isUpdateDnsSupported)) {
      throw new InvalidParameterValueException("String_Node_Str" + networkOfferingId);
    }
    network.setNetworkDomain(domainSuffix);
    restartNetwork=true;
  }
  ReservationContext context=new ReservationContextImpl(null,null,callerUser,callerAccount);
  boolean validStateToShutdown=(network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup || network.getState() == Network.State.Allocated);
  if (restartNetwork) {
    if (validStateToShutdown) {
      s_logger.debug("String_Node_Str" + networkId + "String_Node_Str");
      if (!shutdownNetworkElementsAndResources(context,true,network)) {
        s_logger.warn("String_Node_Str" + network);
        throw new CloudRuntimeException("String_Node_Str" + network);
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str" + network + "String_Node_Str"+ network.getState());
    }
  }
  Network.State networkState=_networksDao.findById(networkId).getState();
  boolean validStateToImplement=(networkState == Network.State.Implemented || networkState == Network.State.Setup || networkState == Network.State.Allocated);
  if (restartNetwork && !validStateToImplement) {
    throw new CloudRuntimeException("String_Node_Str" + network + "String_Node_Str"+ networkState);
  }
  if (networkOfferingId != null) {
    if (networkOfferingChanged) {
      Transaction txn=Transaction.currentTxn();
      txn.start();
      network.setNetworkOfferingId(networkOfferingId);
      _networksDao.update(networkId,network,newSvcProviders);
      List<NicVO> nics=_nicDao.listByNetworkId(networkId);
      for (      NicVO nic : nics) {
        long vmId=nic.getInstanceId();
        VMInstanceVO vm=_vmDao.findById(vmId);
        if (vm == null) {
          s_logger.error("String_Node_Str" + nic.getId() + "String_Node_Str"+ vmId);
          continue;
        }
        long isDefault=(nic.isDefaultNic()) ? 1 : 0;
        UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_REMOVE,vm.getAccountId(),vm.getDataCenterIdToDeployIn(),vm.getId(),null,oldNetworkOfferingId,null,0L);
        _usageEventDao.persist(usageEvent);
        usageEvent=new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN,vm.getAccountId(),vm.getDataCenterIdToDeployIn(),vm.getId(),vm.getHostName(),networkOfferingId,null,isDefault);
        _usageEventDao.persist(usageEvent);
      }
      txn.commit();
    }
 else {
      network.setNetworkOfferingId(networkOfferingId);
      _networksDao.update(networkId,network,finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId),network.getPhysicalNetworkId()));
    }
  }
 else {
    _networksDao.update(networkId,network);
  }
  if (restartNetwork) {
    if (network.getState() != Network.State.Allocated) {
      DeployDestination dest=new DeployDestination(_dcDao.findById(network.getDataCenterId()),null,null,null);
      s_logger.debug("String_Node_Str" + network + "String_Node_Str");
      try {
        implementNetworkElementsAndResources(dest,context,network,_networkOfferingDao.findById(network.getNetworkOfferingId()));
      }
 catch (      Exception ex) {
        s_logger.warn("String_Node_Str" + network + "String_Node_Str",ex);
        throw new CloudRuntimeException("String_Node_Str" + network + "String_Node_Str");
      }
    }
  }
  return getNetwork(network.getId());
}