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

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.Backend;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VDSGroup;
import org.ovirt.engine.core.common.businessentities.VDSStatus;
import org.ovirt.engine.core.common.businessentities.network.Network;
import org.ovirt.engine.core.common.businessentities.network.NetworkCluster;
import org.ovirt.engine.core.common.businessentities.network.NetworkClusterId;
import org.ovirt.engine.core.common.businessentities.network.NetworkStatus;
import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
import org.ovirt.engine.core.common.interfaces.SearchType;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.SearchParameters;
import org.ovirt.engine.core.common.queries.VdcQueryType;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.utils.linq.LinqUtils;
import org.ovirt.engine.core.utils.linq.Predicate;

/**
 * Class to hold common static methods that are used in several different places.
 */
public class NetworkClusterHelper {

    /**
     * Set the network on cluster status in the DB to {@link NetworkStatus#OPERATIONAL} or
     * {@link NetworkStatus#NON_OPERATIONAL} depending on the network state in the cluster (if it's implemented by all
     * the hosts or not) and if it's required or not.
     *
     * @param vdsGroupId
     *            The cluster ID where the network is attached.
     * @param net
     *            The network itself.
     */
    @SuppressWarnings("unchecked")
    public static void setStatus(Guid vdsGroupId, final Network net) {
        NetworkStatus status = NetworkStatus.OPERATIONAL;
        NetworkCluster networkCluster =
                DbFacade.getInstance().getNetworkClusterDao().get(new NetworkClusterId(vdsGroupId, net.getId()));

        if (networkCluster != null) {
            VDSGroup vdsGroup = DbFacade.getInstance().getVdsGroupDao().get(vdsGroupId);

            // Search all vds in cluster that have the specify network, if not the
            // network is not active
            SearchParameters searchParams = new SearchParameters("hosts: cluster = "
                    + vdsGroup.getName(), SearchType.VDS);
            searchParams.setMaxCount(Integer.MAX_VALUE);
            List<VDS> vdsList = Backend.getInstance()
                    .runInternalQuery(VdcQueryType.Search, searchParams).getReturnValue();

            if (networkCluster.isRequired()) {
                for (VDS vds : vdsList) {
                    if (vds.getStatus() != VDSStatus.Up) {
                        continue;
                    }
                    List<VdsNetworkInterface> interfaces = Backend.getInstance()
                            .runInternalQuery(VdcQueryType.GetVdsInterfacesByVdsId,
                                    new IdQueryParameters(vds.getId())).getReturnValue();
                    VdsNetworkInterface iface = LinqUtils.firstOrNull(interfaces, new Predicate<VdsNetworkInterface>() {
                        @Override
                        public boolean eval(VdsNetworkInterface i) {
                            return StringUtils.equals(i.getNetworkName(), net.getName());
                        }
                    });
                    if (iface == null) {
                        status = NetworkStatus.NON_OPERATIONAL;
                        break;
                    }
                }
            }

            networkCluster.setStatus(status);
            DbFacade.getInstance().getNetworkClusterDao().updateStatus(networkCluster);
        }
    }

}

@@ -217,71 +217,8 @@
 DS;%0A
-import org.ovirt.engine.core.common.businessentities.VDSGroup;%0A
 impo
@@ -665,67 +665,8 @@
 ce;%0A
-import org.ovirt.engine.core.common.interfaces.SearchType;%0A
 impo
@@ -728,70 +728,8 @@
 rs;%0A
-import org.ovirt.engine.core.common.queries.SearchParameters;%0A
 impo
@@ -1542,43 +1542,8 @@
  */%0A
-    @SuppressWarnings(%22unchecked%22)%0A
     
@@ -1852,32 +1852,33 @@
 ster != null) %7B%0A
+%0A
             VDSG
@@ -1877,86 +1877,47 @@
     
-VDSGroup vdsGroup = DbFacade.getInstance().getVdsGroupDao().get(vdsGroupId);%0A%0A
+if (networkCluster.isRequired()) %7B%0A    
     
@@ -1973,17 +1973,19 @@
 e specif
-y
+ied
  network
@@ -2000,23 +2000,8 @@
  the
-%0A            //
  net
@@ -2027,205 +2027,8 @@
     
-        SearchParameters searchParams = new SearchParameters(%22hosts: cluster = %22%0A                    + vdsGroup.getName(), SearchType.VDS);%0A            searchParams.setMaxCount(Integer.MAX_VALUE);%0A
     
@@ -2055,23 +2055,24 @@
 sList = 
-Backend
+DbFacade
 .getInst
@@ -2081,147 +2081,51 @@
 ce()
-%0A                    .runInternalQuery(VdcQueryType.Search, searchParams).getReturnValue();%0A%0A            if (networkCluster.isRequired()) %7B
+.getVdsDao().getAllForVdsGroup(vdsGroupId);
 %0A   
@@ -2897,16 +2897,17 @@
     %7D);%0A
+%0A
         

Use VdsDao for fetching hosts in NetworkClusterHelper