import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.append;
import static org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils.getChild;
-import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
import com.google.common.collect.ImmutableSet;
public class DataChangeEventResolver {
-
-
- private static final Logger LOG = LoggerFactory.getLogger(DataChangeEventResolver.class);
-
+ private static final Logger LOG = LoggerFactory.getLogger(DataChangeEventResolver.class);
private static final DOMImmutableDataChangeEvent NO_CHANGE = builder().build();
+ private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
private InstanceIdentifier rootPath;
private ListenerRegistrationNode listenerRoot;
private NodeModification modificationRoot;
private Optional<StoreMetadataNode> beforeRoot;
private Optional<StoreMetadataNode> afterRoot;
- private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
protected InstanceIdentifier getRootPath() {
return rootPath;
builder.merge(resolveCreateEvent(childPath, childListeners, child));
}
- DOMImmutableDataChangeEvent event = builder.build();
- if (listeners.isPresent()) {
- addNotifyTask(listeners.get().getListeners(), event);
- }
- return event;
+ return addNotifyTask(listeners, builder.build());
}
private DOMImmutableDataChangeEvent resolveDeleteEvent(final InstanceIdentifier path,
InstanceIdentifier childPath = StoreUtils.append(path, childId);
builder.merge(resolveDeleteEvent(childPath, childListeners, child));
}
- DOMImmutableDataChangeEvent event = builder.build();
- if (listeners.isPresent()) {
- addNotifyTask(listeners.get().getListeners(), event);
- }
- return event;
-
+ return addNotifyTask(listeners, builder.build());
}
private DOMImmutableDataChangeEvent resolveSubtreeChangeEvent(final InstanceIdentifier path,
return builder().build();
}
- private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope scope,
- final DOMImmutableDataChangeEvent event) {
- Collection<DataChangeListenerRegistration<?>> potential = listenerRegistrationNode.getListeners();
- if(potential.isEmpty()) {
- return;
- }
- ArrayList<DataChangeListenerRegistration<?>> toNotify = new ArrayList<>(potential.size());
- for(DataChangeListenerRegistration<?> listener : potential) {
- if(scope.equals(listener.getScope())) {
- toNotify.add(listener);
+ private DOMImmutableDataChangeEvent addNotifyTask(final Optional<ListenerRegistrationNode> listeners, final DOMImmutableDataChangeEvent event) {
+ if (listeners.isPresent()) {
+ final Collection<DataChangeListenerRegistration<?>> l = listeners.get().getListeners();
+ if (!l.isEmpty()) {
+ tasks.add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(l), event));
}
}
- addNotifyTask(toNotify, event);
+ return event;
}
- private void addNotifyTask(final Collection<DataChangeListenerRegistration<?>> listeners,
+ private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope scope,
final DOMImmutableDataChangeEvent event) {
- if(!listeners.isEmpty()) {
- tasks.add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(listeners),event));
+ Collection<DataChangeListenerRegistration<?>> potential = listenerRegistrationNode.getListeners();
+ if(!potential.isEmpty()) {
+ final Set<DataChangeListenerRegistration<?>> toNotify = new HashSet<>(potential.size());
+ for(DataChangeListenerRegistration<?> listener : potential) {
+ if(scope.equals(listener.getScope())) {
+ toNotify.add(listener);
+ }
+ }
+
+ if (!toNotify.isEmpty()) {
+ tasks.add(new ChangeListenerNotifyTask(toNotify, event));
+ }
}
}
public static DataChangeEventResolver create() {
return new DataChangeEventResolver();
}
-
-
-
}
import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
import org.opendaylight.yangtools.concepts.Identifiable;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Objects;
+import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedLong;
@Override
public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
final InstanceIdentifier path, final L listener, final DataChangeScope scope) {
- LOG.debug("{}: Registering data change listener {} for {}",name,listener,path);
- ListenerRegistrationNode listenerNode = listenerTree;
- for(PathArgument arg : path.getPath()) {
- listenerNode = listenerNode.ensureChild(arg);
- }
/*
* Make sure commit is not occurring right now. Listener has to be registered and its
*/
final DataChangeListenerRegistration<L> reg;
synchronized (this) {
+ LOG.debug("{}: Registering data change listener {} for {}",name,listener,path);
+ ListenerRegistrationNode listenerNode = listenerTree;
+ for(PathArgument arg : path.getPath()) {
+ listenerNode = listenerNode.ensureChild(arg);
+ }
+
reg = listenerNode.registerDataChangeListener(path, listener, scope);
Optional<StoreMetadataNode> currentState = snapshot.get().read(path);
}
}
- return reg;
+ return new AbstractListenerRegistration<L>(listener) {
+ @Override
+ protected void removeRegistration() {
+ synchronized (InMemoryDOMDataStore.this) {
+ reg.close();
+ }
+ }
+ };
}
private synchronized DOMStoreThreePhaseCommitCohort submit(
}
private void commit(final DataAndMetadataSnapshot currentSnapshot,
- final StoreMetadataNode newDataTree, final Iterable<ChangeListenerNotifyTask> listenerTasks) {
+ final StoreMetadataNode newDataTree, final DataChangeEventResolver listenerResolver) {
LOG.debug("Updating Store snaphot version: {} with version:{}",currentSnapshot.getMetadataTree().getSubtreeVersion(),newDataTree.getSubtreeVersion());
if(LOG.isTraceEnabled()) {
final boolean success = snapshot.compareAndSet(currentSnapshot, newSnapshot);
checkState(success, "Store snapshot and transaction snapshot differ. This should never happen.");
- for (ChangeListenerNotifyTask task : listenerTasks) {
+ for (ChangeListenerNotifyTask task : listenerResolver.resolve()) {
executor.submit(task);
}
}
}
- private static class SnapshotBackedReadTransaction implements DOMStoreReadTransaction {
-
- private DataAndMetadataSnapshot stableSnapshot;
+ private static abstract class AbstractDOMStoreTransaction implements DOMStoreTransaction {
private final Object identifier;
- public SnapshotBackedReadTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot) {
+ protected AbstractDOMStoreTransaction(final Object identifier) {
this.identifier = identifier;
- this.stableSnapshot = snapshot;
- LOG.debug("ReadOnly Tx: {} allocated with snapshot {}",identifier,snapshot.getMetadataTree().getSubtreeVersion());
-
}
@Override
- public Object getIdentifier() {
+ public final Object getIdentifier() {
return identifier;
}
+ @Override
+ public final String toString() {
+ return addToStringAttributes(Objects.toStringHelper(this)).toString();
+ }
+
+ /**
+ * Add class-specific toString attributes.
+ *
+ * @param toStringHelper ToStringHelper instance
+ * @return ToStringHelper instance which was passed in
+ */
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return toStringHelper.add("id", identifier);
+ }
+ }
+
+ private static class SnapshotBackedReadTransaction extends AbstractDOMStoreTransaction implements DOMStoreReadTransaction {
+ private DataAndMetadataSnapshot stableSnapshot;
+
+ public SnapshotBackedReadTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot) {
+ super(identifier);
+ this.stableSnapshot = Preconditions.checkNotNull(snapshot);
+ LOG.debug("ReadOnly Tx: {} allocated with snapshot {}", identifier, snapshot.getMetadataTree().getSubtreeVersion());
+ }
+
@Override
public void close() {
+ LOG.debug("Store transaction: {} : Closed", getIdentifier());
stableSnapshot = null;
}
checkState(stableSnapshot != null, "Transaction is closed");
return Futures.immediateFuture(NormalizedNodeUtils.findNode(stableSnapshot.getDataTree(), path));
}
-
- @Override
- public String toString() {
- return "SnapshotBackedReadTransaction [id =" + identifier + "]";
- }
-
}
- private static class SnaphostBackedWriteTransaction implements DOMStoreWriteTransaction {
-
+ private static class SnaphostBackedWriteTransaction extends AbstractDOMStoreTransaction implements DOMStoreWriteTransaction {
private MutableDataTree mutableTree;
- private final Object identifier;
private InMemoryDOMDataStore store;
-
private boolean ready = false;
public SnaphostBackedWriteTransaction(final Object identifier, final DataAndMetadataSnapshot snapshot,
final InMemoryDOMDataStore store, final ModificationApplyOperation applyOper) {
- this.identifier = identifier;
+ super(identifier);
mutableTree = MutableDataTree.from(snapshot, applyOper);
this.store = store;
LOG.debug("Write Tx: {} allocated with snapshot {}",identifier,snapshot.getMetadataTree().getSubtreeVersion());
}
- @Override
- public Object getIdentifier() {
- return identifier;
- }
-
@Override
public void close() {
+ LOG.debug("Store transaction: {} : Closed", getIdentifier());
this.mutableTree = null;
this.store = null;
}
mutableTree.delete(path);
}
- protected boolean isReady() {
+ protected final boolean isReady() {
return ready;
}
- protected void checkNotReady() {
- checkState(!ready, "Transaction is ready. No further modifications allowed.");
+ protected final void checkNotReady() {
+ checkState(!ready, "Transaction %s is ready. No further modifications allowed.", getIdentifier());
}
@Override
public synchronized DOMStoreThreePhaseCommitCohort ready() {
+ checkState(!ready, "Transaction %s is already ready.", getIdentifier());
ready = true;
+
LOG.debug("Store transaction: {} : Ready", getIdentifier());
mutableTree.seal();
return store.submit(this);
}
@Override
- public String toString() {
- return "SnaphostBackedWriteTransaction [id=" + getIdentifier() + ", ready=" + isReady() + "]";
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return toStringHelper.add("ready", isReady());
}
-
}
private static class SnapshotBackedReadWriteTransaction extends SnaphostBackedWriteTransaction implements
public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final InstanceIdentifier path) {
return Futures.immediateFuture(getMutatedView().read(path));
}
-
- @Override
- public String toString() {
- return "SnapshotBackedReadWriteTransaction [id=" + getIdentifier() + ", ready=" + isReady() + "]";
- }
-
}
private class ThreePhaseCommitImpl implements DOMStoreThreePhaseCommitCohort {
private DataAndMetadataSnapshot storeSnapshot;
private Optional<StoreMetadataNode> proposedSubtree;
- private Iterable<ChangeListenerNotifyTask> listenerTasks;
+ private DataChangeEventResolver listenerResolver;
public ThreePhaseCommitImpl(final SnaphostBackedWriteTransaction writeTransaction) {
this.transaction = writeTransaction;
proposedSubtree = operationTree.apply(modification, Optional.of(metadataTree),
increase(metadataTree.getSubtreeVersion()));
- listenerTasks = DataChangeEventResolver.create() //
+ listenerResolver = DataChangeEventResolver.create() //
.setRootPath(PUBLIC_ROOT_PATH) //
.setBeforeRoot(Optional.of(metadataTree)) //
.setAfterRoot(proposedSubtree) //
.setModificationRoot(modification) //
- .setListenerRoot(listenerTree) //
- .resolve();
+ .setListenerRoot(listenerTree);
return null;
}
checkState(proposedSubtree != null,"Proposed subtree must be computed");
checkState(storeSnapshot != null,"Proposed subtree must be computed");
// return ImmediateFuture<>;
- InMemoryDOMDataStore.this.commit(storeSnapshot, proposedSubtree.get(),listenerTasks);
+ InMemoryDOMDataStore.this.commit(storeSnapshot, proposedSubtree.get(),listenerResolver);
return Futures.<Void> immediateFuture(null);
}