blob: 29d384b8f53e7cabb360cb39b5c5941e49825f62 [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;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053021
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import org.onosproject.yangutils.datamodel.CollisionDetector;
23import 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.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053028import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053030import org.onosproject.yangutils.linker.ResolvableType;
31import org.onosproject.yangutils.linker.YangReferenceResolver;
32import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
janani b4e53f9b2016-04-26 18:49:20 +053033import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama1db15562016-05-17 16:16:15 +053034import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035import org.onosproject.yangutils.utils.YangConstructType;
36
37/**
Bharat saraswald9822e92016-04-05 15:13:44 +053038 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053039 */
40public final class DataModelUtils {
41
42 /**
43 * Creates a new data model tree utility.
44 */
45 private DataModelUtils() {
46 }
47
48 /**
49 * Detects the colliding identifier name in a given YANG node and its child.
50 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053051 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053052 * @param dataType type of YANG node asking for detecting collision
53 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053054 * @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 {
janani b4e53f9b2016-04-26 18:49:20 +053058 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
59 detectCollidingForUsesGrouping(identifierName, dataType, node);
60 } else {
61 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053062 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053063 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
64 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
65 }
66 node = node.getChild();
67 while (node != null) {
68 Parsable parsable = (Parsable) node;
69 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053070 && parsable.getYangConstructType() != YangConstructType.USES_DATA
71 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053072 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
73 }
74 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053075 }
76 }
janani b4e53f9b2016-04-26 18:49:20 +053077 }
78
79 /**
80 * Detects colliding of uses and grouping only with uses and grouping respectively.
81 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053082 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053083 * @param dataType type of YANG node asking for detecting collision
84 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053085 * @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
Bharat saraswal33dfa012016-05-17 19:59:16 +053094 && 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 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530104 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530105 * @param identifierName name for which collision detection is to be 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
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530126 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 * @throws DataModelException a violation of data model rules
128 */
janani b4e53f9b2016-04-26 18:49:20 +0530129 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530130 throws DataModelException {
131
janani b4e53f9b2016-04-26 18:49:20 +0530132 if (listOfLeafList == null) {
133 return;
134 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530135 for (YangLeafList leafList : listOfLeafList) {
136 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530137 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530138 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530139 }
140 }
141 }
142
143 /**
144 * Add a resolution information.
145 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530146 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530147 * @throws DataModelException a violation of data model rules
148 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530149 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
150 throws DataModelException {
151
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530152 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530153 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
154 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530156 curNode = curNode.getParent();
157 if (curNode == null) {
158 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
159 }
160 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530161 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530162
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530163 if (resolutionInfo.getEntityToResolveInfo()
164 .getEntityToResolve() instanceof YangType) {
165 resolutionNode.addToResolutionList(resolutionInfo,
166 ResolvableType.YANG_DERIVED_DATA_TYPE);
167 } else {
168 resolutionNode.addToResolutionList(resolutionInfo,
169 ResolvableType.YANG_USES);
170 }
171
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530172 }
173
janani b4e53f9b2016-04-26 18:49:20 +0530174 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530175 * Resolve linking for a resolution list.
176 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530177 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530178 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530179 * @throws DataModelException a violation of data model rules
180 */
181 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530182 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530183 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530184
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530185 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530186 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
187 }
188 }
189
190 /**
191 * Links type/uses referring to typedef/uses of inter YANG file.
192 *
193 * @param resolutionList resolution list for which linking to be done
194 * @param dataModelRootNode module/sub-module node
195 * @throws DataModelException a violation of data model rules
196 */
197 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530198 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530199 throws DataModelException {
200 /*
201 * Run through the resolution list, find type/uses referring to
202 * inter file typedef/grouping, ask for linking.
203 */
204 for (YangResolutionInfo resolutionInfo : resolutionList) {
205 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530206 }
207 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530208
209 /**
210 * Checks if there is any rpc defined in the module or sub-module.
211 *
212 * @param rootNode root node of the data model
213 * @return status of rpc's existence
214 */
215 public static boolean isRpcChildNodePresent(YangNode rootNode) {
216 YangNode childNode = rootNode.getChild();
217 while (childNode != null) {
218 if (childNode instanceof YangRpc) {
219 return true;
220 }
221 childNode = childNode.getNextSibling();
222 }
223 return false;
224 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530225
226 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530227 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530228 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530229 * @param yangFileInfoSet YANG file info set
230 * @param refNodeName name of the node which is referred
231 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530232 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530233 public static YangNode findReferredNode(Set<YangFileInfo> yangFileInfoSet, String refNodeName) {
234 /*
235 * Run through the YANG files to see which YANG file matches the
236 * referred node name.
237 */
238 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
239 YangNode yangNode = yangFileInfo.getRootNode();
240 if (yangNode.getName().equals(refNodeName)) {
241 return yangFileInfo.getRootNode();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530242 }
243 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530244 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530245 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530246}