blob: 530087f54bbf7d629b30117d9df4ed391a6d751e [file] [log] [blame]
Vinod Kumar Scf044422016-02-09 19:53:45 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar Scf044422016-02-09 19:53:45 +05303 *
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
17package org.onosproject.yangutils.datamodel;
18
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Vinod Kumar Scf044422016-02-09 19:53:45 +053020import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021import org.onosproject.yangutils.datamodel.utils.Parsable;
22import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
23import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053024import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vinod Kumar Scf044422016-02-09 19:53:45 +053025
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053026import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString;
27import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
28import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053029
Vinod Kumar Scf044422016-02-09 19:53:45 +053030/*
31 * Reference:RFC 6020.
32 * The "type" statement takes as an argument a string that is the name
33 * of a YANG built-in type or a derived type, followed by an optional
34 * block of sub-statements that are used to put further restrictions
35 * on the type.
36 *
37 * The restrictions that can be applied depend on the type being restricted.
38 * The type's sub-statements
39 *
40 * +------------------+---------+-------------+------------------------------------+
41 * | substatement | section | cardinality | mapped data type |
42 * +------------------+---------+-------------+------------------------------------+
43 * | bit | 9.7.4 | 0..n | - YangBit used in YangBits |
44 * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration |
45 * | length | 9.4.4 | 0..1 | - used for string |
46 * | path | 9.9.2 | 0..1 | - TODO leaf-ref |
47 * | pattern | 9.4.6 | 0..n | - used for string |
48 * | range | 9.2.4 | 0..1 | - used for integer data type |
49 * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier |
50 * | type | 7.4 | 0..n | - TODO union |
51 * +------------------+---------+-------------+------------------------------------+
52 */
53
54/**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053056 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053057 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053058 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053059public class YangType<T>
Bharat saraswal96dfef02016-06-16 00:29:12 +053060 implements Parsable, Resolvable, Serializable {
61
62 private static final long serialVersionUID = 8062016054L;
Vinod Kumar Scf044422016-02-09 19:53:45 +053063
64 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053065 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053066 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053067 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053068
69 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053070 * Java package in which the Java type is defined.
71 */
72 private String javaPackage;
73
74 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +053075 * YANG data type.
76 */
77 private YangDataTypes dataType;
78
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053079 /**
80 * Additional information about data type, example restriction info, named
81 * values, etc. The extra information is based on the data type. Based on
82 * the data type, the extended info can vary.
83 */
84 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053085
86 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053087 * Status of resolution. If completely resolved enum value is "RESOLVED",
88 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
89 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053090 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053091 */
92 private ResolvableStatus resolvableStatus;
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +053096 */
97 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053098
99 nodeIdentifier = new YangNodeIdentifier();
100 resolvableStatus = ResolvableStatus.UNRESOLVED;
101 }
102
103 /**
104 * Returns prefix associated with data type name.
105 *
106 * @return prefix associated with data type name
107 */
108 public String getPrefix() {
109 return nodeIdentifier.getPrefix();
110 }
111
112 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530113 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530114 *
115 * @param prefix prefix associated with data type name
116 */
117 public void setPrefix(String prefix) {
118 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530119 }
120
121 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530122 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530123 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530124 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530125 */
126 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530128 }
129
130 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530131 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530132 *
133 * @param typeName the name to set
134 */
135 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530136 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530137 }
138
139 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530140 * Returns the Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530141 *
142 * @return Java package where the type is defined
143 */
144 public String getJavaPackage() {
145 return javaPackage;
146 }
147
148 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530149 * Sets Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530150 *
151 * @param javaPackage Java package where the type is defined
152 */
153 public void setJavaPackage(String javaPackage) {
154 this.javaPackage = javaPackage;
155 }
156
157 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530158 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530161 */
162 public YangDataTypes getDataType() {
163 return dataType;
164 }
165
166 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530167 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530168 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530169 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530170 */
171 public void setDataType(YangDataTypes dataType) {
172 this.dataType = dataType;
173 }
174
175 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530176 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530177 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530178 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530179 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530180 public T getDataTypeExtendedInfo() {
181 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530182 }
183
184 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530185 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530186 *
187 * @param dataTypeInfo the meta data to set
188 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530189 public void setDataTypeExtendedInfo(T dataTypeInfo) {
190 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530191 }
192
193 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530194 * Returns node identifier.
195 *
196 * @return node identifier
197 */
198 public YangNodeIdentifier getNodeIdentifier() {
199 return nodeIdentifier;
200 }
201
202 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530203 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530204 *
205 * @param nodeIdentifier the node identifier
206 */
207 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
208 this.nodeIdentifier = nodeIdentifier;
209 }
210
211 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530212 * Returns the type of the parsed data.
213 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530214 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530215 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530216 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530217 public YangConstructType getYangConstructType() {
218 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530219 }
220
221 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530222 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530223 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530224 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530225 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530226 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530227 public void validateDataOnEntry()
228 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530229 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530230 }
231
232 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530233 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530234 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530235 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530236 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530237 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530238 public void validateDataOnExit()
239 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530240 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530241 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530242
243 @Override
244 public ResolvableStatus getResolvableStatus() {
245 return resolvableStatus;
246 }
247
248 @Override
249 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
250 this.resolvableStatus = resolvableStatus;
251 }
252
253 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530254 public void resolve()
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530255 throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530256 /*
257 * Check whether the data type is derived.
258 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530259 if (getDataType() != DERIVED) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530260 throw new DataModelException("Linker Error: Resolve should only be called for derived data types.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530261 }
262
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530263 // Check if the derived info is present.
264 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
265 if (derivedInfo == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530266 throw new DataModelException("Linker Error: Derived information is missing.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530267 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530268
269 // Initiate the resolution
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530270 try {
271 setResolvableStatus(derivedInfo.resolve());
272 } catch (DataModelException e) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530273 throw new DataModelException(e.getMessage());
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530274 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530275 }
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +0530276
277 /**
278 * Validates the input data value against the permissible value for the
279 * type as per the YANG file.
280 *
281 * @param value input data value
282 * @return status of validation
283 */
284 public boolean isValidValue(String value) {
285 switch (getDataType()) {
286 case INT8:
287 case INT16:
288 case INT32:
289 case INT64:
290 case UINT8:
291 case UINT16:
292 case UINT32:
293 case UINT64: {
294 isValidValueForRangeRestrictedType(value);
295 }
296 case DECIMAL64: {
297 // TODO
298 }
299 case STRING: {
300 // TODO implement in string restriction similar to range restriction
301 }
302 case ENUMERATION: {
303 // TODO validate using list of YANG enum of enumeration class in extended info.
304 }
305 case BINARY: {
306 // TODO validate based on extended info
307 }
308 case BITS: {
309 // TODO validate based on extended info
310 }
311 case BOOLEAN: {
312 // TODO true or false
313 }
314 case LEAFREF: {
315 // TODO validate based on extended info
316 }
317 case IDENTITYREF: {
318 // TODO TBD
319 }
320 case EMPTY: {
321 // TODO true or false
322 }
323 case UNION: {
324 // TODO validate based on extended info
325 }
326 case INSTANCE_IDENTIFIER: {
327 // TODO TBD
328 }
329 case DERIVED: {
330 if (isOfRangeRestrictedType(((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType())) {
331 try {
332 if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) {
333 getDataObjectFromString(value,
334 ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType());
335 return true;
336 } else {
337 return ((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo())
338 .getResolvedExtendedInfo()).isValidValueString(value);
339 }
340 } catch (Exception e) {
341 return false;
342 }
343 } else {
344 // TODO
345 }
346 }
347 default: {
348 // TODO
349 }
350 }
351 return true;
352 }
353
354 /**
355 * Validates the input data value for range restricted types against the
356 * permissible value for the type as per the YANG file.
357 *
358 * @param value input data value
359 * @return status of validation
360 */
361 private boolean isValidValueForRangeRestrictedType(String value) {
362 try {
363 if (getDataTypeExtendedInfo() == null) {
364 getDataObjectFromString(value, getDataType());
365 return true;
366 } else {
367 return ((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value);
368 }
369 } catch (Exception e) {
370 return false;
371 }
372 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530373}