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

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.lang.math.LongRange;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.businessentities.network.VmNic;
import org.ovirt.engine.core.common.errors.VdcBLLException;
import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
import org.ovirt.engine.core.utils.MacAddressRangeUtils;
import org.ovirt.engine.core.utils.log.Log;
import org.ovirt.engine.core.utils.log.LogFactory;

public class MacPoolManagerRanges implements MacPoolManagerStrategy {

    private static final Log log = LogFactory.getLog(MacPoolManagerRanges.class);

    private final String rangesString;

    private final ReentrantReadWriteLock lockObj = new ReentrantReadWriteLock();
    private final boolean allowDuplicates;
    private boolean initialized;
    private MacsStorage macsStorage;

    public MacPoolManagerRanges(String rangesString, boolean allowDuplicates) {
        this.rangesString = rangesString;
        this.allowDuplicates = allowDuplicates;
    }

    @Override
    public void initialize() {
        lockObj.writeLock().lock();

        try {
            if (initialized) {
                log.error("Trying to initialized " + getClass().getName() + " multiple times.");
                return;
            }

            log.infoFormat("Start initializing " + getClass().getSimpleName());

            this.macsStorage = createMacsStorage(rangesString);
            List<VmNic> interfaces = getVmNicInterfacesFromDb();

            for (VmNic iface : interfaces) {
                forceAddMacWithoutLocking(iface.getMacAddress());
            }
            initialized = true;
            log.infoFormat("Finished initializing. Available MACs in pool: {0}", macsStorage.getAvailableMacsCount());
        } catch (Exception ex) {
            log.errorFormat("Error in initializing MAC Addresses pool manager.", ex);
        } finally {
            lockObj.writeLock().unlock();
        }
    }

    private MacsStorage createMacsStorage(String rangesString) {
        Collection<LongRange> rangesBoundaries = MacAddressRangeUtils.parseRangeString(rangesString);
        MacsStorage macsStorage = new MacsStorage(allowDuplicates);
        for (LongRange range : rangesBoundaries) {
            macsStorage.addRange(range.getMinimumLong(), range.getMaximumLong());
        }

        if (macsStorage.availableMacExist()) {
            return macsStorage;
        } else {
            throw new VdcBLLException(VdcBllErrors.MAC_POOL_INITIALIZATION_FAILED);
        }
    }
<commit_msg>engine: Fix MAC pool initalization<commit_after>package org.ovirt.engine.core.bll.network;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.lang.math.LongRange;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.businessentities.network.VmNic;
import org.ovirt.engine.core.common.errors.VdcBLLException;
import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
import org.ovirt.engine.core.utils.MacAddressRangeUtils;
import org.ovirt.engine.core.utils.log.Log;
import org.ovirt.engine.core.utils.log.LogFactory;

public class MacPoolManagerRanges implements MacPoolManagerStrategy {

    private static final Log log = LogFactory.getLog(MacPoolManagerRanges.class);

    private final String rangesString;

    private final ReentrantReadWriteLock lockObj = new ReentrantReadWriteLock();
    private final boolean allowDuplicates;
    private boolean initialized;
    private MacsStorage macsStorage;

    public MacPoolManagerRanges(String rangesString, boolean allowDuplicates) {
        this.rangesString = rangesString;
        this.allowDuplicates = allowDuplicates;
    }

    @Override
    public void initialize() {
        lockObj.writeLock().lock();

        try {
            if (initialized) {
                log.error("Trying to initialized " + getClass().getName() + " multiple times.");
                return;
            }

            log.infoFormat("Start initializing " + getClass().getSimpleName());

            this.macsStorage = createMacsStorage(rangesString);
            List<VmNic> interfaces = getVmNicInterfacesFromDb();

            for (VmNic iface : interfaces) {
                if (iface.getMacAddress() != null) {
                    forceAddMacWithoutLocking(iface.getMacAddress());
                }
            }
            initialized = true;
            log.infoFormat("Finished initializing. Available MACs in pool: {0}", macsStorage.getAvailableMacsCount());
        } catch (Exception ex) {
            log.errorFormat("Error in initializing MAC Addresses pool manager.", ex);
        } finally {
            lockObj.writeLock().unlock();
        }
    }

    private MacsStorage createMacsStorage(String rangesString) {
        Collection<LongRange> rangesBoundaries = MacAddressRangeUtils.parseRangeString(rangesString);
        MacsStorage macsStorage = new MacsStorage(allowDuplicates);
        for (LongRange range : rangesBoundaries) {
            macsStorage.addRange(range.getMinimumLong(), range.getMaximumLong());
        }

        if (macsStorage.availableMacExist()) {
            return macsStorage;
        } else {
            throw new VdcBLLException(VdcBllErrors.MAC_POOL_INITIALIZATION_FAILED);
        }
    }

{'fraction_non_alphanumeric': 0.09431438127090301, 'fraction_numerical': 0.00033444816053511704, 'mean_word_length': 3.194249649368864, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7397137', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1685, 'n_words': 303}