Program: 105442
Project: 105442
Buggy source:
@Override public Answer copyTemplateToPrimaryStorage(CopyCommand cmd){
  DataTO srcData=cmd.getSrcTO();
  TemplateObjectTO template=(TemplateObjectTO)srcData;
  DataStoreTO srcStore=srcData.getDataStore();
  if (!(srcStore instanceof NfsTO)) {
    return new CopyCmdAnswer("String_Node_Str");
  }
  NfsTO nfsImageStore=(NfsTO)srcStore;
  DataTO destData=cmd.getDestTO();
  DataStoreTO destStore=destData.getDataStore();
  DataStoreTO primaryStore=destStore;
  String secondaryStorageUrl=nfsImageStore.getUrl();
  assert(secondaryStorageUrl != null);
  boolean managed=false;
  String storageHost=null;
  int storagePort=Integer.MIN_VALUE;
  String managedStoragePoolName=null;
  String managedStoragePoolRootVolumeName=null;
  String chapInitiatorUsername=null;
  String chapInitiatorSecret=null;
  String chapTargetUsername=null;
  String chapTargetSecret=null;
  if (destStore instanceof PrimaryDataStoreTO) {
    PrimaryDataStoreTO destPrimaryDataStoreTo=(PrimaryDataStoreTO)destStore;
    Map<String,String> details=destPrimaryDataStoreTo.getDetails();
    if (details != null) {
      managed=Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
      if (managed) {
        storageHost=details.get(PrimaryDataStoreTO.STORAGE_HOST);
        try {
          storagePort=Integer.parseInt(details.get(PrimaryDataStoreTO.STORAGE_PORT));
        }
 catch (        Exception ex) {
          storagePort=3260;
        }
        managedStoragePoolName=details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
        managedStoragePoolRootVolumeName=details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
        chapInitiatorUsername=details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
        chapInitiatorSecret=details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
        chapTargetUsername=details.get(PrimaryDataStoreTO.CHAP_TARGET_USERNAME);
        chapTargetSecret=details.get(PrimaryDataStoreTO.CHAP_TARGET_SECRET);
      }
    }
  }
  String templateUrl=secondaryStorageUrl + "String_Node_Str" + srcData.getPath();
  Pair<String,String> templateInfo=VmwareStorageLayoutHelper.decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl,templateUrl,template.getName());
  VmwareContext context=hostService.getServiceContext(cmd);
  if (context == null) {
    return new CopyCmdAnswer("String_Node_Str");
  }
  try {
    VmwareHypervisorHost hyperHost=hostService.getHyperHost(context,cmd);
    String storageUuid=managed ? managedStoragePoolName : primaryStore.getUuid();
    String templateUuidName=deriveTemplateUuidOnHost(hyperHost,storageUuid,templateInfo.second());
    DatacenterMO dcMo=new DatacenterMO(context,hyperHost.getHyperHostDatacenter());
    VirtualMachineMO templateMo=VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templateUuidName),true);
    DatastoreMO dsMo=null;
    if (templateMo == null) {
      if (s_logger.isInfoEnabled()) {
        s_logger.info("String_Node_Str" + templateInfo.second() + "String_Node_Str"+ templateUuidName);
      }
      final ManagedObjectReference morDs;
      if (managed) {
        morDs=prepareManagedDatastore(context,hyperHost,managedStoragePoolName,storageHost,storagePort,chapInitiatorUsername,chapInitiatorSecret,chapTargetUsername,chapTargetSecret);
      }
 else {
        morDs=HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost,storageUuid);
      }
      assert(morDs != null);
      dsMo=new DatastoreMO(context,morDs);
      if (managed) {
        VirtualMachineMO vmMo=copyTemplateFromSecondaryToPrimary(hyperHost,dsMo,secondaryStorageUrl,templateInfo.first(),templateInfo.second(),managedStoragePoolRootVolumeName,false);
        vmMo.unregisterVm();
        String[] vmwareLayoutFilePair=VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo,managedStoragePoolRootVolumeName,managedStoragePoolRootVolumeName,VmwareStorageLayoutType.VMWARE,false);
        String[] legacyCloudStackLayoutFilePair=VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo,null,managedStoragePoolRootVolumeName,VmwareStorageLayoutType.CLOUDSTACK_LEGACY,false);
        dsMo.moveDatastoreFile(vmwareLayoutFilePair[0],dcMo.getMor(),dsMo.getMor(),legacyCloudStackLayoutFilePair[0],dcMo.getMor(),true);
        dsMo.moveDatastoreFile(vmwareLayoutFilePair[1],dcMo.getMor(),dsMo.getMor(),legacyCloudStackLayoutFilePair[1],dcMo.getMor(),true);
        String folderToDelete=dsMo.getDatastorePath(managedStoragePoolRootVolumeName,true);
        dsMo.deleteFolder(folderToDelete,dcMo.getMor());
      }
 else {
        copyTemplateFromSecondaryToPrimary(hyperHost,dsMo,secondaryStorageUrl,templateInfo.first(),templateInfo.second(),templateUuidName,true);
      }
    }
 else {
      s_logger.info("String_Node_Str" + templateInfo.second() + "String_Node_Str");
    }
    TemplateObjectTO newTemplate=new TemplateObjectTO();
    if (managed) {
      String path=dsMo.getDatastorePath(managedStoragePoolRootVolumeName + "String_Node_Str");
      newTemplate.setPath(path);
    }
 else {
      newTemplate.setPath(templateUuidName);
    }
    return new CopyCmdAnswer(newTemplate);
  }
 catch (  Throwable e) {
    if (e instanceof RemoteException) {
      hostService.invalidateServiceContext(context);
    }
    String msg="String_Node_Str" + VmwareHelper.getExceptionMessage(e);
    s_logger.error(msg,e);
    return new CopyCmdAnswer(msg);
  }
}

