2 * Copyright (c) 2014 Cisco Systems, Inc. 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.netconf.sal.rest.doc.util;
11 import java.util.HashMap;
12 import java.util.Iterator;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 public final class RestDocgenUtil {
23 private RestDocgenUtil() {
26 private static final Map<URI, Map<Optional<Revision>, Module>> NAMESPACE_AND_REVISION_TO_MODULE = new HashMap<>();
29 * Resolve path argument name for {@code node}.
31 * <p>The name can contain also prefix which consists of module name followed by colon. The module
32 * prefix is presented if namespace of {@code node} and its parent is different. In other cases
33 * only name of {@code node} is returned.
35 * @return name of {@code node}
37 public static String resolvePathArgumentsName(final SchemaNode node, final SchemaContext schemaContext) {
38 final Iterable<QName> schemaPath = node.getPath().getPathTowardsRoot();
39 final Iterator<QName> it = schemaPath.iterator();
40 final QName nodeQName = it.next();
42 QName parentQName = null;
44 parentQName = it.next();
46 if (isEqualNamespaceAndRevision(parentQName, nodeQName)) {
47 return node.getQName().getLocalName();
49 return resolveFullNameFromNode(node, schemaContext);
53 private static synchronized String resolveFullNameFromNode(final SchemaNode node,
54 final SchemaContext schemaContext) {
55 final URI namespace = node.getQName().getNamespace();
56 final Optional<Revision> revision = node.getQName().getRevision();
58 Map<Optional<Revision>, Module> revisionToModule =
59 NAMESPACE_AND_REVISION_TO_MODULE.computeIfAbsent(namespace, k -> new HashMap<>());
61 revisionToModule.computeIfAbsent(revision, k -> schemaContext.findModule(namespace, k).orElse(null));
63 return module.getName() + ":" + node.getQName().getLocalName();
65 return node.getQName().getLocalName();
68 public static String resolveNodesName(final SchemaNode node, final Module module,
69 final SchemaContext schemaContext) {
70 if (node.getQName().getNamespace().equals(module.getQNameModule().getNamespace())
71 && node.getQName().getRevision().equals(module.getQNameModule().getRevision())) {
72 return node.getQName().getLocalName();
74 return resolveFullNameFromNode(node, schemaContext);
78 private static boolean isEqualNamespaceAndRevision(final QName parentQName, final QName nodeQName) {
79 if (parentQName == null) {
80 return nodeQName == null;
82 return parentQName.getNamespace().equals(nodeQName.getNamespace())
83 && parentQName.getRevision().equals(nodeQName.getRevision());