Vinod Kumar S | 5a39e01 | 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; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 20 | import org.onosproject.yangutils.utils.YangConstructType; |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.CachedFileHandle; |
Vinod Kumar S | 5a39e01 | 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 | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Default value in string, needs to be converted to the target object, |
| 60 | * based on the type. |
| 61 | */ |
| 62 | private String defaultValueInString; |
| 63 | |
| 64 | /** |
| 65 | * Description of new type. |
| 66 | */ |
| 67 | private String description; |
| 68 | |
| 69 | /** |
| 70 | * reference string. |
| 71 | */ |
| 72 | private String reference; |
| 73 | |
| 74 | /** |
| 75 | * Status of the data type. |
| 76 | */ |
| 77 | private YangStatusType status; |
| 78 | |
| 79 | /** |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 80 | * Maintain the derived type information. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 81 | */ |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 82 | private YangType<YangDerivedType> derivedType; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Units of the data type. |
| 86 | */ |
| 87 | private String units; |
| 88 | |
| 89 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 90 | * package of the generated java code. |
| 91 | */ |
| 92 | private String pkg; |
| 93 | |
| 94 | /** |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 95 | * Create a typedef node. |
| 96 | */ |
| 97 | public YangTypeDef() { |
| 98 | super(YangNodeType.TYPEDEF_NODE); |
| 99 | } |
| 100 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 101 | |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Get the default value. |
| 105 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 106 | * @return the default value |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 107 | */ |
| 108 | public String getDefaultValueInString() { |
| 109 | return defaultValueInString; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set the default value. |
| 114 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 115 | * @param defaultValueInString the default value |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 116 | */ |
| 117 | public void setDefaultValueInString(String defaultValueInString) { |
| 118 | this.defaultValueInString = defaultValueInString; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get the description. |
| 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 | /** |
| 132 | * Set the description. |
| 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 | /** |
| 142 | * Get the textual reference. |
| 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 | /** |
| 152 | * Set the textual reference. |
| 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 | /** |
| 162 | * Get the status. |
| 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 | /** |
| 172 | * Set the status. |
| 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 | /** |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 182 | * Get the derived type. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 183 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 184 | * @return the derived type |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 185 | */ |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 186 | public YangType<YangDerivedType> getDerivedType() { |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 187 | return derivedType; |
| 188 | } |
| 189 | |
| 190 | /** |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 191 | * Set the derived type. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 192 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 193 | * @param derivedType the derived type |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 194 | */ |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 195 | public void setDerivedType(YangType<YangDerivedType> derivedType) { |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 196 | this.derivedType = derivedType; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the unit. |
| 201 | * |
| 202 | * @return the units |
| 203 | */ |
| 204 | public String getUnits() { |
| 205 | return units; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Set the unit. |
| 210 | * |
| 211 | * @param units the units to set |
| 212 | */ |
| 213 | public void setUnits(String units) { |
| 214 | this.units = units; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns the type of the data. |
| 219 | * |
| 220 | * @return returns TYPEDEF_DATA |
| 221 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 222 | @Override |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 223 | public YangConstructType getYangConstructType() { |
| 224 | return YangConstructType.TYPEDEF_DATA; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Validate the data on entering the corresponding parse tree node. |
| 229 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 230 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 231 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 232 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 233 | public void validateDataOnEntry() throws DataModelException { |
| 234 | // TODO auto-generated method stub, to be implemented by parser |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Validate the data on exiting the corresponding parse tree node. |
| 239 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 240 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 241 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 242 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 243 | public void validateDataOnExit() throws DataModelException { |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 244 | YangType<YangDerivedType> type = getDerivedType(); |
| 245 | if (type == null) { |
| 246 | throw new DataModelException("Typedef does not have type info."); |
| 247 | } |
| 248 | if ((type.getDataType() != YangDataTypes.DERIVED) |
| 249 | || (type.getDataTypeName() == null)) { |
| 250 | throw new DataModelException("Typedef type is not derived."); |
| 251 | } |
| 252 | |
| 253 | YangDerivedType derivedTypeInfo = type.getDataTypeExtendedInfo(); |
| 254 | if (derivedTypeInfo == null) { |
| 255 | throw new DataModelException("derrived type does not have derived info."); |
| 256 | } |
| 257 | |
| 258 | YangType<?> baseType = derivedTypeInfo.getBaseType(); |
| 259 | if (baseType == null) { |
| 260 | throw new DataModelException("Base type of a derived type is missing."); |
| 261 | } |
| 262 | |
| 263 | if (derivedTypeInfo.getEffectiveYangBuiltInType() == null) { |
| 264 | /* resolve the effective type from the data tree. */ |
| 265 | /* |
| 266 | * TODO: try to resolve the nested reference, if possible in the |
| 267 | * partial tree, otherwise we need to resolve finally when the |
| 268 | * complete module is created. |
| 269 | */ |
| 270 | YangModule.addToResolveList(this); |
| 271 | } |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 272 | } |
| 273 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 274 | /** |
| 275 | * Get the YANG name of the typedef. |
| 276 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 277 | * @return YANG name of the typedef |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 278 | */ |
| 279 | @Override |
| 280 | public String getName() { |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 281 | if (getDerivedType() != null) { |
| 282 | return getDerivedType().getDataTypeName(); |
| 283 | } |
| 284 | return null; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 285 | } |
| 286 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 287 | /** |
| 288 | * Set YANG name of the typedef. |
| 289 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 290 | * @param name YANG name of the typedef |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 291 | */ |
| 292 | @Override |
| 293 | public void setName(String name) { |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 294 | if (getDerivedType() == null) { |
| 295 | throw new RuntimeException( |
| 296 | "Derrived Type info needs to be set in parser when the typedef listner is processed"); |
| 297 | } |
| 298 | getDerivedType().setDataTypeName(name); |
| 299 | getDerivedType().setDataType(YangDataTypes.DERIVED); |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 300 | } |
| 301 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 302 | /** |
| 303 | * Generate java code snippet corresponding to YANG typedef. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 304 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 305 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 306 | public void generateJavaCodeEntry() { |
| 307 | // TODO Auto-generated method stub |
| 308 | |
| 309 | } |
| 310 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 311 | /** |
| 312 | * Free resource used for code generation of YANG typedef. |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 313 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 314 | @Override |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 315 | public void generateJavaCodeExit() { |
| 316 | // TODO Auto-generated method stub |
| 317 | |
| 318 | } |
| 319 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 320 | /** |
| 321 | * Get the mapped java package. |
| 322 | * |
| 323 | * @return the java package |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 324 | */ |
| 325 | @Override |
| 326 | public String getPackage() { |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 327 | return pkg; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 328 | } |
| 329 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 330 | /** |
| 331 | * Set the mapped java package. |
| 332 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 333 | * @param pakg mapped java package |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 334 | */ |
| 335 | @Override |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 336 | public void setPackage(String pakg) { |
| 337 | pkg = pakg; |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 338 | |
| 339 | } |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 340 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 341 | /** |
| 342 | * Get the file handle of the cached file used during code generation. |
| 343 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 344 | * @return cached file handle |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 345 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 346 | @Override |
| 347 | public CachedFileHandle getFileHandle() { |
| 348 | // TODO Auto-generated method stub |
| 349 | return null; |
| 350 | } |
| 351 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 352 | /** |
| 353 | * Set the file handle to be used used for code generation. |
| 354 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 355 | * @param fileHandle cached file handle |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 356 | */ |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 357 | @Override |
| 358 | public void setFileHandle(CachedFileHandle fileHandle) { |
| 359 | // TODO Auto-generated method stub |
| 360 | |
| 361 | } |
Vinod Kumar S | 5a39e01 | 2016-02-16 01:37:16 +0530 | [diff] [blame] | 362 | } |