Move BindingReflections.isNotificationCallback()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationListenerAdapter.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.mdsal.binding.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.reflect.TypeToken;
14 import java.lang.reflect.Method;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
21 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.opendaylight.yangtools.yang.binding.NotificationListener;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
25
26 final class BindingDOMNotificationListenerAdapter extends AbstractDOMNotificationListenerAdapter {
27     private final ImmutableMap<Absolute, NotificationListenerInvoker> invokers;
28     private final @NonNull NotificationListener delegate;
29
30     BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final NotificationListener delegate) {
31         super(adapterContext);
32         this.delegate = requireNonNull(delegate);
33         invokers = createInvokerMapFor(delegate.getClass());
34     }
35
36     @Override
37     void onNotification(final Absolute domType, final Notification<?> notification) {
38         invokers.get(domType).invokeNotification(delegate, domType.lastNodeIdentifier(), notification);
39     }
40
41     @Override
42     Set<Absolute> getSupportedNotifications() {
43         return invokers.keySet();
44     }
45
46     private static ImmutableMap<Absolute, NotificationListenerInvoker> createInvokerMapFor(
47             final Class<? extends NotificationListener> implClz) {
48         final Map<Absolute, NotificationListenerInvoker> builder = new HashMap<>();
49         for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
50             Class<?> iface = ifaceToken.getRawType();
51             if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
52                 @SuppressWarnings("unchecked")
53                 final Class<? extends NotificationListener> listenerType
54                         = (Class<? extends NotificationListener>) iface;
55                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
56                 for (final Absolute path : getNotificationTypes(listenerType)) {
57                     builder.put(path, invoker);
58                 }
59             }
60         }
61         return ImmutableMap.copyOf(builder);
62     }
63
64     private static Set<Absolute> getNotificationTypes(final Class<? extends NotificationListener> type) {
65         // TODO: Investigate possibility and performance impact if we cache this or expose
66         // it from NotificationListenerInvoker
67         final Set<Absolute> ret = new HashSet<>();
68         for (final Method method : type.getMethods()) {
69             if (NotificationListenerInvoker.isNotificationCallback(method)) {
70                 final Class<?> notification = method.getParameterTypes()[0];
71                 ret.add(Absolute.of(BindingReflections.findQName(notification)));
72             }
73         }
74         return ret;
75     }
76 }