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.Date;
12 import java.util.HashMap;
13 import java.util.Iterator;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
20 public class RestDocgenUtil {
22 private RestDocgenUtil() {
25 private static final Map<URI, Map<Date, Module>> NAMESPACE_AND_REVISION_TO_MODULE = new HashMap<>();
28 * Resolve path argument name for {@code node}.
30 * <p>The name can contain also prefix which consists of module name followed by colon. The module
31 * prefix is presented if namespace of {@code node} and its parent is different. In other cases
32 * only name of {@code node} is returned.
34 * @return name of {@code node}
36 public static String resolvePathArgumentsName(final SchemaNode node, final SchemaContext schemaContext) {
37 final Iterable<QName> schemaPath = node.getPath().getPathTowardsRoot();
38 final Iterator<QName> it = schemaPath.iterator();
39 final QName nodeQName = it.next();
41 QName parentQName = null;
43 parentQName = it.next();
45 if (isEqualNamespaceAndRevision(parentQName, nodeQName)) {
46 return node.getQName().getLocalName();
48 return resolveFullNameFromNode(node, schemaContext);
52 private static synchronized String resolveFullNameFromNode(final SchemaNode node,
53 final SchemaContext schemaContext) {
54 final URI namespace = node.getQName().getNamespace();
55 final Date revision = node.getQName().getRevision();
57 Map<Date, Module> revisionToModule = NAMESPACE_AND_REVISION_TO_MODULE.get(namespace);
58 if (revisionToModule == null) {
59 revisionToModule = new HashMap<>();
60 NAMESPACE_AND_REVISION_TO_MODULE.put(namespace, revisionToModule);
62 Module module = revisionToModule.get(revision);
64 module = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
65 revisionToModule.put(revision, module);
68 return module.getName() + ":" + node.getQName().getLocalName();
70 return node.getQName().getLocalName();
73 public static String resolveNodesName(final SchemaNode node, final Module module,
74 final SchemaContext schemaContext) {
75 if (node.getQName().getNamespace().equals(module.getQNameModule().getNamespace())
76 && node.getQName().getRevision().equals(module.getQNameModule().getRevision())) {
77 return node.getQName().getLocalName();
79 return resolveFullNameFromNode(node, schemaContext);
83 private static boolean isEqualNamespaceAndRevision(final QName parentQName, final QName nodeQName) {
84 if (parentQName == null) {
85 return nodeQName == null;
87 return parentQName.getNamespace().equals(nodeQName.getNamespace())
88 && parentQName.getRevision().equals(nodeQName.getRevision());