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 org.opendaylight.controller.md.sal.dom.api.DOMNotification;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
19 import org.opendaylight.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.opendaylight.yangtools.yang.binding.NotificationListener;
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 BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec,
34 final NotificationListener delegate) {
36 this.delegate = delegate;
37 this.invokers = createInvokerMapFor(delegate.getClass());
41 public void onNotification(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(
63 final Class<? extends NotificationListener> implClz) {
64 final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
65 for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
66 Class<?> iface = ifaceToken.getRawType();
67 if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
68 @SuppressWarnings("unchecked")
69 final Class<? extends NotificationListener> listenerType =
70 (Class<? extends NotificationListener>) iface;
71 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
72 for (final SchemaPath path : getNotificationTypes(listenerType)) {
73 builder.put(path, invoker);
77 return ImmutableMap.copyOf(builder);
80 private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
81 // TODO: Investigate possibility and performance impact if we cache this or expose
82 // it from NotificationListenerInvoker
83 final Set<SchemaPath> ret = new HashSet<>();
84 for (final Method method : type.getMethods()) {
85 if (BindingReflections.isNotificationCallback(method)) {
86 final Class<?> notification = method.getParameterTypes()[0];
87 final QName name = BindingReflections.findQName(notification);
88 ret.add(SchemaPath.create(true, name));