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.rests.services.api.RestconfInvokeOperationsService;
25 import org.opendaylight.restconf.nb.rfc8040.rests.utils.CreateStreamUtil;
26 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfInvokeOperationsUtil;
27 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 * Implementation of {@link RestconfInvokeOperationsService}.
39 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
41 private volatile RpcServiceHandler rpcServiceHandler;
42 private volatile SchemaContextHandler schemaContextHandler;
44 public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
45 final SchemaContextHandler schemaContextHandler) {
46 this.rpcServiceHandler = rpcServiceHandler;
47 this.schemaContextHandler = schemaContextHandler;
51 public synchronized void updateHandlers(final Object... handlers) {
52 for (final Object object : handlers) {
53 if (object instanceof SchemaContextHandler) {
54 schemaContextHandler = (SchemaContextHandler) object;
55 } else if (object instanceof RpcServiceHandler) {
56 rpcServiceHandler = (RpcServiceHandler) object;
62 public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
63 final UriInfo uriInfo) {
64 final EffectiveModelContext refSchemaCtx = this.schemaContextHandler.get();
65 final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
66 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
67 final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
69 final DOMRpcResult response;
70 final EffectiveModelContext schemaContextRef;
71 if (mountPoint == null) {
72 if (namespace.equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE.getNamespace())) {
73 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCRIPTION)) {
74 response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
76 throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
77 ErrorTag.OPERATION_NOT_SUPPORTED);
80 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
81 this.rpcServiceHandler);
83 schemaContextRef = this.schemaContextHandler.get();
85 response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
86 schemaContextRef = mountPoint.getEffectiveModelContext();
89 final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
91 RpcDefinition resultNodeSchema = null;
92 NormalizedNode<?, ?> resultData = null;
93 if (result != null && result.getResult() != null) {
94 resultData = result.getResult();
95 resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
98 if (resultData != null && ((ContainerNode) resultData).getValue().isEmpty()) {
99 throw new WebApplicationException(Response.Status.NO_CONTENT);
101 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint,
102 schemaContextRef), resultData);