2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.databind;
10 import static com.google.common.base.Verify.verify;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.text.ParseException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.restconf.api.ApiPath;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.patch.PatchContext;
19 import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
20 import org.opendaylight.restconf.server.api.DataPatchPath;
21 import org.opendaylight.restconf.server.spi.ApiPathNormalizer;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.patch.rev170222.yang.patch.yang.patch.Edit.Operation;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 public abstract sealed class PatchBody extends AbstractBody permits JsonPatchBody, XmlPatchBody {
31 PatchBody(final InputStream inputStream) {
35 public final @NonNull PatchContext toPatchContext(final @NonNull DataPatchPath path) throws IOException {
36 try (var is = acquireStream()) {
37 return toPatchContext(path, is);
41 abstract @NonNull PatchContext toPatchContext(@NonNull DataPatchPath path, @NonNull InputStream inputStream)
44 static final YangInstanceIdentifier parsePatchTarget(final DataPatchPath path, final String target) {
45 final var urlPath = path.instance();
46 if (target.equals("/")) {
47 verify(!urlPath.isEmpty(),
48 "target resource of URI must not be a datastore resource when target is '/'");
52 // FIXME: NETCONF-1157: these two operations should really be a single ApiPathNormalizer step -- but for that
53 // we need to switch to ApiPath forms
54 final var databind = path.databind();
55 final String targetUrl;
56 if (urlPath.isEmpty()) {
57 targetUrl = target.startsWith("/") ? target.substring(1) : target;
59 targetUrl = new YangInstanceIdentifierSerializer(databind).serializePath(urlPath) + target;
63 return new ApiPathNormalizer(databind).normalizeDataPath(ApiPath.parse(targetUrl)).instance();
64 } catch (ParseException | RestconfDocumentedException e) {
65 throw new RestconfDocumentedException("Failed to parse target " + target,
66 ErrorType.RPC, ErrorTag.MALFORMED_MESSAGE, e);
71 * Not all patch operations support value node. Check if operation requires value or not.
73 * @param operation Patch edit operation
74 * @return true if operation requires value, false otherwise
76 static final boolean requiresValue(final Operation operation) {
77 return switch (operation) {
78 case Create, Insert, Merge, Replace -> true;
79 case Delete, Move, Remove -> false;