Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.datamodel; |
| 18 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 24 | import org.onosproject.yangutils.parser.Parsable; |
| 25 | import org.onosproject.yangutils.parser.ParsableDataType; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.CachedFileHandle; |
| 27 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 28 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
| 29 | import org.onosproject.yangutils.utils.io.impl.FileSystemUtil; |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 30 | /*- |
| 31 | * Reference RFC 6020. |
| 32 | * |
| 33 | * The "container" statement is used to define an interior data node in the |
| 34 | * schema tree. It takes one argument, which is an identifier, followed by a |
| 35 | * block of sub-statements that holds detailed container information. |
| 36 | * |
| 37 | * A container node does not have a value, but it has a list of child nodes in |
| 38 | * the data tree. The child nodes are defined in the container's sub-statements. |
| 39 | * |
| 40 | * Containers with Presence |
| 41 | * |
| 42 | * YANG supports two styles of containers, those that exist only for organizing |
| 43 | * the hierarchy of data nodes, and those whose presence in the configuration |
| 44 | * has an explicit meaning. |
| 45 | * |
| 46 | * In the first style, the container has no meaning of its own, existing only to |
| 47 | * contain child nodes. This is the default style. |
| 48 | * |
| 49 | * For example, the set of scrambling options for Synchronous Optical Network |
| 50 | * (SONET) interfaces may be placed inside a "scrambling" container to enhance |
| 51 | * the organization of the configuration hierarchy, and to keep these nodes |
| 52 | * together. The "scrambling" node itself has no meaning, so removing the node |
| 53 | * when it becomes empty relieves the user from performing this task. |
| 54 | * |
| 55 | * In the second style, the presence of the container itself is configuration |
| 56 | * data, representing a single bit of configuration data. The container acts as |
| 57 | * both a configuration knob and a means of organizing related configuration. |
| 58 | * These containers are explicitly created and deleted. |
| 59 | * |
| 60 | * YANG calls this style a "presence container" and it is indicated using the |
| 61 | * "presence" statement, which takes as its argument a text string indicating |
| 62 | * what the presence of the node means. |
| 63 | * |
| 64 | * The container's Substatements |
| 65 | * |
| 66 | * +--------------+---------+-------------+------------------+ |
| 67 | * | substatement | section | cardinality |data model mapping| |
| 68 | * +--------------+---------+-------------+------------------+ |
| 69 | * | anyxml | 7.10 | 0..n | -not supported | |
| 70 | * | choice | 7.9 | 0..n | -child nodes | |
| 71 | * | config | 7.19.1 | 0..1 | -boolean | |
| 72 | * | container | 7.5 | 0..n | -child nodes | |
| 73 | * | description | 7.19.3 | 0..1 | -string | |
| 74 | * | grouping | 7.11 | 0..n | -child nodes | |
| 75 | * | if-feature | 7.18.2 | 0..n | -TODO | |
| 76 | * | leaf | 7.6 | 0..n | -YangLeaf | |
| 77 | * | leaf-list | 7.7 | 0..n | -YangLeafList | |
| 78 | * | list | 7.8 | 0..n | -child nodes | |
| 79 | * | must | 7.5.3 | 0..n | -TODO | |
| 80 | * | presence | 7.5.5 | 0..1 | -boolean | |
| 81 | * | reference | 7.19.4 | 0..1 | -string | |
| 82 | * | status | 7.19.2 | 0..1 | -YangStatus | |
| 83 | * | typedef | 7.3 | 0..n | -child nodes | |
| 84 | * | uses | 7.12 | 0..n | -child nodes | |
| 85 | * | when | 7.19.5 | 0..1 | -TODO | |
| 86 | * +--------------+---------+-------------+------------------+ |
| 87 | */ |
| 88 | |
| 89 | /** |
| 90 | * Data model node to maintain information defined in YANG container. |
| 91 | */ |
| 92 | public class YangContainer extends YangNode implements YangLeavesHolder, YangCommonInfo, Parsable { |
| 93 | |
| 94 | /** |
| 95 | * Name of the container. |
| 96 | */ |
| 97 | private String name; |
| 98 | |
| 99 | /** |
| 100 | * If container maintains config data. |
| 101 | */ |
| 102 | private boolean isConfig; |
| 103 | |
| 104 | /** |
| 105 | * Description of container. |
| 106 | */ |
| 107 | private String description; |
| 108 | |
| 109 | /** |
| 110 | * List of leaves contained. |
| 111 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 112 | private List<YangLeaf<?>> listOfLeaf; |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * List of leaf-lists contained. |
| 116 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 117 | private List<YangLeafList<?>> listOfLeafList; |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * If it is a presence container, then the textual documentation of presence |
| 121 | * usage. |
| 122 | */ |
| 123 | private String presence; |
| 124 | |
| 125 | /** |
| 126 | * Reference of the module. |
| 127 | */ |
| 128 | private String reference; |
| 129 | |
| 130 | /** |
| 131 | * Status of the node. |
| 132 | */ |
| 133 | private YangStatusType status; |
| 134 | |
| 135 | /** |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 136 | * Package of the generated java code. |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 137 | */ |
| 138 | private String pkg; |
| 139 | |
| 140 | /** |
| 141 | * Cached Java File Handle. |
| 142 | */ |
| 143 | private CachedFileHandle fileHandle; |
| 144 | |
| 145 | /** |
| 146 | * Create a container node. |
| 147 | */ |
| 148 | public YangContainer() { |
| 149 | super(YangNodeType.CONTAINER_NODE); |
| 150 | } |
| 151 | |
| 152 | /* (non-Javadoc) |
| 153 | * @see org.onosproject.yangutils.datamodel.YangNode#getName() |
| 154 | */ |
| 155 | @Override |
| 156 | public String getName() { |
| 157 | return name; |
| 158 | } |
| 159 | |
| 160 | /* (non-Javadoc) |
| 161 | * @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String) |
| 162 | */ |
| 163 | @Override |
| 164 | public void setName(String name) { |
| 165 | this.name = name; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get the config flag. |
| 170 | * |
| 171 | * @return the isConfig |
| 172 | */ |
| 173 | public boolean isConfig() { |
| 174 | return isConfig; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Set the config flag. |
| 179 | * |
| 180 | * @param isCfg the config flag. |
| 181 | */ |
| 182 | public void setConfig(boolean isCfg) { |
| 183 | isConfig = isCfg; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get the description. |
| 188 | * |
| 189 | * @return the description. |
| 190 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 191 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 192 | public String getDescription() { |
| 193 | return description; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Set the description. |
| 198 | * |
| 199 | * @param description set the description. |
| 200 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 201 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 202 | public void setDescription(String description) { |
| 203 | this.description = description; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get the list of leaves. |
| 208 | * |
| 209 | * @return the list of leaves. |
| 210 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 211 | @Override |
| 212 | public List<YangLeaf<?>> getListOfLeaf() { |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 213 | return listOfLeaf; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Set the list of leaves. |
| 218 | * |
| 219 | * @param leafsList the list of leaf to set. |
| 220 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 221 | private void setListOfLeaf(List<YangLeaf<?>> leafsList) { |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 222 | listOfLeaf = leafsList; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Add a leaf. |
| 227 | * |
| 228 | * @param leaf the leaf to be added. |
| 229 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 230 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 231 | public void addLeaf(YangLeaf<?> leaf) { |
| 232 | if (getListOfLeaf() == null) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 233 | setListOfLeaf(new LinkedList<YangLeaf<?>>()); |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | getListOfLeaf().add(leaf); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the list of leaf-list. |
| 241 | * |
| 242 | * @return the list of leaf-list. |
| 243 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 244 | @Override |
| 245 | public List<YangLeafList<?>> getListOfLeafList() { |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 246 | return listOfLeafList; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Set the list of leaf-list. |
| 251 | * |
| 252 | * @param listOfLeafList the list of leaf-list to set. |
| 253 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 254 | private void setListOfLeafList(List<YangLeafList<?>> listOfLeafList) { |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 255 | this.listOfLeafList = listOfLeafList; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Add a leaf-list. |
| 260 | * |
| 261 | * @param leafList the leaf-list to be added. |
| 262 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 263 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 264 | public void addLeafList(YangLeafList<?> leafList) { |
| 265 | if (getListOfLeafList() == null) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 266 | setListOfLeafList(new LinkedList<YangLeafList<?>>()); |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | getListOfLeafList().add(leafList); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get the presence string if present. |
| 274 | * |
| 275 | * @return the isPressence. |
| 276 | */ |
| 277 | public String getPresence() { |
| 278 | return presence; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Set the presence string. |
| 283 | * |
| 284 | * @param presence the presence flag |
| 285 | */ |
| 286 | public void setPresence(String presence) { |
| 287 | this.presence = presence; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Get the textual reference. |
| 292 | * |
| 293 | * @return the reference. |
| 294 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 295 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 296 | public String getReference() { |
| 297 | return reference; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Set the textual reference. |
| 302 | * |
| 303 | * @param reference the reference to set. |
| 304 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 305 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 306 | public void setReference(String reference) { |
| 307 | this.reference = reference; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get the status. |
| 312 | * |
| 313 | * @return the status. |
| 314 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 315 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 316 | public YangStatusType getStatus() { |
| 317 | return status; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Set the status. |
| 322 | * |
| 323 | * @param status the status to set. |
| 324 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 325 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 326 | public void setStatus(YangStatusType status) { |
| 327 | this.status = status; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get the cached file handle. |
| 332 | * |
| 333 | * @return the fileHandle |
| 334 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 335 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 336 | public CachedFileHandle getFileHandle() { |
| 337 | return fileHandle; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Set the cached file handle. |
| 342 | * |
| 343 | * @param handle the fileHandle to set |
| 344 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 345 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 346 | public void setFileHandle(CachedFileHandle handle) { |
| 347 | fileHandle = handle; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Returns the type of the data. |
| 352 | * |
| 353 | * @return returns CONTAINER_DATA. |
| 354 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 355 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 356 | public ParsableDataType getParsableDataType() { |
| 357 | return ParsableDataType.CONTAINER_DATA; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Validate the data on entering the corresponding parse tree node. |
| 362 | * |
| 363 | * @throws DataModelException a violation of data model rules. |
| 364 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 365 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 366 | public void validateDataOnEntry() throws DataModelException { |
| 367 | // TODO auto-generated method stub, to be implemented by parser |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Validate the data on exiting the corresponding parse tree node. |
| 372 | * |
| 373 | * @throws DataModelException a violation of data model rules. |
| 374 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 375 | @Override |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 376 | public void validateDataOnExit() throws DataModelException { |
| 377 | // TODO auto-generated method stub, to be implemented by parser |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get the mapped java package. |
| 382 | * |
| 383 | * @return the java package |
| 384 | */ |
| 385 | @Override |
| 386 | public String getPackage() { |
| 387 | return pkg; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Set the mapped java package. |
| 392 | * |
| 393 | * @param pcg the package to set |
| 394 | */ |
| 395 | @Override |
| 396 | public void setPackage(String pcg) { |
| 397 | pkg = pcg; |
| 398 | } |
| 399 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 400 | @Override |
| 401 | public void generateJavaCodeEntry() throws IOException { |
| 402 | YangNode parent = getParent(); |
| 403 | String modPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), getName()); |
| 404 | setPackage(modPkg); |
| 405 | |
| 406 | CachedFileHandle handle = null; |
| 407 | try { |
| 408 | FileSystemUtil.createPackage(getPackage(), getName()); |
| 409 | handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL); |
| 410 | } catch (IOException e) { |
| 411 | throw new IOException("Failed to create the source files."); |
| 412 | } |
| 413 | setFileHandle(handle); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 414 | addAttributeInParent(); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Adds current node attribute to parent file. |
| 419 | * |
| 420 | * @param pkg java file package path |
| 421 | */ |
| 422 | private void addAttributeInParent() { |
| 423 | if (getParent() != null) { |
| 424 | getParent().getFileHandle().setChildsPackage(getPackage()); |
| 425 | getParent().getFileHandle().addAttributeInfo(null, getName(), false); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void generateJavaCodeExit() throws IOException { |
| 431 | addLeavesAttributes(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 432 | addLeafListAttributes(); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 433 | getFileHandle().close(); |
| 434 | return; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Adds leaf attributes in generated files. |
| 439 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame^] | 440 | private void addLeavesAttributes() { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 441 | |
| 442 | List<YangLeaf<?>> leaves = getListOfLeaf(); |
| 443 | if (leaves != null) { |
| 444 | for (YangLeaf<?> leaf : leaves) { |
| 445 | getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Adds leaf list's attributes in generated files. |
| 452 | */ |
| 453 | private void addLeafListAttributes() { |
| 454 | List<YangLeafList<?>> leavesList = getListOfLeafList(); |
| 455 | if (leavesList != null) { |
| 456 | for (YangLeafList<?> leafList : leavesList) { |
| 457 | getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true); |
| 458 | } |
| 459 | } |
| 460 | return; |
Vinod Kumar S | 6a6ce4c | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 461 | } |
| 462 | } |