66aa42b2a886d3ef5fb89e9a4fa158a4cc54a9ae
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / 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.impl.leafref;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
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;
18 import java.util.Set;
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.TypedDataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
30
31 class LeafRefContextTreeBuilder {
32     private final List<LeafRefContext> leafRefs = new LinkedList<>();
33     private final SchemaContext schemaContext;
34
35     LeafRefContextTreeBuilder(final SchemaContext schemaContext) {
36         this.schemaContext = schemaContext;
37     }
38
39     public LeafRefContext buildLeafRefContextTree() throws IOException,
40             LeafRefYangSyntaxErrorException {
41         final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(schemaContext.getQName(),
42             schemaContext.getPath(), schemaContext);
43
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(
49                         childNode, module);
50
51                 if (childLeafRefContext.hasReferencingChild()
52                         || childLeafRefContext.isReferencing()) {
53                     rootBuilder.addReferencingChild(childLeafRefContext,
54                             childLeafRefContext.getNodeName());
55                 }
56             }
57         }
58
59         for (final Module module : modules) {
60             final Collection<DataSchemaNode> childNodes = module.getChildNodes();
61             for (final DataSchemaNode childNode : childNodes) {
62                 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
63                         childNode, module);
64
65                 if (childLeafRefContext.hasReferencedChild()
66                         || childLeafRefContext.isReferenced()) {
67                     rootBuilder.addReferencedByChild(childLeafRefContext,
68                             childLeafRefContext.getNodeName());
69                 }
70             }
71         }
72
73         // FIXME: it might be useful to merge these subtrees (i.e. referencing
74         // and referencedBy subtree)
75
76         return rootBuilder.build();
77     }
78
79     private LeafRefContext buildLeafRefContextReferencingTree(
80             final DataSchemaNode node, final Module currentModule) throws IOException,
81             LeafRefYangSyntaxErrorException {
82
83         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
84                 node.getQName(), node.getPath(), schemaContext);
85
86         if (node instanceof DataNodeContainer) {
87             final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
88             final Collection<DataSchemaNode> childNodes = dataNodeContainer
89                     .getChildNodes();
90
91             for (final DataSchemaNode childNode : childNodes) {
92                 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
93                         childNode, currentModule);
94
95                 if (childLeafRefContext.hasReferencingChild()
96                         || childLeafRefContext.isReferencing()) {
97                     currentLeafRefContextBuilder.addReferencingChild(
98                             childLeafRefContext,
99                             childLeafRefContext.getNodeName());
100                 }
101             }
102         } else if (node instanceof ChoiceSchemaNode) {
103
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);
109
110                 if (childLeafRefContext.hasReferencingChild()
111                         || childLeafRefContext.isReferencing()) {
112                     currentLeafRefContextBuilder.addReferencingChild(
113                             childLeafRefContext,
114                             childLeafRefContext.getNodeName());
115                 }
116             }
117
118         } else if (node instanceof TypedDataSchemaNode) {
119             final TypeDefinition<?> type = ((TypedDataSchemaNode) node).getType();
120
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();
125
126                 currentLeafRefContextBuilder.setLeafRefTargetPathString(leafRefPathString);
127                 currentLeafRefContextBuilder.setReferencing(true);
128
129                 final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(schemaContext,
130                         checkNotNull(getBaseTypeModule(leafrefType), "Unable to find base module for leafref %s", node),
131                         node);
132
133                 final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPathSourceToSchemaPath(
134                     new ByteArrayInputStream(leafRefPathString.getBytes(StandardCharsets.UTF_8)));
135
136                 currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath);
137
138                 final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build();
139                 leafRefs.add(currentLeafRefContext);
140                 return currentLeafRefContext;
141             }
142         }
143
144         return currentLeafRefContextBuilder.build();
145     }
146
147     private Module getBaseTypeModule(final LeafrefTypeDefinition leafrefType) {
148         /*
149          * Find the first definition of supplied leafref type and return the
150          * module which contains this definition.
151          */
152         LeafrefTypeDefinition baseLeafRefType = leafrefType;
153         while (baseLeafRefType.getBaseType() != null) {
154             baseLeafRefType = baseLeafRefType.getBaseType();
155         }
156         return schemaContext.findModule(baseLeafRefType.getQName().getModule()).orElse(null);
157     }
158
159     private LeafRefContext buildLeafRefContextReferencedByTree(
160             final DataSchemaNode node, final Module currentModule) throws IOException,
161             LeafRefYangSyntaxErrorException {
162
163         final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
164                 node.getQName(), node.getPath(), schemaContext);
165
166         if (node instanceof DataNodeContainer) {
167             final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
168             final Collection<DataSchemaNode> childNodes = dataNodeContainer
169                     .getChildNodes();
170
171             for (final DataSchemaNode childNode : childNodes) {
172                 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
173                         childNode, currentModule);
174
175                 if (childLeafRefContext.hasReferencedChild()
176                         || childLeafRefContext.isReferenced()) {
177                     currentLeafRefContextBuilder.addReferencedByChild(
178                             childLeafRefContext,
179                             childLeafRefContext.getNodeName());
180                 }
181             }
182         } else if (node instanceof ChoiceSchemaNode) {
183             for (final ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
184                 final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(caseNode, currentModule);
185
186                 if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
187                     currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
188                         childLeafRefContext.getNodeName());
189                 }
190             }
191
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);
198                 }
199             }
200         }
201
202         return currentLeafRefContextBuilder.build();
203     }
204
205     private List<LeafRefContext> getLeafRefsFor(final DataSchemaNode node,
206             final Module module) {
207         final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(
208                 node.getPath(), module);
209
210         final List<LeafRefContext> foundLeafRefs = new LinkedList<>();
211
212         for (final LeafRefContext leafref : leafRefs) {
213             final LeafRefPath leafRefTargetPath = leafref
214                     .getAbsoluteLeafRefTargetPath();
215             if (leafRefTargetPath.equals(nodeXPath)) {
216                 foundLeafRefs.add(leafref);
217             }
218         }
219
220         return foundLeafRefs;
221     }
222
223 }