Program: 106699
Project: 106699
Buggy source:
@Override public List<VirtualRouter> addVirtualMachineIntoNetwork(Network network,NicProfile nic,VirtualMachineProfile<UserVm> profile,DeployDestination dest,ReservationContext context,List<DomainRouterVO> routers) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
  List<VirtualRouter> rets=new ArrayList<VirtualRouter>(routers.size());
  boolean sendPasswordAndVmData=true;
  boolean sendDnsDhcpData=true;
  _userVmDao.loadDetails((UserVmVO)profile.getVirtualMachine());
  DataCenter dc=dest.getDataCenter();
  String serviceOffering=_serviceOfferingDao.findByIdIncludingRemoved(profile.getServiceOfferingId()).getDisplayText();
  String zoneName=_dcDao.findById(network.getDataCenterId()).getName();
  boolean isZoneBasic=(dc.getNetworkType() == NetworkType.Basic);
  List<VirtualRouter> connectedRouters=new ArrayList<VirtualRouter>();
  List<VirtualRouter> disconnectedRouters=new ArrayList<VirtualRouter>();
  for (  DomainRouterVO router : routers) {
    if (router.getState() != State.Running) {
      s_logger.warn("String_Node_Str" + profile.getVirtualMachine() + "String_Node_Str"+ router+ "String_Node_Str");
      continue;
    }
    Long podId=null;
    if (isZoneBasic) {
      podId=dest.getPod().getId();
      if (router.getPodIdToDeployIn().longValue() != podId.longValue()) {
        sendPasswordAndVmData=false;
        if (_dnsBasicZoneUpdates.equalsIgnoreCase("String_Node_Str")) {
          sendDnsDhcpData=false;
        }
      }
    }
    Commands cmds=new Commands(OnError.Stop);
    String routerControlIpAddress=null;
    List<NicVO> nics=_nicDao.listByVmId(router.getId());
    for (    NicVO n : nics) {
      NetworkVO nc=_networkDao.findById(n.getNetworkId());
      if (nc.getTrafficType() == TrafficType.Control) {
        routerControlIpAddress=n.getIp4Address();
      }
    }
    if (sendDnsDhcpData) {
      DhcpEntryCommand dhcpCommand=new DhcpEntryCommand(nic.getMacAddress(),nic.getIp4Address(),profile.getVirtualMachine().getHostName());
      dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP,routerControlIpAddress);
      dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP,router.getGuestIpAddress());
      dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME,router.getInstanceName());
      dhcpCommand.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE,dc.getNetworkType().toString());
      cmds.addCommand("String_Node_Str",dhcpCommand);
    }
    if (sendPasswordAndVmData) {
      String password=(String)profile.getParameter(VirtualMachineProfile.Param.VmPassword);
      String userData=profile.getVirtualMachine().getUserData();
      String sshPublicKey=profile.getVirtualMachine().getDetail("String_Node_Str");
      if (password != null && network.isDefault()) {
        final String encodedPassword=PasswordGenerator.rot13(password);
        SavePasswordCommand cmd=new SavePasswordCommand(encodedPassword,nic.getIp4Address(),profile.getVirtualMachine().getHostName());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP,router.getPrivateIpAddress());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP,router.getGuestIpAddress());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME,router.getInstanceName());
        cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE,dc.getNetworkType().toString());
        cmds.addCommand("String_Node_Str",cmd);
      }
      cmds.addCommand("String_Node_Str",generateVmDataCommand(router,nic.getIp4Address(),userData,serviceOffering,zoneName,nic.getIp4Address(),profile.getVirtualMachine().getHostName(),profile.getVirtualMachine().getInstanceName(),profile.getId(),sshPublicKey));
    }
    if (cmds.size() > 0) {
      boolean podLevelException=false;
      if (isZoneBasic && podId != null && profile.getVirtualMachine().getType() == VirtualMachine.Type.User && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == GuestIpType.Direct) {
        podLevelException=true;
      }
      try {
        _agentMgr.send(router.getHostId(),cmds);
      }
 catch (      AgentUnavailableException e) {
        s_logger.warn("String_Node_Str" + router.getHostId(),e);
        disconnectedRouters.add(router);
        continue;
      }
catch (      OperationTimedoutException e) {
        s_logger.warn("String_Node_Str" + router.getHostId(),e);
        disconnectedRouters.add(router);
        continue;
      }
      connectedRouters.add(router);
      Answer answer=cmds.getAnswer("String_Node_Str");
      if (!answer.getResult()) {
        s_logger.error("String_Node_Str" + profile + "String_Node_Str"+ router.getHostName()+ "String_Node_Str"+ answer.getDetails());
        if (podLevelException) {
          throw new ResourceUnavailableException("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails(),Pod.class,podId);
        }
        throw new ResourceUnavailableException("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails(),DataCenter.class,router.getDataCenterIdToDeployIn());
      }
      answer=cmds.getAnswer("String_Node_Str");
      if (answer != null && !answer.getResult()) {
        s_logger.error("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails());
        if (podLevelException) {
          throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),Pod.class,podId);
        }
        throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),DataCenter.class,router.getDataCenterIdToDeployIn());
      }
      answer=cmds.getAnswer("String_Node_Str");
      if (answer != null && !answer.getResult()) {
        s_logger.error("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails());
        if (podLevelException) {
          throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),Pod.class,podId);
        }
        throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),DataCenter.class,router.getDataCenterIdToDeployIn());
      }
    }
    rets.add(router);
  }
  String msg="String_Node_Str";
  if (!connectedRouters.isEmpty()) {
    stopDisconnectedRouters(disconnectedRouters,true,msg);
  }
 else   if (!disconnectedRouters.isEmpty()) {
    for (    VirtualRouter router : disconnectedRouters) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(msg + router.getInstanceName() + "String_Node_Str"+ router.getId()+ "String_Node_Str");
      }
    }
    throw new ResourceUnavailableException(msg,VirtualRouter.class,disconnectedRouters.get(0).getId());
  }
  return rets;
}

