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.collect.ImmutableList;
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.Optional;
19 import java.util.concurrent.CancellationException;
20 import java.util.concurrent.ExecutionException;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.WebApplicationException;
23 import javax.ws.rs.core.Response;
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.DOMRpcException;
28 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
32 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
33 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
34 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
36 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
37 import org.opendaylight.yangtools.yang.common.ErrorTag;
38 import org.opendaylight.yangtools.yang.common.ErrorType;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.opendaylight.yangtools.yang.common.XMLNamespace;
43 import org.opendaylight.yangtools.yang.common.YangConstants;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
47 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
48 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * Implementation of {@link RestconfInvokeOperationsService}.
57 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
58 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsServiceImpl.class);
60 // FIXME: at some point we do not want to have this here
61 private static final XMLNamespace SAL_REMOTE_NAMESPACE =
62 XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote");
64 private final DOMRpcService rpcService;
65 private final SchemaContextHandler schemaContextHandler;
67 public RestconfInvokeOperationsServiceImpl(final DOMRpcService rpcService,
68 final SchemaContextHandler schemaContextHandler) {
69 this.rpcService = requireNonNull(rpcService);
70 this.schemaContextHandler = requireNonNull(schemaContextHandler);
74 public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
75 final UriInfo uriInfo) {
76 final QName schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
77 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
78 final XMLNamespace namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
80 final DOMRpcResult response;
81 final EffectiveModelContext schemaContextRef;
82 if (mountPoint == null) {
83 schemaContextRef = schemaContextHandler.get();
84 // FIXME: this really should be a normal RPC invocation service which has its own interface with JAX-RS
85 if (SAL_REMOTE_NAMESPACE.equals(namespace)) {
86 if (identifier.contains("create-data-change-event-subscription")) {
87 response = CreateStreamUtil.createDataChangeNotifiStream(payload, schemaContextRef);
89 throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
90 ErrorTag.OPERATION_NOT_SUPPORTED);
93 response = invokeRpc(payload.getData(), schemaPath, rpcService);
96 response = invokeRpc(payload.getData(), schemaPath, mountPoint);
97 schemaContextRef = modelContext(mountPoint);
100 final DOMRpcResult result = checkResponse(response);
102 RpcDefinition resultNodeSchema = null;
103 NormalizedNode resultData = null;
104 if (result != null && result.getResult() != null) {
105 resultData = result.getResult();
106 resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
109 if (resultData != null && ((ContainerNode) resultData).isEmpty()) {
110 throw new WebApplicationException(Response.Status.NO_CONTENT);
112 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint,
113 schemaContextRef), resultData);
118 * Invoking rpc via mount point.
120 * @param mountPoint mount point
121 * @param data input data
122 * @param rpc RPC type
123 * @return {@link DOMRpcResult}
125 // FIXME: NETCONF-718: we should be returning a future here
127 static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMMountPoint mountPoint) {
128 return invokeRpc(data, rpc, mountPoint.getService(DOMRpcService.class).orElseThrow(() -> {
129 final String errmsg = "RPC service is missing.";
131 return new RestconfDocumentedException(errmsg);
138 * @param data input data
139 * @param rpc RPC type
140 * @param rpcService rpc service to invoke rpc
141 * @return {@link DOMRpcResult}
143 // FIXME: NETCONF-718: we should be returning a future here
145 static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMRpcService rpcService) {
146 return checkedGet(Futures.catching(
147 rpcService.invokeRpc(rpc, nonnullInput(rpc, data)), DOMRpcException.class,
148 cause -> new DefaultDOMRpcResult(ImmutableList.of(RpcResultBuilder.newError(
149 RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))),
150 MoreExecutors.directExecutor()));
153 private static @NonNull NormalizedNode nonnullInput(final QName type, final NormalizedNode input) {
154 return input != null ? input
155 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getModule()));
159 * Check the validity of the result.
161 * @param response response of rpc
162 * @return {@link DOMRpcResult} result
165 static DOMRpcResult checkResponse(final DOMRpcResult response) {
166 if (response == null) {
170 if (response.getErrors().isEmpty()) {
173 LOG.debug("RpcError message {}", response.getErrors());
174 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
175 } catch (final CancellationException e) {
176 final String errMsg = "The operation was cancelled while executing.";
177 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
178 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
183 static <T> T checkedGet(final ListenableFuture<T> future) {
186 } catch (InterruptedException e) {
187 throw new RestconfDocumentedException("Interrupted while waiting for result of invocation", e);
188 } catch (ExecutionException e) {
189 Throwables.throwIfInstanceOf(e.getCause(), RestconfDocumentedException.class);
190 throw new RestconfDocumentedException("Invocation failed", e);
194 private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
195 return mountPoint.getService(DOMSchemaService.class)
196 .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))