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 java.lang.reflect.Method;
12 import java.util.HashMap;
13 import java.util.HashSet;
16 import org.opendaylight.yangtools.yang.binding.Notification;
17 import org.opendaylight.yangtools.yang.binding.NotificationListener;
18 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
19 import org.opendaylight.yangtools.yang.binding.util.NotificationListenerInvoker;
20 import org.opendaylight.yangtools.yang.common.QName;
22 final class NotificationInvoker implements org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> {
24 private final NotificationListener delegate;
25 private final Map<Class<? extends Notification>,InvokerContext> invokers;
28 private NotificationInvoker(final NotificationListener listener) {
30 final Map<Class<? extends Notification>, InvokerContext> builder = new HashMap<>();
31 for(final Class<?> iface : listener.getClass().getInterfaces()) {
32 if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
33 @SuppressWarnings("unchecked")
34 final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
35 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
36 for(final Class<? extends Notification> type : getNotificationTypes(listenerType)) {
37 builder.put(type, new InvokerContext(BindingReflections.findQName(type) , invoker));
41 invokers = ImmutableMap.copyOf(builder);
44 public static NotificationInvoker invokerFor(final NotificationListener listener) {
45 return new NotificationInvoker(listener);
48 public Set<Class<? extends Notification>> getSupportedNotifications() {
49 return invokers.keySet();
53 public void onNotification(final Notification notification) {
54 getContext(notification.getImplementedInterface()).invoke(notification);
57 private InvokerContext getContext(final Class<?> type) {
58 return invokers.get(type);
61 @SuppressWarnings("unchecked")
62 private static Set<Class<? extends Notification>> getNotificationTypes(final Class<? extends org.opendaylight.yangtools.yang.binding.NotificationListener> type) {
63 // TODO: Investigate possibility and performance impact if we cache this or expose
64 // it from NotificationListenerInvoker
65 final Set<Class<? extends Notification>> ret = new HashSet<>();
66 for(final Method method : type.getMethods()) {
67 if(BindingReflections.isNotificationCallback(method)) {
68 final Class<? extends Notification> notification = (Class<? extends Notification>) method.getParameterTypes()[0];
69 ret.add(notification);
75 private class InvokerContext {
77 private final QName name;
78 private final NotificationListenerInvoker invoker;
80 private InvokerContext(final QName name, final NotificationListenerInvoker invoker) {
82 this.invoker = invoker;
85 public void invoke(final Notification notification) {
86 invoker.invokeNotification(delegate, name, notification);