<commit_before>package org.ovirt.engine.core.vdsbroker.vdsbroker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.network.cluster.ManagementNetworkUtil;
import org.ovirt.engine.core.common.FeatureSupported;
import org.ovirt.engine.core.common.action.CustomPropertiesForVdsNetworkInterface;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.network.HostNetworkQos;
import org.ovirt.engine.core.common.businessentities.network.Network;
import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment;
import org.ovirt.engine.core.common.businessentities.network.NetworkBootProtocol;
import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
import org.ovirt.engine.core.common.validation.MaskValidator;
import org.ovirt.engine.core.common.vdscommands.SetupNetworksVdsCommandParameters;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dao.network.NetworkAttachmentDao;
import org.ovirt.engine.core.utils.NetworkUtils;
import org.ovirt.engine.core.vdsbroker.CalculateBaseNic;
import org.ovirt.engine.core.vdsbroker.EffectiveHostNetworkQos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Replaced by {@link org.ovirt.engine.core.vdsbroker.vdsbroker.HostSetupNetworksVDSCommand}
 */
@Deprecated
public class SetupNetworksVDSCommand<T extends SetupNetworksVdsCommandParameters> extends FutureVDSCommand<T> {

    protected static final String DHCP_BOOT_PROTOCOL = "dhcp";
    protected static final String BOOT_PROTOCOL = "bootproto";
    protected static final String BONDING_OPTIONS = "options";
    protected static final String SLAVES = "nics";
    private static final String DEFAULT_ROUTE = "defaultRoute";
    private static final Map<String, String> REMOVE_OBJ = Collections.singletonMap("remove", Boolean.TRUE.toString());
    private static final Logger log = LoggerFactory.getLogger(SetupNetworksVDSCommand.class);

    @Inject
    private ManagementNetworkUtil managementNetworkUtil;

    @Inject
    NetworkAttachmentDao networkAttachmentDao;

    @Inject
    EffectiveHostNetworkQos effectiveHostNetworkQos;

    @Inject
    CalculateBaseNic calculateBaseNic;

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

    @Override
    protected void executeVdsBrokerCommand() {
        httpTask = getBroker().setupNetworks(generateNetworks(), generateBonds(), generateOptions());
    }

