Introduce restconf.server.api.HttpGetResource
[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.restconf.api.query.PrettyPrintParam;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24
25 /**
26  * A {@link FormattableBody} representing either all of {@link OperationsResource} or a single RPC within it.
27  */
28 abstract sealed class OperationsBody extends FormattableBody permits AllOperations, OneOperation {
29     private final EffectiveModelContext modelContext;
30
31     OperationsBody(final EffectiveModelContext modelContext) {
32         super(() -> PrettyPrintParam.TRUE);
33         this.modelContext = requireNonNull(modelContext);
34     }
35
36     @Override
37     protected final void formatToJSON(final OutputStream out, final FormatParameters format) throws IOException {
38         try (var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
39             formatToJSON(writer);
40         }
41     }
42
43     abstract void formatToJSON(@NonNull Writer out) throws IOException;
44
45     @Override
46     protected final void formatToXML(final OutputStream out, final FormatParameters format) throws IOException {
47         try (var writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
48             formatToXML(writer);
49         }
50     }
51
52     abstract void formatToXML(@NonNull Writer out) throws IOException;
53
54     final @NonNull String jsonPrefix(final QNameModule namespace) {
55         return modelContext.findModuleStatement(namespace).orElseThrow().argument().getLocalName();
56     }
57
58     static final void appendJSON(final Writer out, final String prefix, final QName rpc) throws IOException {
59         out.write('"');
60         out.write(prefix);
61         out.write(':');
62         out.write(rpc.getLocalName());
63         out.write("\" : [null]");
64     }
65
66     static final void appendXML(final Writer out, final QName rpc) throws IOException {
67         out.write('<');
68         out.write(rpc.getLocalName());
69         out.write(' ');
70         out.write("xmlns=\"");
71         out.write(rpc.getNamespace().toString());
72         out.write("\"/>");
73     }
74 }