Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 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 | |
| 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 20 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 22 | |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame^] | 23 | import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED; |
| 24 | |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 25 | /* |
| 26 | * Reference:RFC 6020. |
| 27 | * The "type" statement takes as an argument a string that is the name |
| 28 | * of a YANG built-in type or a derived type, followed by an optional |
| 29 | * block of sub-statements that are used to put further restrictions |
| 30 | * on the type. |
| 31 | * |
| 32 | * The restrictions that can be applied depend on the type being restricted. |
| 33 | * The type's sub-statements |
| 34 | * |
| 35 | * +------------------+---------+-------------+------------------------------------+ |
| 36 | * | substatement | section | cardinality | mapped data type | |
| 37 | * +------------------+---------+-------------+------------------------------------+ |
| 38 | * | bit | 9.7.4 | 0..n | - YangBit used in YangBits | |
| 39 | * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration | |
| 40 | * | length | 9.4.4 | 0..1 | - used for string | |
| 41 | * | path | 9.9.2 | 0..1 | - TODO leaf-ref | |
| 42 | * | pattern | 9.4.6 | 0..n | - used for string | |
| 43 | * | range | 9.2.4 | 0..1 | - used for integer data type | |
| 44 | * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier | |
| 45 | * | type | 7.4 | 0..n | - TODO union | |
| 46 | * +------------------+---------+-------------+------------------------------------+ |
| 47 | */ |
| 48 | |
| 49 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 50 | * Represents the data type information. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 51 | * |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 52 | * @param <T> YANG data type info |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 53 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame^] | 54 | public class YangType<T> |
| 55 | implements Parsable, Resolvable { |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 56 | |
| 57 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 58 | * YANG node identifier. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 59 | */ |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 60 | private YangNodeIdentifier nodeIdentifier; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 63 | * Java package in which the Java type is defined. |
| 64 | */ |
| 65 | private String javaPackage; |
| 66 | |
| 67 | /** |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 68 | * YANG data type. |
| 69 | */ |
| 70 | private YangDataTypes dataType; |
| 71 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 72 | /** |
| 73 | * Additional information about data type, example restriction info, named |
| 74 | * values, etc. The extra information is based on the data type. Based on |
| 75 | * the data type, the extended info can vary. |
| 76 | */ |
| 77 | private T dataTypeExtendedInfo; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 78 | |
| 79 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 80 | * Effective built-in type, requried in case type of typedef is again a |
| 81 | * derived type. This information is to be added during linking. |
| 82 | */ |
| 83 | private YangDataTypes effectiveBuiltInType; |
| 84 | |
| 85 | /** |
| 86 | * Effective pattern restriction, requried in case type of typedef is again |
| 87 | * a derived type. This information is to be added during linking. |
| 88 | */ |
| 89 | private YangPatternRestriction effectivePatternRestriction; |
| 90 | |
| 91 | /** |
| 92 | * Status of resolution. If completely resolved enum value is "RESOLVED", |
| 93 | * if not enum value is "UNRESOLVED", in case reference of grouping/typedef |
| 94 | * is added to uses/type but it's not resolved value of enum should be |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame^] | 95 | * "INTRA_FILE_RESOLVED". |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 96 | */ |
| 97 | private ResolvableStatus resolvableStatus; |
| 98 | |
| 99 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 100 | * Creates a YANG type object. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 101 | */ |
| 102 | public YangType() { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 103 | |
| 104 | nodeIdentifier = new YangNodeIdentifier(); |
| 105 | resolvableStatus = ResolvableStatus.UNRESOLVED; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns prefix associated with data type name. |
| 110 | * |
| 111 | * @return prefix associated with data type name |
| 112 | */ |
| 113 | public String getPrefix() { |
| 114 | return nodeIdentifier.getPrefix(); |
| 115 | } |
| 116 | |
| 117 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 118 | * Sets prefix associated with data type name. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 119 | * |
| 120 | * @param prefix prefix associated with data type name |
| 121 | */ |
| 122 | public void setPrefix(String prefix) { |
| 123 | nodeIdentifier.setPrefix(prefix); |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 127 | * Returns the name of data type. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 128 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 129 | * @return the name of data type |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 130 | */ |
| 131 | public String getDataTypeName() { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 132 | return nodeIdentifier.getName(); |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 136 | * Sets the name of the data type. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 137 | * |
| 138 | * @param typeName the name to set |
| 139 | */ |
| 140 | public void setDataTypeName(String typeName) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 141 | nodeIdentifier.setName(typeName); |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 145 | * Returns the Java package where the type is defined. |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 146 | * |
| 147 | * @return Java package where the type is defined |
| 148 | */ |
| 149 | public String getJavaPackage() { |
| 150 | return javaPackage; |
| 151 | } |
| 152 | |
| 153 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 154 | * Sets Java package where the type is defined. |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 155 | * |
| 156 | * @param javaPackage Java package where the type is defined |
| 157 | */ |
| 158 | public void setJavaPackage(String javaPackage) { |
| 159 | this.javaPackage = javaPackage; |
| 160 | } |
| 161 | |
| 162 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 163 | * Returns the type of data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 164 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 165 | * @return the data type |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 166 | */ |
| 167 | public YangDataTypes getDataType() { |
| 168 | return dataType; |
| 169 | } |
| 170 | |
| 171 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 172 | * Sets the type of data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 173 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 174 | * @param dataType data type |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 175 | */ |
| 176 | public void setDataType(YangDataTypes dataType) { |
| 177 | this.dataType = dataType; |
| 178 | } |
| 179 | |
| 180 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 181 | * Returns the data type meta data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 182 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 183 | * @return the data type meta data |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 184 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 185 | public T getDataTypeExtendedInfo() { |
| 186 | return dataTypeExtendedInfo; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 190 | * Sets the data type meta data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 191 | * |
| 192 | * @param dataTypeInfo the meta data to set |
| 193 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 194 | public void setDataTypeExtendedInfo(T dataTypeInfo) { |
| 195 | this.dataTypeExtendedInfo = dataTypeInfo; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 199 | * Returns node identifier. |
| 200 | * |
| 201 | * @return node identifier |
| 202 | */ |
| 203 | public YangNodeIdentifier getNodeIdentifier() { |
| 204 | return nodeIdentifier; |
| 205 | } |
| 206 | |
| 207 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 208 | * Sets node identifier. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 209 | * |
| 210 | * @param nodeIdentifier the node identifier |
| 211 | */ |
| 212 | public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) { |
| 213 | this.nodeIdentifier = nodeIdentifier; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Return effective built-in type. |
| 218 | * |
| 219 | * @return effective built-in type |
| 220 | */ |
| 221 | public YangDataTypes getEffectiveBuiltInType() { |
| 222 | return effectiveBuiltInType; |
| 223 | } |
| 224 | |
| 225 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 226 | * Sets effective built-in type. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 227 | * |
| 228 | * @param effectiveBuiltInType effective built-in type |
| 229 | */ |
| 230 | public void setEffectiveBuiltInType(YangDataTypes effectiveBuiltInType) { |
| 231 | this.effectiveBuiltInType = effectiveBuiltInType; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Returns effective pattern restriction. |
| 236 | * |
| 237 | * @return effective pattern restriction |
| 238 | */ |
| 239 | public YangPatternRestriction getEffectivePatternRestriction() { |
| 240 | return effectivePatternRestriction; |
| 241 | } |
| 242 | |
| 243 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 244 | * Sets effective pattern restriction. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 245 | * |
| 246 | * @param effectivePatternRestriction effective pattern restriction |
| 247 | */ |
| 248 | public void setEffectivePatternRestriction(YangPatternRestriction effectivePatternRestriction) { |
| 249 | this.effectivePatternRestriction = effectivePatternRestriction; |
| 250 | } |
| 251 | |
| 252 | /** |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 253 | * Returns the type of the parsed data. |
| 254 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 255 | * @return returns TYPE_DATA |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 256 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 257 | @Override |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 258 | public YangConstructType getYangConstructType() { |
| 259 | return YangConstructType.TYPE_DATA; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 263 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 264 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 265 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 266 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 267 | @Override |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame^] | 268 | public void validateDataOnEntry() |
| 269 | throws DataModelException { |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 270 | // TODO auto-generated method stub, to be implemented by parser |
| 271 | |
| 272 | } |
| 273 | |
| 274 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 275 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 276 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 277 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 278 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 279 | @Override |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame^] | 280 | public void validateDataOnExit() |
| 281 | throws DataModelException { |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 282 | // TODO auto-generated method stub, to be implemented by parser |
| 283 | |
| 284 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 285 | |
| 286 | @Override |
| 287 | public ResolvableStatus getResolvableStatus() { |
| 288 | return resolvableStatus; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void setResolvableStatus(ResolvableStatus resolvableStatus) { |
| 293 | this.resolvableStatus = resolvableStatus; |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void resolve() { |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame^] | 298 | /* |
| 299 | Inherit the Restriction from the referred typedef definition. |
| 300 | */ |
| 301 | if (getDataType() != YangDataTypes.DERIVED) { |
| 302 | throw new RuntimeException("Resolve should only be called for derrived data types"); |
| 303 | } |
| 304 | |
| 305 | YangDerivedInfo<?> derrivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo(); |
| 306 | YangType<?> baseType = derrivedInfo.getReferredTypeDef().getTypeDefBaseType(); |
| 307 | if (YangDataTypes.DERIVED == baseType.getDataType()) { |
| 308 | if (baseType.getResolvableStatus() == INTRA_FILE_RESOLVED) { |
| 309 | setResolvableStatus(INTRA_FILE_RESOLVED); |
| 310 | } |
| 311 | } |
| 312 | //TODO: |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 313 | } |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 314 | } |