Deprecate ClassToInstance-taking methods
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / RpcProviderService.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.api;
9
10 import com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableClassToInstanceMap;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Set;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Registration;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.Rpc;
19
20 /**
21  * Provides ability to registered Remote Procedure Call (RPC) service implementations. The RPCs are defined in YANG
22  * models.
23  */
24 public interface RpcProviderService extends BindingService {
25     /**
26      * Register an {@link Rpc} implementation.
27      *
28      * @param implementation implementation object
29      * @return A {@link Registration} controlling unregistration
30      * @throws NullPointerException if {@code implementation} is {@code null}
31      */
32     @NonNull Registration registerRpcImplementation(Rpc<?, ?> implementation);
33
34     /**
35      * Register an {@link Rpc} implementation on a set of datastore context paths.
36      *
37      * @param implementation implementation object
38      * @param paths Datastore paths to service
39      * @return A {@link Registration} controlling unregistration
40      * @throws NullPointerException if any argument is {@code null}
41      */
42     @NonNull Registration registerRpcImplementation(Rpc<?, ?> implementation, Set<InstanceIdentifier<?>> paths);
43
44     /**
45      * Register a set of {@link Rpc} implementations.
46      *
47      * @param implementations implementation objects
48      * @return A {@link Registration} controlling unregistration
49      * @throws NullPointerException if {@code implementations} is, or contains, {@code null}
50      * @throws IllegalArgumentException if there are implementations contains {@link Rpc#implementedInterface()}
51      *                                  duplicates
52      */
53     default @NonNull Registration registerRpcImplementations(final Rpc<?, ?>... implementations) {
54         return registerRpcImplementations(List.of(implementations));
55     }
56
57     /**
58      * Register a set of {@link Rpc} implementations.
59      *
60      * @param implementations implementation objects
61      * @return A {@link Registration} controlling unregistration
62      * @throws NullPointerException if {@code implementations} is, or contains, {@code null}
63      * @throws IllegalArgumentException if there are implementations contains {@link Rpc#implementedInterface()}
64      *                                  duplicates
65      */
66     default @NonNull Registration registerRpcImplementations(final Collection<Rpc<?, ?>> implementations) {
67         return registerRpcImplementations(indexImplementations(implementations));
68     }
69
70     /**
71      * Register a set of {@link Rpc} implementations on a set of datastore context paths. Note that this method does not
72      * support registering multiple implementations of the same {@link Rpc} and hence we require specifying them through
73      * a {@link ClassToInstanceMap}.
74      *
75      * @param implementations implementation objects
76      * @return A {@link Registration} controlling unregistration
77      * @throws NullPointerException if any argument is, or contains, {@code null}
78      * @throws IllegalArgumentException if there are implementations contains {@link Rpc#implementedInterface()}
79      *                                  duplicates
80      */
81     default @NonNull Registration registerRpcImplementations(final Collection<Rpc<?, ?>> implementations,
82             final Set<InstanceIdentifier<?>> paths) {
83         return registerRpcImplementations(indexImplementations(implementations), paths);
84     }
85
86     /**
87      * Register a set of {@link Rpc} implementations. Note that this method does not support registering multiple
88      * implementations of the same {@link Rpc} and hence we require specifying them through a
89      * {@link ClassToInstanceMap}.
90      *
91      * @param implementations implementation objects
92      * @return A {@link Registration} controlling unregistration
93      * @throws NullPointerException if {@code implementations} is {@code null}
94      * @deprecated Use {@link #registerRpcImplementations(Collection)} or {@link #registerRpcImplementations(Rpc...)}
95      *             instead.
96      */
97     @Deprecated(since = "13.0.1")
98     @NonNull Registration registerRpcImplementations(ClassToInstanceMap<Rpc<?, ?>> implementations);
99
100     /**
101      * Register a set of {@link Rpc} implementations on a set of datastore context paths. Note that this method does not
102      * support registering multiple implementations of the same {@link Rpc} and hence we require specifying them through
103      * a {@link ClassToInstanceMap}.
104      *
105      * @param implementations implementation objects
106      * @return A {@link Registration} controlling unregistration
107      * @throws NullPointerException if any argument is {@code null}
108      * @deprecated Use {@link #registerRpcImplementations(Collection, Set)} instead
109      */
110     @Deprecated(since = "13.0.1")
111     @NonNull Registration registerRpcImplementations(ClassToInstanceMap<Rpc<?, ?>> implementations,
112         Set<InstanceIdentifier<?>> paths);
113
114     @SuppressWarnings("unchecked")
115     private static @NonNull ImmutableClassToInstanceMap<Rpc<?, ?>> indexImplementations(
116             final Collection<Rpc<?, ?>> impls) {
117         final var builder = ImmutableClassToInstanceMap.<Rpc<?, ?>>builder();
118         for (var impl : impls) {
119             builder.put((Class<Rpc<?, ?>>) impl.implementedInterface(), impl);
120         }
121         return builder.build();
122     }
123 }