Refactor pretty printing
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / mdsal / MdsalRestconfServer.java
index c688ad00e896e8376dcaeb292de57372bd67d380..c2768bbb70f87b8fcbfc79134b0540f2a3958e56 100644 (file)
@@ -10,110 +10,83 @@ package org.opendaylight.restconf.server.mdsal;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSetMultimap;
 import com.google.common.collect.Maps;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.MoreExecutors;
-import java.io.IOException;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
 import java.net.URI;
-import java.util.Comparator;
+import java.time.format.DateTimeParseException;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Optional;
-import java.util.concurrent.CancellationException;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.mdsal.dom.api.DOMActionException;
-import org.opendaylight.mdsal.dom.api.DOMActionResult;
 import org.opendaylight.mdsal.dom.api.DOMActionService;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
-import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
-import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
+import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfFuture;
-import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
-import org.opendaylight.restconf.common.patch.PatchContext;
-import org.opendaylight.restconf.common.patch.PatchStatusContext;
-import org.opendaylight.restconf.nb.rfc8040.Insert;
-import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
-import org.opendaylight.restconf.nb.rfc8040.databind.ChildBody;
-import org.opendaylight.restconf.nb.rfc8040.databind.DataPostBody;
-import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
-import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
-import org.opendaylight.restconf.nb.rfc8040.databind.OperationInputBody;
-import org.opendaylight.restconf.nb.rfc8040.databind.PatchBody;
-import org.opendaylight.restconf.nb.rfc8040.databind.ResourceBody;
-import org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.QueryParams;
-import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
+import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy.StrategyAndTail;
+import org.opendaylight.restconf.server.api.ChildBody;
+import org.opendaylight.restconf.server.api.DataGetParams;
+import org.opendaylight.restconf.server.api.DataGetResult;
+import org.opendaylight.restconf.server.api.DataPatchResult;
+import org.opendaylight.restconf.server.api.DataPostBody;
 import org.opendaylight.restconf.server.api.DataPostResult;
 import org.opendaylight.restconf.server.api.DataPostResult.CreateResource;
-import org.opendaylight.restconf.server.api.DataPostResult.InvokeOperation;
 import org.opendaylight.restconf.server.api.DataPutResult;
+import org.opendaylight.restconf.server.api.DataYangPatchResult;
+import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.api.InvokeResult;
+import org.opendaylight.restconf.server.api.ModulesGetResult;
+import org.opendaylight.restconf.server.api.OperationInputBody;
 import org.opendaylight.restconf.server.api.OperationsGetResult;
+import org.opendaylight.restconf.server.api.PatchBody;
+import org.opendaylight.restconf.server.api.ResourceBody;
 import org.opendaylight.restconf.server.api.RestconfServer;
-import org.opendaylight.restconf.server.spi.OperationInput;
-import org.opendaylight.restconf.server.spi.OperationOutput;
 import org.opendaylight.restconf.server.spi.RpcImplementation;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.YangApi;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.restconf.Restconf;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
 import org.opendaylight.yangtools.yang.common.Empty;
 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.QNameModule;
 import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-import org.opendaylight.yangtools.yang.common.XMLNamespace;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.common.YangNames;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
 
 /**
  * A RESTCONF server implemented on top of MD-SAL.
  */
 @Singleton
