Reduce ObjectRegistration use
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / ActionProviderServiceAdapter.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 java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.mdsal.binding.api.ActionProviderService;
21 import org.opendaylight.mdsal.binding.api.ActionSpec;
22 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMActionImplementation;
26 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
27 import org.opendaylight.mdsal.dom.api.DOMActionProviderService;
28 import org.opendaylight.mdsal.dom.api.DOMActionResult;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.mdsal.dom.api.DOMService;
31 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
32 import org.opendaylight.yangtools.concepts.Registration;
33 import org.opendaylight.yangtools.yang.binding.Action;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.ErrorTag;
37 import org.opendaylight.yangtools.yang.common.ErrorType;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.opendaylight.yangtools.yang.common.YangConstants;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 @NonNullByDefault
49 public final class ActionProviderServiceAdapter extends AbstractBindingAdapter<DOMActionProviderService>
50         implements ActionProviderService {
51     private static final class Builder extends BindingDOMAdapterBuilder<ActionProviderService> {
52         Builder(final AdapterContext adapterContext) {
53             super(adapterContext);
54         }
55
56         @Override
57         protected ActionProviderService createInstance(final ClassToInstanceMap<DOMService<?, ?>> delegates) {
58             return new ActionProviderServiceAdapter(adapterContext(),
59                 delegates.getInstance(DOMActionProviderService.class));
60         }
61
62         @Override
63         public Set<? extends Class<? extends DOMService<?, ?>>> getRequiredDelegates() {
64             return ImmutableSet.of(DOMActionProviderService.class);
65         }
66     }
67
68     private static final Logger LOG = LoggerFactory.getLogger(ActionProviderServiceAdapter.class);
69
70     static final Factory<ActionProviderService> BUILDER_FACTORY = Builder::new;
71
72     ActionProviderServiceAdapter(final AdapterContext adapterContext, final DOMActionProviderService delegate) {
73         super(adapterContext, delegate);
74     }
75
76     @Override
77     public <P extends DataObject, A extends Action<? extends InstanceIdentifier<P>, ?, ?>>
78             Registration registerImplementation(final ActionSpec<A, P> spec, final A implementation,
79                 final LogicalDatastoreType datastore, final Set<? extends InstanceIdentifier<P>> validNodes) {
80         final CurrentAdapterSerializer serializer = currentSerializer();
81         final Absolute actionPath = serializer.getActionPath(spec);
82         final Impl impl = new Impl(adapterContext(), actionPath, spec.type(), implementation);
83         final DOMActionInstance instance = validNodes.isEmpty()
84             // Register on the entire datastore
85             ? DOMActionInstance.of(actionPath, DOMDataTreeIdentifier.of(datastore, YangInstanceIdentifier.of()))
86                 // Register on specific instances
87                 : DOMActionInstance.of(actionPath, validNodes.stream()
88                     .map(node -> serializer.toDOMDataTreeIdentifier(DataTreeIdentifier.of(datastore, node)))
89                     .collect(Collectors.toUnmodifiableSet()));
90
91
92         return getDelegate().registerActionImplementation(impl, instance);
93     }
94
95     private static final class Impl implements DOMActionImplementation {
96         private final Class<? extends Action<?, ?, ?>> actionInterface;
97         private final AdapterContext adapterContext;
98         @SuppressWarnings("rawtypes")
99         private final Action implementation;
100         private final NodeIdentifier outputName;
101
102         Impl(final AdapterContext adapterContext, final Absolute actionPath,
103                 final Class<? extends Action<?, ?, ?>> actionInterface, final Action<?, ?, ?> implementation) {
104             this.adapterContext = requireNonNull(adapterContext);
105             outputName = NodeIdentifier.create(
106                 YangConstants.operationOutputQName(actionPath.lastNodeIdentifier().getModule()));
107             this.actionInterface = requireNonNull(actionInterface);
108             this.implementation = requireNonNull(implementation);
109         }
110
111         @Override
112         @SuppressWarnings({ "rawtypes", "unchecked" })
113         public ListenableFuture<? extends DOMActionResult> invokeAction(final Absolute type,
114                 final DOMDataTreeIdentifier path, final ContainerNode input) {
115             final CurrentAdapterSerializer codec = adapterContext.currentSerializer();
116             final InstanceIdentifier<DataObject> instance = codec.fromYangInstanceIdentifier(path.path());
117             if (instance == null) {
118                 // Not representable: return an error
119                 LOG.debug("Path {} is not representable in binding, rejecting invocation", path);
120                 return Futures.immediateFuture(new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
121                     ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, "Supplied path cannot be represented"))));
122             }
123             if (instance.isWildcarded()) {
124                 // A wildcard path: return an error
125                 LOG.debug("Path {} maps to a wildcard {}, rejecting invocation", path, instance);
126                 return Futures.immediateFuture(new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
127                     ErrorType.APPLICATION, ErrorTag.INVALID_VALUE,
128                     "Supplied path does not identify a concrete instance"))));
129             }
130
131             final ListenableFuture<RpcResult<?>> userFuture = implementation.invoke(instance,
132                 codec.fromNormalizedNodeActionInput(actionInterface, input));
133             if (userFuture instanceof BindingOperationFluentFuture) {
134                 // If we are looping back through our future we can skip wrapping. This can happen if application
135                 // forwards invocations between multiple instantiations of the same action.
136                 return (BindingOperationFluentFuture) userFuture;
137             }
138
139             return new BindingOperationFluentFuture(userFuture, actionInterface, outputName, adapterContext);
140         }
141     }
142 }