<commit_before>package org.ovirt.engine.core.bll.storage;

import com.woorea.openstack.base.client.OpenStackResponseException;
import com.woorea.openstack.cinder.model.ConnectionForInitialize;
import com.woorea.openstack.cinder.model.SnapshotForCreate;
import com.woorea.openstack.cinder.model.Volume;
import com.woorea.openstack.cinder.model.VolumeForCreate;
import com.woorea.openstack.cinder.model.VolumeForUpdate;
import org.apache.commons.httpclient.HttpStatus;
import org.ovirt.engine.core.bll.provider.storage.OpenStackVolumeProviderProxy;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.businessentities.storage.CinderConnectionInfo;
import org.ovirt.engine.core.common.businessentities.storage.CinderDisk;
import org.ovirt.engine.core.common.businessentities.storage.CinderVolumeDriver;
import org.ovirt.engine.core.common.businessentities.storage.CinderVolumeStatus;
import org.ovirt.engine.core.common.businessentities.storage.DiskInterface;
import org.ovirt.engine.core.common.businessentities.storage.ImageStatus;
import org.ovirt.engine.core.common.businessentities.storage.VolumeFormat;
import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

public class CinderBroker extends AuditLogableBase {

    private static final Logger log = LoggerFactory.getLogger(CinderBroker.class);
    public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    private OpenStackVolumeProviderProxy proxy;
    private ArrayList<String> executeFailedMessages;

    public CinderBroker(Guid storageDomainId, ArrayList<String> executeFailedMessages) {
        this.proxy = getVolumeProviderProxy(storageDomainId);
        this.executeFailedMessages = executeFailedMessages;
    }

    private <T> T execute(Callable<T> callable) {
        try {
            return callable.call();
        } catch (OpenStackResponseException e) {
            executeFailedMessages.add(VdcBllErrors.CINDER_ERROR.name());
            executeFailedMessages.add(String.format("$cinderException %1$s", e.getMessage()));
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String createDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<String>() {
            @Override
            public String call() {
                VolumeForCreate cinderVolume = new VolumeForCreate();
                cinderVolume.setName(cinderDisk.getDiskAlias());
                cinderVolume.setDescription(cinderDisk.getDiskDescription());
                cinderVolume.setSize((int) (cinderDisk.getSizeInGigabytes()));
                cinderVolume.setVolumeType(cinderDisk.getCinderVolumeType());
                return proxy.createVolume(cinderVolume);
            }
        });
    }

