2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.collect.ClassToInstanceMap;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.lang.reflect.Proxy;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.mdsal.binding.api.ActionService;
21 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
23 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMActionService;
26 import org.opendaylight.mdsal.dom.api.DOMService;
27 import org.opendaylight.yangtools.concepts.Delegator;
28 import org.opendaylight.yangtools.yang.binding.Action;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.binding.RpcInput;
32 import org.opendaylight.yangtools.yang.binding.RpcOutput;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
36 final class ActionServiceAdapter
37 extends AbstractBindingLoadingAdapter<DOMActionService, Class<? extends Action<?, ?, ?>>, ActionAdapter>
38 implements ActionService {
39 private static final class Builder extends BindingDOMAdapterBuilder<ActionService> {
40 Builder(final AdapterContext adapterContext) {
41 super(adapterContext);
45 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
46 return ImmutableSet.of(DOMActionService.class);
50 protected ActionService createInstance(final ClassToInstanceMap<DOMService> delegates) {
51 return new ActionServiceAdapter(adapterContext(), delegates.getInstance(DOMActionService.class));
55 private static final class ConstrainedAction implements Delegator<Action<?, ?, ?>>,
56 Action<InstanceIdentifier<?>, RpcInput, RpcOutput> {
57 private final Action<InstanceIdentifier<?>, RpcInput, RpcOutput> delegate;
58 private final Set<? extends DataTreeIdentifier<?>> nodes;
60 ConstrainedAction(final Action<?, ?, ?> delegate, final Set<? extends DataTreeIdentifier<?>> nodes) {
61 this.delegate = requireNonNull((Action) delegate);
62 this.nodes = requireNonNull(nodes);
66 public ListenableFuture<RpcResult<RpcOutput>> invoke(final InstanceIdentifier<?> path, final RpcInput input) {
67 checkState(nodes.contains(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path)),
68 "Cannot service %s", path);
69 return delegate.invoke(path, input);
73 public Action<?, ?, ?> getDelegate() {
78 static final Factory<ActionService> BUILDER_FACTORY = Builder::new;
80 ActionServiceAdapter(final AdapterContext adapterContext, final DOMActionService delegate) {
81 super(adapterContext, delegate);
85 public <O extends DataObject, T extends Action<?, ?, ?>> T getActionHandle(final Class<T> actionInterface,
86 final Set<DataTreeIdentifier<O>> nodes) {
87 return !nodes.isEmpty() ? (T) new ConstrainedAction(getActionHandle(actionInterface, ImmutableSet.of()), nodes)
88 : (T) Proxy.newProxyInstance(actionInterface.getClassLoader(), new Class[] { actionInterface },
89 getAdapter(actionInterface));
93 ActionAdapter loadAdapter(final Class<? extends Action<?, ?, ?>> key) {
94 checkArgument(BindingReflections.isBindingClass(key));
95 checkArgument(key.isInterface(), "Supplied Action type must be an interface.");
96 return new ActionAdapter(adapterContext(), getDelegate(), key);