Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / AllOperations.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 com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.collect.ImmutableSetMultimap;
14 import java.io.IOException;
15 import java.io.Writer;
16 import java.util.Comparator;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22
23 final class AllOperations extends OperationsBody {
24     private final ImmutableSetMultimap<QNameModule, QName> rpcs;
25
26     AllOperations(final EffectiveModelContext modelContext, final ImmutableSetMultimap<QNameModule, QName> rpcs) {
27         super(modelContext);
28         this.rpcs = requireNonNull(rpcs);
29     }
30
31     @Override
32     void formatToJSON(final Writer out) throws IOException {
33         out.write("""
34             {
35               "ietf-restconf:operations" : {\
36             """);
37
38         if (!rpcs.isEmpty()) {
39             final var entryIt = rpcs.asMap().entrySet().stream()
40                 .map(entry -> Map.entry(jsonPrefix(entry.getKey()), entry.getValue()))
41                 .sorted(Comparator.comparing(Entry::getKey))
42                 .iterator();
43             var entry = entryIt.next();
44             var nameIt = entry.getValue().iterator();
45             while (true) {
46                 out.write("\n    ");
47                 appendJSON(out, entry.getKey(), nameIt.next());
48                 if (nameIt.hasNext()) {
49                     out.write(',');
50                     continue;
51                 }
52
53                 if (entryIt.hasNext()) {
54                     out.write(',');
55                     entry = entryIt.next();
56                     nameIt = entry.getValue().iterator();
57                     continue;
58                 }
59
60                 break;
61             }
62         }
63
64         out.write("\n  }\n}");
65     }
66
67     @Override
68     void formatToXML(final Writer out) throws IOException {
69         // Header with namespace declarations for each module
70         out.write("<operations xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"");
71         if (rpcs.isEmpty()) {
72             out.write("/>");
73             return;
74         }
75
76         out.write('>');
77         for (var rpc : rpcs.asMap().entrySet().stream()
78                 .sorted(Comparator.comparing(Entry::getKey))
79                 .flatMap(entry -> entry.getValue().stream())
80                 .toArray(QName[]::new)) {
81             out.write("\n  ");
82             appendXML(out, rpc);
83         }
84         out.write("\n</operations>");
85     }
86
87     @Override
88     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
89         return helper.add("rpcs", rpcs);
90     }
91 }