Turn ApiPath into a record 63/111163/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 1 Apr 2024 21:31:21 +0000 (23:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 1 Apr 2024 21:41:10 +0000 (23:41 +0200)
Expose the canonical constructor and adjust callers to use ApiPath.of().

JIRA: NETCONF-1264
Change-Id: I74f0e17b15d363f096cb7d4a369519a7ba879e7a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
protocol/restconf-api/src/main/java/org/opendaylight/restconf/api/ApiPath.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/server/spi/ApiPathNormalizer.java

index 635e397cec520c811c4b2dd287e42176921d6eb3..9c4c41c76676cf59117d2ba105d837d894d261bd 100644 (file)
@@ -22,6 +22,7 @@ import java.io.ObjectOutputStream;
 import java.io.ObjectStreamException;
 import java.text.ParseException;
 import java.util.HexFormat;
+import java.util.List;
 import java.util.Objects;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -36,7 +37,7 @@ import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
  * as a series of {@link Step}s.
  */
 @NonNullByDefault
-public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
+public record ApiPath(ImmutableList<Step> steps) implements HierarchicalIdentifier<ApiPath> {
     @java.io.Serial
     private static final long serialVersionUID = 1L;
 
@@ -173,10 +174,8 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
 
     private static final ApiPath EMPTY = new ApiPath(ImmutableList.of());
 
-    private final ImmutableList<Step> steps;
-
-    private ApiPath(final ImmutableList<Step> steps) {
-        this.steps = requireNonNull(steps);
+    public ApiPath {
+        requireNonNull(steps);
     }
 
     /**
@@ -188,6 +187,10 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
         return EMPTY;
     }
 
+    public static ApiPath of(final List<Step> steps) {
+        return steps.isEmpty() ? EMPTY : new ApiPath(ImmutableList.copyOf(steps));
+    }
+
     /**
      * Parse an {@link ApiPath} from a raw Request URI fragment or another source. The string is expected to contain
      * percent-encoded bytes. Any sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid
@@ -279,13 +282,7 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
 
     public ApiPath subPath(final int fromIndex, final int toIndex) {
         final var subList = steps.subList(fromIndex, toIndex);
-        if (subList == steps) {
-            return this;
-        } else if (subList.isEmpty()) {
-            return EMPTY;
-        } else {
-            return new ApiPath(subList);
-        }
+        return subList == steps ? this : of(subList);
     }
 
     @Override
@@ -341,7 +338,6 @@ public final class ApiPath implements HierarchicalIdentifier<ApiPath> {
     }
 
     private static ApiPath parseString(final ApiPathParser parser, final String str) throws ParseException {
-        final var steps = parser.parseSteps(str);
-        return steps.isEmpty() ? EMPTY : new ApiPath(steps);
+        return of(parser.parseSteps(str));
     }
 }
index 6f277134e233afa30f61dfc24b38ec5ad90ab3e7..cb175b55d4fcd267f77e721f27b1386535161474 100644 (file)
@@ -92,7 +92,7 @@ import org.opendaylight.restconf.server.api.PatchBody;
 import org.opendaylight.restconf.server.api.ResourceBody;
 import org.opendaylight.restconf.server.spi.ApiPathNormalizer;
 import org.opendaylight.restconf.server.spi.ApiPathNormalizer.InstanceReference;
-import org.opendaylight.restconf.server.spi.ApiPathNormalizer.OperationPath;
+import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Action;
 import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Data;
 import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Rpc;
 import org.opendaylight.restconf.server.spi.OperationInput;
@@ -1289,7 +1289,7 @@ public abstract class RestconfStrategy {
 
     public @NonNull RestconfFuture<OperationsPostResult> operationsPOST(final URI restconfURI, final ApiPath apiPath,
             final OperationInputBody body) {
-        final OperationPath.Rpc path;
+        final Rpc path;
         try {
             path = pathNormalizer.normalizeRpcPath(apiPath);
         } catch (RestconfDocumentedException e) {
@@ -1421,7 +1421,7 @@ public abstract class RestconfStrategy {
                     resourceBody, queryParameters);
             }
         }
-        if (path instanceof OperationPath.Action actionPath) {
+        if (path instanceof Action actionPath) {
             try (var inputBody = body.toOperationInput()) {
                 return dataInvokePOST(actionPath, inputBody);
             }
@@ -1460,8 +1460,7 @@ public abstract class RestconfStrategy {
         return ret;
     }
 
-    private @NonNull RestconfFuture<InvokeOperation> dataInvokePOST(final OperationPath.Action path,
-            final OperationInputBody body) {
+    private @NonNull RestconfFuture<InvokeOperation> dataInvokePOST(final Action path, final OperationInputBody body) {
         final var inference = path.inference();
         final ContainerNode input;
         try {
@@ -1493,7 +1492,7 @@ public abstract class RestconfStrategy {
      * @return {@link DOMActionResult}
      */
     private static RestconfFuture<DOMActionResult> dataInvokePOST(final DOMActionService actionService,
-            final OperationPath.Action path, final @NonNull ContainerNode input) {
+            final Action path, final @NonNull ContainerNode input) {
         final var ret = new SettableRestconfFuture<DOMActionResult>();
 
         Futures.addCallback(actionService.invokeAction(
index 3a7acd415ee7d1f1687d2b0c92b28eafafcfcded..d2841c43430f770fba5a4ceeb85a3786cdc8b593 100644 (file)
@@ -22,6 +22,7 @@ import org.opendaylight.restconf.api.ApiPath.ListInstance;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.Insert.PointNormalizer;
 import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Action;
 import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Data;
 import org.opendaylight.restconf.server.spi.ApiPathNormalizer.Path.Rpc;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
@@ -232,7 +233,7 @@ public final class ApiPathNormalizer implements PointNormalizer {
             final var stack = SchemaInferenceStack.of(modelContext);
             final var stmt = stack.enterSchemaTree(rpc.argument());
             verify(rpc.equals(stmt), "Expecting %s, inferred %s", rpc, stmt);
-            return new OperationPath.Rpc(stack.toInference(), rpc);
+            return new Rpc(stack.toInference(), rpc);
         }
 
         final var stack = SchemaInferenceStack.of(modelContext);
@@ -259,7 +260,7 @@ public final class ApiPathNormalizer implements PointNormalizer {
                     final var actionStmt = action.asEffectiveStatement();
                     verify(actionStmt.equals(stmt), "Expecting %s, inferred %s", actionStmt, stmt);
 
-                    return new OperationPath.Action(stack.toInference(), YangInstanceIdentifier.of(path), actionStmt);
+                    return new Action(stack.toInference(), YangInstanceIdentifier.of(path), actionStmt);
                 }
             }
 
@@ -386,7 +387,7 @@ public final class ApiPathNormalizer implements PointNormalizer {
         if (path instanceof Data dataPath) {
             return dataPath;
         }
-        if (path instanceof OperationPath.Action actionPath) {
+        if (path instanceof Action actionPath) {
             return actionPath;
         }
         throw new RestconfDocumentedException("Unexpected path " + path, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);