blob: 928e87bc641163f01faf7a2cdc8a23a994b6c3d4 [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
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053019import java.util.List;
Bharat saraswald9822e92016-04-05 15:13:44 +053020
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053021import org.onosproject.yangutils.datamodel.CollisionDetector;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053022import org.onosproject.yangutils.datamodel.YangReferenceResolver;
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;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053028import org.onosproject.yangutils.datamodel.YangResolutionInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
janani b4e53f9b2016-04-26 18:49:20 +053030import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031import org.onosproject.yangutils.utils.YangConstructType;
32
33/**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035 */
36public final class DataModelUtils {
37
38 /**
39 * Creates a new data model tree utility.
40 */
41 private DataModelUtils() {
42 }
43
44 /**
45 * Detects the colliding identifier name in a given YANG node and its child.
46 *
47 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053048 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053049 * @param dataType type of YANG node asking for detecting collision
50 * @param node instance of calling node
51 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053052 */
53 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
54 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +053055
janani b4e53f9b2016-04-26 18:49:20 +053056 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
57 detectCollidingForUsesGrouping(identifierName, dataType, node);
58 } else {
59 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053060 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053061 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
62 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
63 }
64 node = node.getChild();
65 while (node != null) {
66 Parsable parsable = (Parsable) node;
67 if (node instanceof CollisionDetector
68 && (parsable.getYangConstructType() != YangConstructType.USES_DATA)
69 && (parsable.getYangConstructType() != YangConstructType.GROUPING_DATA)) {
70 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
71 }
72 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053073 }
74 }
janani b4e53f9b2016-04-26 18:49:20 +053075 }
76
77 /**
78 * Detects colliding of uses and grouping only with uses and grouping respectively.
79 *
80 * @param identifierName name for which collision detection is to be
81 * checked
82 * @param dataType type of YANG node asking for detecting collision
83 * @param node node instance of calling node
84 * @throws DataModelException a violation of data model rules
85 */
86 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
87 throws DataModelException {
88
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053089 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053090 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053091 Parsable parsable = (Parsable) node;
92 if (node instanceof CollisionDetector
93 && (parsable.getYangConstructType() == dataType)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
95 }
96 node = node.getNextSibling();
97 }
98 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053099
100 /**
101 * Detects the colliding identifier name in a given leaf node.
102 *
janani b4e53f9b2016-04-26 18:49:20 +0530103 * @param listOfLeaf List of leaves to detect collision
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530104 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530105 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530106 * @throws DataModelException a violation of data model rules
107 */
janani b4e53f9b2016-04-26 18:49:20 +0530108 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 throws DataModelException {
110
janani b4e53f9b2016-04-26 18:49:20 +0530111 if (listOfLeaf == null) {
112 return;
113 }
114 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530116 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530117 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530118 }
119 }
120 }
121
122 /**
123 * Detects the colliding identifier name in a given leaf-list node.
124 *
janani b4e53f9b2016-04-26 18:49:20 +0530125 * @param listOfLeafList list of leaf-lists to detect collision
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530126 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530127 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530128 * @throws DataModelException a violation of data model rules
129 */
janani b4e53f9b2016-04-26 18:49:20 +0530130 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530131 throws DataModelException {
132
janani b4e53f9b2016-04-26 18:49:20 +0530133 if (listOfLeafList == null) {
134 return;
135 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 for (YangLeafList leafList : listOfLeafList) {
137 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530138 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530140 }
141 }
142 }
143
144 /**
145 * Add a resolution information.
146 *
147 * @param resolutionInfo information about the YANG construct which has to
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530148 * be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530149 * @throws DataModelException a violation of data model rules
150 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530151 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
152 throws DataModelException {
153
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530154 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530155 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
156 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530158 curNode = curNode.getParent();
159 if (curNode == null) {
160 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
161 }
162 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530164
165 if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(),
166 resolutionNode)) {
167 throw new DataModelException("The prefix used is not valid");
168 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530169 resolutionNode.addToResolutionList(resolutionInfo);
170 }
171
janani b4e53f9b2016-04-26 18:49:20 +0530172 /**
173 * Evaluates whether the prefix in uses/type is valid.
174 *
175 * @param entityPrefix prefix in the current module/sub-module
176 * @param resolutionNode uses/type node which has the prefix with it
177 * @return whether prefix is valid or not
178 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530179 private static boolean isPrefixValid(String entityPrefix, YangReferenceResolver resolutionNode) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530180 if (entityPrefix == null) {
181 return true;
182 }
183
184 if (resolutionNode.getPrefix().contentEquals(entityPrefix)) {
185 return true;
186 }
187
188 if (resolutionNode.getImportList() != null) {
189 for (YangImport importedInfo : resolutionNode.getImportList()) {
190 if (importedInfo.getPrefixId().contentEquals(entityPrefix)) {
191 return true;
192 }
193 }
194 }
195
196 if (resolutionNode.getIncludeList() != null) {
197 /**
198 * TODO: check if the prefix matches with the imported data
199
200 for (YangInclude includedInfo : resolutionNode.getIncludeList()) {
201 if (includedInfo.contentEquals(prefix)) {
202 return true;
203 }
204 }*/
205 }
206
207 return false;
208 }
209
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530210 /**
211 * Resolve linking for a resolution list.
212 *
213 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530214 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530215 * @throws DataModelException a violation of data model rules
216 */
217 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530218 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530219 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530220
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530221 for (YangResolutionInfo resolutionInfo : resolutionList) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530222 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix());
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530223 }
224 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530225}