2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Throwables;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.WebApplicationException;
22 import javax.ws.rs.container.AsyncResponse;
23 import javax.ws.rs.core.Response.Status;
24 import javax.ws.rs.core.UriInfo;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
27 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
28 import org.opendaylight.mdsal.dom.api.DOMRpcException;
29 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
32 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
33 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
34 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
35 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
36 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.SubscribeDeviceNotificationInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateDataChangeEventSubscriptionInput;
39 import org.opendaylight.yangtools.yang.common.ErrorTag;
40 import org.opendaylight.yangtools.yang.common.ErrorType;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.QNameModule;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.opendaylight.yangtools.yang.common.YangConstants;
45 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
48 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * Implementation of {@link RestconfInvokeOperationsService}.
58 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
59 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsServiceImpl.class);
61 // FIXME: at some point we do not want to have this here, as this is only used for dispatch
62 private static final QNameModule SAL_REMOTE_NAMESPACE = CreateDataChangeEventSubscriptionInput.QNAME.getModule();
63 private static final QNameModule DEVICE_NOTIFICATION_NAMESPACE = SubscribeDeviceNotificationInput.QNAME.getModule();
65 private final DOMRpcService rpcService;
66 private final DOMMountPointService mountPointService;
67 private final SubscribeToStreamUtil streamUtils;
69 public RestconfInvokeOperationsServiceImpl(final DOMRpcService rpcService,
70 final DOMMountPointService mountPointService, final Configuration configuration) {
71 this.rpcService = requireNonNull(rpcService);
72 this.mountPointService = requireNonNull(mountPointService);
73 streamUtils = configuration.isUseSSE() ? SubscribeToStreamUtil.serverSentEvents()
74 : SubscribeToStreamUtil.webSockets();
78 public void invokeRpc(final String identifier, final NormalizedNodePayload payload, final UriInfo uriInfo,
79 final AsyncResponse ar) {
80 final InstanceIdentifierContext context = payload.getInstanceIdentifierContext();
81 final EffectiveModelContext schemaContext = context.getSchemaContext();
82 final DOMMountPoint mountPoint = context.getMountPoint();
83 final SchemaNode schema = context.getSchemaNode();
84 final QName rpcName = schema.getQName();
86 final ListenableFuture<? extends DOMRpcResult> future;
87 if (mountPoint == null) {
88 // FIXME: this really should be a normal RPC invocation service which has its own interface with JAX-RS,
89 // except ... we check 'identifier' for .contains() instead of exact RPC name!
90 if (SAL_REMOTE_NAMESPACE.equals(rpcName.getModule())) {
91 if (identifier.contains("create-data-change-event-subscription")) {
92 future = Futures.immediateFuture(
93 CreateStreamUtil.createDataChangeNotifiStream(payload, schemaContext));
95 future = Futures.immediateFailedFuture(new RestconfDocumentedException("Unsupported operation",
96 ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED));
98 } else if (DEVICE_NOTIFICATION_NAMESPACE.equals(rpcName.getModule())) {
99 // FIXME: this should be a match on RPC QName
100 final String baseUrl = streamUtils.prepareUriByStreamName(uriInfo, "").toString();
101 future = Futures.immediateFuture(
102 CreateStreamUtil.createDeviceNotificationListener(baseUrl, payload, schemaContext, streamUtils,
105 future = invokeRpc((ContainerNode)payload.getData(), rpcName, rpcService);
108 future = invokeRpc(payload.getData(), rpcName, mountPoint);
111 Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {
113 public void onSuccess(final DOMRpcResult response) {
114 final var errors = response.getErrors();
115 if (!errors.isEmpty()) {
116 LOG.debug("RpcError message {}", response.getErrors());
117 ar.resume(new RestconfDocumentedException("RPCerror message ", null, response.getErrors()));
121 final NormalizedNode resultData = response.getResult();
122 if (resultData == null || ((ContainerNode) resultData).isEmpty()) {
123 ar.resume(new WebApplicationException(Status.NO_CONTENT));
125 ar.resume(NormalizedNodePayload.of(context, resultData));
130 public void onFailure(final Throwable failure) {
133 }, MoreExecutors.directExecutor());
137 * Invoking rpc via mount point.
139 * @param mountPoint mount point
140 * @param data input data
141 * @param rpc RPC type
142 * @return {@link DOMRpcResult}
145 static ListenableFuture<? extends DOMRpcResult> invokeRpc(final NormalizedNode data, final QName rpc,
146 final DOMMountPoint mountPoint) {
147 return invokeRpc((ContainerNode) data, rpc, mountPoint.getService(DOMRpcService.class).orElseThrow(() -> {
148 final String errmsg = "RPC service is missing.";
150 return new RestconfDocumentedException(errmsg);
157 * @param data input data
158 * @param rpc RPC type
159 * @param rpcService rpc service to invoke rpc
160 * @return {@link DOMRpcResult}
163 static ListenableFuture<? extends DOMRpcResult> invokeRpc(final ContainerNode data, final QName rpc,
164 final DOMRpcService rpcService) {
165 return Futures.catching(rpcService.invokeRpc(rpc, nonnullInput(rpc, data)),
166 DOMRpcException.class,
167 cause -> new DefaultDOMRpcResult(List.of(RpcResultBuilder.newError(ErrorType.RPC, ErrorTag.OPERATION_FAILED,
168 cause.getMessage()))),
169 MoreExecutors.directExecutor());
172 private static @NonNull ContainerNode nonnullInput(final QName type, final ContainerNode input) {
173 return input != null ? input
174 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getModule()));
178 static <T> T checkedGet(final ListenableFuture<T> future) {
181 } catch (InterruptedException e) {
182 throw new RestconfDocumentedException("Interrupted while waiting for result of invocation", e);
183 } catch (ExecutionException e) {
184 Throwables.throwIfInstanceOf(e.getCause(), RestconfDocumentedException.class);
185 throw new RestconfDocumentedException("Invocation failed", e);