Program: 106299
Project: 106299
Buggy source:
@Override public Map<? extends ServerResource,Map<String,String>> find(long dcId,Long podId,Long clusterId,URI url,String username,String password,List<String> hostTags) throws DiscoveryException {
  if (s_logger.isInfoEnabled())   s_logger.info("String_Node_Str" + dcId + "String_Node_Str"+ podId+ "String_Node_Str"+ clusterId+ "String_Node_Str"+ url.getHost());
  if (podId == null) {
    if (s_logger.isInfoEnabled())     s_logger.info("String_Node_Str");
    return null;
  }
  ClusterVO cluster=_clusterDao.findById(clusterId);
  if (cluster == null || cluster.getHypervisorType() != HypervisorType.VMware) {
    if (s_logger.isInfoEnabled())     s_logger.info("String_Node_Str");
    return null;
  }
  List<HostVO> hosts=_resourceMgr.listAllHostsInCluster(clusterId);
  if (hosts != null && hosts.size() > 0) {
    int maxHostsPerCluster=_hvCapabilitiesDao.getMaxHostsPerCluster(hosts.get(0).getHypervisorType(),hosts.get(0).getHypervisorVersion());
    if (hosts.size() >= maxHostsPerCluster) {
      String msg="String_Node_Str" + cluster.getName() + "String_Node_Str"+ hosts.size()+ "String_Node_Str"+ maxHostsPerCluster;
      s_logger.error(msg);
      throw new DiscoveredWithErrorException(msg);
    }
  }
  String privateTrafficLabel=null;
  String publicTrafficLabel=null;
  String guestTrafficLabel=null;
  Map<String,String> vsmCredentials=null;
  VirtualSwitchType defaultVirtualSwitchType=VirtualSwitchType.StandardVirtualSwitch;
  String paramGuestVswitchType=null;
  String paramGuestVswitchName=null;
  String paramPublicVswitchType=null;
  String paramPublicVswitchName=null;
  VmwareTrafficLabel guestTrafficLabelObj=new VmwareTrafficLabel(TrafficType.Guest);
  VmwareTrafficLabel publicTrafficLabelObj=new VmwareTrafficLabel(TrafficType.Public);
  Map<String,String> clusterDetails=_clusterDetailsDao.findDetails(clusterId);
  DataCenterVO zone=_dcDao.findById(dcId);
  NetworkType zoneType=zone.getNetworkType();
  _readGlobalConfigParameters();
  if (useDVS) {
    paramGuestVswitchType=_urlParams.get(ApiConstants.VSWITCH_TYPE_GUEST_TRAFFIC);
    paramGuestVswitchName=_urlParams.get(ApiConstants.VSWITCH_NAME_GUEST_TRAFFIC);
    paramPublicVswitchType=_urlParams.get(ApiConstants.VSWITCH_TYPE_PUBLIC_TRAFFIC);
    paramPublicVswitchName=_urlParams.get(ApiConstants.VSWITCH_NAME_PUBLIC_TRAFFIC);
    defaultVirtualSwitchType=getDefaultVirtualSwitchType();
  }
  guestTrafficLabel=_netmgr.getDefaultGuestTrafficLabel(dcId,HypervisorType.VMware);
  guestTrafficLabelObj=getTrafficInfo(TrafficType.Guest,guestTrafficLabel,defaultVirtualSwitchType,paramGuestVswitchType,paramGuestVswitchName,clusterId);
  if (zoneType == NetworkType.Advanced) {
    publicTrafficLabel=_netmgr.getDefaultPublicTrafficLabel(dcId,HypervisorType.VMware);
    publicTrafficLabelObj=getTrafficInfo(TrafficType.Public,publicTrafficLabel,defaultVirtualSwitchType,paramPublicVswitchType,paramPublicVswitchName,clusterId);
    List<? extends PhysicalNetwork> pNetworkListGuestTraffic=_netmgr.getPhysicalNtwksSupportingTrafficType(dcId,TrafficType.Guest);
    List<? extends PhysicalNetwork> pNetworkListPublicTraffic=_netmgr.getPhysicalNtwksSupportingTrafficType(dcId,TrafficType.Public);
    PhysicalNetwork pNetworkPublic=pNetworkListPublicTraffic.get(0);
    if (pNetworkListGuestTraffic.contains(pNetworkPublic)) {
      if (publicTrafficLabelObj.getVirtualSwitchType() != guestTrafficLabelObj.getVirtualSwitchType()) {
        String msg="String_Node_Str" + pNetworkPublic + "String_Node_Str"+ "String_Node_Str";
        s_logger.error(msg);
        throw new InvalidParameterValueException(msg);
      }
    }
  }
 else {
    if (guestTrafficLabelObj.getVirtualSwitchType() != VirtualSwitchType.StandardVirtualSwitch) {
      String msg="String_Node_Str";
      s_logger.error(msg);
      throw new DiscoveredWithErrorException(msg);
    }
  }
  privateTrafficLabel=_netmgr.getDefaultManagementTrafficLabel(dcId,HypervisorType.VMware);
  if (privateTrafficLabel != null) {
    s_logger.info("String_Node_Str" + privateTrafficLabel);
  }
  if (nexusDVS) {
    if (zoneType != NetworkType.Basic) {
      publicTrafficLabel=_netmgr.getDefaultPublicTrafficLabel(dcId,HypervisorType.VMware);
      if (publicTrafficLabel != null) {
        s_logger.info("String_Node_Str" + publicTrafficLabel);
      }
    }
    guestTrafficLabel=_netmgr.getDefaultGuestTrafficLabel(dcId,HypervisorType.VMware);
    if (guestTrafficLabel != null) {
      s_logger.info("String_Node_Str" + guestTrafficLabel);
    }
    String vsmIp=_urlParams.get("String_Node_Str");
    String vsmUser=_urlParams.get("String_Node_Str");
    String vsmPassword=_urlParams.get("String_Node_Str");
    String clusterName=cluster.getName();
    try {
      _nexusElement.validateVsmCluster(vsmIp,vsmUser,vsmPassword,clusterId,clusterName);
    }
 catch (    ResourceInUseException ex) {
      DiscoveryException discEx=new DiscoveryException(ex.getLocalizedMessage() + "String_Node_Str" + ex.getResourceName());
      throw discEx;
    }
    vsmCredentials=_vmwareMgr.getNexusVSMCredentialsByClusterId(clusterId);
  }
  VmwareContext context=null;
  try {
    context=VmwareContextFactory.create(url.getHost(),username,password);
    if (privateTrafficLabel != null)     context.registerStockObject("String_Node_Str",privateTrafficLabel);
    if (nexusDVS) {
      if (vsmCredentials != null) {
        s_logger.info("String_Node_Str");
        context.registerStockObject("String_Node_Str",vsmCredentials);
      }
    }
    List<ManagedObjectReference> morHosts=_vmwareMgr.addHostToPodCluster(context,dcId,podId,clusterId,URLDecoder.decode(url.getPath()));
    if (morHosts == null)     s_logger.info("String_Node_Str");
    if (privateTrafficLabel != null)     context.uregisterStockObject("String_Node_Str");
    if (morHosts == null) {
      s_logger.error("String_Node_Str" + URLDecoder.decode(url.getPath()));
      return null;
    }
    ManagedObjectReference morCluster=null;
    clusterDetails=_clusterDetailsDao.findDetails(clusterId);
    if (clusterDetails.get("String_Node_Str") != null) {
      URI uriFromCluster=new URI(UriUtils.encodeURIComponent(clusterDetails.get("String_Node_Str")));
      morCluster=context.getHostMorByPath(URLDecoder.decode(uriFromCluster.getPath()));
      if (morCluster == null || !morCluster.getProtocol().equalsIgnoreCase("String_Node_Str")) {
        s_logger.warn("String_Node_Str" + clusterDetails.get("String_Node_Str"));
        return null;
      }
 else {
        ClusterMO clusterMo=new ClusterMO(context,morCluster);
        ClusterDasConfigInfo dasConfig=clusterMo.getDasConfig();
        if (dasConfig != null && dasConfig.isEnabled() != null && dasConfig.isEnabled().booleanValue()) {
          clusterDetails.put("String_Node_Str","String_Node_Str");
          _clusterDetailsDao.persist(clusterId,clusterDetails);
        }
      }
    }
    if (!validateDiscoveredHosts(context,morCluster,morHosts)) {
      if (morCluster == null)       s_logger.warn("String_Node_Str");
 else       s_logger.warn("String_Node_Str");
      return null;
    }
    Map<VmwareResource,Map<String,String>> resources=new HashMap<VmwareResource,Map<String,String>>();
    for (    ManagedObjectReference morHost : morHosts) {
      Map<String,String> details=new HashMap<String,String>();
      Map<String,Object> params=new HashMap<String,Object>();
      HostMO hostMo=new HostMO(context,morHost);
      details.put("String_Node_Str",hostMo.getHostName());
      details.put("String_Node_Str",username);
      details.put("String_Node_Str",password);
      String guid=morHost.getProtocol() + "String_Node_Str" + morHost.getValue()+ "String_Node_Str"+ url.getHost();
      details.put("String_Node_Str",guid);
      params.put("String_Node_Str",hostMo.getHostName());
      params.put("String_Node_Str",username);
      params.put("String_Node_Str",password);
      params.put("String_Node_Str",Long.toString(dcId));
      params.put("String_Node_Str",Long.toString(podId));
      params.put("String_Node_Str",Long.toString(clusterId));
      params.put("String_Node_Str",guid);
      if (privateTrafficLabel != null) {
        params.put("String_Node_Str",privateTrafficLabel);
      }
      params.put("String_Node_Str",guestTrafficLabelObj);
      params.put("String_Node_Str",publicTrafficLabelObj);
      VmwareResource resource=new VmwareResource();
      try {
        resource.configure("String_Node_Str",params);
      }
 catch (      ConfigurationException e) {
        _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST,dcId,podId,"String_Node_Str" + url.getHost(),"String_Node_Str" + e.getMessage());
        s_logger.warn("String_Node_Str" + url.getHost(),e);
      }
      resource.start();
      resources.put(resource,details);
    }
    cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString());
    _clusterDao.update(clusterId,cluster);
    return resources;
  }
 catch (  DiscoveredWithErrorException e) {
    throw e;
  }
