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