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