Make LazySerializedDOMNotification a DOMEvent
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationPublishServiceAdapter.java
index 8ab5e8cb8dcc34963fcb03d7220e24090e0ca06c..3ce2684d01bae73cf8a8f13b1d224bce33848c69 100644 (file)
@@ -7,81 +7,78 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter;
 
-import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
-
 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.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
-import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
-import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
-import org.opendaylight.controller.md.sal.dom.api.DOMService;
+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 {
-
-    static final Factory<NotificationPublishService> BUILDER_FACTORY = new BindingDOMAdapterBuilder.Factory<NotificationPublishService>() {
-
-        @Override
-        public BindingDOMAdapterBuilder<NotificationPublishService> newBuilder() {
-            return new Builder();
-        }
+public class BindingDOMNotificationPublishServiceAdapter extends AbstractBindingAdapter<DOMNotificationPublishService>
+        implements NotificationPublishService {
 
-    };
+    static final Factory<NotificationPublishService> BUILDER_FACTORY = Builder::new;
 
-    private final BindingToNormalizedNodeCodec codecRegistry;
-    private final DOMNotificationPublishService domPublishService;
+    public BindingDOMNotificationPublishServiceAdapter(final DOMNotificationPublishService domPublishService,
+            final BindingToNormalizedNodeCodec codec) {
+        super(codec, domPublishService);
+    }
 
-    public BindingDOMNotificationPublishServiceAdapter(final BindingToNormalizedNodeCodec codec, final DOMNotificationPublishService domPublishService) {
-        this.codecRegistry = codec;
-        this.domPublishService = domPublishService;
+    @Deprecated
+    public BindingDOMNotificationPublishServiceAdapter(final BindingToNormalizedNodeCodec codec,
+            final DOMNotificationPublishService 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;
     }
 
     @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);
+    public ListenableFuture<? extends Object> offerNotification(final Notification notification,
+                                                 final int timeout, final TimeUnit unit) throws InterruptedException {
+        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.<Class<? extends DOMService>>of(DOMNotificationPublishService.class);
+            return ImmutableSet.of(DOMNotificationPublishService.class);
         }
 
         @Override
@@ -90,6 +87,5 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification
             final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
             return new BindingDOMNotificationPublishServiceAdapter(codec, domPublish);
         }
-
     }
 }