    private Map<String, Object> generateNetworks() {
        Map<String, Object> networks = new HashMap<String, Object>();
        VDS host = getParameters().getVds();

        boolean hostNetworkQosSupported = FeatureSupported.hostNetworkQos(host.getVdsGroupCompatibilityVersion());

        Set<Version> supportedClusterVersionsSet = host.getSupportedClusterVersionsSet();
        boolean supportedClusterVersionsAvailable =
                supportedClusterVersionsSet != null && supportedClusterVersionsSet.isEmpty();
        if (!supportedClusterVersionsAvailable) {
            log.warn("Host '{}' ('{}') doesn't contain Supported Cluster Versions, therefore 'defaultRoute'"
                    + " will not be sent via the SetupNetworks", host.getName(), host.getId());
        }

        boolean defaultRouteSupported =
                supportedClusterVersionsAvailable
                        && FeatureSupported.defaultRoute(Collections.max(supportedClusterVersionsSet));

        CustomPropertiesForVdsNetworkInterface customProperties = getParameters().getCustomProperties();
        for (Network network : getParameters().getNetworks()) {
            Map<String, Object> opts = new HashMap<String, Object>();
            VdsNetworkInterface iface =
                    findNetworkInterface(network.getName(), getParameters().getInterfaces(), getParameters().getBonds());
            String ifaceNameWithoutVlan = NetworkUtils.stripVlan(iface);
            Boolean bonded = findInterfaceByName(ifaceNameWithoutVlan).getBonded();
            String type = (bonded != null && bonded) ? "bonding" : "nic";
            opts.put(type, ifaceNameWithoutVlan);
            if (NetworkUtils.isVlan(network)) {
                opts.put("vlan", network.getVlanId().toString());
            }

            if (iface.getBootProtocol() != null) {
                addBootProtocol(opts, iface);
            }

            if (network.getMtu() == 0) {
                opts.put("mtu", NetworkUtils.getDefaultMtu().toString());
            } else {
                opts.put("mtu", String.valueOf(network.getMtu()));
            }

            opts.put("bridged", Boolean.toString(network.isVmNetwork()));
            if (network.isVmNetwork()) {
                opts.put(VdsProperties.STP, network.getStp() ? "yes" : "no");
            }

            iface.setVdsId(host.getId());
            VdsNetworkInterface baseNic = calculateBaseNic.getBaseNic(iface);
            NetworkAttachment networkAttachment = baseNic == null || baseNic.getId() == null ? null :
                networkAttachmentDao.getNetworkAttachmentByNicIdAndNetworkId(baseNic.getId(), network.getId());

            if (hostNetworkQosSupported && NetworkUtils.qosConfiguredOnInterface(networkAttachment, network)) {
                HostNetworkQos hostNetworkQos = effectiveHostNetworkQos.getQos(networkAttachment, network);
                new HostNetworkQosMapper(opts).serialize(hostNetworkQos);
            }
<commit_msg>Revert "core: cannot add network to bond using rest and old setupneworks"<commit_after>package org.ovirt.engine.core.vdsbroker.vdsbroker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.network.cluster.ManagementNetworkUtil;
import org.ovirt.engine.core.common.FeatureSupported;
import org.ovirt.engine.core.common.action.CustomPropertiesForVdsNetworkInterface;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.network.HostNetworkQos;
import org.ovirt.engine.core.common.businessentities.network.Network;
import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment;
import org.ovirt.engine.core.common.businessentities.network.NetworkBootProtocol;
import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
import org.ovirt.engine.core.common.validation.MaskValidator;
import org.ovirt.engine.core.common.vdscommands.SetupNetworksVdsCommandParameters;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dao.network.NetworkAttachmentDao;
import org.ovirt.engine.core.utils.NetworkUtils;
import org.ovirt.engine.core.vdsbroker.CalculateBaseNic;
import org.ovirt.engine.core.vdsbroker.EffectiveHostNetworkQos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Replaced by {@link org.ovirt.engine.core.vdsbroker.vdsbroker.HostSetupNetworksVDSCommand}
 */
@Deprecated
public class SetupNetworksVDSCommand<T extends SetupNetworksVdsCommandParameters> extends FutureVDSCommand<T> {

    protected static final String DHCP_BOOT_PROTOCOL = "dhcp";
    protected static final String BOOT_PROTOCOL = "bootproto";
    protected static final String BONDING_OPTIONS = "options";
    protected static final String SLAVES = "nics";
    private static final String DEFAULT_ROUTE = "defaultRoute";
    private static final Map<String, String> REMOVE_OBJ = Collections.singletonMap("remove", Boolean.TRUE.toString());
    private static final Logger log = LoggerFactory.getLogger(SetupNetworksVDSCommand.class);

    @Inject
    private ManagementNetworkUtil managementNetworkUtil;

    @Inject
    NetworkAttachmentDao networkAttachmentDao;

    @Inject
    EffectiveHostNetworkQos effectiveHostNetworkQos;

    @Inject
    CalculateBaseNic calculateBaseNic;

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

    @Override
    protected void executeVdsBrokerCommand() {
        httpTask = getBroker().setupNetworks(generateNetworks(), generateBonds(), generateOptions());
    }

    private Map<String, Object> generateNetworks() {
        Map<String, Object> networks = new HashMap<String, Object>();
        VDS host = getParameters().getVds();

        boolean hostNetworkQosSupported = FeatureSupported.hostNetworkQos(host.getVdsGroupCompatibilityVersion());

        Set<Version> supportedClusterVersionsSet = host.getSupportedClusterVersionsSet();
        boolean supportedClusterVersionsAvailable =
                supportedClusterVersionsSet != null && supportedClusterVersionsSet.isEmpty();
        if (!supportedClusterVersionsAvailable) {
            log.warn("Host '{}' ('{}') doesn't contain Supported Cluster Versions, therefore 'defaultRoute'"
                    + " will not be sent via the SetupNetworks", host.getName(), host.getId());
        }

        boolean defaultRouteSupported =
                supportedClusterVersionsAvailable
                        && FeatureSupported.defaultRoute(Collections.max(supportedClusterVersionsSet));

        CustomPropertiesForVdsNetworkInterface customProperties = getParameters().getCustomProperties();
        for (Network network : getParameters().getNetworks()) {
            Map<String, Object> opts = new HashMap<String, Object>();
            VdsNetworkInterface iface =
                    findNetworkInterface(network.getName(), getParameters().getInterfaces(), getParameters().getBonds());
            String ifaceNameWithoutVlan = NetworkUtils.stripVlan(iface);
            Boolean bonded = findInterfaceByName(ifaceNameWithoutVlan).getBonded();
            String type = (bonded != null && bonded) ? "bonding" : "nic";
            opts.put(type, ifaceNameWithoutVlan);
            if (NetworkUtils.isVlan(network)) {
                opts.put("vlan", network.getVlanId().toString());
            }

            if (iface.getBootProtocol() != null) {
                addBootProtocol(opts, iface);
            }

            if (network.getMtu() == 0) {
                opts.put("mtu", NetworkUtils.getDefaultMtu().toString());
            } else {
                opts.put("mtu", String.valueOf(network.getMtu()));
            }

            opts.put("bridged", Boolean.toString(network.isVmNetwork()));
            if (network.isVmNetwork()) {
                opts.put(VdsProperties.STP, network.getStp() ? "yes" : "no");
            }

            iface.setVdsId(host.getId());
            VdsNetworkInterface baseNic = calculateBaseNic.getBaseNic(iface);
            NetworkAttachment networkAttachment = baseNic == null ? null :
                networkAttachmentDao.getNetworkAttachmentByNicIdAndNetworkId(baseNic.getId(), network.getId());

            if (hostNetworkQosSupported && NetworkUtils.qosConfiguredOnInterface(networkAttachment, network)) {
                HostNetworkQos hostNetworkQos = effectiveHostNetworkQos.getQos(networkAttachment, network);
                new HostNetworkQosMapper(opts).serialize(hostNetworkQos);
            }

{'fraction_non_alphanumeric': 0.1021036880556289, 'fraction_numerical': 0.0005281225244256667, 'mean_word_length': 3.689228229467602, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '1572218', 'n_tokens_mistral': 3099, 'n_tokens_neox': 3071, 'n_words': 536}