Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / util / RestDocgenUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.netconf.sal.rest.doc.util;
9
10 import java.net.URI;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.Map;
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;
20
21 public final class RestDocgenUtil {
22
23     private RestDocgenUtil() {
24     }
25
26     private static final Map<URI, Map<Optional<Revision>, Module>> NAMESPACE_AND_REVISION_TO_MODULE = new HashMap<>();
27
28     /**
29      * Resolve path argument name for {@code node}.
30      *
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.
34      *
35      * @return name of {@code node}
36      */
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();
41
42         QName parentQName = null;
43         if (it.hasNext()) {
44             parentQName = it.next();
45         }
46         if (isEqualNamespaceAndRevision(parentQName, nodeQName)) {
47             return node.getQName().getLocalName();
48         } else {
49             return resolveFullNameFromNode(node, schemaContext);
50         }
51     }
52
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();
57
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);
62         }
63         Module module = revisionToModule.get(revision);
64         if (module == null) {
65             module = schemaContext.findModule(namespace, revision).orElse(null);
66             revisionToModule.put(revision, module);
67         }
68         if (module != null) {
69             return module.getName() + ":" + node.getQName().getLocalName();
70         }
71         return node.getQName().getLocalName();
72     }
73
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();
79         } else {
80             return resolveFullNameFromNode(node, schemaContext);
81         }
82     }
83
84     private static boolean isEqualNamespaceAndRevision(final QName parentQName, final QName nodeQName) {
85         if (parentQName == null) {
86             return nodeQName == null;
87         }
88         return parentQName.getNamespace().equals(nodeQName.getNamespace())
89                 && parentQName.getRevision().equals(nodeQName.getRevision());
90     }
91
92 }