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 public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final NotificationListener delegate) {
36 this.delegate = delegate;
37 this.invokers = createInvokerMapFor(delegate.getClass());
41 public void onNotification(@Nonnull final DOMNotification notification) {
42 final Notification baNotification = deserialize(notification);
43 final QName notificationQName = notification.getType().getLastComponent();
44 getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
47 private Notification deserialize(final DOMNotification notification) {
48 if(notification instanceof LazySerializedDOMNotification) {
49 return ((LazySerializedDOMNotification) notification).getBindingData();
51 return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
54 private NotificationListenerInvoker getInvoker(final SchemaPath type) {
55 return invokers.get(type);
58 protected Set<SchemaPath> getSupportedNotifications() {
59 return invokers.keySet();
62 public static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(final Class<? extends NotificationListener> implClz) {
63 final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
64 for(final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
65 Class<?> iface = ifaceToken.getRawType();
66 if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
67 @SuppressWarnings("unchecked")
68 final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
69 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
70 for(final SchemaPath path : getNotificationTypes(listenerType)) {
71 builder.put(path, invoker);
75 return ImmutableMap.copyOf(builder);
78 private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
79 // TODO: Investigate possibility and performance impact if we cache this or expose
80 // it from NotificationListenerInvoker
81 final Set<SchemaPath> ret = new HashSet<>();
82 for(final Method method : type.getMethods()) {
83 if(BindingReflections.isNotificationCallback(method)) {
84 final Class<?> notification = method.getParameterTypes()[0];
85 final QName name = BindingReflections.findQName(notification);
86 ret.add(SchemaPath.create(true, name));