Migrate restconf to MD-SAL APIs
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / 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.nb.rfc8040.rests.services.impl;
9
10 import java.net.URI;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.core.UriInfo;
13 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
14 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
15 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
16 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
20 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
21 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
22 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
23 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
24 import org.opendaylight.restconf.nb.rfc8040.rests.utils.CreateStreamUtil;
25 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfInvokeOperationsUtil;
26 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30
31 /**
32  * Implementation of {@link RestconfInvokeOperationsService}.
33  *
34  */
35 @Path("/")
36 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
37
38     private volatile RpcServiceHandler rpcServiceHandler;
39     private volatile 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 synchronized void updateHandlers(final Object... handlers) {
49         for (final Object object : handlers) {
50             if (object instanceof SchemaContextHandler) {
51                 schemaContextHandler = (SchemaContextHandler) object;
52             } else if (object instanceof RpcServiceHandler) {
53                 rpcServiceHandler = (RpcServiceHandler) object;
54             }
55         }
56     }
57
58     @Override
59     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
60                                            final UriInfo uriInfo) {
61         final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
62         final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
63         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
64         final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
65         DOMRpcResult response;
66
67         SchemaContextRef schemaContextRef;
68
69         if (mountPoint == null) {
70             if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
71                 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCR)) {
72                     response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
73                 } else {
74                     throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
75                             ErrorTag.OPERATION_NOT_SUPPORTED);
76                 }
77             } else {
78                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
79                         this.rpcServiceHandler);
80             }
81             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
82         } else {
83             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
84             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
85         }
86
87         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
88
89         RpcDefinition resultNodeSchema = null;
90         NormalizedNode<?, ?> resultData = null;
91         if (result != null && result.getResult() != null) {
92             resultData = result.getResult();
93             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
94         }
95         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema,
96                 mountPoint, schemaContextRef.get()), resultData);
97     }
98 }