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.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;
@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 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);
}
return identifier;
}
+ /**
+ * Return the list of current listeners. Any caller wishing to use this method
+ * has to make sure the collection remains unchanged while it's executing. This
+ * means the caller has to synchronize externally both the registration and
+ * unregistration process.
+ *
+ * @return the list of current listeners
+ */
@SuppressWarnings({ "rawtypes", "unchecked" })
public Collection<org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration<?>> getListeners() {
- // FIXME: this is not thread-safe and races with listener (un)registration!
return (Collection) listeners;
}
}
/**
- *
* Registers listener on this node.
*
* @param path Full path on which listener is registered.
* @param scope Scope of triggering event.
* @return
*/
- public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L> registerDataChangeListener(final InstanceIdentifier path,
+ public synchronized <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L> registerDataChangeListener(final InstanceIdentifier path,
final L listener, final DataChangeScope scope) {
DataChangeListenerRegistration<L> listenerReg = new DataChangeListenerRegistration<L>(path,listener, scope, this);
listeners.add(listenerReg);
+ LOG.debug("Listener {} registered", listener);
return listenerReg;
}
- private void removeListener(final DataChangeListenerRegistration<?> listener) {
+ private synchronized void removeListener(final DataChangeListenerRegistration<?> listener) {
listeners.remove(listener);
+ LOG.debug("Listener {} unregistered", listener);
removeThisIfUnused();
}