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