+ private Object callObjectMethod(final Object self, final Method method, final Object[] args) {
+ switch (method.getName()) {
+ case "toString":
+ return type.getName() + "$Adapter{delegate=" + delegate.toString() + "}";
+ case "hashCode":
+ return System.identityHashCode(self);
+ case "equals":
+ return self == args[0];
+ default:
+ return null;
+ }
+ }
+
+ private static ListenableFuture<RpcResult<?>> transformFuture(final SchemaPath rpc,
+ final ListenableFuture<DOMRpcResult> domFuture, final BindingNormalizedNodeSerializer codec) {
+ return Futures.transform(domFuture, input -> {
+ final NormalizedNode<?, ?> domData = input.getResult();
+ final DataObject bindingResult;
+ if (domData != null) {
+ final SchemaPath rpcOutput = rpc.createChild(QName.create(rpc.getLastComponent(), "output"));
+ bindingResult = codec.fromNormalizedNodeRpcData(rpcOutput, (ContainerNode) domData);
+ } else {
+ bindingResult = null;
+ }
+
+ // DOMRpcResult does not have a notion of success, hence we have to reverse-engineer it by looking
+ // at reported errors and checking whether they are just warnings.
+ final Collection<? extends RpcError> errors = input.getErrors();
+ return RpcResult.class.cast(RpcResultBuilder.status(errors.stream()
+ .noneMatch(error -> error.getSeverity() == ErrorSeverity.ERROR))
+ .withResult(bindingResult).withRpcErrors(errors).build());
+ }, MoreExecutors.directExecutor());
+ }
+
+ private abstract class RpcInvocationStrategy {
+
+ private final SchemaPath rpcName;
+
+ protected RpcInvocationStrategy(final SchemaPath path) {
+ rpcName = path;
+ }
+
+ final ListenableFuture<RpcResult<?>> invoke(final DataObject input) {
+ return invoke0(rpcName, serialize(input));
+ }
+
+ abstract NormalizedNode<?, ?> serialize(DataObject input);
+
+ final ListenableFuture<RpcResult<?>> invokeEmpty() {
+ return invoke0(rpcName, null);
+ }
+
+ final SchemaPath getRpcName() {
+ return rpcName;
+ }
+ }
+
+ private final class NonRoutedStrategy extends RpcInvocationStrategy {
+
+ protected NonRoutedStrategy(final SchemaPath path) {
+ super(path);
+ }
+
+ @Override
+ NormalizedNode<?, ?> serialize(final DataObject input) {
+ return LazySerializedContainerNode.create(getRpcName(), input, codec.getCodecRegistry());
+ }
+ }
+
+ private final class RoutedStrategy extends RpcInvocationStrategy {
+
+ private final ContextReferenceExtractor refExtractor;
+ private final NodeIdentifier contextName;
+
+ protected RoutedStrategy(final SchemaPath path, final Method rpcMethod, final QName leafName) {
+ super(path);
+ final Class<? extends DataContainer> inputType = BindingReflections.resolveRpcInputClass(rpcMethod).get();
+ refExtractor = ContextReferenceExtractor.from(inputType);
+ this.contextName = new NodeIdentifier(leafName);
+ }
+
+ @Override
+ NormalizedNode<?, ?> serialize(final DataObject input) {
+ final InstanceIdentifier<?> bindingII = refExtractor.extract(input);
+ if (bindingII != null) {
+ final YangInstanceIdentifier yangII = codec.toYangInstanceIdentifierCached(bindingII);
+ final LeafNode<?> contextRef = ImmutableNodes.leafNode(contextName, yangII);
+ return LazySerializedContainerNode.withContextRef(getRpcName(), input, contextRef,
+ codec.getCodecRegistry());
+ }
+ return LazySerializedContainerNode.create(getRpcName(), input, codec.getCodecRegistry());