Bug 5528 - Connecting RESTful part to restconf + fix bugs
[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 final RpcServiceHandler rpcServiceHandler;
34     private final SchemaContextHandler schemaContextHandler;
35
36     public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
37             final SchemaContextHandler schemaContextHandler) {
38         this.rpcServiceHandler = rpcServiceHandler;
39         this.schemaContextHandler = schemaContextHandler;
40     }
41
42     @Override
43     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
44         final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
45         final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
46         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
47         final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
48         DOMRpcResult response;
49
50         SchemaContextRef schemaContextRef;
51
52         if (mountPoint == null) {
53             if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
54                 response = CreateStreamUtil.createStream(payload, refSchemaCtx);
55             } else {
56                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
57                         this.rpcServiceHandler);
58             }
59             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
60         } else {
61             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
62             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
63         }
64
65         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
66
67         RpcDefinition resultNodeSchema = null;
68         final NormalizedNode<?, ?> resultData = result.getResult();
69         if ((result != null) && (result.getResult() != null)) {
70             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
71         }
72         return new NormalizedNodeContext(new InstanceIdentifierContext<RpcDefinition>(null, resultNodeSchema,
73                 mountPoint, schemaContextRef.get()), resultData);
74     }
75 }