Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 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 | package org.onosproject.yangutils.datamodel; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 19 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 21 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 22 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil; |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 24 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 25 | /*- |
| 26 | * Reference RFC 6020. |
| 27 | * |
| 28 | * The "uses" statement is used to reference a "grouping" definition. It takes |
| 29 | * one argument, which is the name of the grouping. |
| 30 | * |
| 31 | * The effect of a "uses" reference to a grouping is that the nodes defined by |
| 32 | * the grouping are copied into the current schema tree, and then updated |
| 33 | * according to the "refine" and "augment" statements. |
| 34 | * |
| 35 | * The identifiers defined in the grouping are not bound to a namespace until |
| 36 | * the contents of the grouping are added to the schema tree via a "uses" |
| 37 | * statement that does not appear inside a "grouping" statement, at which point |
| 38 | * they are bound to the namespace of the current module. |
| 39 | * |
| 40 | * The uses's sub-statements |
| 41 | * |
| 42 | * +--------------+---------+-------------+------------------+ |
| 43 | * | substatement | section | cardinality |data model mapping| |
| 44 | * +--------------+---------+-------------+------------------+ |
| 45 | * | augment | 7.15 | 0..1 | -child nodes | |
| 46 | * | description | 7.19.3 | 0..1 | -string | |
| 47 | * | if-feature | 7.18.2 | 0..n | -TODO | |
| 48 | * | refine | 7.12.2 | 0..1 | -TODO | |
| 49 | * | reference | 7.19.4 | 0..1 | -string | |
| 50 | * | status | 7.19.2 | 0..1 | -YangStatus | |
| 51 | * | when | 7.19.5 | 0..1 | -TODO | |
| 52 | * +--------------+---------+-------------+------------------+ |
| 53 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 54 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 55 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 56 | * Represents data model node to maintain information defined in YANG uses. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 57 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 58 | public class YangUses |
| 59 | extends YangNode |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 60 | implements YangCommonInfo, Parsable, Resolvable, CollisionDetector { |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 63 | * YANG node identifier. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 64 | */ |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 65 | private YangNodeIdentifier nodeIdentifier; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 66 | |
| 67 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 68 | * Referred group. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 69 | */ |
| 70 | private YangGrouping refGroup; |
| 71 | |
| 72 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 73 | * Description of YANG uses. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 74 | */ |
| 75 | private String description; |
| 76 | |
| 77 | /** |
| 78 | * YANG reference. |
| 79 | */ |
| 80 | private String reference; |
| 81 | |
| 82 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 83 | * Status of YANG uses. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 84 | */ |
| 85 | private YangStatusType status; |
| 86 | |
| 87 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 88 | * Status of resolution. If completely resolved enum value is "RESOLVED", |
| 89 | * if not enum value is "UNRESOLVED", in case reference of grouping/typedef |
| 90 | * is added to uses/type but it's not resolved value of enum should be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 91 | * "INTRA_FILE_RESOLVED". |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 92 | */ |
| 93 | private ResolvableStatus resolvableStatus; |
| 94 | |
| 95 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 96 | * Creates an YANG uses node. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 97 | */ |
| 98 | public YangUses() { |
| 99 | super(YangNodeType.USES_NODE); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 100 | nodeIdentifier = new YangNodeIdentifier(); |
| 101 | resolvableStatus = ResolvableStatus.UNRESOLVED; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 105 | * Returns the referred group. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 106 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 107 | * @return the referred group |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 108 | */ |
| 109 | public YangGrouping getRefGroup() { |
| 110 | return refGroup; |
| 111 | } |
| 112 | |
| 113 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 114 | * Sets the referred group. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 115 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 116 | * @param refGroup the referred group |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 117 | */ |
| 118 | public void setRefGroup(YangGrouping refGroup) { |
| 119 | this.refGroup = refGroup; |
| 120 | } |
| 121 | |
| 122 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 123 | * Returns the description. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 124 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 125 | * @return the description |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 126 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 127 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 128 | public String getDescription() { |
| 129 | return description; |
| 130 | } |
| 131 | |
| 132 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 133 | * Sets the description. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 134 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 135 | * @param description set the description |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 136 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 137 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 138 | public void setDescription(String description) { |
| 139 | this.description = description; |
| 140 | } |
| 141 | |
| 142 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 143 | * Returns the textual reference. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 144 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 145 | * @return the reference |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 146 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 147 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 148 | public String getReference() { |
| 149 | return reference; |
| 150 | } |
| 151 | |
| 152 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 153 | * Sets the textual reference. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 154 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 155 | * @param reference the reference to set |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 156 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 157 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 158 | public void setReference(String reference) { |
| 159 | this.reference = reference; |
| 160 | } |
| 161 | |
| 162 | /** |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 163 | * Returns the status. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 164 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 165 | * @return the status |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 166 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 167 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 168 | public YangStatusType getStatus() { |
| 169 | return status; |
| 170 | } |
| 171 | |
| 172 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 173 | * Sets the status. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 174 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 175 | * @param status the status to set |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 176 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 177 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 178 | public void setStatus(YangStatusType status) { |
| 179 | this.status = status; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns the type of the data. |
| 184 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 185 | * @return returns USES_DATA |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 186 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 187 | @Override |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 188 | public YangConstructType getYangConstructType() { |
| 189 | return YangConstructType.USES_DATA; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 193 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 194 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 195 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 196 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 197 | @Override |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 198 | public void validateDataOnEntry() |
| 199 | throws DataModelException { |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 200 | // TODO auto-generated method stub, to be implemented by parser |
| 201 | } |
| 202 | |
| 203 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 204 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 205 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 206 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 207 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 208 | @Override |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 209 | public void validateDataOnExit() |
| 210 | throws DataModelException { |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 211 | // TODO auto-generated method stub, to be implemented by parser |
| 212 | } |
| 213 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 214 | @Override |
| 215 | public String getName() { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 216 | return nodeIdentifier.getName(); |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 217 | } |
| 218 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 219 | @Override |
| 220 | public void setName(String name) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 221 | nodeIdentifier.setName(name); |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 222 | } |
| 223 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 224 | /** |
| 225 | * Returns node identifier. |
| 226 | * |
| 227 | * @return node identifier |
| 228 | */ |
| 229 | public YangNodeIdentifier getNodeIdentifier() { |
| 230 | return nodeIdentifier; |
| 231 | } |
| 232 | |
| 233 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 234 | * Sets node identifier. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 235 | * |
| 236 | * @param nodeIdentifier the node identifier |
| 237 | */ |
| 238 | public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) { |
| 239 | this.nodeIdentifier = nodeIdentifier; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns prefix associated with uses. |
| 244 | * |
| 245 | * @return prefix associated with uses |
| 246 | */ |
| 247 | public String getPrefix() { |
| 248 | return nodeIdentifier.getPrefix(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Get prefix associated with uses. |
| 253 | * |
| 254 | * @param prefix prefix associated with uses |
| 255 | */ |
| 256 | public void setPrefix(String prefix) { |
| 257 | nodeIdentifier.setPrefix(prefix); |
| 258 | } |
| 259 | |
| 260 | @Override |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 261 | public void resolve() |
| 262 | throws DataModelException { |
| 263 | |
| 264 | YangGrouping referredGrouping = getRefGroup(); |
| 265 | |
| 266 | if (referredGrouping == null) { |
| 267 | throw new DataModelException("YANG uses linker error, cannot resolve uses"); |
| 268 | } |
| 269 | |
| 270 | YangNode usesParentNode = getParentNodeInGenCode(this); |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 271 | if ((!(usesParentNode instanceof YangLeavesHolder)) |
| 272 | || (!(usesParentNode instanceof CollisionDetector))) { |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 273 | throw new DataModelException("YANG uses holder construct is wrong"); |
| 274 | } |
| 275 | |
| 276 | YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode; |
| 277 | if (referredGrouping.getListOfLeaf() != null) { |
| 278 | for (YangLeaf leaf : referredGrouping.getListOfLeaf()) { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 279 | ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getLeafName(), |
| 280 | YangConstructType.LEAF_DATA); |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 281 | usesParentLeavesHolder.addLeaf(leaf); |
| 282 | } |
| 283 | } |
| 284 | if (referredGrouping.getListOfLeafList() != null) { |
| 285 | for (YangLeafList leafList : referredGrouping.getListOfLeafList()) { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 286 | ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getLeafName(), |
| 287 | YangConstructType.LEAF_LIST_DATA); |
Vinod Kumar S | 9aacac5 | 2016-04-20 13:02:58 +0530 | [diff] [blame] | 288 | usesParentLeavesHolder.addLeafList(leafList); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | YangNode.cloneSubTree(getRefGroup(), usesParentNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public ResolvableStatus getResolvableStatus() { |
| 297 | return resolvableStatus; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void setResolvableStatus(ResolvableStatus resolvableStatus) { |
| 302 | this.resolvableStatus = resolvableStatus; |
| 303 | } |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 304 | |
| 305 | @Override |
| 306 | public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException { |
| 307 | detectCollidingChildUtil(identifierName, dataType, this); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException { |
| 312 | |
| 313 | if (getName().equals(identifierName)) { |
| 314 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as uses \"" |
| 315 | + getName() + "\""); |
| 316 | } |
| 317 | } |
| 318 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 319 | } |