Make LazySerializedDOMNotification a DOMEvent
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationPublishServiceAdapter.java
index 0f5d2582fb3ce09b5dcf0f8a72d0f7e391412597..3ce2684d01bae73cf8a8f13b1d224bce33848c69 100644 (file)
@@ -10,52 +10,50 @@ package org.opendaylight.mdsal.binding.dom.adapter;
 import com.google.common.collect.ClassToInstanceMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.time.Instant;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
 import org.opendaylight.mdsal.dom.api.DOMService;
+import org.opendaylight.yangtools.yang.binding.EventInstantAware;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
-public class BindingDOMNotificationPublishServiceAdapter implements NotificationPublishService, AutoCloseable {
+public class BindingDOMNotificationPublishServiceAdapter extends AbstractBindingAdapter<DOMNotificationPublishService>
+        implements NotificationPublishService {
 
-    static final Factory<NotificationPublishService> BUILDER_FACTORY
-            = new BindingDOMAdapterBuilder.Factory<NotificationPublishService>() {
+    static final Factory<NotificationPublishService> BUILDER_FACTORY = Builder::new;
 
-                @Override
-                public BindingDOMAdapterBuilder<NotificationPublishService> newBuilder() {
-                    return new Builder();
-                }
-
-            };
-
-    private final BindingToNormalizedNodeCodec codecRegistry;
-    private final DOMNotificationPublishService domPublishService;
+    public BindingDOMNotificationPublishServiceAdapter(final DOMNotificationPublishService domPublishService,
+            final BindingToNormalizedNodeCodec codec) {
+        super(codec, domPublishService);
+    }
 
+    @Deprecated
     public BindingDOMNotificationPublishServiceAdapter(final BindingToNormalizedNodeCodec codec,
             final DOMNotificationPublishService domPublishService) {
-        this.codecRegistry = codec;
-        this.domPublishService = domPublishService;
+        this(domPublishService, codec);
     }
 
     public BindingToNormalizedNodeCodec getCodecRegistry() {
-        return codecRegistry;
+        return getCodec();
     }
 
     public DOMNotificationPublishService getDomPublishService() {
-        return domPublishService;
+        return getDelegate();
     }
 
     @Override
     public void putNotification(final Notification notification) throws InterruptedException {
-        domPublishService.putNotification(toDomNotification(notification));
+        getDelegate().putNotification(toDomNotification(notification));
     }
 
     @Override
     public ListenableFuture<? extends Object> offerNotification(final Notification notification) {
-        ListenableFuture<?> offerResult = domPublishService.offerNotification(toDomNotification(notification));
+        ListenableFuture<?> offerResult = getDelegate().offerNotification(toDomNotification(notification));
         return DOMNotificationPublishService.REJECTED.equals(offerResult)
                 ? NotificationPublishService.REJECTED
                 : offerResult;
@@ -64,24 +62,20 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification
     @Override
     public ListenableFuture<? extends Object> offerNotification(final Notification notification,
                                                  final int timeout, final TimeUnit unit) throws InterruptedException {
-        ListenableFuture<?> offerResult = domPublishService.offerNotification(
-                toDomNotification(notification), timeout, unit);
+        ListenableFuture<?> offerResult = getDelegate().offerNotification(toDomNotification(notification), timeout,
+            unit);
         return DOMNotificationPublishService.REJECTED.equals(offerResult)
                 ? NotificationPublishService.REJECTED
                 : offerResult;
     }
 
-    private DOMNotification toDomNotification(final Notification notification) {
-        return LazySerializedDOMNotification.create(codecRegistry, notification);
-    }
-
-    @Override
-    public void close() throws Exception {
-
+    private @NonNull DOMNotification toDomNotification(final Notification notification) {
+        final Instant instant = notification instanceof EventInstantAware
+                ? ((EventInstantAware) notification).eventInstant() : Instant.now();
+        return LazySerializedDOMNotification.create(getCodec(), notification, instant);
     }
 
     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
-
         @Override
         public Set<Class<? extends DOMService>> getRequiredDelegates() {
             return ImmutableSet.of(DOMNotificationPublishService.class);
@@ -93,6 +87,5 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification
             final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
             return new BindingDOMNotificationPublishServiceAdapter(codec, domPublish);
         }
-
     }
 }