blob: eda18f3daa183db67ba863c1e0093c1408634127 [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 Sd4deb062016-04-15 18:08:57 +053022import org.onosproject.yangutils.datamodel.YangImport;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053023import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangLeafList;
25import org.onosproject.yangutils.datamodel.YangLeavesHolder;
26import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053027import org.onosproject.yangutils.datamodel.YangReferenceResolver;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053028import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053029import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053030import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
janani b4e53f9b2016-04-26 18:49:20 +053031import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import org.onosproject.yangutils.utils.YangConstructType;
33
34/**
Bharat saraswald9822e92016-04-05 15:13:44 +053035 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053036 */
37public final class DataModelUtils {
38
39 /**
40 * Creates a new data model tree utility.
41 */
42 private DataModelUtils() {
43 }
44
45 /**
46 * Detects the colliding identifier name in a given YANG node and its child.
47 *
48 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053049 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053050 * @param dataType type of YANG node asking for detecting collision
51 * @param node instance of calling node
52 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053053 */
54 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
55 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +053056
janani b4e53f9b2016-04-26 18:49:20 +053057 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
58 detectCollidingForUsesGrouping(identifierName, dataType, node);
59 } else {
60 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053061 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053062 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
63 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
64 }
65 node = node.getChild();
66 while (node != null) {
67 Parsable parsable = (Parsable) node;
68 if (node instanceof CollisionDetector
69 && (parsable.getYangConstructType() != YangConstructType.USES_DATA)
70 && (parsable.getYangConstructType() != YangConstructType.GROUPING_DATA)) {
71 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
72 }
73 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053074 }
75 }
janani b4e53f9b2016-04-26 18:49:20 +053076 }
77
78 /**
79 * Detects colliding of uses and grouping only with uses and grouping respectively.
80 *
81 * @param identifierName name for which collision detection is to be
82 * checked
83 * @param dataType type of YANG node asking for detecting collision
84 * @param node node instance of calling node
85 * @throws DataModelException a violation of data model rules
86 */
87 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
88 throws DataModelException {
89
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053090 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053091 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053092 Parsable parsable = (Parsable) node;
93 if (node instanceof CollisionDetector
94 && (parsable.getYangConstructType() == dataType)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
96 }
97 node = node.getNextSibling();
98 }
99 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530100
101 /**
102 * Detects the colliding identifier name in a given leaf node.
103 *
janani b4e53f9b2016-04-26 18:49:20 +0530104 * @param listOfLeaf List of leaves to detect collision
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530105 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530106 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107 * @throws DataModelException a violation of data model rules
108 */
janani b4e53f9b2016-04-26 18:49:20 +0530109 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530110 throws DataModelException {
111
janani b4e53f9b2016-04-26 18:49:20 +0530112 if (listOfLeaf == null) {
113 return;
114 }
115 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530117 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530119 }
120 }
121 }
122
123 /**
124 * Detects the colliding identifier name in a given leaf-list node.
125 *
janani b4e53f9b2016-04-26 18:49:20 +0530126 * @param listOfLeafList list of leaf-lists to detect collision
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530128 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 * @throws DataModelException a violation of data model rules
130 */
janani b4e53f9b2016-04-26 18:49:20 +0530131 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 throws DataModelException {
133
janani b4e53f9b2016-04-26 18:49:20 +0530134 if (listOfLeafList == null) {
135 return;
136 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530137 for (YangLeafList leafList : listOfLeafList) {
138 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530139 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530140 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530141 }
142 }
143 }
144
145 /**
146 * Add a resolution information.
147 *
148 * @param resolutionInfo information about the YANG construct which has to
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530149 * be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530150 * @throws DataModelException a violation of data model rules
151 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530152 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
153 throws DataModelException {
154
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530155 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530156 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
157 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530158 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530159 curNode = curNode.getParent();
160 if (curNode == null) {
161 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
162 }
163 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530165
166 if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(),
167 resolutionNode)) {
168 throw new DataModelException("The prefix used is not valid");
169 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530170 resolutionNode.addToResolutionList(resolutionInfo);
171 }
172
janani b4e53f9b2016-04-26 18:49:20 +0530173 /**
174 * Evaluates whether the prefix in uses/type is valid.
175 *
176 * @param entityPrefix prefix in the current module/sub-module
177 * @param resolutionNode uses/type node which has the prefix with it
178 * @return whether prefix is valid or not
179 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 private static boolean isPrefixValid(String entityPrefix, YangReferenceResolver resolutionNode) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530181 if (entityPrefix == null) {
182 return true;
183 }
184
185 if (resolutionNode.getPrefix().contentEquals(entityPrefix)) {
186 return true;
187 }
188
189 if (resolutionNode.getImportList() != null) {
190 for (YangImport importedInfo : resolutionNode.getImportList()) {
191 if (importedInfo.getPrefixId().contentEquals(entityPrefix)) {
192 return true;
193 }
194 }
195 }
196
197 if (resolutionNode.getIncludeList() != null) {
198 /**
199 * TODO: check if the prefix matches with the imported data
200
201 for (YangInclude includedInfo : resolutionNode.getIncludeList()) {
202 if (includedInfo.contentEquals(prefix)) {
203 return true;
204 }
205 }*/
206 }
207
208 return false;
209 }
210
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530211 /**
212 * Resolve linking for a resolution list.
213 *
214 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530215 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530216 * @throws DataModelException a violation of data model rules
217 */
218 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530219 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530220 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530221
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530222 for (YangResolutionInfo resolutionInfo : resolutionList) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530223 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix());
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530224 }
225 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530226
227 /**
228 * Checks if there is any rpc defined in the module or sub-module.
229 *
230 * @param rootNode root node of the data model
231 * @return status of rpc's existence
232 */
233 public static boolean isRpcChildNodePresent(YangNode rootNode) {
234 YangNode childNode = rootNode.getChild();
235 while (childNode != null) {
236 if (childNode instanceof YangRpc) {
237 return true;
238 }
239 childNode = childNode.getNextSibling();
240 }
241 return false;
242 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530243}