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