Remove OSGiBindingAdapterFactory
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.reflect.TypeToken;
14 import java.lang.reflect.Method;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import org.opendaylight.mdsal.binding.dom.adapter.invoke.NotificationListenerInvoker;
20 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
21 import org.opendaylight.mdsal.dom.api.DOMEvent;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25 import org.opendaylight.yangtools.yang.binding.NotificationListener;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
28
29 class BindingDOMNotificationListenerAdapter implements DOMNotificationListener {
30
31     private final AdapterContext adapterContext;
32     private final NotificationListener delegate;
33     private final ImmutableMap<Absolute, NotificationListenerInvoker> invokers;
34
35     BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final NotificationListener delegate) {
36         this.adapterContext = requireNonNull(adapterContext);
37         this.delegate = requireNonNull(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().lastNodeIdentifier();
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
55         final CurrentAdapterSerializer serializer = adapterContext.currentSerializer();
56         return notification instanceof DOMEvent ? serializer.fromNormalizedNodeNotification(notification.getType(),
57             notification.getBody(), ((DOMEvent) notification).getEventInstant())
58                 : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
59     }
60
61     private NotificationListenerInvoker getInvoker(final Absolute type) {
62         return invokers.get(type);
63     }
64
65     protected Set<Absolute> getSupportedNotifications() {
66         return invokers.keySet();
67     }
68
69     private static ImmutableMap<Absolute, NotificationListenerInvoker> createInvokerMapFor(
70             final Class<? extends NotificationListener> implClz) {
71         final Map<Absolute, NotificationListenerInvoker> builder = new HashMap<>();
72         for (final TypeToken<?> ifaceToken : TypeToken.of(implClz).getTypes().interfaces()) {
73             Class<?> iface = ifaceToken.getRawType();
74             if (NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
75                 @SuppressWarnings("unchecked")
76                 final Class<? extends NotificationListener> listenerType
77                         = (Class<? extends NotificationListener>) iface;
78                 final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
79                 for (final Absolute path : getNotificationTypes(listenerType)) {
80                     builder.put(path, invoker);
81                 }
82             }
83         }
84         return ImmutableMap.copyOf(builder);
85     }
86
87     private static Set<Absolute> getNotificationTypes(final Class<? extends NotificationListener> type) {
88         // TODO: Investigate possibility and performance impact if we cache this or expose
89         // it from NotificationListenerInvoker
90         final Set<Absolute> ret = new HashSet<>();
91         for (final Method method : type.getMethods()) {
92             if (BindingReflections.isNotificationCallback(method)) {
93                 final Class<?> notification = method.getParameterTypes()[0];
94                 ret.add(Absolute.of(BindingReflections.findQName(notification)));
95             }
96         }
97         return ret;
98     }
99 }