Fixed version (ground truth):
@Override public Answer copyTemplateToPrimaryStorage(CopyCommand cmd){
  DataTO srcData=cmd.getSrcTO();
  TemplateObjectTO template=(TemplateObjectTO)srcData;
  DataStoreTO srcStore=srcData.getDataStore();
  if (!(srcStore instanceof NfsTO)) {
    return new CopyCmdAnswer("String_Node_Str");
  }
  NfsTO nfsImageStore=(NfsTO)srcStore;
  DataTO destData=cmd.getDestTO();
  DataStoreTO destStore=destData.getDataStore();
  DataStoreTO primaryStore=destStore;
  String secondaryStorageUrl=nfsImageStore.getUrl();
  assert(secondaryStorageUrl != null);
  boolean managed=false;
  String storageHost=null;
  int storagePort=Integer.MIN_VALUE;
  String managedStoragePoolName=null;
  String managedStoragePoolRootVolumeName=null;
  String chapInitiatorUsername=null;
  String chapInitiatorSecret=null;
  String chapTargetUsername=null;
  String chapTargetSecret=null;
  if (destStore instanceof PrimaryDataStoreTO) {
    PrimaryDataStoreTO destPrimaryDataStoreTo=(PrimaryDataStoreTO)destStore;
    Map<String,String> details=destPrimaryDataStoreTo.getDetails();
    if (details != null) {
      managed=Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
      if (managed) {
        storageHost=details.get(PrimaryDataStoreTO.STORAGE_HOST);
        try {
          storagePort=Integer.parseInt(details.get(PrimaryDataStoreTO.STORAGE_PORT));
        }
 catch (        Exception ex) {
          storagePort=3260;
        }
        managedStoragePoolName=details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
        managedStoragePoolRootVolumeName=details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
        chapInitiatorUsername=details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
        chapInitiatorSecret=details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
        chapTargetUsername=details.get(PrimaryDataStoreTO.CHAP_TARGET_USERNAME);
        chapTargetSecret=details.get(PrimaryDataStoreTO.CHAP_TARGET_SECRET);
      }
    }
  }
  String templateUrl=secondaryStorageUrl + "String_Node_Str" + srcData.getPath();
  Pair<String,String> templateInfo=VmwareStorageLayoutHelper.decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl,templateUrl,template.getName());
  VmwareContext context=hostService.getServiceContext(cmd);
  if (context == null) {
    return new CopyCmdAnswer("String_Node_Str");
  }
  try {
    VmwareHypervisorHost hyperHost=hostService.getHyperHost(context,cmd);
    String storageUuid=managed ? managedStoragePoolName : primaryStore.getUuid();
    String templateUuidName=deriveTemplateUuidOnHost(hyperHost,storageUuid,templateInfo.second());
    DatacenterMO dcMo=new DatacenterMO(context,hyperHost.getHyperHostDatacenter());
    VirtualMachineMO templateMo=VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templateUuidName),true);
    DatastoreMO dsMo=null;
    if (templateMo == null) {
      if (s_logger.isInfoEnabled()) {
        s_logger.info("String_Node_Str" + templateInfo.second() + "String_Node_Str"+ templateUuidName);
      }
      final ManagedObjectReference morDs;
      if (managed) {
        morDs=prepareManagedDatastore(context,hyperHost,managedStoragePoolName,storageHost,storagePort,chapInitiatorUsername,chapInitiatorSecret,chapTargetUsername,chapTargetSecret);
      }
 else {
        morDs=HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost,storageUuid);
      }
      assert(morDs != null);
      dsMo=new DatastoreMO(context,morDs);
      if (managed) {
        VirtualMachineMO vmMo=copyTemplateFromSecondaryToPrimary(hyperHost,dsMo,secondaryStorageUrl,templateInfo.first(),templateInfo.second(),managedStoragePoolRootVolumeName,false);
        vmMo.unregisterVm();
        String[] vmwareLayoutFilePair=VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo,managedStoragePoolRootVolumeName,managedStoragePoolRootVolumeName,VmwareStorageLayoutType.VMWARE,false);
        String[] legacyCloudStackLayoutFilePair=VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo,null,managedStoragePoolRootVolumeName,VmwareStorageLayoutType.CLOUDSTACK_LEGACY,false);
        dsMo.moveDatastoreFile(vmwareLayoutFilePair[0],dcMo.getMor(),dsMo.getMor(),legacyCloudStackLayoutFilePair[0],dcMo.getMor(),true);
        dsMo.moveDatastoreFile(vmwareLayoutFilePair[1],dcMo.getMor(),dsMo.getMor(),legacyCloudStackLayoutFilePair[1],dcMo.getMor(),true);
        String folderToDelete=dsMo.getDatastorePath(managedStoragePoolRootVolumeName,true);
        dsMo.deleteFolder(folderToDelete,dcMo.getMor());
      }
 else {
        copyTemplateFromSecondaryToPrimary(hyperHost,dsMo,secondaryStorageUrl,templateInfo.first(),templateInfo.second(),templateUuidName,true);
      }
    }
 else {
      s_logger.info("String_Node_Str" + templateInfo.second() + "String_Node_Str");
    }
    TemplateObjectTO newTemplate=new TemplateObjectTO();
    if (managed) {
      if (dsMo != null) {
        String path=dsMo.getDatastorePath(managedStoragePoolRootVolumeName + "String_Node_Str");
        newTemplate.setPath(path);
      }
    }
 else {
      newTemplate.setPath(templateUuidName);
    }
    return new CopyCmdAnswer(newTemplate);
  }
 catch (  Throwable e) {
    if (e instanceof RemoteException) {
      hostService.invalidateServiceContext(context);
    }
    String msg="String_Node_Str" + VmwareHelper.getExceptionMessage(e);
    s_logger.error(msg,e);
    return new CopyCmdAnswer(msg);
  }
}