Fixed version (ground truth):
@Override public List<VirtualRouter> addVirtualMachineIntoNetwork(Network network,NicProfile nic,VirtualMachineProfile<UserVm> profile,DeployDestination dest,ReservationContext context,List<DomainRouterVO> routers) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
  List<VirtualRouter> rets=new ArrayList<VirtualRouter>(routers.size());
  _userVmDao.loadDetails((UserVmVO)profile.getVirtualMachine());
  DataCenter dc=dest.getDataCenter();
  String serviceOffering=_serviceOfferingDao.findByIdIncludingRemoved(profile.getServiceOfferingId()).getDisplayText();
  String zoneName=_dcDao.findById(network.getDataCenterId()).getName();
  boolean isZoneBasic=(dc.getNetworkType() == NetworkType.Basic);
  List<VirtualRouter> connectedRouters=new ArrayList<VirtualRouter>();
  List<VirtualRouter> disconnectedRouters=new ArrayList<VirtualRouter>();
  for (  DomainRouterVO router : routers) {
    boolean sendPasswordAndVmData=true;
    boolean sendDnsDhcpData=true;
    if (router.getState() != State.Running) {
      s_logger.warn("String_Node_Str" + profile.getVirtualMachine() + "String_Node_Str"+ router+ "String_Node_Str");
      continue;
    }
    Long podId=null;
    if (isZoneBasic) {
      podId=dest.getPod().getId();
      if (router.getPodIdToDeployIn().longValue() != podId.longValue()) {
        sendPasswordAndVmData=false;
        if (_dnsBasicZoneUpdates.equalsIgnoreCase("String_Node_Str")) {
          sendDnsDhcpData=false;
        }
      }
    }
    Commands cmds=new Commands(OnError.Stop);
    String routerControlIpAddress=null;
    List<NicVO> nics=_nicDao.listByVmId(router.getId());
    for (    NicVO n : nics) {
      NetworkVO nc=_networkDao.findById(n.getNetworkId());
      if (nc.getTrafficType() == TrafficType.Control) {
        routerControlIpAddress=n.getIp4Address();
      }
    }
    if (sendDnsDhcpData) {
      DhcpEntryCommand dhcpCommand=new DhcpEntryCommand(nic.getMacAddress(),nic.getIp4Address(),profile.getVirtualMachine().getHostName());
      dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP,routerControlIpAddress);
      dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP,router.getGuestIpAddress());
      dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME,router.getInstanceName());
      dhcpCommand.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE,dc.getNetworkType().toString());
      cmds.addCommand("String_Node_Str",dhcpCommand);
    }
    if (sendPasswordAndVmData) {
      String password=(String)profile.getParameter(VirtualMachineProfile.Param.VmPassword);
      String userData=profile.getVirtualMachine().getUserData();
      String sshPublicKey=profile.getVirtualMachine().getDetail("String_Node_Str");
      if (password != null && network.isDefault()) {
        final String encodedPassword=PasswordGenerator.rot13(password);
        SavePasswordCommand cmd=new SavePasswordCommand(encodedPassword,nic.getIp4Address(),profile.getVirtualMachine().getHostName());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP,router.getPrivateIpAddress());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP,router.getGuestIpAddress());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME,router.getInstanceName());
        cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE,dc.getNetworkType().toString());
        cmds.addCommand("String_Node_Str",cmd);
      }
      cmds.addCommand("String_Node_Str",generateVmDataCommand(router,nic.getIp4Address(),userData,serviceOffering,zoneName,nic.getIp4Address(),profile.getVirtualMachine().getHostName(),profile.getVirtualMachine().getInstanceName(),profile.getId(),sshPublicKey));
    }
    if (cmds.size() > 0) {
      boolean podLevelException=false;
      if (isZoneBasic && podId != null && profile.getVirtualMachine().getType() == VirtualMachine.Type.User && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == GuestIpType.Direct) {
        podLevelException=true;
      }
      try {
        _agentMgr.send(router.getHostId(),cmds);
      }
 catch (      AgentUnavailableException e) {
        s_logger.warn("String_Node_Str" + router.getHostId(),e);
        disconnectedRouters.add(router);
        continue;
      }
catch (      OperationTimedoutException e) {
        s_logger.warn("String_Node_Str" + router.getHostId(),e);
        disconnectedRouters.add(router);
        continue;
      }
      connectedRouters.add(router);
      Answer answer=cmds.getAnswer("String_Node_Str");
      if (!answer.getResult()) {
        s_logger.error("String_Node_Str" + profile + "String_Node_Str"+ router.getHostName()+ "String_Node_Str"+ answer.getDetails());
        if (podLevelException) {
          throw new ResourceUnavailableException("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails(),Pod.class,podId);
        }
        throw new ResourceUnavailableException("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails(),DataCenter.class,router.getDataCenterIdToDeployIn());
      }
      answer=cmds.getAnswer("String_Node_Str");
      if (answer != null && !answer.getResult()) {
        s_logger.error("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails());
        if (podLevelException) {
          throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),Pod.class,podId);
        }
        throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),DataCenter.class,router.getDataCenterIdToDeployIn());
      }
      answer=cmds.getAnswer("String_Node_Str");
      if (answer != null && !answer.getResult()) {
        s_logger.error("String_Node_Str" + profile + "String_Node_Str"+ answer.getDetails());
        if (podLevelException) {
          throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),Pod.class,podId);
        }
        throw new ResourceUnavailableException("String_Node_Str" + answer.getDetails(),DataCenter.class,router.getDataCenterIdToDeployIn());
      }
    }
    rets.add(router);
  }
  String msg="String_Node_Str";
  if (!connectedRouters.isEmpty()) {
    stopDisconnectedRouters(disconnectedRouters,true,msg);
  }
 else   if (!disconnectedRouters.isEmpty()) {
    for (    VirtualRouter router : disconnectedRouters) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(msg + router.getInstanceName() + "String_Node_Str"+ router.getId()+ "String_Node_Str");
      }
    }
    throw new ResourceUnavailableException(msg,VirtualRouter.class,disconnectedRouters.get(0).getId());
  }
  return rets;
}