24ea9f2409f8f917437b58441af121a749775128
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / PatchBody.java
1 /*
2  * Copyright (c) 2023 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.nb.rfc8040.databind;
9
10 import static com.google.common.base.Verify.verify;
11
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;
26
27 /**
28  * A YANG Patch body.
29  */
30 public abstract sealed class PatchBody extends AbstractBody permits JsonPatchBody, XmlPatchBody {
31     PatchBody(final InputStream inputStream) {
32         super(inputStream);
33     }
34
35     public final @NonNull PatchContext toPatchContext(final @NonNull DataPatchPath path) throws IOException {
36         try (var is = acquireStream()) {
37             return toPatchContext(path, is);
38         }
39     }
40
41     abstract @NonNull PatchContext toPatchContext(@NonNull DataPatchPath path, @NonNull InputStream inputStream)
42         throws IOException;
43
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 '/'");
49             return urlPath;
50         }
51
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;
58         } else {
59             targetUrl = new YangInstanceIdentifierSerializer(databind).serializePath(urlPath) + target;
60         }
61
62         try {
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);
67         }
68     }
69
70     /**
71      * Not all patch operations support value node. Check if operation requires value or not.
72      *
73      * @param operation Patch edit operation
74      * @return true if operation requires value, false otherwise
75      */
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;
80         };
81     }
82 }