catch (  Exception e) {
    s_logger.warn("String_Node_Str" + url.getHost());
    return null;
  }
 finally {
    if (context != null)     context.close();
  }
}

Fixed version (ground truth):
@Override public Map<? extends ServerResource,Map<String,String>> find(long dcId,Long podId,Long clusterId,URI url,String username,String password,List<String> hostTags) throws DiscoveryException {
  if (s_logger.isInfoEnabled())   s_logger.info("String_Node_Str" + dcId + "String_Node_Str"+ podId+ "String_Node_Str"+ clusterId+ "String_Node_Str"+ url.getHost());
  if (podId == null) {
    if (s_logger.isInfoEnabled())     s_logger.info("String_Node_Str");
    return null;
  }
  ClusterVO cluster=_clusterDao.findById(clusterId);
  if (cluster == null || cluster.getHypervisorType() != HypervisorType.VMware) {
    if (s_logger.isInfoEnabled())     s_logger.info("String_Node_Str");
    return null;
  }
  List<HostVO> hosts=_resourceMgr.listAllHostsInCluster(clusterId);
  if (hosts != null && hosts.size() > 0) {
    int maxHostsPerCluster=_hvCapabilitiesDao.getMaxHostsPerCluster(hosts.get(0).getHypervisorType(),hosts.get(0).getHypervisorVersion());
    if (hosts.size() >= maxHostsPerCluster) {
      String msg="String_Node_Str" + cluster.getName() + "String_Node_Str"+ hosts.size()+ "String_Node_Str"+ maxHostsPerCluster;
      s_logger.error(msg);
      throw new DiscoveredWithErrorException(msg);
    }
  }
  String privateTrafficLabel=null;
  String publicTrafficLabel=null;
  String guestTrafficLabel=null;
  Map<String,String> vsmCredentials=null;
  VirtualSwitchType defaultVirtualSwitchType=VirtualSwitchType.StandardVirtualSwitch;
  String paramGuestVswitchType=null;
  String paramGuestVswitchName=null;
  String paramPublicVswitchType=null;
  String paramPublicVswitchName=null;
  VmwareTrafficLabel guestTrafficLabelObj=new VmwareTrafficLabel(TrafficType.Guest);
  VmwareTrafficLabel publicTrafficLabelObj=new VmwareTrafficLabel(TrafficType.Public);
  Map<String,String> clusterDetails=_clusterDetailsDao.findDetails(clusterId);
  DataCenterVO zone=_dcDao.findById(dcId);
  NetworkType zoneType=zone.getNetworkType();
  _readGlobalConfigParameters();
  if (useDVS) {
    paramGuestVswitchType=_urlParams.get(ApiConstants.VSWITCH_TYPE_GUEST_TRAFFIC);
    paramGuestVswitchName=_urlParams.get(ApiConstants.VSWITCH_NAME_GUEST_TRAFFIC);
    paramPublicVswitchType=_urlParams.get(ApiConstants.VSWITCH_TYPE_PUBLIC_TRAFFIC);
    paramPublicVswitchName=_urlParams.get(ApiConstants.VSWITCH_NAME_PUBLIC_TRAFFIC);
    defaultVirtualSwitchType=getDefaultVirtualSwitchType();
  }
  guestTrafficLabel=_netmgr.getDefaultGuestTrafficLabel(dcId,HypervisorType.VMware);
  guestTrafficLabelObj=getTrafficInfo(TrafficType.Guest,guestTrafficLabel,defaultVirtualSwitchType,paramGuestVswitchType,paramGuestVswitchName,clusterId);
  if (zoneType == NetworkType.Advanced) {
    publicTrafficLabel=_netmgr.getDefaultPublicTrafficLabel(dcId,HypervisorType.VMware);
    publicTrafficLabelObj=getTrafficInfo(TrafficType.Public,publicTrafficLabel,defaultVirtualSwitchType,paramPublicVswitchType,paramPublicVswitchName,clusterId);
    List<? extends PhysicalNetwork> pNetworkListGuestTraffic=_netmgr.getPhysicalNtwksSupportingTrafficType(dcId,TrafficType.Guest);
    List<? extends PhysicalNetwork> pNetworkListPublicTraffic=_netmgr.getPhysicalNtwksSupportingTrafficType(dcId,TrafficType.Public);
    PhysicalNetwork pNetworkPublic=pNetworkListPublicTraffic.get(0);
    if (pNetworkListGuestTraffic.contains(pNetworkPublic)) {
      if (publicTrafficLabelObj.getVirtualSwitchType() != guestTrafficLabelObj.getVirtualSwitchType()) {
        String msg="String_Node_Str" + pNetworkPublic + "String_Node_Str"+ "String_Node_Str";
        s_logger.error(msg);
        throw new InvalidParameterValueException(msg);
      }
    }
  }
 else {
    if (guestTrafficLabelObj.getVirtualSwitchType() != VirtualSwitchType.StandardVirtualSwitch) {
      String msg="String_Node_Str";
      s_logger.error(msg);
      throw new DiscoveredWithErrorException(msg);
    }
  }
  privateTrafficLabel=_netmgr.getDefaultManagementTrafficLabel(dcId,HypervisorType.VMware);
  if (privateTrafficLabel != null) {
    s_logger.info("String_Node_Str" + privateTrafficLabel);
  }
  if (nexusDVS) {
    if (zoneType != NetworkType.Basic) {
      publicTrafficLabel=_netmgr.getDefaultPublicTrafficLabel(dcId,HypervisorType.VMware);
      if (publicTrafficLabel != null) {
        s_logger.info("String_Node_Str" + publicTrafficLabel);
      }
    }
    guestTrafficLabel=_netmgr.getDefaultGuestTrafficLabel(dcId,HypervisorType.VMware);
    if (guestTrafficLabel != null) {
      s_logger.info("String_Node_Str" + guestTrafficLabel);
    }
    String vsmIp=_urlParams.get("String_Node_Str");
    String vsmUser=_urlParams.get("String_Node_Str");
    String vsmPassword=_urlParams.get("String_Node_Str");
    String clusterName=cluster.getName();
    try {
      _nexusElement.validateVsmCluster(vsmIp,vsmUser,vsmPassword,clusterId,clusterName);
    }
 catch (    ResourceInUseException ex) {
      DiscoveryException discEx=new DiscoveryException(ex.getLocalizedMessage() + "String_Node_Str" + ex.getResourceName());
      throw discEx;
    }
    vsmCredentials=_vmwareMgr.getNexusVSMCredentialsByClusterId(clusterId);
  }
  VmwareContext context=null;
  try {
    context=VmwareContextFactory.create(url.getHost(),username,password);
    if (privateTrafficLabel != null)     context.registerStockObject("String_Node_Str",privateTrafficLabel);
    if (nexusDVS) {
      if (vsmCredentials != null) {
        s_logger.info("String_Node_Str");
        context.registerStockObject("String_Node_Str",vsmCredentials);
      }
    }
    List<ManagedObjectReference> morHosts=_vmwareMgr.addHostToPodCluster(context,dcId,podId,clusterId,URLDecoder.decode(url.getPath()));
    if (morHosts == null)     s_logger.info("String_Node_Str");
    if (privateTrafficLabel != null)     context.uregisterStockObject("String_Node_Str");
    if (morHosts == null) {
      s_logger.error("String_Node_Str" + URLDecoder.decode(url.getPath()));
      return null;
    }
    ManagedObjectReference morCluster=null;
    clusterDetails=_clusterDetailsDao.findDetails(clusterId);
    if (clusterDetails.get("String_Node_Str") != null) {
      URI uriFromCluster=new URI(UriUtils.encodeURIComponent(clusterDetails.get("String_Node_Str")));
      morCluster=context.getHostMorByPath(URLDecoder.decode(uriFromCluster.getPath()));
      if (morCluster == null || !morCluster.getType().equalsIgnoreCase("String_Node_Str")) {
        s_logger.warn("String_Node_Str" + clusterDetails.get("String_Node_Str"));
        return null;
      }
 else {
        ClusterMO clusterMo=new ClusterMO(context,morCluster);
        ClusterDasConfigInfo dasConfig=clusterMo.getDasConfig();
        if (dasConfig != null && dasConfig.isEnabled() != null && dasConfig.isEnabled().booleanValue()) {
          clusterDetails.put("String_Node_Str","String_Node_Str");
          _clusterDetailsDao.persist(clusterId,clusterDetails);
        }
      }
    }
    if (!validateDiscoveredHosts(context,morCluster,morHosts)) {
      if (morCluster == null)       s_logger.warn("String_Node_Str");
 else       s_logger.warn("String_Node_Str");
      return null;
    }
    Map<VmwareResource,Map<String,String>> resources=new HashMap<VmwareResource,Map<String,String>>();
    for (    ManagedObjectReference morHost : morHosts) {
      Map<String,String> details=new HashMap<String,String>();
      Map<String,Object> params=new HashMap<String,Object>();
      HostMO hostMo=new HostMO(context,morHost);
      details.put("String_Node_Str",hostMo.getHostName());
      details.put("String_Node_Str",username);
      details.put("String_Node_Str",password);
      String guid=morHost.getType() + "String_Node_Str" + morHost.getValue()+ "String_Node_Str"+ url.getHost();
      details.put("String_Node_Str",guid);
      params.put("String_Node_Str",hostMo.getHostName());
      params.put("String_Node_Str",username);
      params.put("String_Node_Str",password);
      params.put("String_Node_Str",Long.toString(dcId));
      params.put("String_Node_Str",Long.toString(podId));
      params.put("String_Node_Str",Long.toString(clusterId));
      params.put("String_Node_Str",guid);
      if (privateTrafficLabel != null) {
        params.put("String_Node_Str",privateTrafficLabel);
      }
      params.put("String_Node_Str",guestTrafficLabelObj);
      params.put("String_Node_Str",publicTrafficLabelObj);
      VmwareResource resource=new VmwareResource();
      try {
        resource.configure("String_Node_Str",params);
      }
 catch (      ConfigurationException e) {
        _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST,dcId,podId,"String_Node_Str" + url.getHost(),"String_Node_Str" + e.getMessage());
        s_logger.warn("String_Node_Str" + url.getHost(),e);
      }
      resource.start();
      resources.put(resource,details);
    }
    cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString());
    _clusterDao.update(clusterId,cluster);
    return resources;
  }
 catch (  DiscoveredWithErrorException e) {
    throw e;
  }
catch (  Exception e) {
    s_logger.warn("String_Node_Str" + url.getHost());
    return null;
  }
 finally {
    if (context != null)     context.close();
  }
}