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