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