Program: 105702
Project: 105702
Buggy source:
private boolean resetVMSSHKeyInternal(Long vmId,String sshPublicKey,String password) throws ResourceUnavailableException, InsufficientCapacityException {
  Long userId=CallContext.current().getCallingUserId();
  VMInstanceVO vmInstance=_vmDao.findById(vmId);
  VMTemplateVO template=_templateDao.findByIdIncludingRemoved(vmInstance.getTemplateId());
  Nic defaultNic=_networkModel.getDefaultNic(vmId);
  if (defaultNic == null) {
    s_logger.error("String_Node_Str" + vmInstance + "String_Node_Str");
    return false;
  }
  Network defaultNetwork=_networkDao.findById(defaultNic.getNetworkId());
  NicProfile defaultNicProfile=new NicProfile(defaultNic,defaultNetwork,null,null,null,_networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork),_networkModel.getNetworkTag(template.getHypervisorType(),defaultNetwork));
  VirtualMachineProfile vmProfile=new VirtualMachineProfileImpl(vmInstance);
  if (template != null && template.getEnablePassword()) {
    vmProfile.setParameter(VirtualMachineProfile.Param.VmPassword,password);
  }
  UserDataServiceProvider element=_networkMgr.getSSHKeyResetProvider(defaultNetwork);
  if (element == null) {
    throw new CloudRuntimeException("String_Node_Str" + Service.UserData.getName() + "String_Node_Str");
  }
  boolean result=element.saveSSHKey(defaultNetwork,defaultNicProfile,vmProfile,sshPublicKey);
  if (!result) {
    s_logger.debug("String_Node_Str");
    return false;
  }
 else {
    if (vmInstance.getState() == State.Stopped) {
      s_logger.debug("String_Node_Str" + vmInstance + "String_Node_Str");
      return true;
    }
    if (rebootVirtualMachine(userId,vmId) == null) {
      s_logger.warn("String_Node_Str" + vmInstance);
      return false;
    }
 else {
      s_logger.debug("String_Node_Str" + vmInstance + "String_Node_Str");
      return true;
    }
  }
}

Fixed version (ground truth):
private boolean resetVMSSHKeyInternal(Long vmId,String sshPublicKey,String password) throws ResourceUnavailableException, InsufficientCapacityException {
  Long userId=CallContext.current().getCallingUserId();
  VMInstanceVO vmInstance=_vmDao.findById(vmId);
  VMTemplateVO template=_templateDao.findByIdIncludingRemoved(vmInstance.getTemplateId());
  Nic defaultNic=_networkModel.getDefaultNic(vmId);
  if (defaultNic == null) {
    s_logger.error("String_Node_Str" + vmInstance + "String_Node_Str");
    return false;
  }
  Network defaultNetwork=_networkDao.findById(defaultNic.getNetworkId());
  NicProfile defaultNicProfile=new NicProfile(defaultNic,defaultNetwork,null,null,null,_networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork),_networkModel.getNetworkTag(template.getHypervisorType(),defaultNetwork));
  VirtualMachineProfile vmProfile=new VirtualMachineProfileImpl(vmInstance);
  if (template.getEnablePassword()) {
    vmProfile.setParameter(VirtualMachineProfile.Param.VmPassword,password);
  }
  UserDataServiceProvider element=_networkMgr.getSSHKeyResetProvider(defaultNetwork);
  if (element == null) {
    throw new CloudRuntimeException("String_Node_Str" + Service.UserData.getName() + "String_Node_Str");
  }
  boolean result=element.saveSSHKey(defaultNetwork,defaultNicProfile,vmProfile,sshPublicKey);
  if (!result) {
    s_logger.debug("String_Node_Str");
    return false;
  }
 else {
    if (vmInstance.getState() == State.Stopped) {
      s_logger.debug("String_Node_Str" + vmInstance + "String_Node_Str");
      return true;
    }
    if (rebootVirtualMachine(userId,vmId) == null) {
      s_logger.warn("String_Node_Str" + vmInstance);
      return false;
    }
 else {
      s_logger.debug("String_Node_Str" + vmInstance + "String_Node_Str");
      return true;
    }
  }
}