c9a1756435538acfade024a0317f5abedb144c67
[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 java.lang.reflect.Method;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
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;
26
27 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
28
29     private final BindingNormalizedNodeSerializer codec;
30     private final NotificationListener delegate;
31     private final Map<SchemaPath,NotificationListenerInvoker> invokers;
32
33     public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final NotificationListener delegate) {
34         this.codec = codec;
35         this.delegate = delegate;
36         this.invokers = createInvokerMapFor(delegate.getClass());
37     }
38
39     @Override
40     public void onNotification(@Nonnull final DOMNotification notification) {
41         final Notification baNotification =
42                 codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
43         final QName notificationQName = notification.getType().getLastComponent();
44         getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
45     }
46
47     private NotificationListenerInvoker getInvoker(final SchemaPath type) {
48         return invokers.get(type);
49     }
50
51     protected Set<SchemaPath> getSupportedNotifications() {
52         return invokers.keySet();
53     }
54
55     private static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(final Class<? extends NotificationListener> implClz) {
56         final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
57         for(final Class<?> iface : implClz.getInterfaces()) {
58             if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
59                 @SuppressWarnings("unchecked")
60                 final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
61                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
62                 for(final SchemaPath path : getNotificationTypes(listenerType)) {
63                     builder.put(path, invoker);
64                 }
65             }
66         }
67         return ImmutableMap.copyOf(builder);
68     }
69
70     private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
71         // TODO: Investigate possibility and performance impact if we cache this or expose
72         // it from NotificationListenerInvoker
73         final Set<SchemaPath> ret = new HashSet<>();
74         for(final Method method : type.getMethods()) {
75             if(BindingReflections.isNotificationCallback(method)) {
76                 final Class<?> notification = method.getParameterTypes()[0];
77                 final QName name = BindingReflections.findQName(notification);
78                 ret.add(SchemaPath.create(true, name));
79             }
80         }
81         return ret;
82     }
83 }