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.annotations.Beta;
15 import com.google.common.collect.ClassToInstanceMap;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.util.concurrent.FluentFuture;
18 import java.lang.reflect.Proxy;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.mdsal.binding.api.ActionService;
23 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
25 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.dom.api.DOMActionService;
28 import org.opendaylight.mdsal.dom.api.DOMService;
29 import org.opendaylight.yangtools.concepts.Delegator;
30 import org.opendaylight.yangtools.yang.binding.Action;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.RpcInput;
34 import org.opendaylight.yangtools.yang.binding.RpcOutput;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
39 // FIXME: make this class non-public once the controller user is gone
40 public final class ActionServiceAdapter
41 extends AbstractBindingLoadingAdapter<DOMActionService, Class<? extends Action<?, ?, ?>>, ActionAdapter>
42 implements ActionService {
43 private static final class Builder extends BindingDOMAdapterBuilder<ActionService> {
45 protected ActionService createInstance(final @Nullable BindingToNormalizedNodeCodec codec,
46 final ClassToInstanceMap<DOMService> delegates) {
47 final DOMActionService domAction = delegates.getInstance(DOMActionService.class);
48 return new ActionServiceAdapter(requireNonNull(codec), domAction);
52 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
53 return ImmutableSet.of(DOMActionService.class);
57 private static final class ConstrainedAction implements Delegator<Action<?, ?, ?>>,
58 Action<InstanceIdentifier<?>, RpcInput, RpcOutput> {
59 private final Action<InstanceIdentifier<?>, RpcInput, RpcOutput> delegate;
60 private final Set<? extends DataTreeIdentifier<?>> nodes;
62 ConstrainedAction(final Action<?, ?, ?> delegate, final Set<? extends DataTreeIdentifier<?>> nodes) {
63 this.delegate = requireNonNull((Action) delegate);
64 this.nodes = requireNonNull(nodes);
68 public FluentFuture<RpcResult<RpcOutput>> invoke(final InstanceIdentifier<?> path, final RpcInput input) {
69 checkState(nodes.contains(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path)),
70 "Cannot service %s", path);
71 return delegate.invoke(path, input);
75 public Action<?, ?, ?> getDelegate() {
80 static final Factory<ActionService> BUILDER_FACTORY = Builder::new;
82 ActionServiceAdapter(final BindingToNormalizedNodeCodec codec, final DOMActionService delegate) {
83 super(codec, delegate);
87 public static ActionServiceAdapter create(final BindingToNormalizedNodeCodec codec,
88 final DOMActionService delegate) {
89 return new ActionServiceAdapter(codec, delegate);
93 public <O extends DataObject, T extends Action<?, ?, ?>> T getActionHandle(final Class<T> actionInterface,
94 final Set<DataTreeIdentifier<O>> nodes) {
95 return !nodes.isEmpty() ? (T) new ConstrainedAction(getActionHandle(actionInterface, ImmutableSet.of()), nodes)
96 : (T) Proxy.newProxyInstance(actionInterface.getClassLoader(), new Class[] { actionInterface },
97 getAdapter(actionInterface));
101 ActionAdapter loadAdapter(final Class<? extends Action<?, ?, ?>> key) {
102 checkArgument(BindingReflections.isBindingClass(key));
103 checkArgument(key.isInterface(), "Supplied Action type must be an interface.");
104 return new ActionAdapter(getCodec(), getDelegate(), key);