blob: 659773f6d338b1ad6eaa8dacee5975d2f4bb3436 [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;
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 Agrawal95b416c2016-06-07 14:00:26 +053028import org.onosproject.yangutils.datamodel.YangReferenceResolver;
29import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053031import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053034
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035/**
Bharat saraswald9822e92016-04-05 15:13:44 +053036 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037 */
38public final class DataModelUtils {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053039 /**
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 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053048 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053049 * @param dataType type of YANG node asking for detecting collision
50 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053051 * @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 {
janani b4e53f9b2016-04-26 18:49:20 +053055 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
56 detectCollidingForUsesGrouping(identifierName, dataType, node);
57 } else {
58 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053059 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053060 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
61 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
62 }
63 node = node.getChild();
64 while (node != null) {
65 Parsable parsable = (Parsable) node;
66 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053067 && parsable.getYangConstructType() != YangConstructType.USES_DATA
68 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053069 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
70 }
71 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053072 }
73 }
janani b4e53f9b2016-04-26 18:49:20 +053074 }
75
76 /**
77 * Detects colliding of uses and grouping only with uses and grouping respectively.
78 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053079 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053080 * @param dataType type of YANG node asking for detecting collision
81 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053082 * @throws DataModelException a violation of data model rules
83 */
84 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
85 throws DataModelException {
86
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053087 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053088 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053089 Parsable parsable = (Parsable) node;
90 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053091 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
93 }
94 node = node.getNextSibling();
95 }
96 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053097
98 /**
99 * Detects the colliding identifier name in a given leaf node.
100 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530101 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530102 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530103 * @throws DataModelException a violation of data model rules
104 */
janani b4e53f9b2016-04-26 18:49:20 +0530105 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 throws DataModelException {
107
janani b4e53f9b2016-04-26 18:49:20 +0530108 if (listOfLeaf == null) {
109 return;
110 }
111 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530112 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530113 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530114 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530115 }
116 }
117 }
118
119 /**
120 * Detects the colliding identifier name in a given leaf-list node.
121 *
janani b4e53f9b2016-04-26 18:49:20 +0530122 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530123 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530124 * @throws DataModelException a violation of data model rules
125 */
janani b4e53f9b2016-04-26 18:49:20 +0530126 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 throws DataModelException {
128
janani b4e53f9b2016-04-26 18:49:20 +0530129 if (listOfLeafList == null) {
130 return;
131 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530132 for (YangLeafList leafList : listOfLeafList) {
133 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530134 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530135 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530136 }
137 }
138 }
139
140 /**
141 * Add a resolution information.
142 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530143 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530144 * @throws DataModelException a violation of data model rules
145 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530146 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
147 throws DataModelException {
148
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530149 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530150 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
151 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530153 curNode = curNode.getParent();
154 if (curNode == null) {
155 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
156 }
157 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530158 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530159
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530160 if (resolutionInfo.getEntityToResolveInfo()
161 .getEntityToResolve() instanceof YangType) {
162 resolutionNode.addToResolutionList(resolutionInfo,
163 ResolvableType.YANG_DERIVED_DATA_TYPE);
164 } else {
165 resolutionNode.addToResolutionList(resolutionInfo,
166 ResolvableType.YANG_USES);
167 }
168
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530169 }
170
janani b4e53f9b2016-04-26 18:49:20 +0530171 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530172 * Resolve linking for a resolution list.
173 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530174 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530175 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530176 * @throws DataModelException a violation of data model rules
177 */
178 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530179 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530180 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530181
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530182 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530183 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
184 }
185 }
186
187 /**
188 * Links type/uses referring to typedef/uses of inter YANG file.
189 *
190 * @param resolutionList resolution list for which linking to be done
191 * @param dataModelRootNode module/sub-module node
192 * @throws DataModelException a violation of data model rules
193 */
194 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530195 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530196 throws DataModelException {
197 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530198 * Run through the resolution list, find type/uses referring to inter
199 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530200 */
201 for (YangResolutionInfo resolutionInfo : resolutionList) {
202 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530203 }
204 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530205
206 /**
207 * Checks if there is any rpc defined in the module or sub-module.
208 *
209 * @param rootNode root node of the data model
210 * @return status of rpc's existence
211 */
212 public static boolean isRpcChildNodePresent(YangNode rootNode) {
213 YangNode childNode = rootNode.getChild();
214 while (childNode != null) {
215 if (childNode instanceof YangRpc) {
216 return true;
217 }
218 childNode = childNode.getNextSibling();
219 }
220 return false;
221 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530222
223 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530224 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530225 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530226 * @param yangNodeSet YANG node set
227 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530228 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530229 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530230 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530231 /*
232 * Run through the YANG files to see which YANG file matches the
233 * referred node name.
234 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530235 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530236 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530237 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530238 }
239 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530240 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530241 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530242
243 /**
244 * Returns the contained data model parent node.
245 *
246 * @param currentNode current node which parent contained node is required
247 * @return parent node in which the current node is an attribute
248 */
249 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
250
251 /*
252 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
253 * need to check if this needs to be updated for
254 * choice/case/augment/grouping
255 */
256 return currentNode.getParent();
257 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530258}