    public String cloneDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<String>() {
            @Override
            public String call() {
                VolumeForCreate cinderVolume = new VolumeForCreate();
                cinderVolume.setName(cinderDisk.getDiskAlias());
                cinderVolume.setDescription(cinderDisk.getDiskDescription());
                cinderVolume.setSize((int) (cinderDisk.getSizeInGigabytes()));
                cinderVolume.setSourceVolid(cinderDisk.getId().toString());
                return proxy.createVolume(cinderVolume);
            }
        });
    }

    public Void deleteDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                proxy.deleteVolume(cinderDisk.getId().toString());
                return null;
            }
        });
    }

    public Void updateDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                VolumeForUpdate volumeForUpdate = new VolumeForUpdate();
                volumeForUpdate.setName(cinderDisk.getDiskAlias());
                volumeForUpdate.setDescription(cinderDisk.getDiskDescription());
                proxy.updateVolume(cinderDisk.getId().toString(), volumeForUpdate);
                return null;
            }
        });
    }

    public Void extendDisk(final CinderDisk cinderDisk, final int newSize) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                proxy.extendVolume(cinderDisk.getId().toString(), newSize);
                return null;
            }
        });
    }

    public String createSnapshot(final CinderDisk cinderDisk) {
        return execute(new Callable<String>() {
            @Override
            public String call() {
                SnapshotForCreate snapshotForCreate = new SnapshotForCreate();
                snapshotForCreate.setVolumeId(cinderDisk.getId().toString());
                return proxy.createSnapshot(snapshotForCreate);
            }
        });
    }

    public CinderConnectionInfo initializeConnectionForDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<CinderConnectionInfo>() {
            @Override
            public CinderConnectionInfo call() {
                ConnectionForInitialize connectionForInitialize = new ConnectionForInitialize();
                return proxy.initializeConnectionForVolume(cinderDisk.getId().toString(), connectionForInitialize);
            }
        });
    }

    public void updateConnectionInfoForDisk(CinderDisk cinderDisk) {
        try {
            CinderConnectionInfo connectionInfo = initializeConnectionForDisk(cinderDisk);
            CinderVolumeDriver cinderVolumeDriver = CinderVolumeDriver.forValue(connectionInfo.getDriverVolumeType());
            if (cinderVolumeDriver == null) {
                addCustomValue("DiskAlias", cinderDisk.getDiskAlias());<commit_msg>core: Add Cinder delete snapshot for CinderBroker<commit_after>package org.ovirt.engine.core.bll.storage;

import com.woorea.openstack.base.client.OpenStackResponseException;
import com.woorea.openstack.cinder.model.ConnectionForInitialize;
import com.woorea.openstack.cinder.model.SnapshotForCreate;
import com.woorea.openstack.cinder.model.Volume;
import com.woorea.openstack.cinder.model.VolumeForCreate;
import com.woorea.openstack.cinder.model.VolumeForUpdate;
import org.apache.commons.httpclient.HttpStatus;
import org.ovirt.engine.core.bll.provider.storage.OpenStackVolumeProviderProxy;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.businessentities.storage.CinderConnectionInfo;
import org.ovirt.engine.core.common.businessentities.storage.CinderDisk;
import org.ovirt.engine.core.common.businessentities.storage.CinderVolumeDriver;
import org.ovirt.engine.core.common.businessentities.storage.CinderVolumeStatus;
import org.ovirt.engine.core.common.businessentities.storage.DiskInterface;
import org.ovirt.engine.core.common.businessentities.storage.ImageStatus;
import org.ovirt.engine.core.common.businessentities.storage.VolumeFormat;
import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

public class CinderBroker extends AuditLogableBase {

    private static final Logger log = LoggerFactory.getLogger(CinderBroker.class);
    public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    private OpenStackVolumeProviderProxy proxy;
    private ArrayList<String> executeFailedMessages;

    public CinderBroker(Guid storageDomainId, ArrayList<String> executeFailedMessages) {
        this.proxy = getVolumeProviderProxy(storageDomainId);
        this.executeFailedMessages = executeFailedMessages;
    }

    private <T> T execute(Callable<T> callable) {
        try {
            return callable.call();
        } catch (OpenStackResponseException e) {
            executeFailedMessages.add(VdcBllErrors.CINDER_ERROR.name());
            executeFailedMessages.add(String.format("$cinderException %1$s", e.getMessage()));
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String createDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<String>() {
            @Override
            public String call() {
                VolumeForCreate cinderVolume = new VolumeForCreate();
                cinderVolume.setName(cinderDisk.getDiskAlias());
                cinderVolume.setDescription(cinderDisk.getDiskDescription());
                cinderVolume.setSize((int) (cinderDisk.getSizeInGigabytes()));
                cinderVolume.setVolumeType(cinderDisk.getCinderVolumeType());
                return proxy.createVolume(cinderVolume);
            }
        });
    }

    public String cloneDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<String>() {
            @Override
            public String call() {
                VolumeForCreate cinderVolume = new VolumeForCreate();
                cinderVolume.setName(cinderDisk.getDiskAlias());
                cinderVolume.setDescription(cinderDisk.getDiskDescription());
                cinderVolume.setSize((int) (cinderDisk.getSizeInGigabytes()));
                cinderVolume.setSourceVolid(cinderDisk.getId().toString());
                return proxy.createVolume(cinderVolume);
            }
        });
    }

    public Void deleteDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                proxy.deleteVolume(cinderDisk.getId().toString());
                return null;
            }
        });
    }

    public Void updateDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                VolumeForUpdate volumeForUpdate = new VolumeForUpdate();
                volumeForUpdate.setName(cinderDisk.getDiskAlias());
                volumeForUpdate.setDescription(cinderDisk.getDiskDescription());
                proxy.updateVolume(cinderDisk.getId().toString(), volumeForUpdate);
                return null;
            }
        });
    }

    public Void extendDisk(final CinderDisk cinderDisk, final int newSize) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                proxy.extendVolume(cinderDisk.getId().toString(), newSize);
                return null;
            }
        });
    }

    public String createSnapshot(final CinderDisk cinderDisk) {
        return execute(new Callable<String>() {
            @Override
            public String call() {
                SnapshotForCreate snapshotForCreate = new SnapshotForCreate();
                snapshotForCreate.setVolumeId(cinderDisk.getId().toString());
                return proxy.createSnapshot(snapshotForCreate);
            }
        });
    }

    public Void deleteSnapshot(final Guid snapshotId) {
        return execute(new Callable<Void>() {
            @Override
            public Void call() {
                proxy.deleteSnapshot(snapshotId.toString());
                return null;
            }
        });
    }

    public CinderConnectionInfo initializeConnectionForDisk(final CinderDisk cinderDisk) {
        return execute(new Callable<CinderConnectionInfo>() {
            @Override
            public CinderConnectionInfo call() {
                ConnectionForInitialize connectionForInitialize = new ConnectionForInitialize();
                return proxy.initializeConnectionForVolume(cinderDisk.getId().toString(), connectionForInitialize);
            }
        });
    }

    public void updateConnectionInfoForDisk(CinderDisk cinderDisk) {
        try {
            CinderConnectionInfo connectionInfo = initializeConnectionForDisk(cinderDisk);
            CinderVolumeDriver cinderVolumeDriver = CinderVolumeDriver.forValue(connectionInfo.getDriverVolumeType());
            if (cinderVolumeDriver == null) {
                addCustomValue("DiskAlias", cinderDisk.getDiskAlias());
{'fraction_non_alphanumeric': 0.09301412872841444, 'fraction_numerical': 0.00047095761381475666, 'mean_word_length': 2.8809016143770942, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3408028', 'n_tokens_mistral': 3378, 'n_tokens_neox': 3315, 'n_words': 554}