Add NotificationService.registerListener()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractDOMNotificationListenerAdapter.java
diff --git a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/AbstractDOMNotificationListenerAdapter.java b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/AbstractDOMNotificationListenerAdapter.java
new file mode 100644 (file)
index 0000000..f85f2ab
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.adapter;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
+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.model.api.stmt.SchemaNodeIdentifier.Absolute;
+
+abstract class AbstractDOMNotificationListenerAdapter implements DOMNotificationListener {
+    private final AdapterContext adapterContext;
+
+    AbstractDOMNotificationListenerAdapter(final AdapterContext adapterContext) {
+        this.adapterContext = requireNonNull(adapterContext);
+    }
+
+    @Override
+    public final void onNotification(final DOMNotification notification) {
+        onNotification(notification.getType(), verifyNotNull(deserialize(notification)));
+    }
+
+    abstract void onNotification(@NonNull Absolute domType, @NonNull Notification notification);
+
+    abstract Set<Absolute> getSupportedNotifications();
+
+    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();
+        }
+
+        final CurrentAdapterSerializer serializer = adapterContext.currentSerializer();
+        return notification instanceof DOMEvent
+            ? serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody(),
+                ((DOMEvent) notification).getEventInstant())
+                : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
+    }
+}