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.dom.api.DOMActionService;
27 import org.opendaylight.mdsal.dom.api.DOMService;
28 import org.opendaylight.yangtools.concepts.Delegator;
29 import org.opendaylight.yangtools.yang.binding.Action;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.RpcInput;
33 import org.opendaylight.yangtools.yang.binding.RpcOutput;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
38 final class ActionServiceAdapter
39 extends AbstractBindingLoadingAdapter<DOMActionService, Class<? extends Action<?, ?, ?>>, ActionAdapter>
40 implements ActionService {
41 private static final class Builder extends BindingDOMAdapterBuilder<ActionService> {
43 protected ActionService createInstance(final @Nullable BindingToNormalizedNodeCodec codec,
44 final ClassToInstanceMap<DOMService> delegates) {
45 final DOMActionService domAction = delegates.getInstance(DOMActionService.class);
46 return new ActionServiceAdapter(requireNonNull(codec), domAction);
50 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
51 return ImmutableSet.of(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 FluentFuture<RpcResult<RpcOutput>> invoke(final InstanceIdentifier<?> path, final RpcInput input) {
67 checkState(nodes.contains(path), "Cannot service %s", path);
68 return delegate.invoke(path, input);
72 public Action<?, ?, ?> getDelegate() {
77 static final Factory<ActionService> BUILDER_FACTORY = Builder::new;
79 ActionServiceAdapter(final BindingToNormalizedNodeCodec codec, final DOMActionService delegate) {
80 super(codec, delegate);
84 public <O extends DataObject, T extends Action<?, ?, ?>> T getActionHandle(final Class<T> actionInterface,
85 final Set<DataTreeIdentifier<O>> nodes) {
86 return !nodes.isEmpty() ? (T) new ConstrainedAction(getActionHandle(actionInterface, ImmutableSet.of()), nodes)
87 : (T) Proxy.newProxyInstance(actionInterface.getClassLoader(), new Class[] { actionInterface },
88 getAdapter(actionInterface));
92 ActionAdapter loadAdapter(final Class<? extends Action<?, ?, ?>> key) {
93 checkArgument(BindingReflections.isBindingClass(key));
94 checkArgument(key.isInterface(), "Supplied Action type must be an interface.");
95 return new ActionAdapter(getCodec(), getDelegate(), key);