Bug 8351: Enforce check-style rules for restconf - sal-rest-docgen
[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.Date;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
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;
19
20 public class RestDocgenUtil {
21
22     private RestDocgenUtil() {
23     }
24
25     private static final Map<URI, Map<Date, Module>> NAMESPACE_AND_REVISION_TO_MODULE = new HashMap<>();
26
27     /**
28      * Resolve path argument name for {@code node}.
29      *
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.
33      *
34      * @return name of {@code node}
35      */
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();
40
41         QName parentQName = null;
42         if (it.hasNext()) {
43             parentQName = it.next();
44         }
45         if (isEqualNamespaceAndRevision(parentQName, nodeQName)) {
46             return node.getQName().getLocalName();
47         } else {
48             return resolveFullNameFromNode(node, schemaContext);
49         }
50     }
51
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();
56
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);
61         }
62         Module module = revisionToModule.get(revision);
63         if (module == null) {
64             module = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
65             revisionToModule.put(revision, module);
66         }
67         if (module != null) {
68             return module.getName() + ":" + node.getQName().getLocalName();
69         }
70         return node.getQName().getLocalName();
71     }
72
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();
78         } else {
79             return resolveFullNameFromNode(node, schemaContext);
80         }
81     }
82
83     private static boolean isEqualNamespaceAndRevision(final QName parentQName, final QName nodeQName) {
84         if (parentQName == null) {
85             if (nodeQName == null) {
86                 return true;
87             }
88             return false;
89         }
90         return parentQName.getNamespace().equals(nodeQName.getNamespace())
91                 && parentQName.getRevision().equals(nodeQName.getRevision());
92     }
93
94 }