2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.binding.compat;
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.reflect.TypeToken;
12 import java.lang.reflect.Method;
13 import java.util.HashMap;
14 import java.util.HashSet;
17 import org.opendaylight.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
18 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20 import org.opendaylight.yangtools.yang.binding.NotificationListener;
21 import org.opendaylight.yangtools.yang.common.QName;
23 final class NotificationInvoker
24 implements org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> {
26 private final NotificationListener delegate;
27 private final Map<Class<? extends Notification>,InvokerContext> invokers;
30 private NotificationInvoker(final NotificationListener listener) {
32 final Map<Class<? extends Notification>, InvokerContext> builder = new HashMap<>();
33 for (final TypeToken<?> ifaceToken : TypeToken.of(listener.getClass()).getTypes().interfaces()) {
34 final Class<?> iface = ifaceToken.getRawType();
35 if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
36 @SuppressWarnings("unchecked")
37 final Class<? extends NotificationListener> listenerType =
38 (Class<? extends NotificationListener>) iface;
39 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
40 for (final Class<? extends Notification> type : getNotificationTypes(listenerType)) {
41 builder.put(type, new InvokerContext(BindingReflections.findQName(type), invoker));
45 invokers = ImmutableMap.copyOf(builder);
48 public static NotificationInvoker invokerFor(final NotificationListener listener) {
49 return new NotificationInvoker(listener);
52 public Set<Class<? extends Notification>> getSupportedNotifications() {
53 return invokers.keySet();
57 public void onNotification(final Notification notification) {
58 getContext(notification.implementedInterface()).invoke(notification);
61 private InvokerContext getContext(final Class<?> type) {
62 return invokers.get(type);
65 @SuppressWarnings("unchecked")
66 private static Set<Class<? extends Notification>> getNotificationTypes(
67 final Class<? extends org.opendaylight.yangtools.yang.binding.NotificationListener> type) {
68 // TODO: Investigate possibility and performance impact if we cache this or expose
69 // it from NotificationListenerInvoker
70 final Set<Class<? extends Notification>> ret = new HashSet<>();
71 for (final Method method : type.getMethods()) {
72 if (BindingReflections.isNotificationCallback(method)) {
73 final Class<? extends Notification> notification =
74 (Class<? extends Notification>) method.getParameterTypes()[0];
75 ret.add(notification);
81 private final class InvokerContext {
83 private final QName name;
84 private final NotificationListenerInvoker invoker;
86 private InvokerContext(final QName name, final NotificationListenerInvoker invoker) {
88 this.invoker = invoker;
91 public void invoke(final Notification notification) {
92 invoker.invokeNotification(delegate, name, notification);