blob: 489ac5c37badf854d483171a0779e7fb3f74e3d8 [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;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import java.util.Set;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import org.onosproject.yangutils.datamodel.CollisionDetector;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053023import org.onosproject.yangutils.datamodel.ResolvableType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053024import org.onosproject.yangutils.datamodel.YangIfFeature;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053025import org.onosproject.yangutils.datamodel.YangBase;
26import org.onosproject.yangutils.datamodel.YangIdentityRef;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053027import org.onosproject.yangutils.datamodel.YangLeaf;
28import org.onosproject.yangutils.datamodel.YangLeafList;
janani be18b5342016-07-13 21:06:41 +053029import org.onosproject.yangutils.datamodel.YangLeafRef;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053030import org.onosproject.yangutils.datamodel.YangLeavesHolder;
31import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053032import org.onosproject.yangutils.datamodel.YangReferenceResolver;
33import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053035import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053036import org.onosproject.yangutils.datamodel.YangUses;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053038
39/**
Bharat saraswald9822e92016-04-05 15:13:44 +053040 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053041 */
42public final class DataModelUtils {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053043
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053044 /**
45 * Creates a new data model tree utility.
46 */
47 private DataModelUtils() {
48 }
49
50 /**
51 * Detects the colliding identifier name in a given YANG node and its child.
52 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053053 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053054 * @param dataType type of YANG node asking for detecting collision
55 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053056 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053057 */
58 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
59 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053060 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
61 detectCollidingForUsesGrouping(identifierName, dataType, node);
62 } else {
63 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053064 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053065 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
66 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
67 }
68 node = node.getChild();
69 while (node != null) {
70 Parsable parsable = (Parsable) node;
71 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053072 && parsable.getYangConstructType() != YangConstructType.USES_DATA
73 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053074 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
75 }
76 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053077 }
78 }
janani b4e53f9b2016-04-26 18:49:20 +053079 }
80
81 /**
82 * Detects colliding of uses and grouping only with uses and grouping respectively.
83 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053084 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053085 * @param dataType type of YANG node asking for detecting collision
86 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053087 * @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
Bharat saraswal33dfa012016-05-17 19:59:16 +053096 && 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 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530106 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530107 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530108 * @throws DataModelException a violation of data model rules
109 */
janani b4e53f9b2016-04-26 18:49:20 +0530110 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530111 throws DataModelException {
112
janani b4e53f9b2016-04-26 18:49:20 +0530113 if (listOfLeaf == null) {
114 return;
115 }
116 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530117 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530118 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530120 }
121 }
122 }
123
124 /**
125 * Detects the colliding identifier name in a given leaf-list node.
126 *
janani b4e53f9b2016-04-26 18:49:20 +0530127 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530128 * @param identifierName name for which collision detection is to be 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 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148 * @param resolutionInfo information about the YANG construct which has to 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
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530165 if (resolutionInfo.getEntityToResolveInfo()
166 .getEntityToResolve() instanceof YangType) {
167 resolutionNode.addToResolutionList(resolutionInfo,
168 ResolvableType.YANG_DERIVED_DATA_TYPE);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530169 } else if (resolutionInfo.getEntityToResolveInfo()
170 .getEntityToResolve() instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530171 resolutionNode.addToResolutionList(resolutionInfo,
172 ResolvableType.YANG_USES);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530173 } else if (resolutionInfo.getEntityToResolveInfo()
174 .getEntityToResolve() instanceof YangIfFeature) {
175 resolutionNode.addToResolutionList(resolutionInfo,
176 ResolvableType.YANG_IF_FEATURE);
janani be18b5342016-07-13 21:06:41 +0530177 } else if (resolutionInfo.getEntityToResolveInfo()
178 .getEntityToResolve() instanceof YangLeafRef) {
179 resolutionNode.addToResolutionList(resolutionInfo,
180 ResolvableType.YANG_LEAFREF);
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530181 } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) {
182 resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE);
183 } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) {
184 resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530185 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530186 }
187
janani b4e53f9b2016-04-26 18:49:20 +0530188 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530189 * Resolve linking for a resolution list.
190 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530191 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530192 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530193 * @throws DataModelException a violation of data model rules
194 */
195 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530196 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530197 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530198
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530199 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530200 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
201 }
202 }
203
204 /**
205 * Links type/uses referring to typedef/uses of inter YANG file.
206 *
207 * @param resolutionList resolution list for which linking to be done
208 * @param dataModelRootNode module/sub-module node
209 * @throws DataModelException a violation of data model rules
210 */
211 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530212 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530213 throws DataModelException {
214 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530215 * Run through the resolution list, find type/uses referring to inter
216 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530217 */
218 for (YangResolutionInfo resolutionInfo : resolutionList) {
219 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530220 }
221 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530222
223 /**
224 * Checks if there is any rpc defined in the module or sub-module.
225 *
226 * @param rootNode root node of the data model
227 * @return status of rpc's existence
228 */
229 public static boolean isRpcChildNodePresent(YangNode rootNode) {
230 YangNode childNode = rootNode.getChild();
231 while (childNode != null) {
232 if (childNode instanceof YangRpc) {
233 return true;
234 }
235 childNode = childNode.getNextSibling();
236 }
237 return false;
238 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530239
240 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530241 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530242 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530243 * @param yangNodeSet YANG node set
244 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530245 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530246 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530247 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530248 /*
249 * Run through the YANG files to see which YANG file matches the
250 * referred node name.
251 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530252 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530253 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530254 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530255 }
256 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530257 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530258 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530259
260 /**
261 * Returns the contained data model parent node.
262 *
263 * @param currentNode current node which parent contained node is required
264 * @return parent node in which the current node is an attribute
265 */
266 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
267
268 /*
269 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
270 * need to check if this needs to be updated for
271 * choice/case/augment/grouping
272 */
273 return currentNode.getParent();
274 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530275}