Removed sonar warnings.
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / util / NotificationListenerInvoker.java
1 /*
2  * Copyright (c) 2013 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.yangtools.yang.binding.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import java.lang.invoke.MethodHandle;
18 import java.lang.invoke.MethodHandles;
19 import java.lang.invoke.MethodHandles.Lookup;
20 import java.lang.invoke.MethodType;
21 import java.lang.reflect.Method;
22 import java.util.Map;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.binding.NotificationListener;
27 import org.opendaylight.yangtools.yang.common.QName;
28
29 /**
30  * Provides single method invocation of notificatoin callbacks on supplied instance.
31  *
32  * Notification Listener invoker provides common invocation interface for any subtype of {@link NotificationListener}.
33  * via {@link #invokeNotification(NotificationListener, QName, DataContainer)} method.
34  *
35  */
36 public final class NotificationListenerInvoker {
37
38     private static final Lookup LOOKUP = MethodHandles.publicLookup();
39
40     private static final LoadingCache<Class<? extends NotificationListener>, NotificationListenerInvoker> INVOKERS = CacheBuilder
41             .newBuilder().weakKeys()
42             .build(new CacheLoader<Class<? extends NotificationListener>, NotificationListenerInvoker>() {
43
44                 private NotificationListenerInvoker createInvoker(
45                         final Class<? extends NotificationListener> key) {
46                     return new NotificationListenerInvoker(createInvokerMap(key));
47                 }
48
49                 private Map<QName, MethodHandle> createInvokerMap(final Class<? extends NotificationListener> key) {
50                     final Builder<QName, MethodHandle> ret = ImmutableMap.<QName, MethodHandle> builder();
51                     for (final Method method : key.getMethods()) {
52                         if (BindingReflections.isNotificationCallback(method)) {
53
54                             final Class<?> notification = method.getParameterTypes()[0];
55                             final QName name = BindingReflections.findQName(notification);
56                             MethodHandle handle;
57                             try {
58                                 handle = LOOKUP.unreflect(method).asType(MethodType.methodType(void.class,
59                                         NotificationListener.class, DataContainer.class));
60                                 ret.put(name, handle);
61                             } catch (final IllegalAccessException e) {
62                                 throw new IllegalStateException("Can not access public method.", e);
63                             }
64                         }
65
66                     }
67                     return ret.build();
68                 }
69
70                 @Override
71                 public NotificationListenerInvoker load(final Class<? extends NotificationListener> key) throws Exception {
72                     return createInvoker(key);
73                 }
74
75             });
76
77     private final Map<QName, MethodHandle> methodInvokers;
78
79     public NotificationListenerInvoker(final Map<QName, MethodHandle> map) {
80         this.methodInvokers = map;
81     }
82
83     /**
84      *
85      * Creates RPCServiceInvoker for specified RpcService type
86      *
87      * @param type
88      *            RpcService interface, which was generated from model.
89      * @return Cached instance of {@link NotificationListenerInvoker} for
90      *         supplied RPC type.
91      *
92      */
93     public static NotificationListenerInvoker from(final Class<? extends NotificationListener> type) {
94         Preconditions.checkArgument(type.isInterface());
95         Preconditions.checkArgument(BindingReflections.isBindingClass(type));
96         return INVOKERS.getUnchecked(type);
97     }
98
99     /**
100      * Invokes supplied RPC on provided implementation of RPC Service.
101      *
102      * @param impl
103      *            Imlementation on which notifiaction callback should be
104      *            invoked.
105      * @param rpcName
106      *            Name of RPC to be invoked.
107      * @param input
108      *            Input data for RPC.
109      *
110      */
111     public void invokeNotification(@Nonnull final NotificationListener impl, @Nonnull final QName rpcName,
112             @Nullable final DataContainer input) {
113         Preconditions.checkNotNull(impl, "implemetation must be supplied");
114         final MethodHandle invoker = methodInvokers.get(rpcName);
115         Preconditions.checkArgument(invoker != null, "Supplied notification is not valid for implementation %s", impl);
116         try {
117             invoker.invokeExact(impl, input);
118         } catch (final Throwable e) {
119             throw Throwables.propagate(e);
120         }
121     }
122
123 }