<commit_before>package org.ovirt.engine.core.bll.network.vm;

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

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.VmCommand;
import org.ovirt.engine.core.common.action.VmOperationParameterBase;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.VmDevice;
import org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType;
import org.ovirt.engine.core.common.businessentities.network.VmNic;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;

public class ReorderVmNicsCommand<T extends VmOperationParameterBase> extends VmCommand<T> {

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

    @Override
    protected boolean canDoAction() {
        VM vm = getVm();
        if (vm == null || vm.getStaticData() == null) {
            addCanDoActionMessage(EngineMessage.ACTION_TYPE_FAILED_VM_NOT_FOUND);
            return false;
        }

        if (!canRunActionOnNonManagedVm()) {
            return false;
        }

        return true;
    }

    @Override
    protected void executeVmCommand() {
        reorderNics();
        setSucceeded(true);
    }

    private Map<Guid, VmDevice> getVmInterfaceDevices() {
        List<VmDevice> vmInterfaceDevicesList = getVmDeviceDao().getVmDeviceByVmIdAndType(getParameters().getVmId(), VmDeviceGeneralType.INTERFACE);
        Map<Guid, VmDevice> vmInterfaceDevices = new HashMap<>();
        for (VmDevice device : vmInterfaceDevicesList) {
            vmInterfaceDevices.put(device.getDeviceId(), device);
        }
        return vmInterfaceDevices;
    }

    private void reorderNics() {
        Map<Guid, VmDevice> vmInterfaceDevices = getVmInterfaceDevices();
        List<VmNic> nics = getVmNicDao().getAllForVm(getParameters().getVmId());
        List<VmNic> nicsToReorder = new ArrayList<>();
        List<String> macsToReorder = new ArrayList<>();

        for (VmNic nic : nics) {
            VmDevice nicDevice = vmInterfaceDevices.get(nic.getId());
            // If there is not device, or the PCI address is empty
            if (nicDevice == null || StringUtils.isEmpty(nicDevice.getAddress())) {
                nicsToReorder.add(nic);
                // We know that all the NICs have a MAC address
                macsToReorder.add(nic.getMacAddress());
            }
        }

        // Sorting the NICs to reorder by name
        Collections.sort(nicsToReorder, new Comparator<VmNic>() {
            @Override
            public int compare(VmNic nic1, VmNic nic2) {
                return nic1.getName().compareTo(nic2.getName());
            }
        });

        // Sorting the MAC addresses to reorder
        Collections.sort(macsToReorder);
        for (int i = 0; i < nicsToReorder.size(); ++i) {
            VmNic nic = nicsToReorder.get(i);
            nic.setMacAddress(macsToReorder.get(i));
            getVmNicDao().update(nic);
        }
    }
}
<commit_msg>core: ReorderVmNicsCommand NameableComparator<commit_after>package org.ovirt.engine.core.bll.network.vm;

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

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.bll.VmCommand;
import org.ovirt.engine.core.common.action.VmOperationParameterBase;
import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.core.common.businessentities.VmDevice;
import org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType;
import org.ovirt.engine.core.common.businessentities.comparators.NameableComparator;
import org.ovirt.engine.core.common.businessentities.network.VmNic;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;

public class ReorderVmNicsCommand<T extends VmOperationParameterBase> extends VmCommand<T> {

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

    @Override
    protected boolean canDoAction() {
        VM vm = getVm();
        if (vm == null || vm.getStaticData() == null) {
            addCanDoActionMessage(EngineMessage.ACTION_TYPE_FAILED_VM_NOT_FOUND);
            return false;
        }

        if (!canRunActionOnNonManagedVm()) {
            return false;
        }

        return true;
    }

    @Override
    protected void executeVmCommand() {
        reorderNics();
        setSucceeded(true);
    }

    private Map<Guid, VmDevice> getVmInterfaceDevices() {
        List<VmDevice> vmInterfaceDevicesList = getVmDeviceDao().getVmDeviceByVmIdAndType(getParameters().getVmId(), VmDeviceGeneralType.INTERFACE);
        Map<Guid, VmDevice> vmInterfaceDevices = new HashMap<>();
        for (VmDevice device : vmInterfaceDevicesList) {
            vmInterfaceDevices.put(device.getDeviceId(), device);
        }
        return vmInterfaceDevices;
    }

    private void reorderNics() {
        Map<Guid, VmDevice> vmInterfaceDevices = getVmInterfaceDevices();
        List<VmNic> nics = getVmNicDao().getAllForVm(getParameters().getVmId());
        List<VmNic> nicsToReorder = new ArrayList<>();
        List<String> macsToReorder = new ArrayList<>();

        for (VmNic nic : nics) {
            VmDevice nicDevice = vmInterfaceDevices.get(nic.getId());
            // If there is not device, or the PCI address is empty
            if (nicDevice == null || StringUtils.isEmpty(nicDevice.getAddress())) {
                nicsToReorder.add(nic);
                // We know that all the NICs have a MAC address
                macsToReorder.add(nic.getMacAddress());
            }
        }

        // Sorting the NICs to reorder by name
        Collections.sort(nicsToReorder, new NameableComparator());

        // Sorting the MAC addresses to reorder
        Collections.sort(macsToReorder);
        for (int i = 0; i < nicsToReorder.size(); ++i) {
            VmNic nic = nicsToReorder.get(i);
            nic.setMacAddress(macsToReorder.get(i));
            getVmNicDao().update(nic);
        }
    }
}

{'fraction_non_alphanumeric': 0.10747887099346197, 'fraction_numerical': 0.0009567852017222134, 'mean_word_length': 2.876390605686032, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 28, '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': '4829331', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1909, 'n_words': 355}