blob: 410e025ad01ab38ecf1c5106d8758171f70fc3fb [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
Bharat saraswalb1170bd2016-07-14 13:26:18 +053019import java.io.FileInputStream;
20import java.io.IOException;
21import java.io.ObjectInputStream;
22import java.util.ArrayList;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053023import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053024import java.util.Set;
Bharat saraswal96dfef02016-06-16 00:29:12 +053025
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053026import org.onosproject.yangutils.datamodel.CollisionDetector;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053027import org.onosproject.yangutils.datamodel.ResolvableType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053028import org.onosproject.yangutils.datamodel.YangIfFeature;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053029import org.onosproject.yangutils.datamodel.YangAugment;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053030import org.onosproject.yangutils.datamodel.YangBase;
31import org.onosproject.yangutils.datamodel.YangIdentityRef;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import org.onosproject.yangutils.datamodel.YangLeaf;
33import org.onosproject.yangutils.datamodel.YangLeafList;
janani be18b5342016-07-13 21:06:41 +053034import org.onosproject.yangutils.datamodel.YangLeafRef;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035import org.onosproject.yangutils.datamodel.YangLeavesHolder;
36import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053037import org.onosproject.yangutils.datamodel.YangReferenceResolver;
38import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053039import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053040import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053041import org.onosproject.yangutils.datamodel.YangUses;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053042import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053043
44/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053046 */
47public final class DataModelUtils {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053048
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053049 /**
50 * Creates a new data model tree utility.
51 */
52 private DataModelUtils() {
53 }
54
55 /**
56 * Detects the colliding identifier name in a given YANG node and its child.
57 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053058 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053059 * @param dataType type of YANG node asking for detecting collision
60 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053061 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053062 */
63 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
64 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053065 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
66 detectCollidingForUsesGrouping(identifierName, dataType, node);
67 } else {
68 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053069 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053070 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
71 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
72 }
73 node = node.getChild();
74 while (node != null) {
75 Parsable parsable = (Parsable) node;
76 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053077 && parsable.getYangConstructType() != YangConstructType.USES_DATA
78 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053079 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
80 }
81 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 }
83 }
janani b4e53f9b2016-04-26 18:49:20 +053084 }
85
86 /**
87 * Detects colliding of uses and grouping only with uses and grouping respectively.
88 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053089 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053090 * @param dataType type of YANG node asking for detecting collision
91 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053092 * @throws DataModelException a violation of data model rules
93 */
94 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
95 throws DataModelException {
96
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053097 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053098 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053099 Parsable parsable = (Parsable) node;
100 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +0530101 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530102 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
103 }
104 node = node.getNextSibling();
105 }
106 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107
108 /**
109 * Detects the colliding identifier name in a given leaf node.
110 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530111 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530112 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530113 * @throws DataModelException a violation of data model rules
114 */
janani b4e53f9b2016-04-26 18:49:20 +0530115 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530116 throws DataModelException {
117
janani b4e53f9b2016-04-26 18:49:20 +0530118 if (listOfLeaf == null) {
119 return;
120 }
121 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530122 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530123 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530124 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530125 }
126 }
127 }
128
129 /**
130 * Detects the colliding identifier name in a given leaf-list node.
131 *
janani b4e53f9b2016-04-26 18:49:20 +0530132 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530133 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530134 * @throws DataModelException a violation of data model rules
135 */
janani b4e53f9b2016-04-26 18:49:20 +0530136 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530137 throws DataModelException {
138
janani b4e53f9b2016-04-26 18:49:20 +0530139 if (listOfLeafList == null) {
140 return;
141 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 for (YangLeafList leafList : listOfLeafList) {
143 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530144 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530145 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530146 }
147 }
148 }
149
150 /**
151 * Add a resolution information.
152 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530153 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530154 * @throws DataModelException a violation of data model rules
155 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530156 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
157 throws DataModelException {
158
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530159 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530160 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
161 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530163 curNode = curNode.getParent();
164 if (curNode == null) {
165 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
166 }
167 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530168 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530169
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530170 if (resolutionInfo.getEntityToResolveInfo()
171 .getEntityToResolve() instanceof YangType) {
172 resolutionNode.addToResolutionList(resolutionInfo,
173 ResolvableType.YANG_DERIVED_DATA_TYPE);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530174 } else if (resolutionInfo.getEntityToResolveInfo()
175 .getEntityToResolve() instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530176 resolutionNode.addToResolutionList(resolutionInfo,
177 ResolvableType.YANG_USES);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530178 } else if (resolutionInfo.getEntityToResolveInfo()
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530179 .getEntityToResolve() instanceof YangAugment) {
180 resolutionNode.addToResolutionList(resolutionInfo,
181 ResolvableType.YANG_AUGMENT);
182 } else if (resolutionInfo.getEntityToResolveInfo()
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530183 .getEntityToResolve() instanceof YangIfFeature) {
184 resolutionNode.addToResolutionList(resolutionInfo,
185 ResolvableType.YANG_IF_FEATURE);
janani be18b5342016-07-13 21:06:41 +0530186 } else if (resolutionInfo.getEntityToResolveInfo()
187 .getEntityToResolve() instanceof YangLeafRef) {
188 resolutionNode.addToResolutionList(resolutionInfo,
189 ResolvableType.YANG_LEAFREF);
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530190 } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) {
191 resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE);
192 } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) {
193 resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530194 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530195 }
196
janani b4e53f9b2016-04-26 18:49:20 +0530197 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530198 * Resolve linking for a resolution list.
199 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530200 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530201 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530202 * @throws DataModelException a violation of data model rules
203 */
204 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530205 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530206 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530207
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530208 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530209 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
210 }
211 }
212
213 /**
214 * Links type/uses referring to typedef/uses of inter YANG file.
215 *
216 * @param resolutionList resolution list for which linking to be done
217 * @param dataModelRootNode module/sub-module node
218 * @throws DataModelException a violation of data model rules
219 */
220 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530221 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530222 throws DataModelException {
223 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530224 * Run through the resolution list, find type/uses referring to inter
225 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530226 */
227 for (YangResolutionInfo resolutionInfo : resolutionList) {
228 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530229 }
230 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530231
232 /**
233 * Checks if there is any rpc defined in the module or sub-module.
234 *
235 * @param rootNode root node of the data model
236 * @return status of rpc's existence
237 */
238 public static boolean isRpcChildNodePresent(YangNode rootNode) {
239 YangNode childNode = rootNode.getChild();
240 while (childNode != null) {
241 if (childNode instanceof YangRpc) {
242 return true;
243 }
244 childNode = childNode.getNextSibling();
245 }
246 return false;
247 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530248
249 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530250 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530251 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530252 * @param yangNodeSet YANG node set
253 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530254 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530255 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530256 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530257 /*
258 * Run through the YANG files to see which YANG file matches the
259 * referred node name.
260 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530261 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530262 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530263 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530264 }
265 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530266 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530267 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530268
269 /**
270 * Returns the contained data model parent node.
271 *
272 * @param currentNode current node which parent contained node is required
273 * @return parent node in which the current node is an attribute
274 */
275 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
276
277 /*
278 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
279 * need to check if this needs to be updated for
280 * choice/case/augment/grouping
281 */
282 return currentNode.getParent();
283 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530284
285 /**
286 * Returns de-serializes YANG data-model nodes.
287 *
288 * @param serializableInfoSet YANG file info set
289 * @return de-serializes YANG data-model nodes
290 * @throws IOException when fails do IO operations
291 */
292 public static List<YangNode> deSerializeDataModel(List<String> serializableInfoSet) throws IOException {
293
294 List<YangNode> nodes = new ArrayList<>();
295 for (String fileInfo : serializableInfoSet) {
296 YangNode node = null;
297 try {
298 FileInputStream fileInputStream = new FileInputStream(fileInfo);
299 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
300 node = (YangNode) objectInputStream.readObject();
301 nodes.add(node);
302 objectInputStream.close();
303 fileInputStream.close();
304 } catch (IOException | ClassNotFoundException e) {
305 throw new IOException(fileInfo + " not found.");
306 }
307 }
308 return nodes;
309 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530310}