Bug 5528 - Create stream for DS
[netconf.git] / restconf / sal-rest-connector / 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.netconf.sal.restconf.impl.InstanceIdentifierContext;
15 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
16 import org.opendaylight.restconf.common.references.SchemaContextRef;
17 import org.opendaylight.restconf.handlers.RpcServiceHandler;
18 import org.opendaylight.restconf.handlers.SchemaContextHandler;
19 import org.opendaylight.restconf.restful.services.api.RestconfInvokeOperationsService;
20 import org.opendaylight.restconf.restful.utils.CreateStreamUtil;
21 import org.opendaylight.restconf.restful.utils.RestconfInvokeOperationsUtil;
22 import org.opendaylight.restconf.restful.utils.RestconfStreamsConstants;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26
27 /**
28  * Implementation of {@link RestconfInvokeOperationsService}
29  *
30  */
31 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
32
33     private RpcServiceHandler rpcServiceHandler;
34     private SchemaContextHandler schemaContextHandler;
35
36     @Override
37     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
38         final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
39         final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
40         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
41         final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
42         DOMRpcResult response;
43
44         SchemaContextRef schemaContextRef;
45
46         if (mountPoint == null) {
47             if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
48                 response = CreateStreamUtil.createStream(payload, refSchemaCtx);
49             } else {
50                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
51                         this.rpcServiceHandler);
52             }
53             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
54         } else {
55             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
56             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
57         }
58
59         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
60
61         RpcDefinition resultNodeSchema = null;
62         final NormalizedNode<?, ?> resultData = result.getResult();
63         if ((result != null) && (result.getResult() != null)) {
64             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
65         }
66         return new NormalizedNodeContext(new InstanceIdentifierContext<RpcDefinition>(null, resultNodeSchema,
67                 mountPoint, schemaContextRef.get()), resultData);
68     }
69 }