<commit_before>/*
 * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
 *
 * 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
 */
package org.opendaylight.netvirt.elan.cache;

import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
import org.opendaylight.infrautils.caches.CacheProvider;
import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Caches ElanInstances.
 *
 * @author <NAME>
 */
@Singleton
public class ElanInstanceCache extends InstanceIdDataObjectCache<ElanInstance> {
    private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceCache.class);

    private final Map<InstanceIdentifier<ElanInstance>, Collection<Runnable>> waitingJobs = new HashMap<>();

    @Inject
    public ElanInstanceCache(DataBroker dataBroker, CacheProvider cacheProvider) {
        super(ElanInstance.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
                InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class), cacheProvider);
    }

    public Optional<ElanInstance> get(String elanInstanceName) {
        try {
            return get(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
        } catch (ReadFailedException e) {
            LOG.warn("Error reading ElanInstance {}", elanInstanceName, e);
            return Optional.absent();
        }
    }<commit_msg>Elan instance cache cleanup delay<commit_after>/*
 * Copyright (c) 2017 <NAME> and others.  All rights reserved.
 *
 * 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
 */
package org.opendaylight.netvirt.elan.cache;

import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
import org.opendaylight.infrautils.caches.CacheProvider;
import org.opendaylight.netvirt.elan.utils.Scheduler;
import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Caches ElanInstances.
 *
 * @author <NAME>
 */
@Singleton
public class ElanInstanceCache extends InstanceIdDataObjectCache<ElanInstance> {
    private static final Logger LOG = LoggerFactory.getLogger(ElanInstanceCache.class);

    //TODO: Make it configurable
    private static final int ELAN_CLEANUP_DELAY_IN_MINS = 30;

    private final Map<InstanceIdentifier<ElanInstance>, Collection<Runnable>> waitingJobs = new HashMap<>();

    private final Scheduler scheduler;

    @Inject
    public ElanInstanceCache(DataBroker dataBroker, CacheProvider cacheProvider, Scheduler scheduler) {
        super(ElanInstance.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
                InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class), cacheProvider);
        this.scheduler = scheduler;
    }

    @Override
    protected void removed(InstanceIdentifier<ElanInstance> path, ElanInstance dataObject) {
        scheduler.getScheduledExecutorService().schedule(() -> {
            super.removed(path, dataObject);
        }, ELAN_CLEANUP_DELAY_IN_MINS, TimeUnit.MINUTES);
    }

    public Optional<ElanInstance> get(String elanInstanceName) {
        try {
            return get(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
        } catch (ReadFailedException e) {
            LOG.warn("Error reading ElanInstance {}", elanInstanceName, e);
            return Optional.absent();
        }
    }
{'fraction_non_alphanumeric': 0.09649970992071166, 'fraction_numerical': 0.009669309611293754, 'mean_word_length': 4.6960352422907485, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, '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': '538408', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1554, 'n_words': 291}