Program: 89106
Project: 89106
Buggy source:
@DB protected void CheckAndCreateTunnel(VirtualMachine instance,Network nw,DeployDestination dest){
  if (!_isEnabled) {
    return;
  }
  s_logger.debug("String_Node_Str");
  if (instance.getType() != VirtualMachine.Type.User && instance.getType() != VirtualMachine.Type.DomainRouter) {
    s_logger.debug("String_Node_Str");
    return;
  }
  long hostId=dest.getHost().getId();
  int key=getGreKey(nw);
  List<UserVmVO> vms=_userVmDao.listByNetworkIdAndStates(nw.getId(),State.Running,State.Starting,State.Stopping,State.Unknown,State.Migrating);
  List<DomainRouterVO> routers=_routerDao.findByNetwork(nw.getId());
  List<VMInstanceVO> ins=new ArrayList<VMInstanceVO>();
  if (vms != null) {
    ins.addAll(vms);
  }
  if (routers.size() != 0) {
    ins.addAll(routers);
  }
  s_logger.debug("String_Node_Str" + vms.size());
  s_logger.debug("String_Node_Str" + routers.size());
  List<Long> toHostIds=new ArrayList<Long>();
  List<Long> fromHostIds=new ArrayList<Long>();
  for (  VMInstanceVO v : ins) {
    Long rh=v.getHostId();
    if (rh == null || rh.longValue() == hostId) {
      continue;
    }
    OvsTunnelNetworkVO ta=_tunnelNetworkDao.getByFromToNetwork(hostId,rh.longValue(),nw.getId());
    if (ta == null || ta.getState().equals("String_Node_Str")) {
      s_logger.debug("String_Node_Str" + hostId + "String_Node_Str"+ rh.longValue());
      if (ta == null) {
        this.createTunnelRecord(hostId,rh.longValue(),nw.getId());
      }
      if (!toHostIds.contains(rh)) {
        toHostIds.add(rh);
      }
    }
    ta=_tunnelNetworkDao.getByFromToNetwork(rh.longValue(),hostId,nw.getId());
    if (ta == null || ta.getState().equals("String_Node_Str")) {
      s_logger.debug("String_Node_Str" + rh.longValue() + "String_Node_Str"+ hostId);
      if (ta == null) {
        this.createTunnelRecord(rh.longValue(),hostId,nw.getId());
      }
      if (!fromHostIds.contains(rh)) {
        fromHostIds.add(rh);
      }
    }
  }
  try {
    String myIp=getGreEndpointIP(dest.getHost(),nw);
    boolean noHost=true;
    for (    Long i : toHostIds) {
      HostVO rHost=_hostDao.findById(i);
      String otherIp=getGreEndpointIP(rHost,nw);
      Commands cmds=new Commands(new OvsCreateTunnelCommand(otherIp,key,Long.valueOf(hostId),i,nw.getId(),myIp));
      s_logger.debug("String_Node_Str" + hostId + "String_Node_Str"+ i);
      Answer[] answers=_agentMgr.send(hostId,cmds);
      handleCreateTunnelAnswer(answers);
      noHost=false;
    }
    for (    Long i : fromHostIds) {
      HostVO rHost=_hostDao.findById(i);
      String otherIp=getGreEndpointIP(rHost,nw);
      Commands cmds=new Commands(new OvsCreateTunnelCommand(myIp,key,i,Long.valueOf(hostId),nw.getId(),otherIp));
      s_logger.debug("String_Node_Str" + i + "String_Node_Str"+ hostId);
      Answer[] answers=_agentMgr.send(i,cmds);
      handleCreateTunnelAnswer(answers);
      noHost=false;
    }
    if (noHost) {
      Commands cmds=new Commands(new OvsSetupBridgeCommand(key,hostId,nw.getId()));
      s_logger.debug("String_Node_Str" + hostId + "String_Node_Str"+ nw.getId());
      Answer[] answers=_agentMgr.send(hostId,cmds);
      handleSetupBridgeAnswer(answers);
    }
  }
 catch (  Exception e) {
    s_logger.debug("String_Node_Str",e);
  }
}

