2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.rpc;
19 import static net.javacrumbs.futureconverter.java8guava.FutureConverter.toListenableFuture;
21 import com.google.common.base.Function;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import java.util.concurrent.CompletableFuture;
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
32 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
33 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 public final class HoneycombDOMRpcService implements DOMRpcService {
42 private static final Function<? super Exception, DOMRpcException> ANY_EX_TO_RPC_EXCEPTION_MAPPER =
43 (Function<Exception, DOMRpcException>) e -> (e instanceof DOMRpcException)
45 : new RpcException("RPC failed", e);
47 private final BindingNormalizedNodeSerializer serializer;
48 private final RpcRegistry rpcRegistry;
50 public HoneycombDOMRpcService(@Nonnull final BindingNormalizedNodeSerializer serializer,
51 @Nonnull final RpcRegistry rpcRegistry) {
52 this.serializer = serializer;
53 this.rpcRegistry = rpcRegistry;
58 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath schemaPath,
59 @Nullable final NormalizedNode<?, ?> normalizedNode) {
60 DataObject input = null;
61 if (normalizedNode != null) {
62 // RPC input is optional
63 final SchemaPath nodePatch = schemaPath.createChild(normalizedNode.getNodeType());
64 input = serializer.fromNormalizedNodeRpcData(nodePatch, (ContainerNode) normalizedNode);
66 final CompletableFuture<DataObject> result = rpcRegistry.invoke(schemaPath, input).toCompletableFuture();
67 final ListenableFuture<DOMRpcResult> output = getDOMRpcResult(toListenableFuture(result));
68 return Futures.makeChecked(output, ANY_EX_TO_RPC_EXCEPTION_MAPPER);
71 private ListenableFuture<DOMRpcResult> getDOMRpcResult(final ListenableFuture<DataObject> invoke) {
72 return Futures.transform(
74 (Function<DataObject, DOMRpcResult>) output -> {
75 final ContainerNode outputNode = serializer.toNormalizedNodeRpcData(output);
76 return new DefaultDOMRpcResult(outputNode);
82 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T t) {
83 return new ListenerRegistration<T>() {
90 public T getInstance() {