Split Restconf implementations (draft02 and RFC) - move
[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  * @deprecated move to splitted module restconf-nb-rfc8040
34  */
35 @Deprecated
36 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
37
38     private final RpcServiceHandler rpcServiceHandler;
39     private final SchemaContextHandler schemaContextHandler;
40
41     public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
42             final SchemaContextHandler schemaContextHandler) {
43         this.rpcServiceHandler = rpcServiceHandler;
44         this.schemaContextHandler = schemaContextHandler;
45     }
46
47     @Override
48     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
49                                            final UriInfo uriInfo) {
50         final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
51         final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
52         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
53         final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
54         DOMRpcResult response;
55
56         SchemaContextRef schemaContextRef;
57
58         if (mountPoint == null) {
59             if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
60                 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCR)) {
61                     response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
62                 } else {
63                     throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
64                             ErrorTag.OPERATION_NOT_SUPPORTED);
65                 }
66             } else {
67                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
68                         this.rpcServiceHandler);
69             }
70             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
71         } else {
72             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
73             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
74         }
75
76         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
77
78         RpcDefinition resultNodeSchema = null;
79         final NormalizedNode<?, ?> resultData = result.getResult();
80         if ((result != null) && (result.getResult() != null)) {
81             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
82         }
83         return new NormalizedNodeContext(new InstanceIdentifierContext<RpcDefinition>(null, resultNodeSchema,
84                 mountPoint, schemaContextRef.get()), resultData);
85     }
86 }