package org.ovirt.engine.core.bll;

import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.action.RemoveVmInterfaceParameters;
import org.ovirt.engine.core.common.businessentities.VMStatus;
import org.ovirt.engine.core.common.businessentities.VmDeviceId;
import org.ovirt.engine.core.common.businessentities.VmDynamic;
import org.ovirt.engine.core.common.businessentities.VmInterfaceType;
import org.ovirt.engine.core.common.businessentities.VmNetworkInterface;
import org.ovirt.engine.core.dal.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.CustomLogField;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.CustomLogFields;

@CustomLogFields({ @CustomLogField("InterfaceName") })
public class RemoveVmInterfaceCommand<T extends RemoveVmInterfaceParameters> extends VmCommand<T> {

    private static final long serialVersionUID = 997624605993881039L;
    private String _interfaceName = "";

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

    public String getInterfaceName() {
        return _interfaceName;
    }

    @Override
    protected void ExecuteVmCommand() {
        this.setVmName(getVmStaticDAO().get(getParameters().getVmId()).getvm_name());

        // return mac to pool
        VmNetworkInterface iface = getVmNetworkInterfaceDAO().get(getParameters().getInterfaceId());

        if (iface != null) {
            MacPoolManager.getInstance().freeMac(iface.getMacAddress());
            _interfaceName = iface.getName();

            // Get Interface type.
            String interType = VmInterfaceType.forValue(iface.getType()).getInterfaceTranslation();
            if (interType != null) {
                AddCustomValue("InterfaceType", interType);
            }
        }

        // remove from db
        DbFacade dbFacade = DbFacade.getInstance();
        dbFacade.getVmNetworkInterfaceDAO().remove(getParameters().getInterfaceId());
        dbFacade.getVmNetworkStatisticsDAO().remove(getParameters().getInterfaceId());
        dbFacade.getVmDeviceDAO()
                .remove(new VmDeviceId(getParameters().getInterfaceId(), getParameters().getVmId()));
        setSucceeded(true);
    }

    @Override
    protected boolean canDoAction() {
        VmDynamic vm = DbFacade.getInstance().getVmDynamicDAO().get(getParameters().getVmId());
        if (vm.getstatus() != VMStatus.Down && DbFacade.getInstance()
                .getVmDeviceDAO()
                .get(new VmDeviceId(getParameters().getInterfaceId(), getParameters().getVmId()))
                .getIsPlugged()) {
            addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_CANNOT_REMOVE_ACTIVE_DEVICE);
            return false;
        }
        return true;
    }

    @Override
    public AuditLogType getAuditLogTypeValue() {
        return getSucceeded() ? AuditLogType.NETWORK_REMOVE_VM_INTERFACE
                : AuditLogType.NETWORK_REMOVE_VM_INTERFACE_FAILED;
    }
}
