Tighten AbstractDOMRpcImplementationAdapter.serialize()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractDOMRpcImplementationAdapter.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, 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 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13 import static org.opendaylight.mdsal.binding.dom.adapter.StaticConfiguration.ENABLE_CODEC_SHORTCUT;
14
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
18 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.yangtools.yang.binding.RpcInput;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26
27 abstract sealed class AbstractDOMRpcImplementationAdapter implements DOMRpcImplementation
28         permits BindingDOMRpcImplementationAdapter, LegacyDOMRpcImplementationAdapter {
29     private final AdapterContext adapterContext;
30     private final @NonNull QName inputName;
31
32     AbstractDOMRpcImplementationAdapter(final AdapterContext adapterContext, @NonNull QName inputName) {
33         this.adapterContext = requireNonNull(adapterContext);
34         this.inputName = requireNonNull(inputName);
35     }
36
37     @Override
38     public final long invocationCost() {
39         // Default implementations are 0, we need to perform some translation, hence we have a slightly higher cost
40         return 1;
41     }
42
43     @Override
44     public final ListenableFuture<DOMRpcResult> invokeRpc(final DOMRpcIdentifier rpc, final ContainerNode input) {
45         final var serializer = adapterContext.currentSerializer();
46         return LazyDOMRpcResultFuture.create(serializer, invokeRpc(serializer, rpc, input));
47     }
48
49     abstract @NonNull ListenableFuture<RpcResult<?>> invokeRpc(@NonNull CurrentAdapterSerializer serializer,
50         @NonNull DOMRpcIdentifier rpc, @NonNull ContainerNode input);
51
52     final @NonNull RpcInput deserialize(final @NonNull CurrentAdapterSerializer serializer,
53             final @NonNull QName rpcName, final @NonNull ContainerNode input) {
54         if (ENABLE_CODEC_SHORTCUT && input instanceof BindingLazyContainerNode<?> lazy) {
55             return (RpcInput) lazy.getDataObject();
56         }
57
58         // TODO: this is a bit inefficient: typically we get the same CurrentAdapterSerializer and the path is also
59         //       constant, hence we should be able to cache this lookup and just have the appropriate
60         //       BindingDataObjectCodecTreeNode and reuse it directly
61         checkArgument(inputName.equals(input.getIdentifier().getNodeType()),
62             "Unexpected RPC %s input %s", rpcName, input);
63         // TODO: this is a bit inefficient: typically we get the same CurrentAdapterSerializer and the path is also
64         //       constant, hence we should be able to cache this lookup and just have the appropriate
65         //       BindingDataObjectCodecTreeNode and reuse it directly
66         // FIXME: should be a guaranteed return, as innput is @NonNull
67         return verifyNotNull((RpcInput) serializer.fromNormalizedNodeRpcData(Absolute.of(rpcName, inputName), input));
68     }
69 }