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 java.lang.reflect.Method;
12 import java.util.HashMap;
13 import java.util.HashSet;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
19 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21 import org.opendaylight.yangtools.yang.binding.NotificationListener;
22 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
23 import org.opendaylight.yangtools.yang.binding.util.NotificationListenerInvoker;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
29 private final BindingNormalizedNodeSerializer codec;
30 private final NotificationListener delegate;
31 private final Map<SchemaPath,NotificationListenerInvoker> invokers;
33 public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final NotificationListener delegate) {
35 this.delegate = delegate;
36 this.invokers = createInvokerMapFor(delegate.getClass());
40 public void onNotification(@Nonnull final DOMNotification notification) {
41 final Notification baNotification = deserialize(notification);
42 final QName notificationQName = notification.getType().getLastComponent();
43 getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
46 private Notification deserialize(final DOMNotification notification) {
47 if(notification instanceof LazySerializedDOMNotification) {
48 return ((LazySerializedDOMNotification) notification).getBindingData();
50 return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
53 private NotificationListenerInvoker getInvoker(final SchemaPath type) {
54 return invokers.get(type);
57 protected Set<SchemaPath> getSupportedNotifications() {
58 return invokers.keySet();
61 public static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(final Class<? extends NotificationListener> implClz) {
62 final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
63 for(final Class<?> iface : implClz.getInterfaces()) {
64 if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
65 @SuppressWarnings("unchecked")
66 final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
67 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
68 for(final SchemaPath path : getNotificationTypes(listenerType)) {
69 builder.put(path, invoker);
73 return ImmutableMap.copyOf(builder);
76 private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
77 // TODO: Investigate possibility and performance impact if we cache this or expose
78 // it from NotificationListenerInvoker
79 final Set<SchemaPath> ret = new HashSet<>();
80 for(final Method method : type.getMethods()) {
81 if(BindingReflections.isNotificationCallback(method)) {
82 final Class<?> notification = method.getParameterTypes()[0];
83 final QName name = BindingReflections.findQName(notification);
84 ret.add(SchemaPath.create(true, name));