Reduce ObjectRegistration use
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationServiceAdapter.java
index ce26cbca98224edf2da4bae818bff06faff324e1..10c5040a0a7bfdff79c1b65f8673073823e2b830 100644 (file)
@@ -12,19 +12,21 @@ 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.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.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Notification;
-import org.opendaylight.yangtools.yang.binding.NotificationListener;
+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;
 
@@ -38,38 +40,22 @@ public class BindingDOMNotificationServiceAdapter implements NotificationService
     }
 
     @Override
-    public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
-        final BindingDOMNotificationListenerAdapter domListener
-                = new BindingDOMNotificationListenerAdapter(adapterContext, 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()));
     }
 
     @Override
-    public <N extends Notification> Registration registerListener(final Class<N> type, final Listener<N> listener,
-            final Executor executor) {
-        final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, type, listener, executor);
-        return domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
-    }
-
-    public DOMNotificationService getDomService() {
-        return domNotifService;
-    }
-
-    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;
+    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> {
@@ -78,13 +64,13 @@ public class BindingDOMNotificationServiceAdapter implements NotificationService
         }
 
         @Override
-        protected NotificationService createInstance(final ClassToInstanceMap<DOMService> delegates) {
-            final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
-            return new BindingDOMNotificationServiceAdapter(adapterContext(), 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);
         }
     }