Remove AbstractNormalizedNodeBodyReader
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / JsonOperationInputBody.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 com.google.gson.JsonParseException;
11 import com.google.gson.stream.JsonReader;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.nio.charset.StandardCharsets;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.yangtools.yang.common.ErrorTag;
18 import org.opendaylight.yangtools.yang.common.ErrorType;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
21 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public final class JsonOperationInputBody extends OperationInputBody {
27     private static final Logger LOG = LoggerFactory.getLogger(JsonOperationInputBody.class);
28
29     public JsonOperationInputBody(final InputStream inputStream) {
30         super(inputStream);
31     }
32
33     @Override
34     void streamTo(final InputStream inputStream, final Inference inference, final NormalizedNodeStreamWriter writer)
35             throws IOException {
36         try {
37             JsonParserStream.create(writer,
38                 JSONCodecFactorySupplier.RFC7951.getShared(inference.getEffectiveModelContext()), inference)
39                 .parse(new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)));
40         } catch (JsonParseException e) {
41             LOG.debug("Error parsing JSON input", e);
42             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
43                     ErrorTag.MALFORMED_MESSAGE, e);
44         }
45     }
46 }