<commit_before>/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Authors : <NAME>, <NAME>, <NAME>
 */
package org.opendaylight.ovsdb.openstack.netvirt.impl;

import org.opendaylight.controller.sal.core.Node;
import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class ProviderNetworkManagerImpl implements NetworkingProviderManager {

    static final Logger logger = LoggerFactory.getLogger(ProviderNetworkManagerImpl.class);
    // The provider for each of these services is resolved by the OSGi Service Manager
    private volatile ConfigurationService configurationService;

    private HashMap<Long, ProviderEntry> providers = Maps.newHashMap();
    private HashMap<Node, NetworkingProvider> nodeToProviderMapping = Maps.newHashMap();

    public NetworkingProvider getProvider(Node node) {
        // This will change if/when we move to a commons library
        Preconditions.checkArgument(node.getType().equals("OVS"));

        if (nodeToProviderMapping.get(node) != null) {
            return nodeToProviderMapping.get(node);
        }

        final String targetVersion = configurationService.getOpenflowVersion(node);

        Predicate<ProviderEntry> providerEntryPredicate = new Predicate<ProviderEntry>() {
            @Override
            public boolean apply(ProviderEntry providerEntry) {
                //ToDo: This should match on southboundProtocol and providerType too
                return providerEntry.getProperties().get(Constants.OPENFLOW_VERSION_PROPERTY).equals(targetVersion);
            }
        };

        Iterable<ProviderEntry> matchingProviders = Iterables.filter(providers.values(), providerEntryPredicate);
        if (!matchingProviders.iterator().hasNext()) {<commit_msg>Bug 1461 - precondition in getProvider failing<commit_after>/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Authors : <NAME>, <NAME>, <NAME>
 */
package org.opendaylight.ovsdb.openstack.netvirt.impl;

import java.util.HashMap;
import java.util.Map;

import org.opendaylight.controller.sal.core.Node;
import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

public class ProviderNetworkManagerImpl implements NetworkingProviderManager {

    static final Logger logger = LoggerFactory.getLogger(ProviderNetworkManagerImpl.class);
    // The provider for each of these services is resolved by the OSGi Service Manager
    private volatile ConfigurationService configurationService;

    private HashMap<Long, ProviderEntry> providers = Maps.newHashMap();
    private HashMap<Node, NetworkingProvider> nodeToProviderMapping = Maps.newHashMap();

    @Override
    public NetworkingProvider getProvider(Node node) {
        if (nodeToProviderMapping.get(node) != null) {
            return nodeToProviderMapping.get(node);
        }

        final String targetVersion = Constants.OPENFLOW13;
        /*
         * Since we have hard depedencies on OpenFlow1.3 to get any of the services supported, we are
         * Hardcoding the Openflow13 as the only version that we are interested in
         */
        // final String targetVersion = configurationService.getOpenflowVersion(node);

        Predicate<ProviderEntry> providerEntryPredicate = new Predicate<ProviderEntry>() {
            @Override
            public boolean apply(ProviderEntry providerEntry) {
                //ToDo: This should match on southboundProtocol and providerType too
                return providerEntry.getProperties().get(Constants.OPENFLOW_VERSION_PROPERTY).equals(targetVersion);
            }
        };

        Iterable<ProviderEntry> matchingProviders = Iterables.filter(providers.values(), providerEntryPredicate);
        if (!matchingProviders.iterator().hasNext()) {
{'fraction_non_alphanumeric': 0.08701576795795211, 'fraction_numerical': 0.005839984426708195, 'mean_word_length': 4.0921704658077305, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6445950', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1354, 'n_words': 337}