From 9f1f923d51b8fe8df4ec570055c3d39eb79e8af6 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Wed, 8 Apr 2015 13:45:35 +0200 Subject: [PATCH] Bug 2351: Introduced LazySerializedDOMNotification Change-Id: I4a6c5c19b2d59050ddf01f92a76ef0d5add2bfb2 Signed-off-by: Tony Tkacik --- ...BindingDOMNotificationListenerAdapter.java | 10 +++- ...gDOMNotificationPublishServiceAdapter.java | 39 ++---------- .../impl/LazySerializedDOMNotification.java | 60 +++++++++++++++++++ 3 files changed, 74 insertions(+), 35 deletions(-) create mode 100644 opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazySerializedDOMNotification.java diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationListenerAdapter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationListenerAdapter.java index c9a1756435..668030e1a6 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationListenerAdapter.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationListenerAdapter.java @@ -38,12 +38,18 @@ class BindingDOMNotificationListenerAdapter implements DOMNotificationListener { @Override public void onNotification(@Nonnull final DOMNotification notification) { - final Notification baNotification = - codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody()); + final Notification baNotification = deserialize(notification); final QName notificationQName = notification.getType().getLastComponent(); getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification); } + private Notification deserialize(final DOMNotification notification) { + if(notification instanceof LazySerializedDOMNotification) { + return ((LazySerializedDOMNotification) notification).getBindingData(); + } + return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody()); + } + private NotificationListenerInvoker getInvoker(final SchemaPath type) { return invokers.get(type); } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java index 0a736ffc79..6c54553e6f 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java @@ -12,7 +12,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.ListenableFuture; import java.util.Set; import java.util.concurrent.TimeUnit; -import javax.annotation.Nonnull; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory; import org.opendaylight.controller.md.sal.dom.api.DOMNotification; @@ -20,8 +19,6 @@ import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService; import org.opendaylight.controller.md.sal.dom.api.DOMService; import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.yangtools.yang.binding.Notification; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; public class BindingDOMNotificationPublishServiceAdapter implements NotificationPublishService, AutoCloseable { @@ -37,7 +34,7 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification private final BindingNormalizedNodeSerializer codecRegistry; private final DOMNotificationPublishService domPublishService; - public BindingDOMNotificationPublishServiceAdapter(BindingNormalizedNodeSerializer codecRegistry, DOMNotificationPublishService domPublishService) { + public BindingDOMNotificationPublishServiceAdapter(final BindingNormalizedNodeSerializer codecRegistry, final DOMNotificationPublishService domPublishService) { this.codecRegistry = codecRegistry; this.domPublishService = domPublishService; } @@ -61,8 +58,7 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification } private DOMNotification toDomNotification(final Notification notification) { - final ContainerNode domNotification = codecRegistry.toNormalizedNodeNotification(notification); - return new DOMNotificationImpl(domNotification); + return LazySerializedDOMNotification.create(codecRegistry, notification); } @Override @@ -70,29 +66,6 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification } - private static class DOMNotificationImpl implements DOMNotification { - - private final SchemaPath type; - private final ContainerNode body; - - public DOMNotificationImpl(final ContainerNode body) { - this.type = SchemaPath.create(true, body.getIdentifier().getNodeType()); - this.body = body; - } - - @Nonnull - @Override - public SchemaPath getType() { - return this.type; - } - - @Nonnull - @Override - public ContainerNode getBody() { - return this.body; - } - } - protected static class Builder extends BindingDOMAdapterBuilder { @Override @@ -101,10 +74,10 @@ public class BindingDOMNotificationPublishServiceAdapter implements Notification } @Override - protected NotificationPublishService createInstance(BindingToNormalizedNodeCodec codec, - ClassToInstanceMap delegates) { - BindingNormalizedNodeSerializer codecReg = codec.getCodecRegistry(); - DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class); + protected NotificationPublishService createInstance(final BindingToNormalizedNodeCodec codec, + final ClassToInstanceMap delegates) { + final BindingNormalizedNodeSerializer codecReg = codec.getCodecRegistry(); + final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class); return new BindingDOMNotificationPublishServiceAdapter(codecReg, domPublish); } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazySerializedDOMNotification.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazySerializedDOMNotification.java new file mode 100644 index 0000000000..e0cc4d4fa0 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/LazySerializedDOMNotification.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * 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.controller.md.sal.binding.impl; + +import org.opendaylight.controller.md.sal.dom.api.DOMNotification; +import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; +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. + * + */ +class LazySerializedDOMNotification implements DOMNotification { + + private final BindingNormalizedNodeSerializer codec; + private final Notification data; + private final SchemaPath type; + + private ContainerNode domBody; + + private LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification data, final SchemaPath type) { + super(); + this.codec = codec; + this.data = data; + this.type = type; + } + + 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); + } + + @Override + public SchemaPath getType() { + return type; + } + + @Override + public ContainerNode getBody() { + if (domBody == null) { + domBody = codec.toNormalizedNodeNotification(data); + } + return domBody; + } + + protected Notification getBindingData() { + return data; + } +} -- 2.36.6