Deprecate Clustered(DOM)DataTreeChangeListener
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationListenerAdapter.java
index 6c5e43a7338eac44d180dd07c31d13cd1716d95b..7ce80210c3ed7cca5a7b05d85cb719464f7563c8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,88 +7,65 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter;
 
-import com.google.common.collect.ImmutableMap;
-import com.google.common.reflect.TypeToken;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import javax.annotation.Nonnull;
-import org.opendaylight.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
-import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import java.util.concurrent.Executor;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener.Component;
+import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
+import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
+import org.opendaylight.mdsal.dom.api.DOMEvent;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
+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.binding.util.BindingReflections;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-
-class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
-    private final BindingNormalizedNodeSerializer codec;
-    private final NotificationListener delegate;
-    private final Map<SchemaPath,NotificationListenerInvoker> invokers;
+final class BindingDOMNotificationListenerAdapter<N extends Notification<N> & DataObject>
+        implements DOMNotificationListener {
+    private final AdapterContext adapterContext;
+    private final Listener<N> delegate;
+    private final Executor executor;
+    private final Class<N> type;
 
-    BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec,
-            final NotificationListener delegate) {
-        this.codec = codec;
-        this.delegate = delegate;
-        this.invokers = createInvokerMapFor(delegate.getClass());
+    BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final Class<N> type,
+            final Listener<N> delegate, final Executor executor) {
+        this.adapterContext = requireNonNull(adapterContext);
+        this.type = requireNonNull(type);
+        this.delegate = requireNonNull(delegate);
+        this.executor = requireNonNull(executor);
     }
 
-    @Override
-    public void onNotification(@Nonnull final DOMNotification notification) {
-        final Notification baNotification = deserialize(notification);
-        final QName notificationQName = notification.getType().getLastComponent();
-        getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
+    BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final Component<N> component,
+            final Executor executor) {
+        this(adapterContext, component.type(), component.listener(), executor);
     }
 
-    private Notification deserialize(final DOMNotification notification) {
-        if (notification instanceof LazySerializedDOMNotification) {
-            return ((LazySerializedDOMNotification) notification).getBindingData();
-        }
-        return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
+    @NonNull Absolute schemaPath() {
+        return Absolute.of(BindingReflections.findQName(type));
     }
 
-    private NotificationListenerInvoker getInvoker(final SchemaPath type) {
-        return invokers.get(type);
-    }
-
-    protected Set<SchemaPath> getSupportedNotifications() {
-        return invokers.keySet();
+    @Override
+    public void onNotification(final DOMNotification notification) {
+        final var binding = type.cast(verifyNotNull(deserialize(notification)));
+        executor.execute(() -> delegate.onNotification(binding));
     }
 
-    public static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(
-            final Class<? extends NotificationListener> implClz) {
-        final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
-        for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
-            Class<?> iface = ifaceToken.getRawType();
-            if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
-                @SuppressWarnings("unchecked")
-                final Class<? extends NotificationListener> listenerType
-                        = (Class<? extends NotificationListener>) iface;
-                final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
-                for (final SchemaPath path : getNotificationTypes(listenerType)) {
-                    builder.put(path, invoker);
-                }
-            }
+    private Notification<?> deserialize(final DOMNotification notification) {
+        if (notification instanceof LazySerializedNotification lazy) {
+            // TODO: This is a routed-back notification, for which we may end up losing event time here, but that is
+            //       okay, for now at least.
+            return lazy.getBindingData();
         }
-        return ImmutableMap.copyOf(builder);
-    }
 
-    private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
-        // TODO: Investigate possibility and performance impact if we cache this or expose
-        // it from NotificationListenerInvoker
-        final Set<SchemaPath> ret = new HashSet<>();
-        for (final Method method : type.getMethods()) {
-            if (BindingReflections.isNotificationCallback(method)) {
-                final Class<?> notification = method.getParameterTypes()[0];
-                final QName name = BindingReflections.findQName(notification);
-                ret.add(SchemaPath.create(true, name));
-            }
-        }
-        return ret;
+        final var serializer = adapterContext.currentSerializer();
+        final var result = notification instanceof DOMEvent domEvent
+            ? serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody(),
+                domEvent.getEventInstant())
+                : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
+        verify(result instanceof Notification, "Unexpected codec result %s", result);
+        return (Notification<?>) result;
     }
-}
\ No newline at end of file
+}