Use mdsal.dom.codec instead of yangtools.data.codec
[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.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22 import org.opendaylight.yangtools.yang.binding.NotificationListener;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24 import org.opendaylight.yangtools.yang.binding.util.NotificationListenerInvoker;
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     public BindingDOMNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final NotificationListener delegate) {
35         this.codec = codec;
36         this.delegate = delegate;
37         this.invokers = createInvokerMapFor(delegate.getClass());
38     }
39
40     @Override
41     public void onNotification(@Nonnull final DOMNotification notification) {
42         final Notification baNotification = deserialize(notification);
43         final QName notificationQName = notification.getType().getLastComponent();
44         getInvoker(notification.getType()).invokeNotification(delegate, notificationQName, baNotification);
45     }
46
47     private Notification deserialize(final DOMNotification notification) {
48         if(notification instanceof LazySerializedDOMNotification) {
49             return ((LazySerializedDOMNotification) notification).getBindingData();
50         }
51         return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
52     }
53
54     private NotificationListenerInvoker getInvoker(final SchemaPath type) {
55         return invokers.get(type);
56     }
57
58     protected Set<SchemaPath> getSupportedNotifications() {
59         return invokers.keySet();
60     }
61
62     public static Map<SchemaPath, NotificationListenerInvoker> createInvokerMapFor(final Class<? extends NotificationListener> implClz) {
63         final Map<SchemaPath, NotificationListenerInvoker> builder = new HashMap<>();
64         for(final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
65             Class<?> iface = ifaceToken.getRawType();
66             if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
67                 @SuppressWarnings("unchecked")
68                 final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
69                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
70                 for(final SchemaPath path : getNotificationTypes(listenerType)) {
71                     builder.put(path, invoker);
72                 }
73             }
74         }
75         return ImmutableMap.copyOf(builder);
76     }
77
78     private static Set<SchemaPath> getNotificationTypes(final Class<? extends NotificationListener> type) {
79         // TODO: Investigate possibility and performance impact if we cache this or expose
80         // it from NotificationListenerInvoker
81         final Set<SchemaPath> ret = new HashSet<>();
82         for(final Method method : type.getMethods()) {
83             if(BindingReflections.isNotificationCallback(method)) {
84                 final Class<?> notification = method.getParameterTypes()[0];
85                 final QName name = BindingReflections.findQName(notification);
86                 ret.add(SchemaPath.create(true, name));
87             }
88         }
89         return ret;
90     }
91 }