Program: 89148
Project: 89148
Buggy source:
@Override public List<VirtualRouter> applyUserData(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;
    if (router.getState() != State.Running) {
      s_logger.warn("String_Node_Str" + profile.getVirtualMachine() + "String_Node_Str"+ router+ "String_Node_Str");
      continue;
    }
    if (router.isStopPending()) {
      if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) {
        throw new ResourceUnavailableException("String_Node_Str" + router.getInstanceName() + "String_Node_Str",VirtualRouter.class,router.getId());
      }
      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;
      }
    }
    Commands cmds=new Commands(OnError.Stop);
    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 && nic.isDefaultNic()) {
        final String encodedPassword=PasswordGenerator.rot13(password);
        SavePasswordCommand cmd=new SavePasswordCommand(encodedPassword,nic.getIp4Address(),profile.getVirtualMachine().getHostName());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP,getRouterControlIp(router.getId()));
        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() == Network.GuestType.Shared) {
        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 != 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()) {
    handleSingleWorkingRedundantRouter(connectedRouters,disconnectedRouters,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> applyUserData(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>();
  Long podId=null;
  if (isZoneBasic) {
    podId=dest.getPod().getId();
  }
  boolean podLevelException=false;
  if (isZoneBasic && podId != null && profile.getVirtualMachine().getType() == VirtualMachine.Type.User && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Shared) {
    podLevelException=true;
  }
  for (  DomainRouterVO router : routers) {
    boolean sendPasswordAndVmData=true;
    if (router.getState() != State.Running) {
      s_logger.warn("String_Node_Str" + profile.getVirtualMachine() + "String_Node_Str"+ router+ "String_Node_Str");
      continue;
    }
    if (router.isStopPending()) {
      if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) {
        throw new ResourceUnavailableException("String_Node_Str" + router.getInstanceName() + "String_Node_Str",DataCenter.class,router.getDataCenterIdToDeployIn());
      }
      s_logger.warn("String_Node_Str" + profile.getVirtualMachine() + "String_Node_Str"+ router+ "String_Node_Str");
      continue;
    }
    if (isZoneBasic) {
      if (router.getPodIdToDeployIn().longValue() != podId.longValue()) {
        sendPasswordAndVmData=false;
      }
    }
    Commands cmds=new Commands(OnError.Stop);
    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 && nic.isDefaultNic()) {
        final String encodedPassword=PasswordGenerator.rot13(password);
        SavePasswordCommand cmd=new SavePasswordCommand(encodedPassword,nic.getIp4Address(),profile.getVirtualMachine().getHostName());
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP,getRouterControlIp(router.getId()));
        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) {
      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 != 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,dc.getId());
      }
      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,dc.getId());
      }
    }
    rets.add(router);
  }
  String msg="String_Node_Str";
  if (!connectedRouters.isEmpty()) {
    if (!isZoneBasic) {
      handleSingleWorkingRedundantRouter(connectedRouters,disconnectedRouters,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");
      }
    }
    if (podLevelException) {
      throw new ResourceUnavailableException(msg,Pod.class,podId);
    }
    throw new ResourceUnavailableException(msg,DataCenter.class,dc.getId());
  }
  return rets;
}