Fix action invocation and registration
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / ActionServiceAdapter.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.dom.adapter;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.lang.reflect.Proxy;
15 import java.util.Set;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.opendaylight.mdsal.binding.api.ActionService;
18 import org.opendaylight.mdsal.binding.api.ActionSpec;
19 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
21 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.mdsal.dom.api.DOMActionService;
23 import org.opendaylight.mdsal.dom.api.DOMService;
24 import org.opendaylight.yangtools.yang.binding.Action;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27
28 @NonNullByDefault
29 final class ActionServiceAdapter
30         extends AbstractBindingLoadingAdapter<DOMActionService, ActionSpec<?, ?>, ActionAdapter>
31         implements ActionService {
32     private static final class Builder extends BindingDOMAdapterBuilder<ActionService> {
33         Builder(final AdapterContext adapterContext) {
34             super(adapterContext);
35         }
36
37         @Override
38         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
39             return ImmutableSet.of(DOMActionService.class);
40         }
41
42         @Override
43         protected ActionService createInstance(final ClassToInstanceMap<DOMService> delegates) {
44             return new ActionServiceAdapter(adapterContext(), delegates.getInstance(DOMActionService.class));
45         }
46     }
47
48     static final Factory<ActionService> BUILDER_FACTORY = Builder::new;
49
50     ActionServiceAdapter(final AdapterContext adapterContext, final DOMActionService delegate) {
51         super(adapterContext, delegate);
52     }
53
54     @Override
55     public <P extends DataObject, A extends Action<InstanceIdentifier<P>, ?, ?>> A getActionHandle(
56             final ActionSpec<A, P> spec, final Set<DataTreeIdentifier<P>> nodes) {
57         final var type = spec.type();
58         final var adapter = getAdapter(spec);
59         return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type },
60             nodes.isEmpty() ? adapter : new ActionAdapterFilter(adapter, Set.copyOf(nodes))));
61     }
62
63     @Override
64     ActionAdapter loadAdapter(final ActionSpec<?, ?> key) {
65         final var type = key.type();
66         checkArgument(BindingReflections.isBindingClass(type));
67         checkArgument(type.isInterface(), "Supplied Action type must be an interface.");
68         return new ActionAdapter(adapterContext(), getDelegate(), key);
69     }
70 }