Bug 7686 - Make notifications defined by yangs automatic loaded
[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.netconf.sal.restconf.impl.RestconfDocumentedException;
17 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
18 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
19 import org.opendaylight.restconf.common.references.SchemaContextRef;
20 import org.opendaylight.restconf.handlers.RpcServiceHandler;
21 import org.opendaylight.restconf.handlers.SchemaContextHandler;
22 import org.opendaylight.restconf.restful.services.api.RestconfInvokeOperationsService;
23 import org.opendaylight.restconf.restful.utils.CreateStreamUtil;
24 import org.opendaylight.restconf.restful.utils.RestconfInvokeOperationsUtil;
25 import org.opendaylight.restconf.restful.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 final RpcServiceHandler rpcServiceHandler;
37     private final 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 NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
47         final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
48         final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
49         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
50         final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
51         DOMRpcResult response;
52
53         SchemaContextRef schemaContextRef;
54
55         if (mountPoint == null) {
56             if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
57                 if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCR)) {
58                     response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
59                 } else {
60                     throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
61                             ErrorTag.OPERATION_NOT_SUPPORTED);
62                 }
63             } else {
64                 response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
65                         this.rpcServiceHandler);
66             }
67             schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
68         } else {
69             response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
70             schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
71         }
72
73         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
74
75         RpcDefinition resultNodeSchema = null;
76         final NormalizedNode<?, ?> resultData = result.getResult();
77         if ((result != null) && (result.getResult() != null)) {
78             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
79         }
80         return new NormalizedNodeContext(new InstanceIdentifierContext<RpcDefinition>(null, resultNodeSchema,
81                 mountPoint, schemaContextRef.get()), resultData);
82     }
83 }