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.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 SchemaContextRef refSchemaCtx = new SchemaContextRef(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();
68 DOMRpcResult response;
70 SchemaContextRef schemaContextRef;
72 if (mountPoint == null) {
73 if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
74 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCR)) {
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 = new SchemaContextRef(this.schemaContextHandler.get());
86 response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
87 schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
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,
103 mountPoint, schemaContextRef.get()), resultData);