blob: e243d5ed6483d23be3d5449cf5d32926df2753a9 [file] [log] [blame]
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.datamodel.utils;
18
Vidyashree Rama1db15562016-05-17 16:16:15 +053019import java.util.Iterator;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053020import java.util.List;
Bharat saraswald9822e92016-04-05 15:13:44 +053021
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import org.onosproject.yangutils.datamodel.CollisionDetector;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053023import org.onosproject.yangutils.datamodel.YangImport;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import org.onosproject.yangutils.datamodel.YangReferenceResolver;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053029import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
janani b4e53f9b2016-04-26 18:49:20 +053032import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama1db15562016-05-17 16:16:15 +053033import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import org.onosproject.yangutils.utils.YangConstructType;
35
36/**
Bharat saraswald9822e92016-04-05 15:13:44 +053037 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053038 */
39public final class DataModelUtils {
40
41 /**
42 * Creates a new data model tree utility.
43 */
44 private DataModelUtils() {
45 }
46
47 /**
48 * Detects the colliding identifier name in a given YANG node and its child.
49 *
50 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053051 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053052 * @param dataType type of YANG node asking for detecting collision
53 * @param node instance of calling node
54 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053055 */
56 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
57 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +053058
janani b4e53f9b2016-04-26 18:49:20 +053059 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
60 detectCollidingForUsesGrouping(identifierName, dataType, node);
61 } else {
62 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053063 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053064 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
65 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
66 }
67 node = node.getChild();
68 while (node != null) {
69 Parsable parsable = (Parsable) node;
70 if (node instanceof CollisionDetector
71 && (parsable.getYangConstructType() != YangConstructType.USES_DATA)
72 && (parsable.getYangConstructType() != YangConstructType.GROUPING_DATA)) {
73 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
74 }
75 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053076 }
77 }
janani b4e53f9b2016-04-26 18:49:20 +053078 }
79
80 /**
81 * Detects colliding of uses and grouping only with uses and grouping respectively.
82 *
83 * @param identifierName name for which collision detection is to be
84 * checked
85 * @param dataType type of YANG node asking for detecting collision
86 * @param node node instance of calling node
87 * @throws DataModelException a violation of data model rules
88 */
89 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
90 throws DataModelException {
91
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053093 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053094 Parsable parsable = (Parsable) node;
95 if (node instanceof CollisionDetector
96 && (parsable.getYangConstructType() == dataType)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053097 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
98 }
99 node = node.getNextSibling();
100 }
101 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530102
103 /**
104 * Detects the colliding identifier name in a given leaf node.
105 *
janani b4e53f9b2016-04-26 18:49:20 +0530106 * @param listOfLeaf List of leaves to detect collision
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530108 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530109 * @throws DataModelException a violation of data model rules
110 */
janani b4e53f9b2016-04-26 18:49:20 +0530111 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530112 throws DataModelException {
113
janani b4e53f9b2016-04-26 18:49:20 +0530114 if (listOfLeaf == null) {
115 return;
116 }
117 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530119 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530120 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530121 }
122 }
123 }
124
125 /**
126 * Detects the colliding identifier name in a given leaf-list node.
127 *
janani b4e53f9b2016-04-26 18:49:20 +0530128 * @param listOfLeafList list of leaf-lists to detect collision
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530130 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530131 * @throws DataModelException a violation of data model rules
132 */
janani b4e53f9b2016-04-26 18:49:20 +0530133 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530134 throws DataModelException {
135
janani b4e53f9b2016-04-26 18:49:20 +0530136 if (listOfLeafList == null) {
137 return;
138 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139 for (YangLeafList leafList : listOfLeafList) {
140 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530141 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530143 }
144 }
145 }
146
147 /**
148 * Add a resolution information.
149 *
150 * @param resolutionInfo information about the YANG construct which has to
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530151 * be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530152 * @throws DataModelException a violation of data model rules
153 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530154 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
155 throws DataModelException {
156
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530157 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530158 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
159 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530160 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530161 curNode = curNode.getParent();
162 if (curNode == null) {
163 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
164 }
165 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530166 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530167
168 if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(),
169 resolutionNode)) {
170 throw new DataModelException("The prefix used is not valid");
171 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530172 resolutionNode.addToResolutionList(resolutionInfo);
173 }
174
janani b4e53f9b2016-04-26 18:49:20 +0530175 /**
176 * Evaluates whether the prefix in uses/type is valid.
177 *
178 * @param entityPrefix prefix in the current module/sub-module
179 * @param resolutionNode uses/type node which has the prefix with it
180 * @return whether prefix is valid or not
181 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530182 private static boolean isPrefixValid(String entityPrefix, YangReferenceResolver resolutionNode) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530183 if (entityPrefix == null) {
184 return true;
185 }
186
187 if (resolutionNode.getPrefix().contentEquals(entityPrefix)) {
188 return true;
189 }
190
191 if (resolutionNode.getImportList() != null) {
192 for (YangImport importedInfo : resolutionNode.getImportList()) {
193 if (importedInfo.getPrefixId().contentEquals(entityPrefix)) {
194 return true;
195 }
196 }
197 }
198
199 if (resolutionNode.getIncludeList() != null) {
200 /**
201 * TODO: check if the prefix matches with the imported data
202
203 for (YangInclude includedInfo : resolutionNode.getIncludeList()) {
204 if (includedInfo.contentEquals(prefix)) {
205 return true;
206 }
207 }*/
208 }
209
210 return false;
211 }
212
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530213 /**
214 * Resolve linking for a resolution list.
215 *
216 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530217 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530218 * @throws DataModelException a violation of data model rules
219 */
220 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530221 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530222 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530223
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530224 for (YangResolutionInfo resolutionInfo : resolutionList) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530225 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix());
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530226 }
227 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530228
229 /**
230 * Checks if there is any rpc defined in the module or sub-module.
231 *
232 * @param rootNode root node of the data model
233 * @return status of rpc's existence
234 */
235 public static boolean isRpcChildNodePresent(YangNode rootNode) {
236 YangNode childNode = rootNode.getChild();
237 while (childNode != null) {
238 if (childNode instanceof YangRpc) {
239 return true;
240 }
241 childNode = childNode.getNextSibling();
242 }
243 return false;
244 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530245
246 /**
247 * Returns module's data model node to which sub-module belongs to.
248 *
249 * @param yangFileInfo YANG file information
250 * @param belongsToModuleName name of the module to which sub-module belongs to
251 * @return module node to which sub-module belongs to
252 * @throws DataModelException when belongs to module node is not found
253 */
254 public static YangNode findBelongsToModuleNode(List<YangFileInfo> yangFileInfo,
255 String belongsToModuleName) throws DataModelException {
256 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
257 while (yangFileIterator.hasNext()) {
258 YangFileInfo yangFile = yangFileIterator.next();
259 YangNode yangNode = yangFile.getRootNode();
260 if (yangNode.getName().equals(belongsToModuleName)) {
261 return yangNode;
262 }
263 }
264 throw new DataModelException("YANG file error : Module " + belongsToModuleName + " to which sub-module " +
265 "belongs to is not found.");
266 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530267}