X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fbinding%2Fimpl%2FBindingDOMNotificationListenerAdapter.java;h=d0434f595c0fda0a52e29137be20fcc202faec98;hp=03da29642ce2d8a13ef808d035020efbb413bd5f;hb=3ec97cd0a86ad1b79f6854dc6924eb7b06e359a3;hpb=f9a9cd1ea40d2477ccb16b03c71a87595226595a 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..d0434f595c 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,88 @@ */ package org.opendaylight.controller.md.sal.binding.impl; -import javax.annotation.Nonnull; +import com.google.common.collect.ImmutableMap; +import com.google.common.reflect.TypeToken; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; 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.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker; +import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer; +import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.yangtools.yang.binding.Notification; +import org.opendaylight.yangtools.yang.binding.NotificationListener; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; +@Deprecated 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) { + 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); + public void onNotification(final DOMNotification notification) { + final Notification baNotification = deserialize(notification); + final QName notificationQName = notification.getType().getLastComponent(); + getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification); } -} \ No newline at end of file + + 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); + } + + protected Set getSupportedNotifications() { + return invokers.keySet(); + } + + public static Map createInvokerMapFor( + final Class implClz) { + final Map builder = new HashMap<>(); + for (final TypeToken ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) { + Class iface = ifaceToken.getRawType(); + 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; + } +}