Reduce ObjectRegistration use
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationServiceAdapter.java
index 71620cb202b35bc5307c6130226806639b8d6c98..10c5040a0a7bfdff79c1b65f8673073823e2b830 100644 (file)
@@ -7,77 +7,71 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ClassToInstanceMap;
 import com.google.common.collect.ImmutableSet;
+import java.util.HashMap;
 import java.util.Set;
+import java.util.concurrent.Executor;
 import org.opendaylight.mdsal.binding.api.NotificationService;
 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
-import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
 import org.opendaylight.mdsal.dom.api.DOMService;
-import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.NotificationListener;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Notification;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
+@VisibleForTesting
+// FIXME: 10.0.0: make this class final
 public class BindingDOMNotificationServiceAdapter implements NotificationService {
-
     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
 
-    private final BindingNormalizedNodeSerializer codec;
+    private final AdapterContext adapterContext;
     private final DOMNotificationService domNotifService;
 
-    public BindingDOMNotificationServiceAdapter(final DOMNotificationService domNotifService,
-            final BindingNormalizedNodeSerializer codec) {
-        this.codec = codec;
-        this.domNotifService = domNotifService;
-    }
-
-    @Deprecated
-    public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
+    public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
             final DOMNotificationService domNotifService) {
-        this(domNotifService, codec);
+        this.adapterContext = requireNonNull(adapterContext);
+        this.domNotifService = domNotifService;
     }
 
     @Override
-    public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
-        final BindingDOMNotificationListenerAdapter domListener
-                = new BindingDOMNotificationListenerAdapter(codec, listener);
-        final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
-                domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
-        return new ListenerRegistrationImpl<>(listener, domRegistration);
+    public <N extends Notification<N> & DataObject> Registration registerListener(final Class<N> type,
+            final Listener<N> listener, final Executor executor) {
+        final var domListener = new BindingDOMNotificationListenerAdapter<>(adapterContext, type, listener, executor);
+        return domNotifService.registerNotificationListener(domListener, Set.of(domListener.schemaPath()));
     }
 
-    private static class ListenerRegistrationImpl<T extends NotificationListener>
-            extends AbstractListenerRegistration<T> {
-        private final ListenerRegistration<?> listenerRegistration;
-
-        ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
-            super(listener);
-            this.listenerRegistration = listenerRegistration;
+    @Override
+    public Registration registerCompositeListener(final CompositeListener listener, final Executor executor) {
+        final var exec = requireNonNull(executor);
+        final var listeners = new HashMap<Absolute, DOMNotificationListener>();
+        for (var constituent : listener.constituents()) {
+            final var domListener = new BindingDOMNotificationListenerAdapter<>(adapterContext, constituent, exec);
+            listeners.put(domListener.schemaPath(), domListener);
         }
 
-        @Override
-        protected void removeRegistration() {
-            listenerRegistration.close();
-        }
+        return domNotifService.registerNotificationListeners(listeners);
     }
 
     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
+        Builder(final AdapterContext adapterContext) {
+            super(adapterContext);
+        }
 
         @Override
-        protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
-                final ClassToInstanceMap<DOMService> delegates) {
-            final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
-            return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
+        protected NotificationService createInstance(final ClassToInstanceMap<DOMService<?, ?>> delegates) {
+            return new BindingDOMNotificationServiceAdapter(adapterContext(),
+                delegates.getInstance(DOMNotificationService.class));
         }
 
         @Override
-        public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
+        public Set<? extends Class<? extends DOMService<?, ?>>> getRequiredDelegates() {
             return ImmutableSet.of(DOMNotificationService.class);
         }
     }
-
-    public DOMNotificationService getDomService() {
-        return domNotifService;
-    }
 }