1e7b9bde9f3fcd97ae0291df90ad5bec67bf1f56
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / NotificationInvoker.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.binding.compat;
9
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;
15 import java.util.Map;
16 import java.util.Set;
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;
22
23 @Deprecated
24 final class NotificationInvoker
25         implements org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> {
26
27     private final NotificationListener delegate;
28     private final Map<Class<? extends Notification>,InvokerContext> invokers;
29
30
31     private NotificationInvoker(final NotificationListener listener) {
32         delegate = listener;
33         final Map<Class<? extends Notification>, InvokerContext> builder = new HashMap<>();
34         for (final TypeToken<?> ifaceToken : TypeToken.of(listener.getClass()).getTypes().interfaces()) {
35             final Class<?> iface = ifaceToken.getRawType();
36             if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
37                 @SuppressWarnings("unchecked")
38                 final Class<? extends NotificationListener> listenerType =
39                         (Class<? extends NotificationListener>) iface;
40                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
41                 for (final Class<? extends Notification> type : getNotificationTypes(listenerType)) {
42                     builder.put(type, new InvokerContext(BindingReflections.findQName(type), invoker));
43                 }
44             }
45         }
46         invokers = ImmutableMap.copyOf(builder);
47     }
48
49     public static NotificationInvoker invokerFor(final NotificationListener listener) {
50         return new NotificationInvoker(listener);
51     }
52
53     public Set<Class<? extends Notification>> getSupportedNotifications() {
54         return invokers.keySet();
55     }
56
57     @Override
58     public void onNotification(final Notification notification) {
59         getContext(notification.implementedInterface()).invoke(notification);
60     }
61
62     private InvokerContext getContext(final Class<?> type) {
63         return invokers.get(type);
64     }
65
66     @SuppressWarnings("unchecked")
67     private static Set<Class<? extends Notification>> getNotificationTypes(
68             final Class<? extends org.opendaylight.yangtools.yang.binding.NotificationListener> type) {
69         // TODO: Investigate possibility and performance impact if we cache this or expose
70         // it from NotificationListenerInvoker
71         final Set<Class<? extends Notification>> ret = new HashSet<>();
72         for (final Method method : type.getMethods()) {
73             if (BindingReflections.isNotificationCallback(method)) {
74                 final Class<? extends Notification> notification =
75                         (Class<? extends Notification>) method.getParameterTypes()[0];
76                 ret.add(notification);
77             }
78         }
79         return ret;
80     }
81
82     private final class InvokerContext {
83
84         private final QName name;
85         private final NotificationListenerInvoker invoker;
86
87         private InvokerContext(final QName name, final NotificationListenerInvoker invoker) {
88             this.name = name;
89             this.invoker = invoker;
90         }
91
92         public void invoke(final Notification notification) {
93             invoker.invokeNotification(delegate, name, notification);
94         }
95     }
96 }