Split off DefaultBindingCodecTreeFactory
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / LazySerializedDOMNotification.java
index f6fb552b6c287c606508b665134d6cded9d94e1e..f117ca0d03a9a84c568d817ef369523c030e9a48 100644 (file)
@@ -7,38 +7,48 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter;
 
-import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
-import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
+import static java.util.Objects.requireNonNull;
+
+import java.time.Instant;
+import org.eclipse.jdt.annotation.NonNull;
+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.yangtools.yang.binding.Notification;
-import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
  * Lazy serialized implementation of DOM Notification.
  *
- * This implementation performs serialization of data, only if receiver
- * of notification actually accessed data from notification.
- *
+ * <p>
+ * This implementation performs serialization of data, only if receiver of notification actually accessed data from
+ * notification.
  */
-public final class LazySerializedDOMNotification implements DOMNotification {
-
-    private final BindingNormalizedNodeSerializer codec;
-    private final Notification data;
-    private final SchemaPath type;
+public final class LazySerializedDOMNotification implements DOMNotification, DOMEvent {
+    private final @NonNull BindingNormalizedNodeSerializer codec;
+    private final @NonNull Notification data;
+    private final @NonNull SchemaPath type;
+    private final @NonNull Instant eventInstant;
 
-    private ContainerNode domBody;
+    private volatile ContainerNode domBody;
 
-    private LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification data, final SchemaPath type) {
-        super();
-        this.codec = codec;
-        this.data = data;
-        this.type = type;
+    LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification data,
+            final SchemaPath type, final Instant eventInstant) {
+        this.codec = requireNonNull(codec);
+        this.data = requireNonNull(data);
+        this.type = requireNonNull(type);
+        this.eventInstant = requireNonNull(eventInstant);
     }
 
-    static DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data) {
-        final SchemaPath type = SchemaPath.create(true, BindingReflections.findQName(data.getImplementedInterface()));
-        return new LazySerializedDOMNotification(codec, data, type);
+    static @NonNull DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data,
+            final Instant eventInstant) {
+        // TODO: for nested (YANG 1.1) notifications we will need the SchemaPath where the notification is being invoked
+        //       and use that instead of ROOT. How Binding users will refer to it is TBD (but probably
+        //       InstanceIdentifier, which means we will need to do some lifting to find the SchemaPath)
+        final SchemaPath type = SchemaPath.ROOT.createChild(BindingReflections.findQName(data.implementedInterface()));
+        return new LazySerializedDOMNotification(codec, data, type, eventInstant);
     }
 
     @Override
@@ -48,13 +58,19 @@ public final class LazySerializedDOMNotification implements DOMNotification {
 
     @Override
     public ContainerNode getBody() {
-        if (domBody == null) {
-            domBody = codec.toNormalizedNodeNotification(data);
+        ContainerNode local = domBody;
+        if (local == null) {
+            domBody = local = codec.toNormalizedNodeNotification(data);
         }
-        return domBody;
+        return local;
+    }
+
+    @Override
+    public Instant getEventInstant() {
+        return eventInstant;
     }
 
-    public Notification getBindingData() {
+    public @NonNull Notification getBindingData() {
         return data;
     }
 }