<commit_before>/*
 * Copyright (c) 2013 Cisco Systems, Inc. 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.bgpcep.bgp.topology.provider;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.opendaylight.bgpcep.topology.TopologyReference;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.protocol.bgp.rib.RibReference;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractTopologyBuilder<T extends Route> implements AutoCloseable, DataChangeListener, LocRIBListener, TopologyReference {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractTopologyBuilder.class);
    private final RibReference locRibReference;
    private final InstanceIdentifier<Topology> topology;
    private final DataBroker dataProvider;
    private final Class<T> idClass;

    protected AbstractTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
            final TopologyId topologyId, final TopologyTypes types, final Class<T> idClass) {
        this.dataProvider = Preconditions.checkNotNull(dataProvider);
        this.locRibReference = Preconditions.checkNotNull(locRibReference);
        this.idClass = Preconditions.checkNotNull(idClass);

        final TopologyKey tk = new TopologyKey(Preconditions.checkNotNull(topologyId));
        this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, tk).toInstance();

        LOG.debug("Initiating topology builder from {} at {}", locRibReference, this.topology);

        final ReadWriteTransaction t = dataProvider.newReadWriteTransaction();
        final Optional<Topology> o;
        try {
            o = t.read(LogicalDatastoreType.OPERATIONAL, this.topology).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new IllegalStateException("Failed to read topology " + topology, e);
        }
        Preconditions.checkState(!o.isPresent(), "Data provider conflict detected on object {}", this.topology);

        t.put(LogicalDatastoreType.OPERATIONAL, this.topology,
                new TopologyBuilder().setKey(tk).setServerProvided(Boolean.TRUE).setTopologyTypes(types).build());
        Futures.addCallback(t.submit(), new FutureCallback<Void>() {
            @Override
            public void onSuccess(final Void result) {
                LOG.trace("Transaction {} committed successfully", t.getIdentifier());
            }

            @Override
            public void onFailure(final Throwable t) {
                LOG.error("Failed to initiate topology {} by listener {}", AbstractTopologyBuilder.this.topology,
                        AbstractTopologyBuilder.this, t);
            }
        });
    }

    public final InstanceIdentifier<Tables> tableInstanceIdentifier(final Class<? extends AddressFamily> afi,
            final Class<? extends SubsequentAddressFamily> safi) {
        return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).toInstance();
    }

    protected abstract void createObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);

    protected abstract void removeObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);

    public final DataBroker getDataProvider() {
        return this.dataProvider;
    }

    @Override
    public final InstanceIdentifier<Topology> getInstanceIdentifier() {
        return this.topology;
    }

    private void addIdentifier(final InstanceIdentifier<?> i, final String set, final Set<InstanceIdentifier<T>> out) {
        final InstanceIdentifier<T> id = i.firstIdentifierOf(this.idClass);
        if (id != null) {
            out.add(id);
        } else {
            LOG.debug("Identifier {} in {} set does not contain listening class {}, ignoring it", i, set, this.idClass);
        }
    }

    @Override
    public final void onLocRIBChange(final ReadWriteTransaction trans,
            final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
        LOG.debug("Received data change {} event with transaction {}", event, trans.getIdentifier());

        // FIXME: speed this up
        final Set<InstanceIdentifier<T>> ids = new HashSet<>();
        for (final InstanceIdentifier<?> i : event.getRemovedPaths()) {
            addIdentifier(i, "remove", ids);
        }
        for (final InstanceIdentifier<?> i : event.getUpdatedData().keySet()) {
            addIdentifier(i, "update", ids);
        }
        for (final InstanceIdentifier<?> i : event.getCreatedData().keySet()) {
            addIdentifier(i, "create", ids);
        }

        final Map<InstanceIdentifier<?>, ? extends DataObject> o = event.getOriginalData();
        final Map<InstanceIdentifier<?>, DataObject> n = event.getUpdatedData();
        for (final InstanceIdentifier<T> i : ids) {
            final T oldValue = this.idClass.cast(o.get(i));
            final T newValue = this.idClass.cast(n.get(i));

            LOG.debug("Updating object {} value {} -> {}", i, oldValue, newValue);
            if (oldValue != null) {
                removeObject(trans, i, oldValue);
            }
            if (newValue != null) {
                createObject(trans, i, newValue);
            }
        }

        Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
            @Override
            public void onSuccess(final Void result) {
                LOG.trace("Transaction {} committed successfully", trans.getIdentifier());
            }

            @Override
            public void onFailure(final Throwable t) {
                LOG.error("Failed to propagate change by listener {}", AbstractTopologyBuilder.this);
            }
        });
    }

    @Override
    public final void close() throws TransactionCommitFailedException {
        LOG.info("Shutting down builder for {}", getInstanceIdentifier());
        final WriteTransaction trans = this.dataProvider.newWriteOnlyTransaction();
        trans.delete(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier());
        trans.submit().checkedGet();
    }

    @Override
    public final void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
        final ReadWriteTransaction trans = this.dataProvider.newReadWriteTransaction();

        try {
            onLocRIBChange(trans, change);
        } catch (final RuntimeException e) {
            LOG.warn("Data change {} was not completely propagated to listener {}", change, this, e);
            return;
        }
    }
}
<commit_msg>Fixed lookup of created routes in BGP Topology Exporter.<commit_after>/*
 * Copyright (c) 2013 Cisco Systems, Inc. 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.bgpcep.bgp.topology.provider;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.opendaylight.bgpcep.topology.TopologyReference;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.protocol.bgp.rib.RibReference;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractTopologyBuilder<T extends Route> implements AutoCloseable, DataChangeListener, LocRIBListener, TopologyReference {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractTopologyBuilder.class);
    private final RibReference locRibReference;
    private final InstanceIdentifier<Topology> topology;
    private final DataBroker dataProvider;
    private final Class<T> idClass;

    protected AbstractTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
            final TopologyId topologyId, final TopologyTypes types, final Class<T> idClass) {
        this.dataProvider = Preconditions.checkNotNull(dataProvider);
        this.locRibReference = Preconditions.checkNotNull(locRibReference);
        this.idClass = Preconditions.checkNotNull(idClass);

        final TopologyKey tk = new TopologyKey(Preconditions.checkNotNull(topologyId));
        this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, tk).toInstance();

        LOG.debug("Initiating topology builder from {} at {}", locRibReference, this.topology);

        final ReadWriteTransaction t = dataProvider.newReadWriteTransaction();
        final Optional<Topology> o;
        try {
            o = t.read(LogicalDatastoreType.OPERATIONAL, this.topology).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new IllegalStateException("Failed to read topology " + topology, e);
        }
        Preconditions.checkState(!o.isPresent(), "Data provider conflict detected on object {}", this.topology);

        t.put(LogicalDatastoreType.OPERATIONAL, this.topology,
                new TopologyBuilder().setKey(tk).setServerProvided(Boolean.TRUE).setTopologyTypes(types).build());
        Futures.addCallback(t.submit(), new FutureCallback<Void>() {
            @Override
            public void onSuccess(final Void result) {
                LOG.trace("Transaction {} committed successfully", t.getIdentifier());
            }

            @Override
            public void onFailure(final Throwable t) {
                LOG.error("Failed to initiate topology {} by listener {}", AbstractTopologyBuilder.this.topology,
                        AbstractTopologyBuilder.this, t);
            }
        });
    }

    public final InstanceIdentifier<Tables> tableInstanceIdentifier(final Class<? extends AddressFamily> afi,
            final Class<? extends SubsequentAddressFamily> safi) {
        return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).toInstance();
    }

    protected abstract void createObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);

    protected abstract void removeObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);

    public final DataBroker getDataProvider() {
        return this.dataProvider;
    }

    @Override
    public final InstanceIdentifier<Topology> getInstanceIdentifier() {
        return this.topology;
    }

    private void addIdentifier(final InstanceIdentifier<?> i, final String set, final Set<InstanceIdentifier<T>> out) {
        final InstanceIdentifier<T> id = i.firstIdentifierOf(this.idClass);
        if (id != null) {
            out.add(id);
        } else {
            LOG.debug("Identifier {} in {} set does not contain listening class {}, ignoring it", i, set, this.idClass);
        }
    }

    @Override
    public final void onLocRIBChange(final ReadWriteTransaction trans,
            final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
        LOG.debug("Received data change {} event with transaction {}", event, trans.getIdentifier());

        // FIXME: speed this up
        final Set<InstanceIdentifier<T>> ids = new HashSet<>();
        for (final InstanceIdentifier<?> i : event.getRemovedPaths()) {
            addIdentifier(i, "remove", ids);
        }
        for (final InstanceIdentifier<?> i : event.getUpdatedData().keySet()) {
            addIdentifier(i, "update", ids);
        }
        for (final InstanceIdentifier<?> i : event.getCreatedData().keySet()) {
            addIdentifier(i, "create", ids);
        }

        final Map<InstanceIdentifier<?>, ? extends DataObject> o = event.getOriginalData();
        final Map<InstanceIdentifier<?>, DataObject> u = event.getUpdatedData();
        final Map<InstanceIdentifier<?>, DataObject> c = event.getCreatedData();
        for (final InstanceIdentifier<T> i : ids) {
            final T oldValue = this.idClass.cast(o.get(i));
            T newValue = this.idClass.cast(u.get(i));
            if (newValue == null) {
                newValue = this.idClass.cast(c.get(i));
            }

            LOG.debug("Updating object {} value {} -> {}", i, oldValue, newValue);
            if (oldValue != null) {
                removeObject(trans, i, oldValue);
            }
            if (newValue != null) {
                createObject(trans, i, newValue);
            }
        }

        Futures.addCallback(trans.submit(), new FutureCallback<Void>() {
            @Override
            public void onSuccess(final Void result) {
                LOG.trace("Transaction {} committed successfully", trans.getIdentifier());
            }

            @Override
            public void onFailure(final Throwable t) {
                LOG.error("Failed to propagate change by listener {}", AbstractTopologyBuilder.this);
            }
        });
    }

    @Override
    public final void close() throws TransactionCommitFailedException {
        LOG.info("Shutting down builder for {}", getInstanceIdentifier());
        final WriteTransaction trans = this.dataProvider.newWriteOnlyTransaction();
        trans.delete(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier());
        trans.submit().checkedGet();
    }

    @Override
    public final void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
        final ReadWriteTransaction trans = this.dataProvider.newReadWriteTransaction();

        try {
            onLocRIBChange(trans, change);
        } catch (final RuntimeException e) {
            LOG.warn("Data change {} was not completely propagated to listener {}", change, this, e);
            return;
        }
    }
}

{'fraction_non_alphanumeric': 0.10891143106457242, 'fraction_numerical': 0.01025305410122164, 'mean_word_length': 3.4702584105314482, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 24}, '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': '4746071', 'n_tokens_mistral': 5355, 'n_tokens_neox': 5161, 'n_words': 1001}