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.server.api;
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;
28 * The body of a resource identified in the request URL, i.e. a {@code PUT} or a plain {@code PATCH} request on RESTCONF
31 public abstract sealed class ResourceBody extends AbstractBody permits JsonResourceBody, XmlResourceBody {
32 private static final Logger LOG = LoggerFactory.getLogger(ResourceBody.class);
33 private static final NodeIdentifier DATA_NID = NodeIdentifier.create(Data.QNAME);
35 ResourceBody(final InputStream inputStream) {
40 * Acquire the {@link NormalizedNode} representation of this body.
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}
45 @SuppressWarnings("checkstyle:illegalCatch")
46 public @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, acquireStream(), 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) {
58 } catch (RuntimeException e) {
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();
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);
83 abstract void streamTo(DatabindPath.@NonNull Data path, @NonNull PathArgument name,
84 @NonNull InputStream inputStream, @NonNull NormalizedNodeStreamWriter writer) throws IOException;