2 * Copyright (c) 2024 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.spi;
10 import static java.util.Objects.requireNonNull;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.io.Writer;
16 import java.nio.charset.StandardCharsets;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.restconf.api.FormattableBody;
19 import org.opendaylight.restconf.api.query.PrettyPrintParam;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 * A {@link FormattableBody} representing either all of {@link OperationsResource} or a single RPC within it.
27 abstract sealed class OperationsBody extends FormattableBody permits AllOperations, OneOperation {
28 private final EffectiveModelContext modelContext;
30 OperationsBody(final EffectiveModelContext modelContext) {
31 this.modelContext = requireNonNull(modelContext);
35 public final void formatToJSON(final PrettyPrintParam prettyPrint, final OutputStream out) throws IOException {
36 try (var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
41 abstract void formatToJSON(@NonNull Writer out) throws IOException;
44 public final void formatToXML(final PrettyPrintParam prettyPrint, final OutputStream out) throws IOException {
45 try (var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
50 abstract void formatToXML(@NonNull Writer out) throws IOException;
52 final @NonNull String jsonPrefix(final QNameModule namespace) {
53 return modelContext.findModuleStatement(namespace).orElseThrow().argument().getLocalName();
56 static final void appendJSON(final Writer out, final String prefix, final QName rpc) throws IOException {
60 out.write(rpc.getLocalName());
61 out.write("\" : [null]");
64 static final void appendXML(final Writer out, final QName rpc) throws IOException {
66 out.write(rpc.getLocalName());
68 out.write("xmlns=\"");
69 out.write(rpc.getNamespace().toString());