Fixed version (ground truth):
@DB protected void CheckAndCreateTunnel(VirtualMachine instance,Network nw,DeployDestination dest){
  if (!_isEnabled) {
    return;
  }
  s_logger.debug("String_Node_Str");
  if (instance.getType() != VirtualMachine.Type.User && instance.getType() != VirtualMachine.Type.DomainRouter) {
    s_logger.debug("String_Node_Str");
    return;
  }
  long hostId=dest.getHost().getId();
  int key=getGreKey(nw);
  List<UserVmVO> vms=_userVmDao.listByNetworkIdAndStates(nw.getId(),State.Running,State.Starting,State.Stopping,State.Unknown,State.Migrating);
  List<DomainRouterVO> routers=_routerDao.findByNetwork(nw.getId());
  List<VMInstanceVO> ins=new ArrayList<VMInstanceVO>();
  if (vms != null) {
    ins.addAll(vms);
  }
  if (routers.size() != 0) {
    ins.addAll(routers);
  }
  s_logger.debug("String_Node_Str" + vms.size());
  s_logger.debug("String_Node_Str" + routers.size());
  List<Long> toHostIds=new ArrayList<Long>();
  List<Long> fromHostIds=new ArrayList<Long>();
  for (  VMInstanceVO v : ins) {
    Long rh=v.getHostId();
    if (rh == null || rh.longValue() == hostId) {
      continue;
    }
    OvsTunnelNetworkVO ta=_tunnelNetworkDao.getByFromToNetwork(hostId,rh.longValue(),nw.getId());
    if (ta == null || ta.getState().equals("String_Node_Str")) {
      s_logger.debug("String_Node_Str" + hostId + "String_Node_Str"+ rh.longValue());
      if (ta == null) {
        this.createTunnelRecord(hostId,rh.longValue(),nw.getId(),key);
      }
      if (!toHostIds.contains(rh)) {
        toHostIds.add(rh);
      }
    }
    ta=_tunnelNetworkDao.getByFromToNetwork(rh.longValue(),hostId,nw.getId());
    if (ta == null || ta.getState().equals("String_Node_Str")) {
      s_logger.debug("String_Node_Str" + rh.longValue() + "String_Node_Str"+ hostId);
      if (ta == null) {
        this.createTunnelRecord(rh.longValue(),hostId,nw.getId(),key);
      }
      if (!fromHostIds.contains(rh)) {
        fromHostIds.add(rh);
      }
    }
  }
  try {
    String myIp=getGreEndpointIP(dest.getHost(),nw);
    boolean noHost=true;
    for (    Long i : toHostIds) {
      HostVO rHost=_hostDao.findById(i);
      String otherIp=getGreEndpointIP(rHost,nw);
      Commands cmds=new Commands(new OvsCreateTunnelCommand(otherIp,key,Long.valueOf(hostId),i,nw.getId(),myIp));
      s_logger.debug("String_Node_Str" + hostId + "String_Node_Str"+ i);
      Answer[] answers=_agentMgr.send(hostId,cmds);
      handleCreateTunnelAnswer(answers);
      noHost=false;
    }
    for (    Long i : fromHostIds) {
      HostVO rHost=_hostDao.findById(i);
      String otherIp=getGreEndpointIP(rHost,nw);
      Commands cmds=new Commands(new OvsCreateTunnelCommand(myIp,key,i,Long.valueOf(hostId),nw.getId(),otherIp));
      s_logger.debug("String_Node_Str" + i + "String_Node_Str"+ hostId);
      Answer[] answers=_agentMgr.send(i,cmds);
      handleCreateTunnelAnswer(answers);
      noHost=false;
    }
    if (noHost) {
      Commands cmds=new Commands(new OvsSetupBridgeCommand(key,hostId,nw.getId()));
      s_logger.debug("String_Node_Str" + hostId + "String_Node_Str"+ nw.getId());
      Answer[] answers=_agentMgr.send(hostId,cmds);
      handleSetupBridgeAnswer(answers);
    }
  }
 catch (  Exception e) {
    s_logger.debug("String_Node_Str",e);
  }
}