Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / DefaultResourceContext.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.server.spi;
9
10 import org.eclipse.jdt.annotation.NonNullByDefault;
11 import org.opendaylight.restconf.api.ApiPath;
12 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
13 import org.opendaylight.restconf.server.api.DatabindPath.Data;
14 import org.opendaylight.restconf.server.api.PatchBody.ResourceContext;
15 import org.opendaylight.yangtools.yang.common.ErrorTag;
16 import org.opendaylight.yangtools.yang.common.ErrorType;
17
18 /**
19  * Default implementation of a {@link ResourceContext}.
20  */
21 @NonNullByDefault
22 public final class DefaultResourceContext extends ResourceContext {
23     public DefaultResourceContext(final Data path) {
24         super(path);
25     }
26
27     @Override
28     protected ResourceContext resolveRelative(final ApiPath apiPath) {
29         // If subResource is empty just return this resource
30         if (apiPath.isEmpty()) {
31             return this;
32         }
33
34         final var normalizer = new ApiPathNormalizer(path.databind());
35         final var urlPath = path.instance();
36         if (urlPath.isEmpty()) {
37             // URL indicates the datastore resource, let's just normalize targetPath
38             return new DefaultResourceContext(normalizer.normalizeDataPath(apiPath));
39         }
40
41         // Defer to normalizeSteps(), faking things a bit. Then check the result.
42         final var it = apiPath.steps().iterator();
43         final var resolved = normalizer.normalizeSteps(path.inference().toSchemaInferenceStack(), path.schema(),
44             urlPath.getPathArguments(), urlPath.getLastPathArgument().getNodeType().getModule(), it.next(), it);
45         if (resolved instanceof Data dataPath) {
46             return new DefaultResourceContext(dataPath);
47         }
48         throw new RestconfDocumentedException("Sub-resource '" + apiPath + "' resolves to non-data " + resolved,
49             ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
50     }
51 }