Hide BindingRpcFutureAware
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationListenerAdapter.java
index 15f89f4c6d8e1f158ccf302e888ca10fd6947ce9..2785500fa2f9af350a37e07fbe673bea650110fb 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableMap;
 import com.google.common.reflect.TypeToken;
 import java.lang.reflect.Method;
@@ -15,53 +17,58 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import org.opendaylight.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
-import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 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.Notification;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
 
-    private final BindingNormalizedNodeSerializer codec;
+    private final AdapterContext adapterContext;
     private final NotificationListener delegate;
-    private final ImmutableMap<SchemaPath, NotificationListenerInvoker> invokers;
+    private final ImmutableMap<Absolute, NotificationListenerInvoker> invokers;
 
-    BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec,
-            final NotificationListener delegate) {
-        this.codec = codec;
-        this.delegate = delegate;
+    BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final NotificationListener delegate) {
+        this.adapterContext = requireNonNull(adapterContext);
+        this.delegate = requireNonNull(delegate);
         this.invokers = createInvokerMapFor(delegate.getClass());
     }
 
     @Override
     public void onNotification(final DOMNotification notification) {
         final Notification baNotification = deserialize(notification);
-        final QName notificationQName = notification.getType().getLastComponent();
+        final QName notificationQName = notification.getType().lastNodeIdentifier();
         getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
     }
 
     private Notification deserialize(final DOMNotification notification) {
         if (notification instanceof LazySerializedDOMNotification) {
+            // 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 ((LazySerializedDOMNotification) notification).getBindingData();
         }
-        return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
+
+        final CurrentAdapterSerializer serializer = adapterContext.currentSerializer();
+        return notification instanceof DOMEvent ? serializer.fromNormalizedNodeNotification(notification.getType(),
+            notification.getBody(), ((DOMEvent) notification).getEventInstant())
+                : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
     }
 
-    private NotificationListenerInvoker getInvoker(final SchemaPath type) {
+    private NotificationListenerInvoker getInvoker(final Absolute type) {
         return invokers.get(type);
     }
 
-    protected Set<SchemaPath> getSupportedNotifications() {
+    protected Set<Absolute> getSupportedNotifications() {
         return invokers.keySet();
     }
 
-    private static ImmutableMap<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(
+    private static ImmutableMap<Absolute, NotificationListenerInvoker> createInvokerMapFor(
             final Class<? extends NotificationListener> implClz) {
-        final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
+        final Map<Absolute, 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)) {
@@ -69,7 +76,7 @@ class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
                 final Class<? extends NotificationListener> listenerType
                         = (Class<? extends NotificationListener>) iface;
                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
-                for (final SchemaPath path : getNotificationTypes(listenerType)) {
+                for (final Absolute path : getNotificationTypes(listenerType)) {
                     builder.put(path, invoker);
                 }
             }
@@ -77,15 +84,14 @@ class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
         return ImmutableMap.copyOf(builder);
     }
 
-    private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
+    private static Set<Absolute> 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<>();
+        final Set<Absolute> 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));
+                ret.add(Absolute.of(BindingReflections.findQName(notification)));
             }
         }
         return ret;