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 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.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
20 import org.opendaylight.mdsal.dom.api.DOMEvent;
21 import org.opendaylight.mdsal.dom.api.DOMNotification;
22 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24 import org.opendaylight.yangtools.yang.binding.NotificationListener;
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 ImmutableMap<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(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 // TODO: This is a routed-back notification, for which we may end up losing event time here, but that is
51 // okay, for now at least.
52 return ((LazySerializedDOMNotification) notification).getBindingData();
54 return notification instanceof DOMEvent ? codec.fromNormalizedNodeNotification(notification.getType(),
55 notification.getBody(), ((DOMEvent) notification).getEventInstant())
56 : codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
59 private NotificationListenerInvoker getInvoker(final SchemaPath type) {
60 return invokers.get(type);
63 protected Set<SchemaPath> getSupportedNotifications() {
64 return invokers.keySet();
67 private static ImmutableMap<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(
68 final Class<? extends NotificationListener> implClz) {
69 final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
70 for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
71 Class<?> iface = ifaceToken.getRawType();
72 if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
73 @SuppressWarnings("unchecked")
74 final Class<? extends NotificationListener> listenerType
75 = (Class<? extends NotificationListener>) iface;
76 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
77 for (final SchemaPath path : getNotificationTypes(listenerType)) {
78 builder.put(path, invoker);
82 return ImmutableMap.copyOf(builder);
85 private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
86 // TODO: Investigate possibility and performance impact if we cache this or expose
87 // it from NotificationListenerInvoker
88 final Set<SchemaPath> ret = new HashSet<>();
89 for (final Method method : type.getMethods()) {
90 if (BindingReflections.isNotificationCallback(method)) {
91 final Class<?> notification = method.getParameterTypes()[0];
92 final QName name = BindingReflections.findQName(notification);
93 ret.add(SchemaPath.create(true, name));