Do not access model context multiple times
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataServiceImpl.java
index d557ca7b7a98b17e1f9f1dfa85b46bb74cf913cc..8257b6394e0cc986552805cadb9b8211ff8e2582 100644 (file)
@@ -16,7 +16,6 @@ import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsCo
 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH_PART;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.net.URI;
@@ -43,7 +42,6 @@ import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteOperations;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
@@ -75,7 +73,6 @@ import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -222,16 +219,12 @@ public class RestconfDataServiceImpl implements RestconfDataService {
 
         final InstanceIdentifierContext iid = payload.getInstanceIdentifierContext();
 
-        validInputData(iid.getSchemaNode(), payload);
+        validInputData(iid.getSchemaNode() != null, payload);
         validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
         validateListKeysEqualityInPayloadAndUri(payload);
 
-        final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
-        final EffectiveModelContext ref = mountPoint == null
-                ? schemaContextHandler.get() : modelContext(mountPoint);
-
-        final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
-        return PutDataTransactionUtil.putData(payload, ref, strategy, params);
+        final RestconfStrategy strategy = getRestconfStrategy(iid.getMountPoint());
+        return PutDataTransactionUtil.putData(payload, iid.getSchemaContext(), strategy, params);
     }
 
     @Override
@@ -242,14 +235,14 @@ public class RestconfDataServiceImpl implements RestconfDataService {
     @Override
     public Response postData(final NormalizedNodePayload payload, final UriInfo uriInfo) {
         requireNonNull(payload);
-        if (payload.getInstanceIdentifierContext().getSchemaNode() instanceof ActionDefinition) {
+        final InstanceIdentifierContext iid = payload.getInstanceIdentifierContext();
+        if (iid.getSchemaNode() instanceof ActionDefinition) {
             return invokeAction(payload);
         }
 
         final WriteDataParams params = QueryParams.newWriteDataParams(uriInfo);
-        final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
-        final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
-        return PostDataTransactionUtil.postData(uriInfo, payload, strategy, getSchemaContext(mountPoint), params);
+        final RestconfStrategy strategy = getRestconfStrategy(iid.getMountPoint());
+        return PostDataTransactionUtil.postData(uriInfo, payload, strategy, iid.getSchemaContext(), params);
     }
 
     @Override
@@ -269,11 +262,11 @@ public class RestconfDataServiceImpl implements RestconfDataService {
 
     @Override
     public PatchStatusContext patchData(final PatchContext context, final UriInfo uriInfo) {
-        final DOMMountPoint mountPoint = RestconfDocumentedException.throwIfNull(context,
+        final InstanceIdentifierContext iid = RestconfDocumentedException.throwIfNull(context,
             ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, "No patch documented provided")
-            .getInstanceIdentifierContext().getMountPoint();
-        final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
-        return PatchDataTransactionUtil.patchData(context, strategy, getSchemaContext(mountPoint));
+            .getInstanceIdentifierContext();
+        final RestconfStrategy strategy = getRestconfStrategy(iid.getMountPoint());
+        return PatchDataTransactionUtil.patchData(context, strategy, iid.getSchemaContext());
     }
 
     @Override
@@ -281,20 +274,12 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         requireNonNull(payload);
 
         final InstanceIdentifierContext iid = payload.getInstanceIdentifierContext();
-        validInputData(iid.getSchemaNode(), payload);
+        validInputData(iid.getSchemaNode() != null, payload);
         validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
         validateListKeysEqualityInPayloadAndUri(payload);
 
-        final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
-        final EffectiveModelContext ref = mountPoint == null
-                ? schemaContextHandler.get() : modelContext(mountPoint);
-        final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
-
-        return PlainPatchDataTransactionUtil.patchData(payload, strategy, ref);
-    }
-
-    private EffectiveModelContext getSchemaContext(final DOMMountPoint mountPoint) {
-        return mountPoint == null ? schemaContextHandler.get() : modelContext(mountPoint);
+        final RestconfStrategy strategy = getRestconfStrategy(iid.getMountPoint());
+        return PlainPatchDataTransactionUtil.patchData(payload, strategy, iid.getSchemaContext());
     }
 
     // FIXME: why is this synchronized?
@@ -327,27 +312,18 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         }
 
         final DOMMountPoint mountPoint = context.getMountPoint();
-        final Absolute schemaPath = Absolute.of(ImmutableList.copyOf(context.getSchemaNode().getPath()
-            .getPathFromRoot()));
+        final Absolute schemaPath = context.inference().toSchemaInferenceStack().toSchemaNodeIdentifier();
         final DOMActionResult response;
-        final EffectiveModelContext schemaContextRef;
         if (mountPoint != null) {
             response = invokeAction((ContainerNode) data, schemaPath, yangIIdContext, mountPoint);
-            schemaContextRef = modelContext(mountPoint);
         } else {
             response = invokeAction((ContainerNode) data, schemaPath, yangIIdContext, actionService);
-            schemaContextRef = schemaContextHandler.get();
         }
         final DOMActionResult result = checkActionResponse(response);
 
-        ActionDefinition resultNodeSchema = null;
         ContainerNode resultData = null;
         if (result != null) {
-            final Optional<ContainerNode> optOutput = result.getOutput();
-            if (optOutput.isPresent()) {
-                resultData = optOutput.get();
-                resultNodeSchema = (ActionDefinition) context.getSchemaNode();
-            }
+            resultData = result.getOutput().orElse(null);
         }
 
         if (resultData != null && resultData.isEmpty()) {
@@ -355,9 +331,7 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         }
 
         return Response.status(Status.OK)
-            .entity(NormalizedNodePayload.ofNullable(
-                new InstanceIdentifierContext(yangIIdContext, resultNodeSchema, mountPoint, schemaContextRef),
-                resultData))
+            .entity(NormalizedNodePayload.ofNullable(context, resultData))
             .build();
     }
 
@@ -390,8 +364,8 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         return RestconfInvokeOperationsServiceImpl.checkedGet(Futures.catching(actionService.invokeAction(
             schemaPath, new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
             DOMActionException.class,
-            cause -> new SimpleDOMActionResult(ImmutableList.of(RpcResultBuilder.newError(
-                RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))),
+            cause -> new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
+                ErrorType.RPC, ErrorTag.OPERATION_FAILED, cause.getMessage()))),
             MoreExecutors.directExecutor()));
     }
 
@@ -420,16 +394,20 @@ public class RestconfDataServiceImpl implements RestconfDataService {
     }
 
     /**
-     * Valid input data with {@link SchemaNode}.
+     * Valid input data based on presence of a schema node.
      *
-     * @param schemaNode {@link SchemaNode}
+     * @param haveSchemaNode true if there is an underlying schema node
      * @param payload    input data
      */
     @VisibleForTesting
-    public static void validInputData(final SchemaNode schemaNode, final NormalizedNodePayload payload) {
-        if (schemaNode != null && payload.getData() == null) {
-            throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
-        } else if (schemaNode == null && payload.getData() != null) {
+    public static void validInputData(final boolean haveSchemaNode, final NormalizedNodePayload payload) {
+        final boolean haveData = payload.getData() != null;
+        if (haveSchemaNode) {
+            if (!haveData) {
+                throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL,
+                    ErrorTag.MALFORMED_MESSAGE);
+            }
+        } else if (haveData) {
             throw new RestconfDocumentedException("No input expected.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
         }
     }
@@ -499,10 +477,4 @@ public class RestconfDataServiceImpl implements RestconfDataService {
             }
         }
     }
-
-    private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
-        return mountPoint.getService(DOMSchemaService.class)
-            .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
-            .orElse(null);
-    }
 }