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.mdsal.binding.dom.adapter;
10 import static java.util.Objects.requireNonNull;
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;
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;
25 final class BindingDOMNotificationListenerAdapter extends AbstractDOMNotificationListenerAdapter {
26 private final ImmutableMap<Absolute, NotificationListenerInvoker> invokers;
27 private final NotificationListener delegate;
29 BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final NotificationListener delegate) {
30 super(adapterContext);
31 this.delegate = requireNonNull(delegate);
32 invokers = createInvokerMapFor(delegate.getClass());
36 void onNotification(final Absolute domType, final Notification notification) {
37 invokers.get(domType).invokeNotification(delegate, domType.lastNodeIdentifier(), notification);
41 Set<Absolute> getSupportedNotifications() {
42 return invokers.keySet();
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);
60 return ImmutableMap.copyOf(builder);
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)));