Improve RpcProviderService.registerRpcImplementations()
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / RpcProviderService.java
index 8de4f78950c2c3c4678c4c2171fae345676afe7b..6e34376d8938e3e438b6c651ace015ff2cf78d2b 100644 (file)
@@ -8,6 +8,9 @@
 package org.opendaylight.mdsal.binding.api;
 
 import com.google.common.collect.ClassToInstanceMap;
+import com.google.common.collect.ImmutableClassToInstanceMap;
+import java.util.Collection;
+import java.util.List;
 import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.Registration;
@@ -38,6 +41,48 @@ public interface RpcProviderService extends BindingService {
      */
     @NonNull Registration registerRpcImplementation(Rpc<?, ?> implementation, Set<InstanceIdentifier<?>> paths);
 
+    /**
+     * Register a set of {@link Rpc} implementations.
+     *
+     * @param implementations implementation objects
+     * @return A {@link Registration} controlling unregistration
+     * @throws NullPointerException if {@code implementations} is, or contains, {@code null}
+     * @throws IllegalArgumentException if there are implementations contains {@link Rpc#implementedInterface()}
+     *                                  duplicates
+     */
+    default @NonNull Registration registerRpcImplementations(final Rpc<?, ?>... implementations) {
+        return registerRpcImplementations(List.of(implementations));
+    }
+
+    /**
+     * Register a set of {@link Rpc} implementations.
+     *
+     * @param implementations implementation objects
+     * @return A {@link Registration} controlling unregistration
+     * @throws NullPointerException if {@code implementations} is, or contains, {@code null}
+     * @throws IllegalArgumentException if there are implementations contains {@link Rpc#implementedInterface()}
+     *                                  duplicates
+     */
+    default @NonNull Registration registerRpcImplementations(final Collection<Rpc<?, ?>> implementations) {
+        return registerRpcImplementations(indexImplementations(implementations));
+    }
+
+    /**
+     * Register a set of {@link Rpc} implementations on a set of datastore context paths. Note that this method does not
+     * support registering multiple implementations of the same {@link Rpc} and hence we require specifying them through
+     * a {@link ClassToInstanceMap}.
+     *
+     * @param implementations implementation objects
+     * @return A {@link Registration} controlling unregistration
+     * @throws NullPointerException if any argument is, or contains, {@code null}
+     * @throws IllegalArgumentException if there are implementations contains {@link Rpc#implementedInterface()}
+     *                                  duplicates
+     */
+    default @NonNull Registration registerRpcImplementations(final Collection<Rpc<?, ?>> implementations,
+            final Set<InstanceIdentifier<?>> paths) {
+        return registerRpcImplementations(indexImplementations(implementations), paths);
+    }
+
     /**
      * Register a set of {@link Rpc} implementations. Note that this method does not support registering multiple
      * implementations of the same {@link Rpc} and hence we require specifying them through a
@@ -60,4 +105,14 @@ public interface RpcProviderService extends BindingService {
      */
     @NonNull Registration registerRpcImplementations(ClassToInstanceMap<Rpc<?, ?>> implementations,
         Set<InstanceIdentifier<?>> paths);
+
+    @SuppressWarnings("unchecked")
+    private static @NonNull ImmutableClassToInstanceMap<Rpc<?, ?>> indexImplementations(
+            final Collection<Rpc<?, ?>> impls) {
+        final var builder = ImmutableClassToInstanceMap.<Rpc<?, ?>>builder();
+        for (var impl : impls) {
+            builder.put((Class<Rpc<?, ?>>) impl.implementedInterface(), impl);
+        }
+        return builder.build();
+    }
 }