ff8cf900e066af6ebaba2b1488bfa3bb84038290
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / 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.controller.md.sal.binding.impl;
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 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.adapter.invoke.NotificationListenerInvoker;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.opendaylight.yangtools.yang.binding.NotificationListener;
24 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
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 Map<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(@Nonnull 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             return ((LazySerializedDOMNotification) notification).getBindingData();
51         }
52         return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
53     }
54
55     private NotificationListenerInvoker getInvoker(final SchemaPath type) {
56         return invokers.get(type);
57     }
58
59     protected Set<SchemaPath> getSupportedNotifications() {
60         return invokers.keySet();
61     }
62
63     public static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(
64             final Class<? extends NotificationListener> implClz) {
65         final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
66         for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
67             Class<?> iface = ifaceToken.getRawType();
68             if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
69                 @SuppressWarnings("unchecked")
70                 final Class<? extends NotificationListener> listenerType =
71                         (Class<? extends NotificationListener>) iface;
72                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
73                 for (final SchemaPath path : getNotificationTypes(listenerType)) {
74                     builder.put(path, invoker);
75                 }
76             }
77         }
78         return ImmutableMap.copyOf(builder);
79     }
80
81     private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
82         // TODO: Investigate possibility and performance impact if we cache this or expose
83         // it from NotificationListenerInvoker
84         final Set<SchemaPath> ret = new HashSet<>();
85         for (final Method method : type.getMethods()) {
86             if (BindingReflections.isNotificationCallback(method)) {
87                 final Class<?> notification = method.getParameterTypes()[0];
88                 final QName name = BindingReflections.findQName(notification);
89                 ret.add(SchemaPath.create(true, name));
90             }
91         }
92         return ret;
93     }
94 }