blob: c4609dcd14f7709f75306c84388f3184f4a028b2 [file] [log] [blame]
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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 Agrawal8e8770a2016-02-27 03:57:50 +053020import org.onosproject.yangutils.datamodel.CollisionDetector;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053021import org.onosproject.yangutils.datamodel.HasResolutionInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import org.onosproject.yangutils.datamodel.YangLeaf;
23import org.onosproject.yangutils.datamodel.YangLeafList;
24import org.onosproject.yangutils.datamodel.YangLeavesHolder;
25import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053026import org.onosproject.yangutils.datamodel.YangResolutionInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053027import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
28import org.onosproject.yangutils.utils.YangConstructType;
29
30/**
31 * Utilities for data model tree.
32 */
33public final class DataModelUtils {
34
35 /**
36 * Creates a new data model tree utility.
37 */
38 private DataModelUtils() {
39 }
40
41 /**
42 * Detects the colliding identifier name in a given YANG node and its child.
43 *
44 * @param identifierName name for which collision detection is to be
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053045 * checked
46 * @param dataType type of YANG node asking for detecting collision
47 * @param node instance of calling node
48 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053049 */
50 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
51 throws DataModelException {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053052 if (dataType == YangConstructType.LEAF_DATA) {
53 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
54 if (leavesHolder.getListOfLeaf() != null) {
55 detectCollidingLeaf(leavesHolder, identifierName);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053056 }
57 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053058 if (dataType == YangConstructType.LEAF_LIST_DATA) {
59 if (((YangLeavesHolder) node).getListOfLeafList() != null) {
60 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
61 detectCollidingLeafList(leavesHolder, identifierName);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053062 }
63 }
64 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053065 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053066 if (node instanceof CollisionDetector) {
67 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
68 }
69 node = node.getNextSibling();
70 }
71 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053072
73 /**
74 * Detects the colliding identifier name in a given leaf node.
75 *
76 * @param leavesHolder leaves node against which collision to be checked
77 * @param identifierName name for which collision detection is to be
78 * checked
79 * @throws DataModelException a violation of data model rules
80 */
81 private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName) throws
82 DataModelException {
83 for (YangLeaf leaf : leavesHolder.getListOfLeaf()) {
84 if (leaf.getLeafName().equals(identifierName)) {
85 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
86 + leaf.getLeafName() + "\"");
87 }
88 }
89 }
90
91 /**
92 * Detects the colliding identifier name in a given leaf-list node.
93 *
94 * @param leavesHolder leaves node against which collision to be checked
95 * @param identifierName name for which collision detection is to be
96 * checked
97 * @throws DataModelException a violation of data model rules
98 */
99 private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName) throws
100 DataModelException {
101 for (YangLeafList leafList : leavesHolder.getListOfLeafList()) {
102 if (leafList.getLeafName().equals(identifierName)) {
103 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
104 "list \"" + leafList.getLeafName() + "\"");
105 }
106 }
107 }
108
109 /**
110 * Add a resolution information.
111 *
112 * @param resolutionInfo information about the YANG construct which has to
113 * be resolved
114 * @throws DataModelException a violation of data model rules
115 */
116 public static void addResolutionInfo(YangResolutionInfo resolutionInfo) throws DataModelException {
117 /* get the module node to add maintain the list of nested reference */
118 YangNode curNode = resolutionInfo.getHolderOfEntityToResolve();
119 while (!(curNode instanceof HasResolutionInfo)) {
120 curNode = curNode.getParent();
121 if (curNode == null) {
122 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
123 }
124 }
125 HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode;
126 resolutionNode.addToResolutionList(resolutionInfo);
127 }
128
129 /**
130 * Resolve linking for a resolution list.
131 *
132 * @param resolutionList resolution list for which linking to be done
133 * @param resolutionInfoNode module/sub-module node
134 * @throws DataModelException a violation of data model rules
135 */
136 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
137 HasResolutionInfo resolutionInfoNode)
138 throws DataModelException {
139 for (YangResolutionInfo resolutionInfo : resolutionList) {
140 if (resolutionInfo.getPrefix() == null ||
141 resolutionInfo.getPrefix().equals(resolutionInfoNode.getPrefix())) {
142 resolutionInfo.resolveLinkingForResolutionInfo(resolutionInfoNode.getPrefix());
143 }
144 }
145 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530146}