-@Component
-public final class MdsalRestconfServer implements RestconfServer {
-    private static final Logger LOG = LoggerFactory.getLogger(MdsalRestconfServer.class);
+@Component(service = RestconfServer.class)
+public final class MdsalRestconfServer implements RestconfServer, AutoCloseable {
     private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
-    private static final String YANG_LIBRARY_REVISION = YangLibrary.QNAME.getRevision().orElseThrow().toString();
     private static final VarHandle LOCAL_STRATEGY;
 
     static {
         try {
             LOCAL_STRATEGY = MethodHandles.lookup()
-                .findVarHandle(MdsalRestconfServer.class, "localStrategy", RestconfStrategy.class);
+                .findVarHandle(MdsalRestconfServer.class, "localStrategy", MdsalRestconfStrategy.class);
         } catch (NoSuchFieldException | IllegalAccessException e) {
             throw new ExceptionInInitializerError(e);
         }
@@ -121,401 +94,273 @@ public final class MdsalRestconfServer implements RestconfServer {
 
     private final @NonNull ImmutableMap<QName, RpcImplementation> localRpcs;
     private final @NonNull DOMMountPointService mountPointService;
-    private final @NonNull DatabindProvider databindProvider;
+    private final @NonNull MdsalDatabindProvider databindProvider;
     private final @NonNull DOMDataBroker dataBroker;
     private final @Nullable DOMRpcService rpcService;
     private final @Nullable DOMActionService actionService;
 
-    @SuppressWarnings("unused")
-    private volatile RestconfStrategy localStrategy;
+    @SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
+    private volatile MdsalRestconfStrategy localStrategy;
 
     @Inject
     @Activate
-    public MdsalRestconfServer(@Reference final DatabindProvider databindProvider,
+    public MdsalRestconfServer(@Reference final MdsalDatabindProvider databindProvider,
             @Reference final DOMDataBroker dataBroker, @Reference final DOMRpcService rpcService,
             @Reference final DOMActionService actionService,
             @Reference final DOMMountPointService mountPointService,
-            @Reference final List<RpcImplementation> localRpcs) {
+            @Reference(policyOption = ReferencePolicyOption.GREEDY) final List<RpcImplementation> localRpcs) {
         this.databindProvider = requireNonNull(databindProvider);
         this.dataBroker = requireNonNull(dataBroker);
         this.rpcService = requireNonNull(rpcService);
         this.actionService = requireNonNull(actionService);
         this.mountPointService = requireNonNull(mountPointService);
+
         this.localRpcs = Maps.uniqueIndex(localRpcs, RpcImplementation::qname);
+        localStrategy = createLocalStrategy(databindProvider.currentDatabind());
     }
 
-    public MdsalRestconfServer(final DatabindProvider databindProvider, final DOMDataBroker dataBroker,
+    public MdsalRestconfServer(final MdsalDatabindProvider databindProvider, final DOMDataBroker dataBroker,
             final DOMRpcService rpcService, final DOMActionService actionService,
             final DOMMountPointService mountPointService, final RpcImplementation... localRpcs) {
         this(databindProvider, dataBroker, rpcService, actionService, mountPointService, List.of(localRpcs));
     }
 
-    @Override
-    public RestconfFuture<Empty> dataDELETE(final String identifier) {
-        final var reqPath = bindRequestPath(identifier);
-        final var strategy = getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint());
-        return strategy.delete(reqPath.getInstanceIdentifier());
+    private @NonNull MdsalRestconfStrategy createLocalStrategy(final DatabindContext databind) {
+        return new MdsalRestconfStrategy(databind, dataBroker, rpcService, actionService,
+            databindProvider.sourceProvider(), mountPointService, localRpcs);
     }
 
-    @Override
-    public RestconfFuture<NormalizedNodePayload> dataGET(final ReadDataParams readParams) {
-        return readData(bindRequestRoot(), readParams);
+    private @NonNull MdsalRestconfStrategy localStrategy() {
+        final var strategy = verifyNotNull((@NonNull MdsalRestconfStrategy) LOCAL_STRATEGY.getAcquire(this));
+        final var databind = databindProvider.currentDatabind();
+        return databind.equals(strategy.databind()) ? strategy : updateLocalStrategy(databind);
+    }
+
+    private @NonNull MdsalRestconfStrategy updateLocalStrategy(final DatabindContext databind) {
+        final var strategy = createLocalStrategy(databind);
+        localStrategy = strategy;
+        return strategy;
     }
 
+    @PreDestroy
+    @Deactivate
     @Override
-    public RestconfFuture<NormalizedNodePayload> dataGET(final String identifier, final ReadDataParams readParams) {
-        return readData(bindRequestPath(identifier), readParams);
+    public void close() {
+        localStrategy = null;
     }
 
-    private @NonNull RestconfFuture<NormalizedNodePayload> readData(final InstanceIdentifierContext reqPath,
-            final ReadDataParams readParams) {
-        final var queryParams = QueryParams.newQueryParameters(readParams, reqPath);
-        final var fieldPaths = queryParams.fieldPaths();
-        final var strategy = getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint());
-        final NormalizedNode node;
-        if (fieldPaths != null && !fieldPaths.isEmpty()) {
-            node = strategy.readData(readParams.content(), reqPath.getInstanceIdentifier(), readParams.withDefaults(),
-                fieldPaths);
-        } else {
-            node = strategy.readData(readParams.content(), reqPath.getInstanceIdentifier(), readParams.withDefaults());
-        }
-        if (node == null) {
-            return RestconfFuture.failed(new RestconfDocumentedException(
-                "Request could not be completed because the relevant data model content does not exist",
-                ErrorType.PROTOCOL, ErrorTag.DATA_MISSING));
+    @Override
+    public RestconfFuture<Empty> dataDELETE(final ApiPath identifier) {
+        final StrategyAndTail stratAndTail;
+        try {
+            stratAndTail = localStrategy().resolveStrategy(identifier);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
-
-        return RestconfFuture.of(new NormalizedNodePayload(reqPath.inference(), node, queryParams));
+        return stratAndTail.strategy().dataDELETE(stratAndTail.tail());
     }
 
     @Override
-    public RestconfFuture<Empty> dataPATCH(final ResourceBody body) {
-        return dataPATCH(bindRequestRoot(), body);
+    public RestconfFuture<DataGetResult> dataGET(final DataGetParams params) {
+        return localStrategy().dataGET(ApiPath.empty(), params);
     }
 
     @Override
-    public RestconfFuture<Empty> dataPATCH(final String identifier, final ResourceBody body) {
-        return dataPATCH(bindRequestPath(identifier), body);
+    public RestconfFuture<DataGetResult> dataGET(final ApiPath identifier, final DataGetParams params) {
+        final StrategyAndTail stratAndTail;
+        try {
+            stratAndTail = localStrategy().resolveStrategy(identifier);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
+        }
+        return stratAndTail.strategy().dataGET(stratAndTail.tail(), params);
     }
 
-    private @NonNull RestconfFuture<Empty> dataPATCH(final InstanceIdentifierContext reqPath, final ResourceBody body) {
-        final var req = bindResourceRequest(reqPath, body);
-        return req.strategy().merge(req.path(), req.data());
+    @Override
+    public RestconfFuture<DataPatchResult> dataPATCH(final ResourceBody body) {
+        return localStrategy().dataPATCH(ApiPath.empty(), body);
     }
 
     @Override
-    public RestconfFuture<PatchStatusContext> dataPATCH(final PatchBody body) {
-        return dataPATCH(bindRequestRoot(), body);
+    public RestconfFuture<DataPatchResult> dataPATCH(final ApiPath identifier, final ResourceBody body) {
+        final StrategyAndTail strategyAndTail;
+        try {
+            strategyAndTail = localStrategy().resolveStrategy(identifier);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
+        }
+        return strategyAndTail.strategy().dataPATCH(strategyAndTail.tail(), body);
     }
 
     @Override
-    public RestconfFuture<PatchStatusContext> dataPATCH(final String identifier, final PatchBody body) {
-        return dataPATCH(bindRequestPath(identifier), body);
+    public RestconfFuture<DataYangPatchResult> dataPATCH(final PatchBody body) {
+        return localStrategy().dataPATCH(ApiPath.empty(), body);
     }
 
-    private @NonNull RestconfFuture<PatchStatusContext> dataPATCH(final InstanceIdentifierContext reqPath,
-            final PatchBody body) {
-        final var modelContext = reqPath.getSchemaContext();
-        final PatchContext patch;
+    @Override
+    public RestconfFuture<DataYangPatchResult> dataPATCH(final ApiPath identifier, final PatchBody body) {
+        final StrategyAndTail strategyAndTail;
         try {
-            patch = body.toPatchContext(modelContext, reqPath.getInstanceIdentifier());
-        } catch (IOException e) {
-            LOG.debug("Error parsing YANG Patch input", e);
-            return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
-                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
+            strategyAndTail = localStrategy().resolveStrategy(identifier);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
-        return getRestconfStrategy(modelContext, reqPath.getMountPoint()).patchData(patch);
+        return strategyAndTail.strategy().dataPATCH(strategyAndTail.tail(), body);
     }
 
     @Override
     public RestconfFuture<CreateResource> dataPOST(final ChildBody body, final Map<String, String> queryParameters) {
-        return dataCreatePOST(bindRequestRoot(), body, queryParameters);
+        return localStrategy().dataCreatePOST(body, queryParameters);
     }
 
     @Override
-    public RestconfFuture<? extends DataPostResult> dataPOST(final String identifier, final DataPostBody body,
+    public RestconfFuture<? extends DataPostResult> dataPOST(final ApiPath identifier, final DataPostBody body,
             final Map<String, String> queryParameters) {
-        final var reqPath = bindRequestPath(identifier);
-        if (reqPath.getSchemaNode() instanceof ActionDefinition) {
-            try (var inputBody = body.toOperationInput()) {
-                return dataInvokePOST(reqPath, inputBody);
-            }
-        }
-
-        try (var childBody = body.toResource()) {
-            return dataCreatePOST(reqPath, childBody, queryParameters);
-        }
-    }
-
-    private @NonNull RestconfFuture<CreateResource> dataCreatePOST(final InstanceIdentifierContext reqPath,
-            final ChildBody body, final Map<String, String> queryParameters) {
-        final var inference = reqPath.inference();
-        final var modelContext = inference.getEffectiveModelContext();
-
-        final Insert insert;
+        final StrategyAndTail strategyAndTail;
         try {
-            insert = Insert.ofQueryParameters(modelContext, queryParameters);
-        } catch (IllegalArgumentException e) {
-            return RestconfFuture.failed(new RestconfDocumentedException(e.getMessage(),
-                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e));
+            strategyAndTail = localStrategy().resolveStrategy(identifier);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
-
-        final var parentPath = reqPath.getInstanceIdentifier();
-        final var payload = body.toPayload(parentPath, inference);
-        return getRestconfStrategy(modelContext, reqPath.getMountPoint())
-            .postData(concat(parentPath, payload.prefix()), payload.body(), insert);
+        return strategyAndTail.strategy().dataPOST(strategyAndTail.tail(), body, queryParameters);
     }
 
-    private static YangInstanceIdentifier concat(final YangInstanceIdentifier parent, final List<PathArgument> args) {
-        var ret = parent;
-        for (var arg : args) {
-            ret = ret.node(arg);
-        }
-        return ret;
+    @Override
+    public RestconfFuture<DataPutResult> dataPUT(final ResourceBody body, final Map<String, String> query) {
+        return localStrategy().dataPUT(ApiPath.empty(), body, query);
     }
 
-    private RestconfFuture<InvokeOperation> dataInvokePOST(final InstanceIdentifierContext reqPath,
-            final OperationInputBody body) {
-        final var yangIIdContext = reqPath.getInstanceIdentifier();
-        final var inference = reqPath.inference();
-        final ContainerNode input;
+    @Override
+    public RestconfFuture<DataPutResult> dataPUT(final ApiPath identifier, final ResourceBody body,
+             final Map<String, String> queryParameters) {
+        final StrategyAndTail strategyAndTail;
         try {
-            input = body.toContainerNode(inference);
-        } catch (IOException e) {
-            LOG.debug("Error reading input", e);
-            return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
-                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
+            strategyAndTail = localStrategy().resolveStrategy(identifier);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
-
-        final var mountPoint = reqPath.getMountPoint();
-        final var schemaPath = inference.toSchemaInferenceStack().toSchemaNodeIdentifier();
-        final var future = mountPoint != null ? dataInvokePOST(input, schemaPath, yangIIdContext, mountPoint)
-            : dataInvokePOST(input, schemaPath, yangIIdContext, actionService);
-
-        return future.transform(result -> result.getOutput()
-            .flatMap(output -> output.isEmpty() ? Optional.empty()
-                : Optional.of(new InvokeOperation(new NormalizedNodePayload(reqPath.inference(), output))))
-            .orElse(InvokeOperation.EMPTY));
+        return strategyAndTail.strategy().dataPUT(strategyAndTail.tail(), body, queryParameters);
     }
 
-    /**
-     * Invoke Action via ActionServiceHandler.
-     *
-     * @param data input data
-     * @param yangIId invocation context
-     * @param schemaPath schema path of data
-     * @param actionService action service to invoke action
-     * @return {@link DOMActionResult}
-     */
-    private static RestconfFuture<DOMActionResult> dataInvokePOST(final ContainerNode data, final Absolute schemaPath,
-            final YangInstanceIdentifier yangIId, final DOMActionService actionService) {
-        final var ret = new SettableRestconfFuture<DOMActionResult>();
-
-        Futures.addCallback(actionService.invokeAction(schemaPath,
-            new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
-            new FutureCallback<DOMActionResult>() {
-                @Override
-                public void onSuccess(final DOMActionResult result) {
-                    final var errors = result.getErrors();
-                    LOG.debug("InvokeAction Error Message {}", errors);
-                    if (errors.isEmpty()) {
-                        ret.set(result);
-                    } else {
-                        ret.setFailure(new RestconfDocumentedException("InvokeAction Error Message ", null, errors));
-                    }
-                }
-
-                @Override
-                public void onFailure(final Throwable cause) {
-                    if (cause instanceof DOMActionException) {
-                        ret.set(new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
-                            ErrorType.RPC, ErrorTag.OPERATION_FAILED, cause.getMessage()))));
-                    } else if (cause instanceof RestconfDocumentedException e) {
-                        ret.setFailure(e);
-                    } else if (cause instanceof CancellationException) {
-                        ret.setFailure(new RestconfDocumentedException("Action cancelled while executing",
-                            ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, cause));
-                    } else {
-                        ret.setFailure(new RestconfDocumentedException("Invocation failed", cause));
-                    }
-                }
-            }, MoreExecutors.directExecutor());
-
-        return ret;
-    }
-
-    /**
-     * Invoking Action via mount point.
-     *
-     * @param mountPoint mount point
-     * @param data input data
-     * @param schemaPath schema path of data
-     * @return {@link DOMActionResult}
-     */
-    private static RestconfFuture<DOMActionResult> dataInvokePOST(final ContainerNode data, final Absolute schemaPath,
-            final YangInstanceIdentifier yangIId, final DOMMountPoint mountPoint) {
-        final var actionService = mountPoint.getService(DOMActionService.class);
-        return actionService.isPresent() ? dataInvokePOST(data, schemaPath, yangIId, actionService.orElseThrow())
-            : RestconfFuture.failed(new RestconfDocumentedException("DOMActionService is missing."));
+    @Override
+    public RestconfFuture<ModulesGetResult> modulesYangGET(final String fileName, final String revision) {
+        return modulesGET(fileName, revision, YangTextSource.class);
     }
 
     @Override
-    public RestconfFuture<DataPutResult> dataPUT(final ResourceBody body, final Map<String, String> query) {
-        return dataPUT(bindRequestRoot(), body, query);
+    public RestconfFuture<ModulesGetResult> modulesYangGET(final ApiPath mountPath, final String fileName,
+            final String revision) {
+        return modulesGET(mountPath, fileName, revision, YangTextSource.class);
     }
 
     @Override
-    public RestconfFuture<DataPutResult> dataPUT(final String identifier, final ResourceBody body,
-             final Map<String, String> queryParameters) {
-        return dataPUT(bindRequestPath(identifier), body, queryParameters);
+    public RestconfFuture<ModulesGetResult> modulesYinGET(final String fileName, final String revision) {
+        return modulesGET(fileName, revision, YinTextSource.class);
     }
 
-    private @NonNull RestconfFuture<DataPutResult> dataPUT(final InstanceIdentifierContext reqPath,
-            final ResourceBody body, final Map<String, String> queryParameters) {
-        final Insert insert;
-        try {
-            insert = Insert.ofQueryParameters(reqPath.getSchemaContext(), queryParameters);
-        } catch (IllegalArgumentException e) {
-            return RestconfFuture.failed(new RestconfDocumentedException(e.getMessage(),
-                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e));
-        }
-        final var req = bindResourceRequest(reqPath, body);
-        return req.strategy().putData(req.path(), req.data(), insert);
+    @Override
+    public RestconfFuture<ModulesGetResult> modulesYinGET(final ApiPath mountPath, final String fileName,
+            final String revision) {
+        return modulesGET(mountPath, fileName, revision, YinTextSource.class);
     }
 
-    @Override
-    public OperationsGetResult operationsGET() {
-        return operationsGET(databindProvider.currentContext().modelContext());
+    private @NonNull RestconfFuture<ModulesGetResult> modulesGET(final String fileName, final String revision,
+            final Class<? extends SourceRepresentation> representation) {
+        return modulesGET(localStrategy(), fileName, revision, representation);
     }
 
-    @Override
-    public OperationsGetResult operationsGET(final String operation) {
-        // get current module RPCs/actions by RPC/action name
-        final var inference = bindRequestPath(operation).inference();
-        if (inference.isEmpty()) {
-            return operationsGET(inference.getEffectiveModelContext());
+    private @NonNull RestconfFuture<ModulesGetResult> modulesGET(final ApiPath mountPath, final String fileName,
+            final String revision, final Class<? extends SourceRepresentation> representation) {
+        final var mountOffset = mountPath.indexOf("yang-ext", "mount");
+        if (mountOffset != mountPath.steps().size() - 1) {
+            return RestconfFuture.failed(new RestconfDocumentedException("Mount path has to end with yang-ext:mount"));
         }
 
-        final var stmt = inference.toSchemaInferenceStack().currentStatement();
-        if (stmt instanceof RpcEffectiveStatement rpc) {
-            return new OperationsGetResult.Leaf(inference.getEffectiveModelContext(), rpc.argument());
+        final StrategyAndTail stratAndTail;
+        try {
+            stratAndTail = localStrategy().resolveStrategy(mountPath);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
-        LOG.debug("Operation '{}' resulted in non-RPC {}", operation, stmt);
-        return null;
+        // FIXME: require remnant to be empty
+        return modulesGET(stratAndTail.strategy(), fileName, revision, representation);
     }
 
-    private static @NonNull OperationsGetResult operationsGET(final EffectiveModelContext modelContext) {
-        final var modules = modelContext.getModuleStatements();
-        if (modules.isEmpty()) {
-            // No modules, or defensive return empty content
-            return new OperationsGetResult.Container(modelContext, ImmutableSetMultimap.of());
+    private static @NonNull RestconfFuture<ModulesGetResult> modulesGET(final RestconfStrategy strategy,
+            final String moduleName, final String revisionStr,
+            final Class<? extends SourceRepresentation> representation) {
+        if (moduleName == null) {
+            return RestconfFuture.failed(new RestconfDocumentedException("Module name must be supplied",
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE));
         }
-
-        // RPCs by their XMLNamespace/Revision
-        final var table = HashBasedTable.<XMLNamespace, Revision, ImmutableSet<QName>>create();
-        for (var entry : modules.entrySet()) {
-            final var module = entry.getValue();
-            final var rpcNames = module.streamEffectiveSubstatements(RpcEffectiveStatement.class)
-                .map(RpcEffectiveStatement::argument)
-                .collect(ImmutableSet.toImmutableSet());
-            if (!rpcNames.isEmpty()) {
-                final var namespace = entry.getKey();
-                table.put(namespace.getNamespace(), namespace.getRevision().orElse(null), rpcNames);
-            }
+        if (moduleName.isEmpty() || !YangNames.IDENTIFIER_START.matches(moduleName.charAt(0))) {
+            return RestconfFuture.failed(new RestconfDocumentedException(
+                "Identifier must start with character from set 'a-zA-Z_", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE));
         }
-
-        // Now pick the latest revision for each namespace
-        final var rpcs = ImmutableSetMultimap.<QNameModule, QName>builder();
-        for (var entry : table.rowMap().entrySet()) {
-            entry.getValue().entrySet().stream()
-                .sorted(Comparator.comparing(Entry::getKey, (first, second) -> Revision.compare(second, first)))
-                .findFirst()
-                .ifPresent(row -> rpcs.putAll(QNameModule.create(entry.getKey(), row.getKey()), row.getValue()));
+        if (moduleName.toUpperCase(Locale.ROOT).startsWith("XML")) {
+            return RestconfFuture.failed(new RestconfDocumentedException(
+                "Identifier must NOT start with XML ignore case", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE));
+        }
+        if (YangNames.NOT_IDENTIFIER_PART.matchesAnyOf(moduleName.substring(1))) {
+            return RestconfFuture.failed(new RestconfDocumentedException(
+                "Supplied name has not expected identifier format", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE));
         }
-        return new OperationsGetResult.Container(modelContext, rpcs.build());
-    }
 
-    @Override
-    public RestconfFuture<OperationOutput> operationsPOST(final URI restconfURI, final String apiPath,
-            final OperationInputBody body) {
-        final var currentContext = databindProvider.currentContext();
-        final var reqPath = bindRequestPath(currentContext, apiPath);
-        final var inference = reqPath.inference();
-        final ContainerNode input;
+        // YANG Revision-compliant string is required
+        final Revision revision;
         try {
-            input = body.toContainerNode(inference);
-        } catch (IOException e) {
-            LOG.debug("Error reading input", e);
-            return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
-                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
+            revision = Revision.ofNullable(revisionStr).orElse(null);
+        } catch (final DateTimeParseException e) {
+            return RestconfFuture.failed(new RestconfDocumentedException(
+                "Supplied revision is not in expected date format YYYY-mm-dd",
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e));
         }
 
-        return getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint())
-            .invokeRpc(restconfURI, reqPath.getSchemaNode().getQName(),
-                new OperationInput(currentContext, inference, input));
+        return strategy.resolveSource(new SourceIdentifier(moduleName, revision), representation)
+            .transform(ModulesGetResult::new);
     }
 
     @Override
-    public NormalizedNodePayload yangLibraryVersionGET() {
-        final var stack = SchemaInferenceStack.of(databindProvider.currentContext().modelContext());
-        stack.enterYangData(YangApi.NAME);
-        stack.enterDataTree(Restconf.QNAME);
-        stack.enterDataTree(YANG_LIBRARY_VERSION);
-        return new NormalizedNodePayload(stack.toInference(),
-            ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, YANG_LIBRARY_REVISION));
+    public RestconfFuture<OperationsGetResult> operationsGET() {
+        return localStrategy().operationsGET();
     }
 
-    private @NonNull InstanceIdentifierContext bindRequestPath(final String identifier) {
-        return bindRequestPath(databindProvider.currentContext(), identifier);
-    }
-
-    private @NonNull InstanceIdentifierContext bindRequestPath(final DatabindContext databind,
-            final String identifier) {
-        // FIXME: go through ApiPath first. That part should eventually live in callers
-        // FIXME: DatabindContext looks like it should be internal
-        return verifyNotNull(ParserIdentifier.toInstanceIdentifier(requireNonNull(identifier), databind.modelContext(),
-            mountPointService));
-    }
-
-    private @NonNull InstanceIdentifierContext bindRequestRoot() {
-        return InstanceIdentifierContext.ofLocalRoot(databindProvider.currentContext().modelContext());
-    }
-
-    private @NonNull ResourceRequest bindResourceRequest(final InstanceIdentifierContext reqPath,
-            final ResourceBody body) {
-        final var inference = reqPath.inference();
-        final var path = reqPath.getInstanceIdentifier();
-        final var data = body.toNormalizedNode(path, inference, reqPath.getSchemaNode());
-
-        return new ResourceRequest(
-            getRestconfStrategy(inference.getEffectiveModelContext(), reqPath.getMountPoint()), path, data);
-    }
-
-    @VisibleForTesting
-    @NonNull RestconfStrategy getRestconfStrategy(final EffectiveModelContext modelContext,
-            final @Nullable DOMMountPoint mountPoint) {
-        if (mountPoint == null) {
-            return localStrategy(modelContext);
+    @Override
+    public RestconfFuture<OperationsGetResult> operationsGET(final ApiPath operation) {
+        final StrategyAndTail strategyAndTail;
+        try {
+            strategyAndTail = localStrategy().resolveStrategy(operation);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
+        return strategyAndTail.strategy().operationsGET(strategyAndTail.tail());
+    }
 
-        final var ret = RestconfStrategy.forMountPoint(modelContext, mountPoint);
-        if (ret == null) {
-            final var mountId = mountPoint.getIdentifier();
-            LOG.warn("Mount point {} does not expose a suitable access interface", mountId);
-            throw new RestconfDocumentedException("Could not find a supported access interface in mount point",
-                ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, mountId);
+    @Override
+    public RestconfFuture<InvokeResult> operationsPOST(final URI restconfURI, final ApiPath apiPath,
+            final Map<String, String> queryParameters, final OperationInputBody body) {
+        final StrategyAndTail strategyAndTail;
+        try {
+            strategyAndTail = localStrategy().resolveStrategy(apiPath);
+        } catch (RestconfDocumentedException e) {
+            return RestconfFuture.failed(e);
         }
-        return ret;
+        final var strategy = strategyAndTail.strategy();
+        return strategy.operationsPOST(restconfURI, strategyAndTail.tail(), queryParameters, body);
     }
 
-    private @NonNull RestconfStrategy localStrategy(final EffectiveModelContext modelContext) {
-        final var local = (RestconfStrategy) LOCAL_STRATEGY.getAcquire(this);
-        if (local != null && modelContext.equals(local.modelContext())) {
-            return local;
+    @Override
+    public RestconfFuture<NormalizedNodePayload> yangLibraryVersionGET() {
+        final var stack = SchemaInferenceStack.of(localStrategy().modelContext());
+        try {
+            stack.enterYangData(YangApi.NAME);
+            stack.enterDataTree(Restconf.QNAME);
+            stack.enterDataTree(YANG_LIBRARY_VERSION);
+        } catch (IllegalArgumentException e) {
+            return RestconfFuture.failed(new RestconfDocumentedException("RESTCONF is not available"));
         }
-
-        final var created = new MdsalRestconfStrategy(modelContext, dataBroker, rpcService, localRpcs);
-        LOCAL_STRATEGY.setRelease(this, created);
-        return created;
+        return RestconfFuture.of(new NormalizedNodePayload(stack.toInference(),
+            ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, stack.modelContext().findModuleStatements("ietf-yang-library")
+                .iterator().next().localQNameModule().revisionUnion().unionString())));
     }
 }