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 = NAMESPACE_AND_REVISION_TO_MODULE.get(namespace);
59 if (revisionToModule == null) {
60 revisionToModule = new HashMap<>();
61 NAMESPACE_AND_REVISION_TO_MODULE.put(namespace, revisionToModule);
63 Module module = revisionToModule.get(revision);
65 module = schemaContext.findModule(namespace, revision).orElse(null);
66 revisionToModule.put(revision, module);
69 return module.getName() + ":" + node.getQName().getLocalName();
71 return node.getQName().getLocalName();
74 public static String resolveNodesName(final SchemaNode node, final Module module,
75 final SchemaContext schemaContext) {
76 if (node.getQName().getNamespace().equals(module.getQNameModule().getNamespace())
77 && node.getQName().getRevision().equals(module.getQNameModule().getRevision())) {
78 return node.getQName().getLocalName();
80 return resolveFullNameFromNode(node, schemaContext);
84 private static boolean isEqualNamespaceAndRevision(final QName parentQName, final QName nodeQName) {
85 if (parentQName == null) {
86 return nodeQName == null;
88 return parentQName.getNamespace().equals(nodeQName.getNamespace())
89 && parentQName.getRevision().equals(nodeQName.getRevision());