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;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.WebApplicationException;
13 import javax.ws.rs.core.Response;
14 import javax.ws.rs.core.UriInfo;
15 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
16 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
17 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
18 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
21 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
23 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
24 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
25 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
26 import org.opendaylight.restconf.nb.rfc8040.rests.utils.CreateStreamUtil;
27 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfInvokeOperationsUtil;
28 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 * Implementation of {@link RestconfInvokeOperationsService}.
40 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
42 private volatile RpcServiceHandler rpcServiceHandler;
43 private volatile SchemaContextHandler schemaContextHandler;
45 public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
46 final SchemaContextHandler schemaContextHandler) {
47 this.rpcServiceHandler = rpcServiceHandler;
48 this.schemaContextHandler = schemaContextHandler;
52 public synchronized void updateHandlers(final Object... handlers) {
53 for (final Object object : handlers) {
54 if (object instanceof SchemaContextHandler) {
55 schemaContextHandler = (SchemaContextHandler) object;
56 } else if (object instanceof RpcServiceHandler) {
57 rpcServiceHandler = (RpcServiceHandler) object;
63 public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
64 final UriInfo uriInfo) {
65 final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
66 final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
67 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
68 final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
70 final DOMRpcResult response;
71 final EffectiveModelContext schemaContextRef;
72 if (mountPoint == null) {
73 if (namespace.equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE.getNamespace())) {
74 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCRIPTION)) {
75 response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
77 throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
78 ErrorTag.OPERATION_NOT_SUPPORTED);
81 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
82 this.rpcServiceHandler);
84 schemaContextRef = this.schemaContextHandler.get();
86 response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
87 schemaContextRef = mountPoint.getEffectiveModelContext();
90 final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
92 RpcDefinition resultNodeSchema = null;
93 NormalizedNode<?, ?> resultData = null;
94 if (result != null && result.getResult() != null) {
95 resultData = result.getResult();
96 resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
99 if (resultData != null && ((ContainerNode) resultData).getValue().isEmpty()) {
100 throw new WebApplicationException(Response.Status.NO_CONTENT);
102 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint,
103 schemaContextRef), resultData);