Refactoring of web-sockets in RESTCONF RFC-8040
[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.references.SchemaContextRef;
25 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
26 import org.opendaylight.restconf.nb.rfc8040.rests.utils.CreateStreamUtil;
27 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfInvokeOperationsUtil;
28 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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 SchemaContextRef refSchemaCtx = new SchemaContextRef(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         DOMRpcResult response;
69
70         SchemaContextRef schemaContextRef;
71
72         if (mountPoint == null) {
73             if (namespace.equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE.getNamespace())) {
74                 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCRIPTION)) {
75                     response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
76                 } else {
77                     throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
78                             ErrorTag.OPERATION_NOT_SUPPORTED);
79                 }
80             } else {
81                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
82                         this.rpcServiceHandler);
83             }
84             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
85         } else {
86             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
87             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
88         }
89
90         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
91
92         RpcDefinition resultNodeSchema = null;
93         NormalizedNode<?, ?> resultData = null;
94         if (result != null && result.getResult() != null) {
95             resultData = result.getResult();
96             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
97         }
98
99         if (resultData != null && ((ContainerNode) resultData).getValue().isEmpty()) {
100             throw new WebApplicationException(Response.Status.NO_CONTENT);
101         } else {
102             return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema,
103                     mountPoint, schemaContextRef.get()), resultData);
104         }
105     }
106 }