From 0d491c8867a9d212a8dfae260cef896722dee6bc Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 3 Apr 2024 18:49:00 +0200 Subject: [PATCH] Remove DataPostPath We have a more powerful tool in DatabindPath.Data, use that instead. JIRA: NETCONF-1288 Change-Id: I80e1e122aebec9348d83b0a8d28cd82cea7ad840 Signed-off-by: Robert Varga --- .../rests/transactions/RestconfStrategy.java | 12 ++--- .../restconf/server/api/ChildBody.java | 4 +- .../restconf/server/api/DataPostBody.java | 1 - .../restconf/server/api/DataPostPath.java | 46 ------------------- .../restconf/server/api/DatabindPath.java | 6 +++ .../restconf/server/api/JsonChildBody.java | 5 +- .../restconf/server/api/RestconfServer.java | 8 ++++ .../restconf/server/api/XmlChildBody.java | 4 +- .../server/spi/ApiPathNormalizer.java | 4 +- .../rfc8040/databind/JsonChildBodyTest.java | 11 +++-- .../nb/rfc8040/databind/XmlChildBodyTest.java | 16 +++---- 11 files changed, 40 insertions(+), 77 deletions(-) delete mode 100644 restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostPath.java diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java index 9ab34b15cd..fba2fe19e1 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java @@ -74,13 +74,13 @@ 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.DataPostPath; 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.DatabindPath; import org.opendaylight.restconf.server.api.DatabindPath.Action; import org.opendaylight.restconf.server.api.DatabindPath.Data; import org.opendaylight.restconf.server.api.DatabindPath.InstanceReference; @@ -140,7 +140,6 @@ import org.opendaylight.yangtools.yang.model.api.source.YinTextSource; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement; -import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -1414,8 +1413,7 @@ public abstract class RestconfStrategy { } if (path instanceof Data dataPath) { try (var resourceBody = body.toResource()) { - return dataCreatePOST(new DataPostPath(databind, dataPath.inference(), dataPath.instance()), - resourceBody, queryParameters); + return dataCreatePOST(dataPath, resourceBody, queryParameters); } } if (path instanceof Action actionPath) { @@ -1430,12 +1428,10 @@ public abstract class RestconfStrategy { public @NonNull RestconfFuture dataCreatePOST(final ChildBody body, final Map queryParameters) { - return dataCreatePOST(new DataPostPath(databind, - SchemaInferenceStack.of(databind.modelContext()).toInference(), YangInstanceIdentifier.of()), body, - queryParameters); + return dataCreatePOST(new DatabindPath.Data(databind), body, queryParameters); } - private @NonNull RestconfFuture dataCreatePOST(final DataPostPath path, final ChildBody body, + private @NonNull RestconfFuture dataCreatePOST(final DatabindPath.Data path, final ChildBody body, final Map queryParameters) { final Insert insert; try { diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/ChildBody.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/ChildBody.java index dbc5f5c8ba..9772a55d2d 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/ChildBody.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/ChildBody.java @@ -33,9 +33,9 @@ public abstract sealed class ChildBody extends AbstractBody permits JsonChildBod * @param path POST request path * @return A {@link PrefixAndBody} */ - public final @NonNull PrefixAndBody toPayload(final @NonNull DataPostPath path) { + public final @NonNull PrefixAndBody toPayload(final DatabindPath.@NonNull Data path) { return toPayload(path, acquireStream()); } - abstract @NonNull PrefixAndBody toPayload(@NonNull DataPostPath path, @NonNull InputStream inputStream); + abstract @NonNull PrefixAndBody toPayload(DatabindPath.@NonNull Data path, @NonNull InputStream inputStream); } diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostBody.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostBody.java index 3285dd3deb..48096f1311 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostBody.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostBody.java @@ -13,7 +13,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; /** * Body of a {@code POST} request as defined in * RFC8040 section 4.4. - * @see DataPostPath */ @NonNullByDefault public abstract sealed class DataPostBody extends AbstractBody permits JsonDataPostBody, XmlDataPostBody { diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostPath.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostPath.java deleted file mode 100644 index 49c6fa0f2f..0000000000 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DataPostPath.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.restconf.server.api; - -import static java.util.Objects.requireNonNull; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.opendaylight.restconf.api.ApiPath; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; -import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; - -/** - * An {@link ApiPath} subpath of {@code /data} {@code POST} HTTP operation, as defined in - * RFC8040 section 4.4. - * - * @param databind Associated {@link DatabindContext} - * @param inference Associated {@link Inference} pointing to the {@link EffectiveStatement} of the last ApiPath element. - * This can be one of: - *
    - *
  • a datatore, inference being {@link Inference#isEmpty() empty}
  • - *
  • a data resource, inference pointing to the the {@code data schema node} identified by - * {@code instance}
  • - *
  • an {@code rpc} or an {@code action} invocation, inference pointing to the statement
  • - *
- * @param instance Associated {@link YangInstanceIdentifier} - * @see DataPostBody - */ -@NonNullByDefault -public record DataPostPath(DatabindContext databind, Inference inference, YangInstanceIdentifier instance) - implements DatabindAware { - public DataPostPath { - requireNonNull(databind); - requireNonNull(inference); - requireNonNull(instance); - } - - public DataPostPath(final DatabindContext databind) { - this(databind, Inference.ofDataTreePath(databind.modelContext()), YangInstanceIdentifier.of()); - } -} diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DatabindPath.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DatabindPath.java index 6532d6d324..b92cfdbc5c 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DatabindPath.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/DatabindPath.java @@ -91,6 +91,12 @@ public sealed interface DatabindPath extends DatabindAware { requireNonNull(instance); requireNonNull(schema); } + + // FIXME: this is the 'Datastore' constructor + public Data(final DatabindContext databind) { + this(databind, Inference.ofDataTreePath(databind.modelContext()), YangInstanceIdentifier.of(), + databind.schemaTree().getRoot()); + } } /** diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/JsonChildBody.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/JsonChildBody.java index 75192adaa3..8ac5701a11 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/JsonChildBody.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/JsonChildBody.java @@ -38,7 +38,7 @@ public final class JsonChildBody extends ChildBody { @Override @SuppressWarnings("checkstyle:illegalCatch") - PrefixAndBody toPayload(final DataPostPath path, final InputStream inputStream) { + PrefixAndBody toPayload(final DatabindPath.Data path, final InputStream inputStream) { NormalizedNode result; try { result = toNormalizedNode(path, inputStream); @@ -73,7 +73,8 @@ public final class JsonChildBody extends ChildBody { return new PrefixAndBody(iiToDataList.build(), result); } - private static @NonNull NormalizedNode toNormalizedNode(final DataPostPath path, final InputStream inputStream) { + private static @NonNull NormalizedNode toNormalizedNode(final DatabindPath.Data path, + final InputStream inputStream) { final var resultHolder = new NormalizationResultHolder(); final var writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder); final var jsonParser = JsonParserStream.create(writer, path.databind().jsonCodecs(), path.inference()); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/RestconfServer.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/RestconfServer.java index 0c7743ec8a..864eebfb17 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/RestconfServer.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/RestconfServer.java @@ -88,6 +88,14 @@ public interface RestconfServer { RestconfFuture dataPOST(ChildBody body, Map queryParameters); + /** + * Create or invoke a operation, as described in + * RFC8040 section 4.4. + * + * @param identifier path to target + * @param body body of the post request + * @param queryParameters query parameters + */ RestconfFuture dataPOST(ApiPath identifier, DataPostBody body, Map queryParameters); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/XmlChildBody.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/XmlChildBody.java index e174a4c4a2..38b58c2cda 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/XmlChildBody.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/api/XmlChildBody.java @@ -45,7 +45,7 @@ public final class XmlChildBody extends ChildBody { @Override @SuppressWarnings("checkstyle:illegalCatch") - PrefixAndBody toPayload(final DataPostPath path, final InputStream inputStream) { + PrefixAndBody toPayload(final DatabindPath.Data path, final InputStream inputStream) { try { return parse(path, UntrustedXML.newDocumentBuilder().parse(inputStream)); } catch (final RestconfDocumentedException e) { @@ -58,7 +58,7 @@ public final class XmlChildBody extends ChildBody { } } - private static @NonNull PrefixAndBody parse(final DataPostPath path, final Document doc) + private static @NonNull PrefixAndBody parse(final DatabindPath.Data path, final Document doc) throws XMLStreamException, IOException, SAXException, URISyntaxException { final var pathInference = path.inference(); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/ApiPathNormalizer.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/ApiPathNormalizer.java index 6beae7ce94..22105bef84 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/ApiPathNormalizer.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/ApiPathNormalizer.java @@ -52,7 +52,6 @@ import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode; import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement; import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack; -import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; /** * Utility for normalizing {@link ApiPath}s. An {@link ApiPath} can represent a number of different constructs, as @@ -73,8 +72,7 @@ public final class ApiPathNormalizer implements PointNormalizer { public @NonNull DatabindPath normalizePath(final ApiPath apiPath) { final var it = apiPath.steps().iterator(); if (!it.hasNext()) { - return new Data(databind, Inference.ofDataTreePath(databind.modelContext()), YangInstanceIdentifier.of(), - databind.schemaTree().getRoot()); + return new Data(databind); } // First step is somewhat special: diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/JsonChildBodyTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/JsonChildBodyTest.java index f2a60cb6c4..bc466eb962 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/JsonChildBodyTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/JsonChildBodyTest.java @@ -12,19 +12,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.opendaylight.restconf.server.api.DataPostPath; import org.opendaylight.restconf.server.api.DatabindContext; +import org.opendaylight.restconf.server.api.DatabindPath; import org.opendaylight.restconf.server.api.JsonChildBody; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes; -import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; class JsonChildBodyTest extends AbstractBodyTest { - private static DataPostPath CONT_PATH; + private static DatabindPath.Data CONT_PATH; @BeforeAll static void beforeAll() throws Exception { @@ -32,8 +31,10 @@ class JsonChildBodyTest extends AbstractBodyTest { testFiles.addAll(loadFiles("/modules")); final var modelContext = YangParserTestUtils.parseYangFiles(testFiles); - CONT_PATH = new DataPostPath(DatabindContext.ofModel(modelContext), - Inference.ofDataTreePath(modelContext, CONT_QNAME), YangInstanceIdentifier.of(CONT_QNAME)); + final var contPath = YangInstanceIdentifier.of(CONT_QNAME); + final var databind = DatabindContext.ofModel(modelContext); + final var nodeAndStack = databind.schemaTree().enterPath(contPath).orElseThrow(); + CONT_PATH = new DatabindPath.Data(databind, nodeAndStack.stack().toInference(), contPath, nodeAndStack.node()); } @Test diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/XmlChildBodyTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/XmlChildBodyTest.java index 6c4935d52f..8ad3757d2e 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/XmlChildBodyTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/databind/XmlChildBodyTest.java @@ -12,8 +12,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.List; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.opendaylight.restconf.server.api.DataPostPath; import org.opendaylight.restconf.server.api.DatabindContext; +import org.opendaylight.restconf.server.api.DatabindPath; import org.opendaylight.restconf.server.api.XmlChildBody; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -21,14 +21,13 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdent import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes; -import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; class XmlChildBodyTest extends AbstractBodyTest { private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list"); - private static DataPostPath EMPTY_PATH; - private static DataPostPath CONT_PATH; + private static DatabindPath.Data EMPTY_PATH; + private static DatabindPath.Data CONT_PATH; @BeforeAll static void beforeAll() throws Exception { @@ -37,10 +36,11 @@ class XmlChildBodyTest extends AbstractBodyTest { testFiles.addAll(loadFiles("/foo-xml-test/yang")); final var modelContext = YangParserTestUtils.parseYangFiles(testFiles); - CONT_PATH = new DataPostPath(DatabindContext.ofModel(modelContext), - Inference.ofDataTreePath(modelContext, CONT_QNAME), YangInstanceIdentifier.of(CONT_QNAME)); - EMPTY_PATH = new DataPostPath(DatabindContext.ofModel(modelContext), - Inference.ofDataTreePath(modelContext), YangInstanceIdentifier.of()); + final var contPath = YangInstanceIdentifier.of(CONT_QNAME); + final var databind = DatabindContext.ofModel(modelContext); + final var nodeAndStack = databind.schemaTree().enterPath(contPath).orElseThrow(); + CONT_PATH = new DatabindPath.Data(databind, nodeAndStack.stack().toInference(), contPath, nodeAndStack.node()); + EMPTY_PATH = new DatabindPath.Data(DatabindContext.ofModel(modelContext)); } @Test -- 2.36.6