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