Split off DefaultBindingCodecTreeFactory
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationListenerAdapter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
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;
15 import java.util.Map;
16 import java.util.Set;
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;
27
28 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
29
30     private final BindingNormalizedNodeSerializer codec;
31     private final NotificationListener delegate;
32     private final ImmutableMap<SchemaPath, NotificationListenerInvoker> invokers;
33
34     BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec,
35             final NotificationListener delegate) {
36         this.codec = codec;
37         this.delegate = delegate;
38         this.invokers = createInvokerMapFor(delegate.getClass());
39     }
40
41     @Override
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);
46     }
47
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();
53         }
54         return notification instanceof DOMEvent ? codec.fromNormalizedNodeNotification(notification.getType(),
55             notification.getBody(), ((DOMEvent) notification).getEventInstant())
56                 : codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
57     }
58
59     private NotificationListenerInvoker getInvoker(final SchemaPath type) {
60         return invokers.get(type);
61     }
62
63     protected Set<SchemaPath> getSupportedNotifications() {
64         return invokers.keySet();
65     }
66
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);
79                 }
80             }
81         }
82         return ImmutableMap.copyOf(builder);
83     }
84
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));
94             }
95         }
96         return ret;
97     }
98 }