package com.cloud.storage.template;

import java.io.File;
import java.util.Map;

import javax.naming.ConfigurationException;

import org.apache.log4j.Logger;

import com.cloud.exception.InternalErrorException;
import com.cloud.storage.StorageLayer;
import com.cloud.storage.Storage.ImageFormat;

public class VmdkProcessor implements Processor {
    private static final Logger s_logger = Logger.getLogger(VmdkProcessor.class);

    String _name;
    StorageLayer _storage;
	
    @Override
    public FormatInfo process(String templatePath, ImageFormat format, String templateName) throws InternalErrorException {
        if (format != null) {
        	if(s_logger.isInfoEnabled())
        		s_logger.info("We currently don't handle conversion from " + format + " to VMDK.");
            return null;
        }
        
        s_logger.info("Template processing. templatePath: " + templatePath + ", templateName: " + templateName);
        String templateFilePath = templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension();
        if (!_storage.exists(templateFilePath)) {
        	if(s_logger.isInfoEnabled())
        		s_logger.info("Unable to find the vmware template file: " + templateFilePath);
            return null;
        }
        
        FormatInfo info = new FormatInfo();
        info.format = ImageFormat.OVA;
        info.filename = templateName + "." + ImageFormat.OVA.getFileExtension();
        info.size = _storage.getSize(templateFilePath);
        info.virtualSize = info.size;
        return info;
    }
    
    @Override
    public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
        _name = name;
        _storage = (StorageLayer)params.get(StorageLayer.InstanceConfigKey);
        if (_storage == null) {
            throw new ConfigurationException("Unable to get storage implementation");
        }
    	
    	return true;
    }
    
    @Override
    public String getName() {
        return _name;
    }
    
    @Override
    public boolean start() {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}

@@ -299,16 +299,55 @@
 Format;%0D
+%0Aimport com.cloud.utils.script.Script;%0D
 %0A%0D%0Apubli
@@ -1336,32 +1336,648 @@
    %7D%0D%0A        %0D%0A
+        s_logger.info(%22Template processing - untar OVA package. templatePath: %22 + templatePath + %22, templateName: %22 + templateName);%0D%0A        File templateFile = new File(templatePath);%0D%0A        Script command = new Script(true, %22tar%22, 0, s_logger);%0D%0A        command.add(%22-xf%22, templatePath);%0D%0A        command.setWorkDir(templateFile.getParent());%0D%0A        String result = command.execute();%0D%0A        if (result != null) %7B%0D%0A            s_logger.info(%22failed to untar OVA package due to %22 + result + %22. templatePath: %22 + templatePath + %22, templateName: %22 + templateName);%0D%0A        %09return null;%0D%0A        %7D%0D%0A        %0D%0A
         FormatIn
@@ -2222,16 +2222,87 @@
 o.size;%0D
+%0A%0D%0A        // delete original OVA file%0D%0A        templateFile.delete();%0D
 %0A       

add vmware template post-processing