98bfc9be7c5426887db01cc81a3ef772d8b68924
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / leafref / LeafRefContextTreeBuilder.java
1 /*
2  * Copyright (c) 2015 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.yangtools.yang.data.tree.leafref;
9
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;
28
29 final class LeafRefContextTreeBuilder {
30     private final List<LeafRefContext> leafRefs = new LinkedList<>();
31     private final EffectiveModelContext schemaContext;
32
33     LeafRefContextTreeBuilder(final EffectiveModelContext schemaContext) {
34         this.schemaContext = schemaContext;
35     }
36
37     LeafRefContext buildLeafRefContextTree() throws LeafRefYangSyntaxErrorException {
38         final SchemaInferenceStack stack = SchemaInferenceStack.of(schemaContext);
39         final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(SchemaContext.NAME, SchemaPath.ROOT,
40             schemaContext);
41
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);
47                 stack.exit();
48                 if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
49                     rootBuilder.addReferencingChild(childLeafRefContext, childLeafRefContext.getNodeName());
50                 }
51             }
52         }
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,
57                         stack);
58                 stack.exit();
59                 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
60                     rootBuilder.addReferencedByChild(childLeafRefContext, childLeafRefContext.getNodeName());
61                 }
62             }
63         }
64
65         // FIXME: it might be useful to merge these subtrees (i.e. referencing
66         // and referencedBy subtree)
67
68         return rootBuilder.build();
69     }
70
71     private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node,
72             final SchemaInferenceStack stack) {
73         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
74             stack.toSchemaPath(), schemaContext);
75
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);
80                 stack.exit();
81                 if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
82                     currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
83                         childLeafRefContext.getNodeName());
84                 }
85             }
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);
91                 stack.exit();
92                 if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
93                     currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
94                         childLeafRefContext.getNodeName());
95                 }
96             }
97
98         } else if (node instanceof TypedDataSchemaNode typedNode) {
99             final TypeDefinition<?> type = typedNode.getType();
100
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);
106
107                 currentLeafRefContextBuilder.setLeafRefTargetPathString(path.getOriginalString());
108                 currentLeafRefContextBuilder.setReferencing(true);
109                 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
110
111                 final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
112                 leafRefs.add(currentLeafRefContext);
113                 return currentLeafRefContext;
114             }
115         }
116
117         return currentLeafRefContextBuilder.build();
118     }
119
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);
129                 stack.exit();
130                 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
131                     currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
132                         childLeafRefContext.getNodeName());
133                 }
134             }
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,
139                         stack);
140                 stack.exit();
141                 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
142                     currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
143                         childLeafRefContext.getNodeName());
144                 }
145             }
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);
152                 }
153             }
154         }
155
156         return currentLeafRefContextBuilder.build();
157     }
158
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);
166             }
167         }
168
169         return foundLeafRefs;
170     }
171 }