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.yangtools.yang.binding.Notification;
18 import org.opendaylight.yangtools.yang.binding.NotificationListener;
19 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
20 import org.opendaylight.yangtools.yang.binding.util.NotificationListenerInvoker;
21 import org.opendaylight.yangtools.yang.common.QName;
23 final class NotificationInvoker implements org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> {
25 private final NotificationListener delegate;
26 private final Map<Class<? extends Notification>,InvokerContext> invokers;
29 private NotificationInvoker(final NotificationListener listener) {
31 final Map<Class<? extends Notification>, InvokerContext> builder = new HashMap<>();
32 for(final TypeToken<?> ifaceToken : TypeToken.of(listener.getClass()).getTypes().interfaces()) {
33 Class<?> iface = ifaceToken.getRawType();
34 if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
35 @SuppressWarnings("unchecked")
36 final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
37 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
38 for(final Class<? extends Notification> type : getNotificationTypes(listenerType)) {
39 builder.put(type, new InvokerContext(BindingReflections.findQName(type) , invoker));
43 invokers = ImmutableMap.copyOf(builder);
46 public static NotificationInvoker invokerFor(final NotificationListener listener) {
47 return new NotificationInvoker(listener);
50 public Set<Class<? extends Notification>> getSupportedNotifications() {
51 return invokers.keySet();
55 public void onNotification(final Notification notification) {
56 getContext(notification.getImplementedInterface()).invoke(notification);
59 private InvokerContext getContext(final Class<?> type) {
60 return invokers.get(type);
63 @SuppressWarnings("unchecked")
64 private static Set<Class<? extends Notification>> getNotificationTypes(final Class<? extends org.opendaylight.yangtools.yang.binding.NotificationListener> type) {
65 // TODO: Investigate possibility and performance impact if we cache this or expose
66 // it from NotificationListenerInvoker
67 final Set<Class<? extends Notification>> ret = new HashSet<>();
68 for(final Method method : type.getMethods()) {
69 if(BindingReflections.isNotificationCallback(method)) {
70 final Class<? extends Notification> notification = (Class<? extends Notification>) method.getParameterTypes()[0];
71 ret.add(notification);
77 private class InvokerContext {
79 private final QName name;
80 private final NotificationListenerInvoker invoker;
82 private InvokerContext(final QName name, final NotificationListenerInvoker invoker) {
84 this.invoker = invoker;
87 public void invoke(final Notification notification) {
88 invoker.invokeNotification(delegate, name, notification);