package org.ovirt.engine.core.bll;

import org.ovirt.engine.core.bll.context.CommandContext;
import org.ovirt.engine.core.bll.storage.RemoveCinderSnapshotCommandCallback;
import org.ovirt.engine.core.bll.tasks.interfaces.CommandCallback;
import org.ovirt.engine.core.common.action.ImagesContainterParametersBase;
import org.ovirt.engine.core.common.businessentities.storage.DiskImage;
import org.ovirt.engine.core.common.businessentities.storage.ImageStatus;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.utils.transaction.TransactionMethod;
import org.ovirt.engine.core.utils.transaction.TransactionSupport;

@InternalCommandAttribute
public class RemoveCinderSnapshotDiskCommand<T extends ImagesContainterParametersBase> extends BaseImagesCommand<T> {

    public RemoveCinderSnapshotDiskCommand(T parameters) {
        super(parameters);
    }

    public RemoveCinderSnapshotDiskCommand(T parameters, CommandContext cmdContext) {
        super(parameters, cmdContext);
    }

    @Override
    protected void executeCommand() {
        deleteSnapshot();
        persistCommand(getParameters().getParentCommand(), true);
        getReturnValue().setActionReturnValue(getImageId());
        setSucceeded(true);
    }

    private void deleteSnapshot() {
        getCinderBroker().deleteSnapshot(getImageId());
    }

    public Guid getStorageDomainId() {
        return getDiskImage().getStorageIds().get(0);
    }

    @Override
    protected void endWithFailure() {
        super.endWithFailure();
    }

    @Override
    protected void endSuccessfully() {
        if (getDestinationDiskImage() != null) {
            DiskImage curr = getDestinationDiskImage();
            DiskImage volumeBasedOnsnapshot = getDiskImageDao().getAllSnapshotsForLeaf(curr.getImageId()).get(0);
            getImageDao().remove(curr.getImageId());
            volumeBasedOnsnapshot.setParentId(curr.getParentId());
            getBaseDiskDao().update(volumeBasedOnsnapshot);
            getImageDao().update(volumeBasedOnsnapshot.getImage());
        }
        if (!getParameters().isParentHasTasks()) {
            getBackend().endAction(getParameters().getParentCommand(),
                    getParameters().getParentParameters(),
                    null);
        }
        setSucceeded(true);
    }

    @Override
    public void rollback() {
        TransactionSupport.executeInNewTransaction(new TransactionMethod<Object>() {
            @Override
            public Object runInTransaction() {
                if (!getParameters().isLeaveLocked()) {
                    DiskImage diskImage = getImage();
                    if (diskImage != null) {
                        getImageDao().updateStatus(diskImage.getImage().getId(), ImageStatus.OK);
                    }
                    unLockImage();
                }
                return null;
            }
        });
    }

    @Override
    public CommandCallback getCallback() {
        return new RemoveCinderSnapshotCommandCallback();
    }

}

@@ -29,16 +29,40 @@
 e.bll;%0A%0A
+import java.util.List;%0A%0A
 import o
@@ -1702,32 +1702,33 @@
 ionDiskImage();%0A
+%0A
             Disk
@@ -1727,39 +1727,140 @@
     
-DiskImage volumeBasedOns
+// Set the parent snapshot to be dependent on the current snapshot descendant id.%0A            List%3CDiskImage%3E orderedCinderS
 napshot
+s
  = g
@@ -1898,12 +1898,14 @@
 sFor
-Leaf
+Parent
 (cur
@@ -1919,23 +1919,16 @@
 ageId())
-.get(0)
 ;%0A      
@@ -1937,48 +1937,48 @@
     
-getImageDao().remove(curr.getImageId());
+if (!orderedCinderSnapshots.isEmpty()) %7B
 %0A   
@@ -1986,16 +1986,30 @@
         
+    DiskImage 
 volumeBa
@@ -2025,99 +2025,110 @@
 shot
-.setParentId(curr.getParentId());%0A            getBaseDiskDao().update(volumeBasedOnsnapshot
+ = orderedCinderSnapshots.get(0);%0A                volumeBasedOnsnapshot.setParentId(curr.getParentId()
 );%0A 
@@ -2130,32 +2130,36 @@
 ));%0A            
+    
 getImageDao().up
@@ -2210,215 +2210,64 @@
     
-%7D%0A
     
-    if (!getParameters().isParentHasTasks()) %7B%0A            getBackend().endAction(getParameters().getParentCommand(),%0A                    getParameters().getParentParameters(),%0A                    null
+%7D%0A            getImageDao().remove(curr.getImageId()
 );%0A 

Update Cinder snapshot decendants with parent id