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.impl;
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;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22 import org.opendaylight.yangtools.yang.binding.NotificationListener;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24 import org.opendaylight.yangtools.yang.binding.util.NotificationListenerInvoker;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
30 private final BindingNormalizedNodeSerializer codec;
31 private final NotificationListener delegate;
32 private final Map<SchemaPath,NotificationListenerInvoker> invokers;
34 BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec,
35 final NotificationListener delegate) {
37 this.delegate = delegate;
38 this.invokers = createInvokerMapFor(delegate.getClass());
42 public void onNotification(@Nonnull final DOMNotification notification) {
43 final Notification baNotification = deserialize(notification);
44 final QName notificationQName = notification.getType().getLastComponent();
45 getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
48 private Notification deserialize(final DOMNotification notification) {
49 if (notification instanceof LazySerializedDOMNotification) {
50 return ((LazySerializedDOMNotification) notification).getBindingData();
52 return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
55 private NotificationListenerInvoker getInvoker(final SchemaPath type) {
56 return invokers.get(type);
59 protected Set<SchemaPath> getSupportedNotifications() {
60 return invokers.keySet();
63 public static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(
64 final Class<? extends NotificationListener> implClz) {
65 final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
66 for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
67 Class<?> iface = ifaceToken.getRawType();
68 if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
69 @SuppressWarnings("unchecked")
70 final Class<? extends NotificationListener> listenerType =
71 (Class<? extends NotificationListener>) iface;
72 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
73 for (final SchemaPath path : getNotificationTypes(listenerType)) {
74 builder.put(path, invoker);
78 return ImmutableMap.copyOf(builder);
81 private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
82 // TODO: Investigate possibility and performance impact if we cache this or expose
83 // it from NotificationListenerInvoker
84 final Set<SchemaPath> ret = new HashSet<>();
85 for (final Method method : type.getMethods()) {
86 if (BindingReflections.isNotificationCallback(method)) {
87 final Class<?> notification = method.getParameterTypes()[0];
88 final QName name = BindingReflections.findQName(notification);
89 ret.add(SchemaPath.create(true, name));