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