package org.ovirt.engine.core.bll.network.dc;

import java.util.Collections;
import java.util.List;

import org.ovirt.engine.core.bll.CommandBase;
import org.ovirt.engine.core.bll.utils.PermissionSubject;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.AddNetworkStoragePoolParameters;
import org.ovirt.engine.core.common.businessentities.Network;
import org.ovirt.engine.core.common.businessentities.VDSGroup;
import org.ovirt.engine.core.common.config.Config;
import org.ovirt.engine.core.common.config.ConfigValues;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dal.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.CustomLogField;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.CustomLogFields;
import org.ovirt.engine.core.utils.linq.LinqUtils;
import org.ovirt.engine.core.utils.linq.Predicate;

@SuppressWarnings("serial")
@CustomLogFields({ @CustomLogField("NetworkName") })
public abstract class NetworkCommon<T extends AddNetworkStoragePoolParameters> extends CommandBase<T> {
    public NetworkCommon(T parameters) {
        super(parameters);
        this.setStoragePoolId(getParameters().getNetwork().getstorage_pool_id());
    }

    public String getNetworkName() {
        return getParameters().getNetwork().getname();
    }

    @Override
    protected void setActionMessageParameters() {
        addCanDoActionMessage(VdcBllMessages.VAR__TYPE__NETWORK);
    }

    protected boolean validateVmNetwork() {
        boolean retVal = true;

        if (!getParameters().getNetwork().isVmNetwork()) {
            Version version = getStoragePool().getcompatibility_version();
            retVal = Config.<Boolean> GetValue(ConfigValues.NonVmNetworkSupported, version.getValue());
            if (!retVal) {
                addCanDoActionMessage(VdcBllMessages.NON_VM_NETWORK_NOT_SUPPORTED_FOR_POOL_LEVEL);
            }
        }

        return retVal;
    }

    protected boolean validateStpProperty() {
        boolean stpIsAllowed = true;
        if (!getParameters().getNetwork().isVmNetwork() && getParameters().getNetwork().getstp()) {
            addCanDoActionMessage(VdcBllMessages.NON_VM_NETWORK_CANNOT_SUPPORT_STP);
            stpIsAllowed = false;
        }
        return stpIsAllowed;
    }

    protected boolean validateMTUOverrideSupport() {
        boolean mtuSupported = true;

        if (getParameters().getNetwork().getMtu() != 0) {
            mtuSupported =
                    Config.<Boolean> GetValue(ConfigValues.MTUOverrideSupported,
                            getStoragePool().getcompatibility_version().getValue());
            if (!mtuSupported) {
                addCanDoActionMessage(VdcBllMessages.NETWORK_MTU_OVERRIDE_NOT_SUPPORTED);
            }
        }
        return mtuSupported;
    }

    protected boolean validateVlanId(List<Network> networks) {
        if (getParameters().getNetwork().getvlan_id() != null
                && null != LinqUtils.firstOrNull(networks, new Predicate<Network>() {
                    @Override
                    public boolean eval(Network n) {
                        if (n.getvlan_id() != null) {
                            return n.getvlan_id().equals(getParameters().getNetwork().getvlan_id())
                                    && n.getstorage_pool_id().equals(getParameters().getNetwork().getstorage_pool_id())
                                    && !n.getId().equals(getParameters().getNetwork().getId());
                        }
                        return false;
                    }
                })) {
            addCanDoActionMessage(String.format("$vlanId %d", getParameters().getNetwork().getvlan_id()));
            addCanDoActionMessage(VdcBllMessages.NETWORK_VLAN_IN_USE);
            return false;
        }
        return true;
    }

    protected boolean networkNotAttachedToCluster(final Network network) {
        if (network.getstorage_pool_id() != null) {
            List<VDSGroup> clusters = getVdsGroupDAO().getAllForStoragePool(
                    network.getstorage_pool_id().getValue());
            for (VDSGroup cluster : clusters) {
                Network attachedNetwork = getNetworkDAO().getByNameAndCluster(network.getName(), cluster.getId());

                if (attachedNetwork != null) {
                    addCanDoActionMessage(VdcBllMessages.NETWORK_CLUSTER_NETWORK_IN_USE);
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public List<PermissionSubject> getPermissionCheckSubjects() {
        Network network = getParameters().getNetwork();
        Guid networkId = network == null ? null : network.getId();

        return Collections.singletonList(new PermissionSubject(networkId,
                VdcObjectType.Network,
                getActionType().getActionGroup()));
    }
}

@@ -391,71 +391,8 @@
 rk;%0A
-import org.ovirt.engine.core.common.businessentities.VDSGroup;%0A
 impo
@@ -3936,406 +3936,79 @@
 if (
-n
+!getN
 etwork
-.getstorage_pool_id() != null) %7B%0A            List%3CVDSGroup%3E clusters = getVdsGroupDAO().getAllForStoragePool(%0A                    network.getstorage_pool_id().getValue());%0A            for (VDSGroup cluster : clusters) %7B%0A                Network attachedNetwork = getNetworkDAO().getByNameAndCluster(network.getName(), cluster.getId());%0A%0A                if (attachedNetwork != null) %7B%0A        
+ClusterDAO().getAllForNetwork(network.getId()).isEmpty()) %7B%0A
     
@@ -4093,32 +4093,24 @@
             
-        
 return false
@@ -4123,41 +4123,10 @@
     
-        %7D%0A            %7D%0A        
 %7D
+%0A
 %0A   

Replace validation by simpler one