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.impl.leafref;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.nio.charset.StandardCharsets;
15 import java.util.Collection;
16 import java.util.LinkedList;
17 import java.util.List;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
31 class LeafRefContextTreeBuilder {
32 private final List<LeafRefContext> leafRefs = new LinkedList<>();
33 private final SchemaContext schemaContext;
35 LeafRefContextTreeBuilder(final SchemaContext schemaContext) {
36 this.schemaContext = schemaContext;
39 public LeafRefContext buildLeafRefContextTree() throws IOException,
40 LeafRefYangSyntaxErrorException {
41 final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(schemaContext.getQName(),
42 schemaContext.getPath(), schemaContext);
44 final Set<Module> modules = schemaContext.getModules();
45 for (final Module module : modules) {
46 final Collection<DataSchemaNode> childNodes = module.getChildNodes();
47 for (final DataSchemaNode childNode : childNodes) {
48 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
51 if (childLeafRefContext.hasReferencingChild()
52 || childLeafRefContext.isReferencing()) {
53 rootBuilder.addReferencingChild(childLeafRefContext,
54 childLeafRefContext.getNodeName());
59 for (final Module module : modules) {
60 final Collection<DataSchemaNode> childNodes = module.getChildNodes();
61 for (final DataSchemaNode childNode : childNodes) {
62 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
65 if (childLeafRefContext.hasReferencedChild()
66 || childLeafRefContext.isReferenced()) {
67 rootBuilder.addReferencedByChild(childLeafRefContext,
68 childLeafRefContext.getNodeName());
73 // FIXME: it might be useful to merge these subtrees (i.e. referencing
74 // and referencedBy subtree)
76 return rootBuilder.build();
79 private LeafRefContext buildLeafRefContextReferencingTree(
80 final DataSchemaNode node, final Module currentModule) throws IOException,
81 LeafRefYangSyntaxErrorException {
83 final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
84 node.getQName(), node.getPath(), schemaContext);
86 if (node instanceof DataNodeContainer) {
87 final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
88 final Collection<DataSchemaNode> childNodes = dataNodeContainer
91 for (final DataSchemaNode childNode : childNodes) {
92 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
93 childNode, currentModule);
95 if (childLeafRefContext.hasReferencingChild()
96 || childLeafRefContext.isReferencing()) {
97 currentLeafRefContextBuilder.addReferencingChild(
99 childLeafRefContext.getNodeName());
102 } else if (node instanceof ChoiceSchemaNode) {
104 final ChoiceSchemaNode choice = (ChoiceSchemaNode) node;
105 // :FIXME choice without case
106 for (final ChoiceCaseNode caseNode : choice.getCases().values()) {
107 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
108 caseNode, currentModule);
110 if (childLeafRefContext.hasReferencingChild()
111 || childLeafRefContext.isReferencing()) {
112 currentLeafRefContextBuilder.addReferencingChild(
114 childLeafRefContext.getNodeName());
118 } else if (node instanceof TypedSchemaNode) {
119 final TypeDefinition<?> type = ((TypedSchemaNode) node).getType();
121 // FIXME: fix case when type is e.g. typedef -> typedef -> leafref
122 if (type instanceof LeafrefTypeDefinition) {
123 final LeafrefTypeDefinition leafrefType = (LeafrefTypeDefinition) type;
124 final String leafRefPathString = leafrefType.getPathStatement().toString();
126 currentLeafRefContextBuilder.setLeafRefTargetPathString(leafRefPathString);
127 currentLeafRefContextBuilder.setReferencing(true);
129 final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(schemaContext,
130 checkNotNull(getBaseTypeModule(leafrefType), "Unable to find base module for leafref %s", node),
133 final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPathSourceToSchemaPath(
134 new ByteArrayInputStream(leafRefPathString.getBytes(StandardCharsets.UTF_8)));
136 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
138 final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
139 leafRefs.add(currentLeafRefContext);
140 return currentLeafRefContext;
144 return currentLeafRefContextBuilder.build();
147 private Module getBaseTypeModule(final LeafrefTypeDefinition leafrefType) {
149 * Find the first definition of supplied leafref type and return the
150 * module which contains this definition.
152 LeafrefTypeDefinition baseLeafRefType = leafrefType;
153 while (baseLeafRefType.getBaseType() != null) {
154 baseLeafRefType = baseLeafRefType.getBaseType();
156 return schemaContext.findModule(baseLeafRefType.getQName().getModule()).orElse(null);
159 private LeafRefContext buildLeafRefContextReferencedByTree(
160 final DataSchemaNode node, final Module currentModule) throws IOException,
161 LeafRefYangSyntaxErrorException {
163 final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
164 node.getQName(), node.getPath(), schemaContext);
166 if (node instanceof DataNodeContainer) {
167 final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
168 final Collection<DataSchemaNode> childNodes = dataNodeContainer
171 for (final DataSchemaNode childNode : childNodes) {
172 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
173 childNode, currentModule);
175 if (childLeafRefContext.hasReferencedChild()
176 || childLeafRefContext.isReferenced()) {
177 currentLeafRefContextBuilder.addReferencedByChild(
179 childLeafRefContext.getNodeName());
182 } else if (node instanceof ChoiceSchemaNode) {
183 for (final ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
184 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(caseNode, currentModule);
186 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
187 currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
188 childLeafRefContext.getNodeName());
192 } else if (node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode) {
193 final List<LeafRefContext> foundLeafRefs = getLeafRefsFor(node, currentModule);
194 if (!foundLeafRefs.isEmpty()) {
195 currentLeafRefContextBuilder.setReferencedBy(true);
196 for (final LeafRefContext leafRef : foundLeafRefs) {
197 currentLeafRefContextBuilder.addReferencedByLeafRefCtx(leafRef.getNodeName(), leafRef);
202 return currentLeafRefContextBuilder.build();
205 private List<LeafRefContext> getLeafRefsFor(final DataSchemaNode node,
206 final Module module) {
207 final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(
208 node.getPath(), module);
210 final List<LeafRefContext> foundLeafRefs = new LinkedList<>();
212 for (final LeafRefContext leafref : leafRefs) {
213 final LeafRefPath leafRefTargetPath = leafref
214 .getAbsoluteLeafRefTargetPath();
215 if (leafRefTargetPath.equals(nodeXPath)) {
216 foundLeafRefs.add(leafref);
220 return foundLeafRefs;