Fix action invocation and registration
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / ActionService.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.annotations.Beta;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Set;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yangtools.yang.binding.Action;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.RpcInput;
19
20 /**
21  * Provides access to registered {@code action} implementations. Each action is defined in a YANG model,
22  * and implementations are added dynamically at runtime, via {@link ActionProviderService}.
23  *
24  * @author Robert Varga
25  */
26 @Beta
27 @NonNullByDefault
28 public interface ActionService extends BindingService {
29     /**
30      * Returns an implementation of a requested {@link Action}. Returned instance is not an actual implementation
31      * of the Action service interface, but a proxy implementation of the interface that forwards to an actual
32      * implementation, if any.
33      *
34      * <p>
35      * The following describes the behavior of the proxy when invoking
36      * {@link Action#invoke(InstanceIdentifier, RpcInput)}:
37      * <ul>
38      * <li>If an actual implementation is registered with the MD-SAL, all invocations are forwarded to the registered
39      * implementation.</li>
40      * <li>If no actual implementation is registered, all invocations will fail by throwing
41      * {@link IllegalStateException}.</li>
42      * <li>Prior to invoking the actual implementation, the method arguments are are validated. If any are invalid,
43      * an {@link IllegalArgumentException} is thrown.
44      * </ul>
45      *
46      * <p>
47      * The returned proxy is automatically updated with the most recent registered implementation, hence there is no
48      * guarantee that multiple consecutive invocations will be handled by the same implementation.
49      *
50      * @param spec Action instance specification
51      * @param validNodes Set of nodes this service will be constrained to, empty if no constraints are known
52      * @return A proxy implementation of the generated interface
53      * @throws NullPointerException if {@code actionInterface} is null
54      * @throws IllegalArgumentException when {@code actionInterface} does not conform to the Binding Specification
55      */
56     <P extends DataObject, A extends Action<InstanceIdentifier<P>, ?, ?>> A getActionHandle(ActionSpec<A, P> spec,
57         Set<DataTreeIdentifier<P>> validNodes);
58
59     default <P extends DataObject, A extends Action<InstanceIdentifier<P>, ?, ?>> A getActionHandle(
60             final ActionSpec<A, P> spec) {
61         return getActionHandle(spec, ImmutableSet.of());
62     }
63
64     default <P extends DataObject, A extends Action<InstanceIdentifier<P>, ?, ?>> A getActionHandle(
65             final ActionSpec<A, P> spec, final LogicalDatastoreType dataStore, final InstanceIdentifier<P> path) {
66         return getActionHandle(spec, ImmutableSet.of(DataTreeIdentifier.create(dataStore, path)));
67     }
68
69     default <P extends DataObject, A extends Action<InstanceIdentifier<P>, ?, ?>> A getActionHandle(
70             final ActionSpec<A, P> spec, final InstanceIdentifier<P> path) {
71         return getActionHandle(spec, LogicalDatastoreType.OPERATIONAL, path);
72     }
73
74     default <P extends DataObject, A extends Action<InstanceIdentifier<P>, ?, ?>> A getActionHandle(
75             final ActionSpec<A, P> spec, @SuppressWarnings("unchecked") final DataTreeIdentifier<P>... nodes) {
76         return getActionHandle(spec, ImmutableSet.copyOf(nodes));
77     }
78 }