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