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