Rework body formatting wiring
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / OperationsBody.java
1 /*
2  * Copyright (c) 2024 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.spi;
9
10 import static java.util.Objects.requireNonNull;
11
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.FormatParameters;
19 import org.opendaylight.restconf.api.FormattableBody;
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;
23
24 /**
25  * A {@link FormattableBody} representing either all of {@link OperationsResource} or a single RPC within it.
26  */
27 abstract sealed class OperationsBody extends FormattableBody permits AllOperations, OneOperation {
28     private final EffectiveModelContext modelContext;
29
30     OperationsBody(final EffectiveModelContext modelContext) {
31         this.modelContext = requireNonNull(modelContext);
32     }
33
34     @Override
35     public final void formatToJSON(final FormatParameters format, final OutputStream out) throws IOException {
36         try (var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
37             formatToJSON(writer);
38         }
39     }
40
41     abstract void formatToJSON(@NonNull Writer out) throws IOException;
42
43     @Override
44     public final void formatToXML(final FormatParameters format, final OutputStream out) throws IOException {
45         try (var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
46             formatToXML(writer);
47         }
48     }
49
50     abstract void formatToXML(@NonNull Writer out) throws IOException;
51
52     final @NonNull String jsonPrefix(final QNameModule namespace) {
53         return modelContext.findModuleStatement(namespace).orElseThrow().argument().getLocalName();
54     }
55
56     static final void appendJSON(final Writer out, final String prefix, final QName rpc) throws IOException {
57         out.write('"');
58         out.write(prefix);
59         out.write(':');
60         out.write(rpc.getLocalName());
61         out.write("\" : [null]");
62     }
63
64     static final void appendXML(final Writer out, final QName rpc) throws IOException {
65         out.write('<');
66         out.write(rpc.getLocalName());
67         out.write(' ');
68         out.write("xmlns=\"");
69         out.write(rpc.getNamespace().toString());
70         out.write("\"/>");
71     }
72 }