From 123daf0f2b4f600d30c460fe9729ab925b96b545 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Tue, 7 Apr 2015 14:02:39 +0200 Subject: [PATCH 1/1] Notification Listener Adapter uses NotificationListenerInvoker Change-Id: I64bd1c031d979b087ad1dbc659020e1716213e42 Signed-off-by: Tony Tkacik --- ...BindingDOMNotificationListenerAdapter.java | 60 +++++++++++++++++-- .../BindingDOMNotificationServiceAdapter.java | 31 +--------- 2 files changed, 58 insertions(+), 33 deletions(-) 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 03da29642c..c9a1756435 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 @@ -7,27 +7,77 @@ */ package org.opendaylight.controller.md.sal.binding.impl; +import com.google.common.collect.ImmutableMap; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import javax.annotation.Nonnull; import org.opendaylight.controller.md.sal.dom.api.DOMNotification; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener; -import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory; import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.yangtools.yang.binding.Notification; +import org.opendaylight.yangtools.yang.binding.NotificationListener; +import org.opendaylight.yangtools.yang.binding.util.BindingReflections; +import org.opendaylight.yangtools.yang.binding.util.NotificationListenerInvoker; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; class BindingDOMNotificationListenerAdapter implements DOMNotificationListener { - private final NotificationInvokerFactory.NotificationInvoker invoker; private final BindingNormalizedNodeSerializer codec; + private final NotificationListener delegate; + private final Map invokers; - public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final NotificationInvokerFactory.NotificationInvoker invoker) { + public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final NotificationListener delegate) { this.codec = codec; - this.invoker = invoker; + this.delegate = delegate; + this.invokers = createInvokerMapFor(delegate.getClass()); } @Override public void onNotification(@Nonnull final DOMNotification notification) { final Notification baNotification = codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody()); - invoker.getInvocationProxy().onNotification(baNotification); + final QName notificationQName = notification.getType().getLastComponent(); + getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification); + } + + private NotificationListenerInvoker getInvoker(final SchemaPath type) { + return invokers.get(type); + } + + protected Set getSupportedNotifications() { + return invokers.keySet(); + } + + private static Map createInvokerMapFor(final Class implClz) { + final Map builder = new HashMap<>(); + for(final Class iface : implClz.getInterfaces()) { + if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) { + @SuppressWarnings("unchecked") + final Class listenerType = (Class) iface; + final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType); + for(final SchemaPath path : getNotificationTypes(listenerType)) { + builder.put(path, invoker); + } + } + } + return ImmutableMap.copyOf(builder); + } + + private static Set getNotificationTypes(final Class type) { + // TODO: Investigate possibility and performance impact if we cache this or expose + // it from NotificationListenerInvoker + final Set ret = new HashSet<>(); + for(final Method method : type.getMethods()) { + if(BindingReflections.isNotificationCallback(method)) { + final Class notification = method.getParameterTypes()[0]; + final QName name = BindingReflections.findQName(notification); + ret.add(SchemaPath.create(true, name)); + } + } + return ret; } } \ No newline at end of file diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationServiceAdapter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationServiceAdapter.java index 6006266cc2..cdf03fa552 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationServiceAdapter.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationServiceAdapter.java @@ -9,13 +9,9 @@ package org.opendaylight.controller.md.sal.binding.impl; import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.ImmutableSet; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; import java.util.Set; import org.opendaylight.controller.md.sal.binding.api.NotificationService; import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory; -import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService; import org.opendaylight.controller.md.sal.dom.api.DOMService; import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder; @@ -23,10 +19,7 @@ import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory; import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.yangtools.concepts.AbstractListenerRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration; -import org.opendaylight.yangtools.yang.binding.Notification; import org.opendaylight.yangtools.yang.binding.NotificationListener; -import org.opendaylight.yangtools.yang.binding.util.BindingReflections; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable { @@ -40,34 +33,20 @@ public class BindingDOMNotificationServiceAdapter implements NotificationService }; private final BindingNormalizedNodeSerializer codec; private final DOMNotificationService domNotifService; - private final NotificationInvokerFactory notificationInvokerFactory; public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec, final DOMNotificationService domNotifService, final NotificationInvokerFactory notificationInvokerFactory) { this.codec = codec; this.domNotifService = domNotifService; - this.notificationInvokerFactory = notificationInvokerFactory; } @Override public ListenerRegistration registerNotificationListener(final T listener) { - final NotificationInvokerFactory.NotificationInvoker invoker = notificationInvokerFactory.invokerFor(listener); - final DOMNotificationListener domListener = new BindingDOMNotificationListenerAdapter(codec, invoker); - final Collection schemaPaths = convertNotifTypesToSchemaPath(invoker.getSupportedNotifications()); - final ListenerRegistration domRegistration = - domNotifService.registerNotificationListener(domListener, schemaPaths); + final BindingDOMNotificationListenerAdapter domListener = new BindingDOMNotificationListenerAdapter(codec, listener); + final ListenerRegistration domRegistration = + domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications()); return new ListenerRegistrationImpl<>(listener, domRegistration); } - - - private Collection convertNotifTypesToSchemaPath(final Set> notificationTypes) { - final List schemaPaths = new ArrayList<>(); - for (final Class notificationType : notificationTypes) { - schemaPaths.add(SchemaPath.create(true, BindingReflections.findQName(notificationType))); - } - return schemaPaths; - } - @Override public void close() throws Exception { @@ -89,7 +68,6 @@ public class BindingDOMNotificationServiceAdapter implements NotificationService private static class Builder extends BindingDOMAdapterBuilder { - @Override protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec, final ClassToInstanceMap delegates) { @@ -102,8 +80,5 @@ public class BindingDOMNotificationServiceAdapter implements NotificationService public Set> getRequiredDelegates() { return ImmutableSet.of(DOMNotificationService.class); } - - - } } -- 2.36.6