Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +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 | package org.onosproject.yangutils.datamodel; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 19 | import org.onosproject.yangutils.parser.Parsable; |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.translator.CachedFileHandle; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 22 | |
| 23 | /*- |
| 24 | * Reference RFC 6020. |
| 25 | * |
| 26 | * The "typedef" statement defines a new type that may be used locally in the |
| 27 | * module, in modules or submodules which include it, and by other modules that |
| 28 | * import from it. The new type is called the "derived type", and the type from |
| 29 | * which it was derived is called the "base type". All derived types can be |
| 30 | * traced back to a YANG built-in type. |
| 31 | * |
| 32 | * The "typedef" statement's argument is an identifier that is the name of the |
| 33 | * type to be defined, and MUST be followed by a block of sub-statements that |
| 34 | * holds detailed typedef information. |
| 35 | * |
| 36 | * The name of the type MUST NOT be one of the YANG built-in types. If the |
| 37 | * typedef is defined at the top level of a YANG module or submodule, the name |
| 38 | * of the type to be defined MUST be unique within the module. |
| 39 | * The typedef's sub-statements |
| 40 | * |
| 41 | * +--------------+---------+-------------+------------------+ |
| 42 | * | substatement | section | cardinality |data model mapping| |
| 43 | * +--------------+---------+-------------+------------------+ |
| 44 | * | default | 7.3.4 | 0..1 |-string | |
| 45 | * | description | 7.19.3 | 0..1 |-string | |
| 46 | * | reference | 7.19.4 | 0..1 |-string | |
| 47 | * | status | 7.19.2 | 0..1 |-YangStatus | |
| 48 | * | type | 7.3.2 | 1 |-yangType | |
| 49 | * | units | 7.3.3 | 0..1 |-string | |
| 50 | * +--------------+---------+-------------+------------------+ |
| 51 | */ |
| 52 | /** |
| 53 | * Data model node to maintain information defined in YANG typedef. |
| 54 | */ |
| 55 | public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable { |
| 56 | |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 57 | /** |
| 58 | * Default value in string, needs to be converted to the target object, |
| 59 | * based on the type. |
| 60 | */ |
| 61 | private String defaultValueInString; |
| 62 | |
| 63 | /** |
| 64 | * Description of new type. |
| 65 | */ |
| 66 | private String description; |
| 67 | |
| 68 | /** |
| 69 | * reference string. |
| 70 | */ |
| 71 | private String reference; |
| 72 | |
| 73 | /** |
| 74 | * Status of the data type. |
| 75 | */ |
| 76 | private YangStatusType status; |
| 77 | |
| 78 | /** |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 79 | * Maintain the derived type information. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 80 | */ |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 81 | private YangType<YangDerivedType> derivedType; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Units of the data type. |
| 85 | */ |
| 86 | private String units; |
| 87 | |
| 88 | /** |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 89 | * package of the generated java code. |
| 90 | */ |
| 91 | private String pkg; |
| 92 | |
| 93 | /** |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 94 | * Create a typedef node. |
| 95 | */ |
| 96 | public YangTypeDef() { |
| 97 | super(YangNodeType.TYPEDEF_NODE); |
| 98 | } |
| 99 | |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 100 | /** |
| 101 | * Get the default value. |
| 102 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 103 | * @return the default value |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 104 | */ |
| 105 | public String getDefaultValueInString() { |
| 106 | return defaultValueInString; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Set the default value. |
| 111 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 112 | * @param defaultValueInString the default value |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 113 | */ |
| 114 | public void setDefaultValueInString(String defaultValueInString) { |
| 115 | this.defaultValueInString = defaultValueInString; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the description. |
| 120 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 121 | * @return the description |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 122 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 123 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 124 | public String getDescription() { |
| 125 | return description; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set the description. |
| 130 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 131 | * @param description set the description |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 132 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 133 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 134 | public void setDescription(String description) { |
| 135 | this.description = description; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the textual reference. |
| 140 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 141 | * @return the reference |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 142 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 143 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 144 | public String getReference() { |
| 145 | return reference; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set the textual reference. |
| 150 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 151 | * @param reference the reference to set |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 152 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 153 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 154 | public void setReference(String reference) { |
| 155 | this.reference = reference; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the status. |
| 160 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 161 | * @return the status |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 162 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 163 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 164 | public YangStatusType getStatus() { |
| 165 | return status; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Set the status. |
| 170 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 171 | * @param status the status to set |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 172 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 173 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 174 | public void setStatus(YangStatusType status) { |
| 175 | this.status = status; |
| 176 | } |
| 177 | |
| 178 | /** |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 179 | * Get the derived type. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 180 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 181 | * @return the derived type |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 182 | */ |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 183 | public YangType<YangDerivedType> getDerivedType() { |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 184 | return derivedType; |
| 185 | } |
| 186 | |
| 187 | /** |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 188 | * Set the derived type. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 189 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 190 | * @param derivedType the derived type |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 191 | */ |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 192 | public void setDerivedType(YangType<YangDerivedType> derivedType) { |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 193 | this.derivedType = derivedType; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the unit. |
| 198 | * |
| 199 | * @return the units |
| 200 | */ |
| 201 | public String getUnits() { |
| 202 | return units; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Set the unit. |
| 207 | * |
| 208 | * @param units the units to set |
| 209 | */ |
| 210 | public void setUnits(String units) { |
| 211 | this.units = units; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns the type of the data. |
| 216 | * |
| 217 | * @return returns TYPEDEF_DATA |
| 218 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 219 | @Override |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 220 | public YangConstructType getYangConstructType() { |
| 221 | return YangConstructType.TYPEDEF_DATA; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Validate the data on entering the corresponding parse tree node. |
| 226 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 227 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 228 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 229 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 230 | public void validateDataOnEntry() throws DataModelException { |
| 231 | // TODO auto-generated method stub, to be implemented by parser |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Validate the data on exiting the corresponding parse tree node. |
| 236 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 237 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 238 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 239 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 240 | public void validateDataOnExit() throws DataModelException { |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 241 | YangType<YangDerivedType> type = getDerivedType(); |
| 242 | if (type == null) { |
| 243 | throw new DataModelException("Typedef does not have type info."); |
| 244 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 245 | if (type.getDataType() != YangDataTypes.DERIVED |
| 246 | || type.getDataTypeName() == null) { |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 247 | throw new DataModelException("Typedef type is not derived."); |
| 248 | } |
| 249 | |
| 250 | YangDerivedType derivedTypeInfo = type.getDataTypeExtendedInfo(); |
| 251 | if (derivedTypeInfo == null) { |
| 252 | throw new DataModelException("derrived type does not have derived info."); |
| 253 | } |
| 254 | |
| 255 | YangType<?> baseType = derivedTypeInfo.getBaseType(); |
| 256 | if (baseType == null) { |
| 257 | throw new DataModelException("Base type of a derived type is missing."); |
| 258 | } |
| 259 | |
| 260 | if (derivedTypeInfo.getEffectiveYangBuiltInType() == null) { |
| 261 | /* resolve the effective type from the data tree. */ |
| 262 | /* |
| 263 | * TODO: try to resolve the nested reference, if possible in the |
| 264 | * partial tree, otherwise we need to resolve finally when the |
| 265 | * complete module is created. |
| 266 | */ |
| 267 | YangModule.addToResolveList(this); |
| 268 | } |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 271 | /** |
| 272 | * Get the YANG name of the typedef. |
| 273 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 274 | * @return YANG name of the typedef |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 275 | */ |
| 276 | @Override |
| 277 | public String getName() { |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 278 | if (getDerivedType() != null) { |
| 279 | return getDerivedType().getDataTypeName(); |
| 280 | } |
| 281 | return null; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 282 | } |
| 283 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 284 | /** |
| 285 | * Set YANG name of the typedef. |
| 286 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 287 | * @param name YANG name of the typedef |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 288 | */ |
| 289 | @Override |
| 290 | public void setName(String name) { |
Vinod Kumar S | 71cba68 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 291 | if (getDerivedType() == null) { |
| 292 | throw new RuntimeException( |
| 293 | "Derrived Type info needs to be set in parser when the typedef listner is processed"); |
| 294 | } |
| 295 | getDerivedType().setDataTypeName(name); |
| 296 | getDerivedType().setDataType(YangDataTypes.DERIVED); |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 297 | } |
| 298 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 299 | /** |
| 300 | * Generate java code snippet corresponding to YANG typedef. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 301 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 302 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 303 | public void generateJavaCodeEntry() { |
| 304 | // TODO Auto-generated method stub |
| 305 | |
| 306 | } |
| 307 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 308 | /** |
| 309 | * Free resource used for code generation of YANG typedef. |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 310 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 311 | @Override |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 312 | public void generateJavaCodeExit() { |
| 313 | // TODO Auto-generated method stub |
| 314 | |
| 315 | } |
| 316 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 317 | /** |
| 318 | * Get the mapped java package. |
| 319 | * |
| 320 | * @return the java package |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 321 | */ |
| 322 | @Override |
| 323 | public String getPackage() { |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 324 | return pkg; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 325 | } |
| 326 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 327 | /** |
| 328 | * Set the mapped java package. |
| 329 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 330 | * @param pakg mapped java package |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 331 | */ |
| 332 | @Override |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 333 | public void setPackage(String pakg) { |
| 334 | pkg = pakg; |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 335 | |
| 336 | } |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 337 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 338 | /** |
| 339 | * Get the file handle of the cached file used during code generation. |
| 340 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 341 | * @return cached file handle |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 342 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 343 | @Override |
| 344 | public CachedFileHandle getFileHandle() { |
| 345 | // TODO Auto-generated method stub |
| 346 | return null; |
| 347 | } |
| 348 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 349 | /** |
| 350 | * Set the file handle to be used used for code generation. |
| 351 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 352 | * @param fileHandle cached file handle |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 353 | */ |
Bharat saraswal | 594bc6d | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 354 | @Override |
| 355 | public void setFileHandle(CachedFileHandle fileHandle) { |
| 356 | // TODO Auto-generated method stub |
| 357 | |
| 358 | } |
Vinod Kumar S | 2ff139c | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 359 | } |