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 java.util.Optional;
13 import javax.ws.rs.Path;
14 import javax.ws.rs.WebApplicationException;
15 import javax.ws.rs.core.Response;
16 import javax.ws.rs.core.UriInfo;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.mdsal.dom.api.DOMRpcService;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
22 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
25 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
27 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
28 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfInvokeOperationsUtil;
29 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.XMLNamespace;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 * Implementation of {@link RestconfInvokeOperationsService}.
42 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
43 private static final XMLNamespace SAL_REMOTE_NAMESPACE =
44 XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote");
46 private final DOMRpcService rpcService;
47 private final SchemaContextHandler schemaContextHandler;
49 public RestconfInvokeOperationsServiceImpl(final DOMRpcService rpcService,
50 final SchemaContextHandler schemaContextHandler) {
51 this.rpcService = requireNonNull(rpcService);
52 this.schemaContextHandler = requireNonNull(schemaContextHandler);
56 public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
57 final UriInfo uriInfo) {
58 final EffectiveModelContext refSchemaCtx = this.schemaContextHandler.get();
59 final QName schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
60 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
61 final XMLNamespace namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
63 final DOMRpcResult response;
64 final EffectiveModelContext schemaContextRef;
65 if (mountPoint == null) {
66 if (SAL_REMOTE_NAMESPACE.equals(namespace)) {
67 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCRIPTION)) {
68 response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
70 throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
71 ErrorTag.OPERATION_NOT_SUPPORTED);
74 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath, this.rpcService);
76 schemaContextRef = this.schemaContextHandler.get();
78 response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
79 schemaContextRef = modelContext(mountPoint);
82 final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
84 RpcDefinition resultNodeSchema = null;
85 NormalizedNode resultData = null;
86 if (result != null && result.getResult() != null) {
87 resultData = result.getResult();
88 resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
91 if (resultData != null && ((ContainerNode) resultData).isEmpty()) {
92 throw new WebApplicationException(Response.Status.NO_CONTENT);
94 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint,
95 schemaContextRef), resultData);
99 private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
100 return mountPoint.getService(DOMSchemaService.class)
101 .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))