e20520369a0fe1933a1e827118cd9bd82979b3a7
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / ResourceBody.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 java.io.IOException;
11 import java.io.InputStream;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
14 import org.opendaylight.restconf.server.api.DataPutPath;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.restconf.restconf.Data;
16 import org.opendaylight.yangtools.yang.common.ErrorTag;
17 import org.opendaylight.yangtools.yang.common.ErrorType;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The body of a resource identified in the request URL, i.e. a {@code PUT} or a plain {@code PATCH} request on RESTCONF
31  * data service.
32  */
33 public abstract sealed class ResourceBody extends AbstractBody permits JsonResourceBody, XmlResourceBody {
34     private static final Logger LOG = LoggerFactory.getLogger(ResourceBody.class);
35     private static final NodeIdentifier DATA_NID = NodeIdentifier.create(Data.QNAME);
36
37     ResourceBody(final InputStream inputStream) {
38         super(inputStream);
39     }
40
41     /**
42      * Acquire the {@link NormalizedNode} representation of this body.
43      *
44      * @param path A {@link YangInstanceIdentifier} corresponding to the body
45      * @throws RestconfDocumentedException if the body cannot be decoded or it does not match {@code path}
46      */
47     @SuppressWarnings("checkstyle:illegalCatch")
48     public @NonNull NormalizedNode toNormalizedNode(final @NonNull DataPutPath path) {
49         final var instance = path.instance();
50         final var expectedName = instance.isEmpty() ? DATA_NID : instance.getLastPathArgument();
51         final var holder = new NormalizationResultHolder();
52         try (var streamWriter = ImmutableNormalizedNodeStreamWriter.from(holder)) {
53             streamTo(path, expectedName, acquireStream(), streamWriter);
54         } catch (IOException e) {
55             LOG.debug("Error reading input", e);
56             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
57                     ErrorTag.MALFORMED_MESSAGE, e);
58         } catch (RestconfDocumentedException e) {
59             throw e;
60         } catch (RuntimeException e) {
61             throwIfYangError(e);
62             throw e;
63         }
64
65         final var parsedData = holder.getResult().data();
66         final NormalizedNode data;
67         if (parsedData instanceof MapNode map) {
68             // TODO: This is a weird special case: a PUT target cannot specify the entire map, but the body parser
69             //       always produces a single-entry map for entries. We need to undo that damage here.
70             data = map.body().iterator().next();
71         } else {
72             data = parsedData;
73         }
74
75         final var dataName = data.name();
76         if (!dataName.equals(expectedName)) {
77             throw new RestconfDocumentedException(
78                 "Payload name (" + dataName + ") is different from identifier name (" + expectedName + ")",
79                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
80         }
81
82         return data;
83     }
84
85     abstract void streamTo(@NonNull DataPutPath path, @NonNull PathArgument name, @NonNull InputStream inputStream,
86         @NonNull NormalizedNodeStreamWriter writer) throws IOException;
87 }