Add entity ownership binding -> DOM adapter
[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 org.opendaylight.mdsal.dom.api.DOMNotification;
11 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
12
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.reflect.TypeToken;
15 import java.lang.reflect.Method;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yangtools.binding.data.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.binding.util.NotificationListenerInvoker;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28
29 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
30
31     private final BindingNormalizedNodeSerializer codec;
32     private final NotificationListener delegate;
33     private final Map<SchemaPath,NotificationListenerInvoker> invokers;
34
35     public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, 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(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 = (Class<? extends NotificationListener>) iface;
70                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
71                 for(final SchemaPath path : getNotificationTypes(listenerType)) {
72                     builder.put(path, invoker);
73                 }
74             }
75         }
76         return ImmutableMap.copyOf(builder);
77     }
78
79     private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
80         // TODO: Investigate possibility and performance impact if we cache this or expose
81         // it from NotificationListenerInvoker
82         final Set<SchemaPath> ret = new HashSet<>();
83         for(final Method method : type.getMethods()) {
84             if(BindingReflections.isNotificationCallback(method)) {
85                 final Class<?> notification = method.getParameterTypes()[0];
86                 final QName name = BindingReflections.findQName(notification);
87                 ret.add(SchemaPath.create(true, name));
88             }
89         }
90         return ret;
91     }
92 }