2 * Copyright (c) 2015 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.yangtools.yang.data.tree.leafref;
10 import java.util.Collection;
11 import java.util.LinkedList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.PathExpression;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
29 final class LeafRefContextTreeBuilder {
30 private final List<LeafRefContext> leafRefs = new LinkedList<>();
31 private final EffectiveModelContext schemaContext;
33 LeafRefContextTreeBuilder(final EffectiveModelContext schemaContext) {
34 this.schemaContext = schemaContext;
37 LeafRefContext buildLeafRefContextTree() throws LeafRefYangSyntaxErrorException {
38 final SchemaInferenceStack stack = SchemaInferenceStack.of(schemaContext);
39 final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(SchemaContext.NAME, SchemaPath.ROOT,
42 final Collection<? extends Module> modules = schemaContext.getModules();
43 for (final Module module : modules) {
44 for (final DataSchemaNode childNode : module.getChildNodes()) {
45 stack.enterSchemaTree(childNode.getQName());
46 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, stack);
48 if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
49 rootBuilder.addReferencingChild(childLeafRefContext, childLeafRefContext.getNodeName());
53 for (final Module module : modules) {
54 for (final DataSchemaNode childNode : module.getChildNodes()) {
55 stack.enterSchemaTree(childNode.getQName());
56 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(childNode, module,
59 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
60 rootBuilder.addReferencedByChild(childLeafRefContext, childLeafRefContext.getNodeName());
65 // FIXME: it might be useful to merge these subtrees (i.e. referencing
66 // and referencedBy subtree)
68 return rootBuilder.build();
71 private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node,
72 final SchemaInferenceStack stack) {
73 final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
74 stack.toSchemaPath(), schemaContext);
76 if (node instanceof DataNodeContainer container) {
77 for (final DataSchemaNode childNode : container.getChildNodes()) {
78 stack.enterSchemaTree(childNode.getQName());
79 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, stack);
81 if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
82 currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
83 childLeafRefContext.getNodeName());
86 } else if (node instanceof ChoiceSchemaNode choice) {
87 // :FIXME choice without case
88 for (final CaseSchemaNode caseNode : choice.getCases()) {
89 stack.enterSchemaTree(caseNode.getQName());
90 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(caseNode, stack);
92 if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
93 currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
94 childLeafRefContext.getNodeName());
98 } else if (node instanceof TypedDataSchemaNode typedNode) {
99 final TypeDefinition<?> type = typedNode.getType();
101 // FIXME: fix case when type is e.g. typedef -> typedef -> leafref
102 if (type instanceof LeafrefTypeDefinition leafrefType) {
103 final PathExpression path = leafrefType.getPathStatement();
104 final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(leafrefType, typedNode);
105 final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPath(path);
107 currentLeafRefContextBuilder.setLeafRefTargetPathString(path.getOriginalString());
108 currentLeafRefContextBuilder.setReferencing(true);
109 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
111 final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
112 leafRefs.add(currentLeafRefContext);
113 return currentLeafRefContext;
117 return currentLeafRefContextBuilder.build();
120 private LeafRefContext buildLeafRefContextReferencedByTree(final DataSchemaNode node, final Module currentModule,
121 final SchemaInferenceStack stack) {
122 final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
123 stack.toSchemaPath(), schemaContext);
124 if (node instanceof DataNodeContainer container) {
125 for (final DataSchemaNode childNode : container.getChildNodes()) {
126 stack.enterSchemaTree(childNode.getQName());
127 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(childNode,
128 currentModule, stack);
130 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
131 currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
132 childLeafRefContext.getNodeName());
135 } else if (node instanceof ChoiceSchemaNode choice) {
136 for (final CaseSchemaNode caseNode : choice.getCases()) {
137 stack.enterSchemaTree(caseNode.getQName());
138 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(caseNode, currentModule,
141 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
142 currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
143 childLeafRefContext.getNodeName());
146 } else if (node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode) {
147 final List<LeafRefContext> foundLeafRefs = getLeafRefsFor(currentModule, stack);
148 if (!foundLeafRefs.isEmpty()) {
149 currentLeafRefContextBuilder.setReferencedBy(true);
150 for (final LeafRefContext leafRef : foundLeafRefs) {
151 currentLeafRefContextBuilder.addReferencedByLeafRefCtx(leafRef.getNodeName(), leafRef);
156 return currentLeafRefContextBuilder.build();
159 private List<LeafRefContext> getLeafRefsFor(final Module module, final SchemaInferenceStack stack) {
160 final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(stack.toSchemaPath(), module);
161 final List<LeafRefContext> foundLeafRefs = new LinkedList<>();
162 for (final LeafRefContext leafref : leafRefs) {
163 final LeafRefPath leafRefTargetPath = leafref.getAbsoluteLeafRefTargetPath();
164 if (leafRefTargetPath.equals(nodeXPath)) {
165 foundLeafRefs.add(leafref);
169 return foundLeafRefs;