Pick up byte-buddy from yangtools
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMStoreTreeChangePublisher.java
index d98697ecde6c679e29e7973643f3420a6429f00a..87b6612bd3c0f8ecbf2a3ce771230acc5969209b 100644 (file)
@@ -7,20 +7,15 @@
  */
 package org.opendaylight.mdsal.dom.store.inmemory;
 
-import static com.google.common.base.Preconditions.checkState;
-
 import java.util.List;
-import java.util.Optional;
-import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executor;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
-import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTreeChangePublisher;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.util.concurrent.EqualityQueuedNotificationManager;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidates;
@@ -31,22 +26,20 @@ final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChan
     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
 
     // Registrations use identity for equality, hence we can skip wrapping them
-    private final EqualityQueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
-        notificationManager;
+    private final EqualityQueuedNotificationManager<Reg, DataTreeCandidate> notificationManager;
 
-    InMemoryDOMStoreTreeChangePublisher(final String dsName, final ExecutorService listenerExecutor,
-            final int maxQueueSize) {
+    InMemoryDOMStoreTreeChangePublisher(final String dsName, final Executor listenerExecutor, final int maxQueueSize) {
         notificationManager = new EqualityQueuedNotificationManager<>("DataTreeChangeListenerQueueMgr + dsName",
             listenerExecutor, maxQueueSize,
             (listener, notifications) -> {
-                if (!listener.isClosed()) {
-                    listener.getInstance().onDataTreeChanged(notifications);
+                if (listener.notClosed()) {
+                    listener.listener().onDataTreeChanged(notifications);
                 }
             });
     }
 
-    private InMemoryDOMStoreTreeChangePublisher(final EqualityQueuedNotificationManager<
-            AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
+    private InMemoryDOMStoreTreeChangePublisher(
+            final EqualityQueuedNotificationManager<Reg, DataTreeCandidate> notificationManager) {
         this.notificationManager = notificationManager;
     }
 
@@ -55,49 +48,48 @@ final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChan
     }
 
     @Override
-    protected void notifyListener(final AbstractDOMDataTreeChangeListenerRegistration<?> registration,
-            final List<DataTreeCandidate> changes) {
+    protected void notifyListener(final Reg registration, final List<DataTreeCandidate> changes) {
         LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
         notificationManager.submitNotifications(registration, changes);
     }
 
     @Override
-    protected synchronized void registrationRemoved(
-            final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
+    protected synchronized void registrationRemoved(final Reg registration) {
         LOG.debug("Closing registration {}", registration);
 
         // FIXME: remove the queue for this registration and make sure we clear it
     }
 
-    <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
-            final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
-        final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
-        final Optional<NormalizedNode> preExistingData = snapshot.readNode(YangInstanceIdentifier.empty());
-        if (!preExistingData.isPresent()) {
+    Registration registerTreeChangeListener(final YangInstanceIdentifier treeId,
+            final DOMDataTreeChangeListener listener, final DataTreeSnapshot snapshot) {
+        final var reg = registerTreeChangeListener(treeId, listener);
+        final var preExistingData = snapshot.readNode(YangInstanceIdentifier.of());
+        if (preExistingData.isEmpty()) {
             listener.onInitialData();
             return reg;
         }
 
-        final NormalizedNode data = preExistingData.get();
+        final var data = preExistingData.orElseThrow();
         if (treeId.isEmpty()) {
-            checkState(data instanceof DataContainerNode, "Unexpected root node %s", data);
-            if (((DataContainerNode) data).isEmpty()) {
-                // If we are listening on root of data tree we still get empty normalized node, root is always present,
-                // we should filter this out separately and notify it by 'onInitialData()' once.
-                // Otherwise, it is just a valid data node with empty value which also should be notified by
-                // "onDataTreeChanged(List<DataTreeCandidate>)".
-                listener.onInitialData();
-                return reg;
+            if (data instanceof DataContainerNode container) {
+                if (container.isEmpty()) {
+                    // If we are listening on root of data tree we still get empty normalized node, root is always
+                    // present, we should filter this out separately and notify it by 'onInitialData()' once.
+                    // Otherwise, it is just a valid data node with empty value which also should be notified by
+                    // "onDataTreeChanged(List<DataTreeCandidate>)".
+                    listener.onInitialData();
+                    return reg;
+                }
+            } else {
+                throw new IllegalStateException("Unexpected root node type " + data.contract());
             }
         }
 
-        final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.empty(), data);
-        final InMemoryDOMStoreTreeChangePublisher publisher = new InMemoryDOMStoreTreeChangePublisher(
-            notificationManager);
+        final var candidate = DataTreeCandidates.fromNormalizedNode(YangInstanceIdentifier.of(), data);
+        final var publisher = new InMemoryDOMStoreTreeChangePublisher(notificationManager);
         publisher.registerTreeChangeListener(treeId, listener);
         if (!publisher.publishChange(candidate)) {
-            // There is no data in the conceptual data tree then
-            // notify with 'onInitialData()'.
+            // There is no data in the conceptual data tree then notify with 'onInitialData()'.
             listener.onInitialData();
         }