package org.ovirt.engine.core.bll.storage;

import java.util.List;

import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.action.AddNetworkStoragePoolParameters;
import org.ovirt.engine.core.common.businessentities.VDSGroup;
import org.ovirt.engine.core.common.businessentities.Network;
import org.ovirt.engine.core.dal.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.utils.linq.LinqUtils;
import org.ovirt.engine.core.utils.linq.Predicate;

public class RemoveNetworkCommand<T extends AddNetworkStoragePoolParameters> extends NetworkCommon<T> {
    public RemoveNetworkCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected void executeCommand() {
        DbFacade.getInstance().getNetworkDao().remove(getParameters().getNetwork().getId());
        setSucceeded(true);
    }

    @Override
    protected boolean canDoAction() {
        addCanDoActionMessage(VdcBllMessages.VAR__ACTION__REMOVE);
        addCanDoActionMessage(VdcBllMessages.VAR__TYPE__NETWORK);

        boolean retVal = true;
        if (retVal) {
            retVal = CommonNetworkValidation(getParameters().getNetwork(), getReturnValue().getCanDoActionMessages());
        }
        return retVal;
    }

    public static boolean CommonNetworkValidation(final Network network, java.util.ArrayList<String> canDoActionMessages) {
        // check that network is not in use by cluster
        if (network.getstorage_pool_id() != null) {
            List<VDSGroup> groups = DbFacade.getInstance().getVdsGroupDao().getAllForStoragePool(
                    network.getstorage_pool_id().getValue());
            for (VDSGroup cluster : groups) {
                List<Network> networks = DbFacade.getInstance().getNetworkDao()
                        .getAllForCluster(cluster.getId());

                if (null != LinqUtils.firstOrNull(networks, new Predicate<Network>() {
                    @Override
                    public boolean eval(Network n) {
                        return n.getname().equals(network.getname());
                    }
                })) {
                    canDoActionMessages.add(VdcBllMessages.NETWORK_CLUSTER_NETWORK_IN_USE.toString());
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public AuditLogType getAuditLogTypeValue() {
        return getSucceeded() ? AuditLogType.NETWORK_REMOVE_NETWORK : AuditLogType.NETWORK_REMOVE_NETWORK_FAILED;
    }
}

@@ -240,24 +240,23 @@
 ntities.
-VDSGroup
+Network
 ;%0Aimport
@@ -294,39 +294,40 @@
 usinessentities.
-Network
+VDSGroup
 ;%0Aimport org.ovi
@@ -416,110 +416,8 @@
 ade;
-%0Aimport org.ovirt.engine.core.utils.linq.LinqUtils;%0Aimport org.ovirt.engine.core.utils.linq.Predicate;
 %0A%0Apu
@@ -660,39 +660,16 @@
         
-DbFacade.getInstance().
 getNetwo
@@ -671,18 +671,18 @@
 NetworkD
-ao
+AO
 ().remov
@@ -955,73 +955,14 @@
     
-boolean retVal = true;%0A        if (retVal) %7B%0A            retVal =
+return
  Com
@@ -1060,41 +1060,8 @@
 ));%0A
-        %7D%0A        return retVal;%0A
     
@@ -1520,32 +1520,56 @@
     
-List%3C
 Network
-%3E n
+ removedN
 etwork
-s
  =
+%0A                       
  DbF
@@ -1607,350 +1607,109 @@
 ao()
-%0A                        .getAllForCluster(cluster.getId());%0A%0A                if (null != LinqUtils.firstOrNull(networks, new Predicate%3CNetwork%3E() %7B%0A                    @Override%0A                    public boolean eval(Network n) %7B%0A                        return n.getname().equals(network.getname());%0A                    %7D%0A                %7D)
+.getByNameAndCluster(network.getName(), cluster.getId());%0A%0A                if (removedNetwork != null
 ) %7B%0A

Use proper dao api to detect used network