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 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;
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;
23 final class AllOperations extends OperationsBody {
24 private final ImmutableSetMultimap<QNameModule, QName> rpcs;
26 AllOperations(final EffectiveModelContext modelContext, final ImmutableSetMultimap<QNameModule, QName> rpcs) {
28 this.rpcs = requireNonNull(rpcs);
32 void formatToJSON(final Writer out) throws IOException {
35 "ietf-restconf:operations" : {\
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))
43 var entry = entryIt.next();
44 var nameIt = entry.getValue().iterator();
47 appendJSON(out, entry.getKey(), nameIt.next());
48 if (nameIt.hasNext()) {
53 if (entryIt.hasNext()) {
55 entry = entryIt.next();
56 nameIt = entry.getValue().iterator();
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\"");
77 for (var rpc : rpcs.asMap().entrySet().stream()
78 .sorted(Comparator.comparing(Entry::getKey))
79 .flatMap(entry -> entry.getValue().stream())
80 .toArray(QName[]::new)) {
84 out.write("\n</operations>");
88 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
89 return helper.add("rpcs", rpcs);