Do not report failure on empty action output
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfInvokeOperationsServiceImpl.java
index deb3fd160fb7c421ecc95b91d92d50d50e2afa99..cb6283be6f1ffeeb70cee83d04c525a25de77fb3 100644 (file)
@@ -7,26 +7,32 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
-import java.net.URI;
+import static java.util.Objects.requireNonNull;
+
+import java.util.Optional;
 import javax.ws.rs.Path;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
-import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
-import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService;
-import org.opendaylight.restconf.nb.rfc8040.rests.utils.CreateStreamUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfInvokeOperationsUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
+import org.opendaylight.yangtools.yang.common.ErrorType;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
  * Implementation of {@link RestconfInvokeOperationsService}.
@@ -34,65 +40,65 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
  */
 @Path("/")
 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
+    private static final XMLNamespace SAL_REMOTE_NAMESPACE =
+        XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote");
 
-    private volatile RpcServiceHandler rpcServiceHandler;
-    private volatile SchemaContextHandler schemaContextHandler;
+    private final DOMRpcService rpcService;
+    private final SchemaContextHandler schemaContextHandler;
 
-    public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
+    public RestconfInvokeOperationsServiceImpl(final DOMRpcService rpcService,
             final SchemaContextHandler schemaContextHandler) {
-        this.rpcServiceHandler = rpcServiceHandler;
-        this.schemaContextHandler = schemaContextHandler;
-    }
-
-    @Override
-    public synchronized void updateHandlers(final Object... handlers) {
-        for (final Object object : handlers) {
-            if (object instanceof SchemaContextHandler) {
-                schemaContextHandler = (SchemaContextHandler) object;
-            } else if (object instanceof RpcServiceHandler) {
-                rpcServiceHandler = (RpcServiceHandler) object;
-            }
-        }
+        this.rpcService = requireNonNull(rpcService);
+        this.schemaContextHandler = requireNonNull(schemaContextHandler);
     }
 
     @Override
     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
-                                           final UriInfo uriInfo) {
-        final SchemaContextRef refSchemaCtx = new SchemaContextRef(this.schemaContextHandler.get());
-        final SchemaPath schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getPath();
+            final UriInfo uriInfo) {
+        final EffectiveModelContext refSchemaCtx = this.schemaContextHandler.get();
+        final QName schemaPath = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
-        final URI namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
-        DOMRpcResult response;
-
-        SchemaContextRef schemaContextRef;
+        final XMLNamespace namespace = payload.getInstanceIdentifierContext().getSchemaNode().getQName().getNamespace();
 
+        final DOMRpcResult response;
+        final EffectiveModelContext schemaContextRef;
         if (mountPoint == null) {
-            if (namespace.toString().equals(RestconfStreamsConstants.SAL_REMOTE_NAMESPACE)) {
-                if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCR)) {
+            if (SAL_REMOTE_NAMESPACE.equals(namespace)) {
+                if (identifier.contains(RestconfStreamsConstants.CREATE_DATA_SUBSCRIPTION)) {
                     response = CreateStreamUtil.createDataChangeNotifiStream(payload, refSchemaCtx);
                 } else {
                     throw new RestconfDocumentedException("Not supported operation", ErrorType.RPC,
                             ErrorTag.OPERATION_NOT_SUPPORTED);
                 }
             } else {
-                response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath,
-                        this.rpcServiceHandler);
+                response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath, this.rpcService);
             }
-            schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
+            schemaContextRef = this.schemaContextHandler.get();
         } else {
-            response = RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(mountPoint, payload.getData(), schemaPath);
-            schemaContextRef = new SchemaContextRef(mountPoint.getSchemaContext());
+            response = RestconfInvokeOperationsUtil.invokeRpc(payload.getData(), schemaPath, mountPoint);
+            schemaContextRef = modelContext(mountPoint);
         }
 
         final DOMRpcResult result = RestconfInvokeOperationsUtil.checkResponse(response);
 
         RpcDefinition resultNodeSchema = null;
-        NormalizedNode<?, ?> resultData = null;
+        NormalizedNode resultData = null;
         if (result != null && result.getResult() != null) {
             resultData = result.getResult();
             resultNodeSchema = (RpcDefinition) payload.getInstanceIdentifierContext().getSchemaNode();
         }
-        return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema,
-                mountPoint, schemaContextRef.get()), resultData);
+
+        if (resultData != null && ((ContainerNode) resultData).isEmpty()) {
+            throw new WebApplicationException(Response.Status.NO_CONTENT);
+        } else {
+            return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, resultNodeSchema, mountPoint,
+                    schemaContextRef), resultData);
+        }
+    }
+
+    private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
+        return mountPoint.getService(DOMSchemaService.class)
+            .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
+            .orElse(null);
     }
 }