Split Restconf implementations (draft02 and RFC) - move restconf
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / restconf / restful / services / impl / RestconfInvokeOperationsServiceImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.restful.services.impl;
9
10 import java.net.URI;
11 import javax.ws.rs.core.UriInfo;
12 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
13 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
14 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
15 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
19 import org.opendaylight.restconf.common.references.SchemaContextRef;
20 import org.opendaylight.restconf.handlers.RpcServiceHandler;
21 import org.opendaylight.restconf.handlers.SchemaContextHandler;
22 import org.opendaylight.restconf.restful.services.api.RestconfInvokeOperationsService;
23 import org.opendaylight.restconf.restful.utils.CreateStreamUtil;
24 import org.opendaylight.restconf.restful.utils.RestconfInvokeOperationsUtil;
25 import org.opendaylight.restconf.restful.utils.RestconfStreamsConstants;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29
30 /**
31  * Implementation of {@link RestconfInvokeOperationsService}.
32  *
33  */
34 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
35
36     private final RpcServiceHandler rpcServiceHandler;
37     private final SchemaContextHandler schemaContextHandler;
38
39     public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
40             final SchemaContextHandler schemaContextHandler) {
41         this.rpcServiceHandler = rpcServiceHandler;
42         this.schemaContextHandler = schemaContextHandler;
43     }
44
45     @Override
46     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
47                                            final UriInfo uriInfo) {
48         final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
49         final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
50         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
51         final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
52         DOMRpcResult response;
53
54         SchemaContextRef schemaContextRef;
55
56         if (mountPoint == null) {
57             if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
58                 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCR)) {
59                     response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
60                 } else {
61                     throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
62                             ErrorTag.OPERATION_NOT_SUPPORTED);
63                 }
64             } else {
65                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
66                         this.rpcServiceHandler);
67             }
68             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
69         } else {
70             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
71             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
72         }
73
74         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
75
76         RpcDefinition resultNodeSchema = null;
77         final NormalizedNode<?, ?> resultData = result.getResult();
78         if ((result != null) && (result.getResult() != null)) {
79             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
80         }
81         return new NormalizedNodeContext(new InstanceIdentifierContext<RpcDefinition>(null, resultNodeSchema,
82                 mountPoint, schemaContextRef.get()), resultData);
83     }
84 }