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 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 19 | import java.io.Serializable; |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 20 | import java.math.BigInteger; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.ListIterator; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 23 | |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 25 | import org.onosproject.yangutils.datamodel.utils.DataModelUtils; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 27 | import org.onosproject.yangutils.datamodel.utils.ResolvableStatus; |
| 28 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 29 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.DataTypeException; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 31 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangUint64; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 32 | |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.datamodel.BuiltInTypeObjectFactory.getDataObjectFromString; |
| 34 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType; |
| 35 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 36 | |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 37 | /* |
| 38 | * Reference:RFC 6020. |
| 39 | * The "type" statement takes as an argument a string that is the name |
| 40 | * of a YANG built-in type or a derived type, followed by an optional |
| 41 | * block of sub-statements that are used to put further restrictions |
| 42 | * on the type. |
| 43 | * |
| 44 | * The restrictions that can be applied depend on the type being restricted. |
| 45 | * The type's sub-statements |
| 46 | * |
| 47 | * +------------------+---------+-------------+------------------------------------+ |
| 48 | * | substatement | section | cardinality | mapped data type | |
| 49 | * +------------------+---------+-------------+------------------------------------+ |
| 50 | * | bit | 9.7.4 | 0..n | - YangBit used in YangBits | |
| 51 | * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration | |
| 52 | * | length | 9.4.4 | 0..1 | - used for string | |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 53 | * | path | 9.9.2 | 0..1 | - path for referred leaf/leaf-list | |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 54 | * | pattern | 9.4.6 | 0..n | - used for string | |
| 55 | * | range | 9.2.4 | 0..1 | - used for integer data type | |
| 56 | * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier | |
| 57 | * | type | 7.4 | 0..n | - TODO union | |
| 58 | * +------------------+---------+-------------+------------------------------------+ |
| 59 | */ |
| 60 | |
| 61 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 62 | * Represents the data type information. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 63 | * |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 64 | * @param <T> YANG data type info |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 65 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 66 | public class YangType<T> |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 67 | implements Parsable, Resolvable, Serializable { |
| 68 | |
| 69 | private static final long serialVersionUID = 8062016054L; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 70 | |
| 71 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 72 | * YANG node identifier. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 73 | */ |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 74 | private YangNodeIdentifier nodeIdentifier; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * YANG data type. |
| 78 | */ |
| 79 | private YangDataTypes dataType; |
| 80 | |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 81 | /** |
| 82 | * Additional information about data type, example restriction info, named |
| 83 | * values, etc. The extra information is based on the data type. Based on |
| 84 | * the data type, the extended info can vary. |
| 85 | */ |
| 86 | private T dataTypeExtendedInfo; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 87 | |
| 88 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 89 | * Status of resolution. If completely resolved enum value is "RESOLVED", |
| 90 | * if not enum value is "UNRESOLVED", in case reference of grouping/typedef |
| 91 | * 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] | 92 | * "INTRA_FILE_RESOLVED". |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 93 | */ |
| 94 | private ResolvableStatus resolvableStatus; |
| 95 | |
| 96 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 97 | * Creates a YANG type object. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 98 | */ |
| 99 | public YangType() { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 100 | |
| 101 | nodeIdentifier = new YangNodeIdentifier(); |
| 102 | resolvableStatus = ResolvableStatus.UNRESOLVED; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns prefix associated with data type name. |
| 107 | * |
| 108 | * @return prefix associated with data type name |
| 109 | */ |
| 110 | public String getPrefix() { |
| 111 | return nodeIdentifier.getPrefix(); |
| 112 | } |
| 113 | |
| 114 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 115 | * Sets prefix associated with data type name. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 116 | * |
| 117 | * @param prefix prefix associated with data type name |
| 118 | */ |
| 119 | public void setPrefix(String prefix) { |
| 120 | nodeIdentifier.setPrefix(prefix); |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 124 | * Returns the name of data type. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 125 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 126 | * @return the name of data type |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 127 | */ |
| 128 | public String getDataTypeName() { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 129 | return nodeIdentifier.getName(); |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 133 | * Sets the name of the data type. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 134 | * |
| 135 | * @param typeName the name to set |
| 136 | */ |
| 137 | public void setDataTypeName(String typeName) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 138 | nodeIdentifier.setName(typeName); |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 142 | * Returns the type of data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 143 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 144 | * @return the data type |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 145 | */ |
| 146 | public YangDataTypes getDataType() { |
| 147 | return dataType; |
| 148 | } |
| 149 | |
| 150 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 151 | * Sets the type of data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 152 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 153 | * @param dataType data type |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 154 | */ |
| 155 | public void setDataType(YangDataTypes dataType) { |
| 156 | this.dataType = dataType; |
| 157 | } |
| 158 | |
| 159 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 160 | * Returns the data type meta data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 161 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 162 | * @return the data type meta data |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 163 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 164 | public T getDataTypeExtendedInfo() { |
| 165 | return dataTypeExtendedInfo; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 169 | * Sets the data type meta data. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 170 | * |
| 171 | * @param dataTypeInfo the meta data to set |
| 172 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 173 | public void setDataTypeExtendedInfo(T dataTypeInfo) { |
| 174 | this.dataTypeExtendedInfo = dataTypeInfo; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 178 | * Returns node identifier. |
| 179 | * |
| 180 | * @return node identifier |
| 181 | */ |
| 182 | public YangNodeIdentifier getNodeIdentifier() { |
| 183 | return nodeIdentifier; |
| 184 | } |
| 185 | |
| 186 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 187 | * Sets node identifier. |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 188 | * |
| 189 | * @param nodeIdentifier the node identifier |
| 190 | */ |
| 191 | public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) { |
| 192 | this.nodeIdentifier = nodeIdentifier; |
| 193 | } |
| 194 | |
| 195 | /** |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 196 | * Resets the class attributes to its default value. |
| 197 | */ |
| 198 | public void resetYangType() { |
| 199 | nodeIdentifier = new YangNodeIdentifier(); |
| 200 | resolvableStatus = ResolvableStatus.UNRESOLVED; |
| 201 | dataType = null; |
| 202 | dataTypeExtendedInfo = null; |
| 203 | } |
| 204 | |
| 205 | /** |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 206 | * Returns the type of the parsed data. |
| 207 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 208 | * @return returns TYPE_DATA |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 209 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 210 | @Override |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 211 | public YangConstructType getYangConstructType() { |
| 212 | return YangConstructType.TYPE_DATA; |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 216 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 217 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 218 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 219 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 220 | @Override |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 221 | public void validateDataOnEntry() |
| 222 | throws DataModelException { |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 223 | // TODO auto-generated method stub, to be implemented by parser |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 227 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 228 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 229 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 230 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 231 | @Override |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 232 | public void validateDataOnExit() |
| 233 | throws DataModelException { |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 234 | // TODO auto-generated method stub, to be implemented by parser |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 235 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 236 | |
| 237 | @Override |
| 238 | public ResolvableStatus getResolvableStatus() { |
| 239 | return resolvableStatus; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void setResolvableStatus(ResolvableStatus resolvableStatus) { |
| 244 | this.resolvableStatus = resolvableStatus; |
| 245 | } |
| 246 | |
| 247 | @Override |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 248 | public Object resolve() |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 249 | throws DataModelException { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 250 | /* |
| 251 | * Check whether the data type is derived. |
| 252 | */ |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 253 | if (getDataType() != DERIVED) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 254 | throw new DataModelException("Linker Error: Resolve should only be called for derived data types."); |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 255 | } |
| 256 | |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 257 | // Check if the derived info is present. |
| 258 | YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo(); |
| 259 | if (derivedInfo == null) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 260 | throw new DataModelException("Linker Error: Derived information is missing."); |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 261 | } |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 262 | |
| 263 | // Initiate the resolution |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 264 | try { |
| 265 | setResolvableStatus(derivedInfo.resolve()); |
| 266 | } catch (DataModelException e) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 267 | throw new DataModelException(e.getMessage()); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 268 | } |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 269 | return null; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 270 | } |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Validates the input data value against the permissible value for the |
| 274 | * type as per the YANG file. |
| 275 | * |
| 276 | * @param value input data value |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 277 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 278 | */ |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 279 | public void isValidValue(String value) throws DataModelException { |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 280 | switch (getDataType()) { |
| 281 | case INT8: |
| 282 | case INT16: |
| 283 | case INT32: |
| 284 | case INT64: |
| 285 | case UINT8: |
| 286 | case UINT16: |
| 287 | case UINT32: |
| 288 | case UINT64: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 289 | if (getDataTypeExtendedInfo() == null) { |
| 290 | getDataObjectFromString(value, getDataType()); |
| 291 | } else { |
| 292 | ((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value); |
| 293 | } |
| 294 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 295 | } |
| 296 | case DECIMAL64: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 297 | // Fraction-Digits and range needs to get it from yang |
| 298 | YangDecimal64<YangRangeRestriction> decimal64 = |
| 299 | (YangDecimal64<YangRangeRestriction>) getDataTypeExtendedInfo(); |
| 300 | validateDecimal64(value, decimal64.getFractionDigit(), |
| 301 | decimal64.getRangeRestrictedExtendedInfo()); |
| 302 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 303 | } |
| 304 | case STRING: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 305 | if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value) && |
| 306 | ((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnPatternRestriction(value))) { |
| 307 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 308 | "string"); |
| 309 | } |
| 310 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 311 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 312 | case BOOLEAN: |
| 313 | if (!(value.equals(DataModelUtils.TRUE) || value.equals(DataModelUtils.FALSE))) { |
| 314 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 315 | "boolean"); |
| 316 | } |
| 317 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 318 | case ENUMERATION: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 319 | Iterator<YangEnum> iterator = ((YangEnumeration) getDataTypeExtendedInfo()).getEnumSet().iterator(); |
| 320 | boolean isValidated = false; |
| 321 | while (iterator.hasNext()) { |
| 322 | YangEnum enumTemp = (YangEnum) iterator.next(); |
| 323 | if (enumTemp.getNamedValue().equals(value)) { |
| 324 | isValidated = true; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (!isValidated) { |
| 330 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 331 | "union"); |
| 332 | } |
| 333 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 334 | } |
| 335 | case BITS: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 336 | YangBits bits = (YangBits) getDataTypeExtendedInfo(); |
| 337 | if (bits.fromString(value) == null) { |
| 338 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 339 | "bits"); |
| 340 | } |
| 341 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 342 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 343 | case BINARY: { |
| 344 | if (!isValidBinary(value, (YangRangeRestriction) getDataTypeExtendedInfo())) { |
| 345 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 346 | "binary"); |
| 347 | } |
| 348 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 349 | } |
| 350 | case LEAFREF: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 351 | YangLeafRef<?> leafRef = (YangLeafRef<?>) getDataTypeExtendedInfo(); |
| 352 | leafRef.validateDataOnExit(); |
| 353 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 354 | } |
| 355 | case IDENTITYREF: { |
| 356 | // TODO TBD |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 357 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 358 | } |
| 359 | case EMPTY: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 360 | throw new DataTypeException("YANG file error : Input value \"" + value |
| 361 | + "\" is not a allowed for a data type " + "empty"); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 362 | } |
| 363 | case UNION: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 364 | ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList() |
| 365 | .listIterator(); |
| 366 | boolean isValidated = false; |
| 367 | while (listIterator.hasNext()) { |
| 368 | YangType<?> type = (YangType<?>) listIterator.next(); |
| 369 | try { |
| 370 | type.isValidValue(value); |
| 371 | // If it is not thrown exception then validation is success |
| 372 | isValidated = true; |
| 373 | break; |
| 374 | } catch (Exception e) { |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (!isValidated) { |
| 379 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 380 | "union"); |
| 381 | } |
| 382 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 383 | } |
| 384 | case INSTANCE_IDENTIFIER: { |
| 385 | // TODO TBD |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 386 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 387 | } |
| 388 | case DERIVED: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 389 | YangDataTypes dataType = ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType(); |
| 390 | if (isOfRangeRestrictedType(dataType)) { |
| 391 | if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) { |
| 392 | getDataObjectFromString(value, |
| 393 | ((YangDerivedInfo) getDataTypeExtendedInfo()) |
| 394 | .getEffectiveBuiltInType()); |
| 395 | } else { |
| 396 | if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo()) |
| 397 | .getResolvedExtendedInfo()).isValidValueString(value)) { |
| 398 | throw new DataTypeException("YANG file error : Input value \"" + value |
| 399 | + "\" is not a valid " + dataType.toString()); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 400 | } |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 401 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 402 | } else if (dataType == YangDataTypes.STRING) { |
| 403 | if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() != null) { |
| 404 | YangStringRestriction stringRestriction = |
| 405 | ((YangStringRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo()) |
| 406 | .getResolvedExtendedInfo()); |
| 407 | if (!(stringRestriction.isValidStringOnLengthRestriction(value) && |
| 408 | stringRestriction.isValidStringOnPatternRestriction(value))) { |
| 409 | throw new DataTypeException("YANG file error : Input value \"" + value |
| 410 | + "\" is not a valid " + dataType.toString()); |
| 411 | } |
| 412 | } |
| 413 | } else if (dataType == YangDataTypes.BITS) { |
| 414 | YangBits bits = (YangBits) getDataTypeExtendedInfo(); |
| 415 | if (bits.fromString(value) == null) { |
| 416 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 417 | "bits"); |
| 418 | } |
| 419 | } else if (dataType == YangDataTypes.BINARY) { |
| 420 | if (!isValidBinary(value, (YangRangeRestriction) ((YangDerivedInfo) |
| 421 | getDataTypeExtendedInfo()).getResolvedExtendedInfo())) { |
| 422 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 423 | dataType.toString()); |
| 424 | } |
| 425 | } else if (dataType == YangDataTypes.DECIMAL64) { |
| 426 | YangDerivedInfo derivedInfo = (YangDerivedInfo) getDataTypeExtendedInfo(); |
| 427 | YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); |
| 428 | YangType<YangDecimal64> decimal64Type = |
| 429 | (YangType<YangDecimal64>) typedef.getTypeList().iterator().next(); |
| 430 | YangDecimal64<YangRangeRestriction> decimal64 = decimal64Type.getDataTypeExtendedInfo(); |
| 431 | // Fraction-Digits and range needs to get it from yang |
| 432 | validateDecimal64(value, decimal64.getFractionDigit(), |
| 433 | decimal64.getRangeRestrictedExtendedInfo()); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 434 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 435 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 436 | } |
| 437 | default: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 438 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is for unsupported " + |
| 439 | "data type."); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 440 | } |
| 441 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 442 | } |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * Checks whether specific string is valid decimal64 value. |
| 447 | * |
| 448 | * @param value decimal64 value |
| 449 | */ |
| 450 | private void validateDecimal64(String value, int fractionDigit, YangRangeRestriction rangeRestriction) |
| 451 | throws DataModelException { |
| 452 | YangDecimal64<YangRangeRestriction> decimal64 = YangDecimal64.fromString(value); |
| 453 | decimal64.setFractionDigit(fractionDigit); |
| 454 | decimal64.setRangeRestrictedExtendedInfo(rangeRestriction); |
| 455 | decimal64.validateDecimal64(); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 459 | * Checks whether specific string is valid binary. |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 460 | * |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 461 | * @param value binary value |
| 462 | * @return true if validation success otherwise false |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 463 | */ |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 464 | private boolean isValidBinary(String value, YangRangeRestriction lengthRestriction) { |
| 465 | YangBinary binary = new YangBinary(value); |
| 466 | |
| 467 | // After decoding binary, its length should not be zero |
| 468 | if (binary.getBinaryData().length == 0) { |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 469 | return false; |
| 470 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 471 | |
| 472 | if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null |
| 473 | || lengthRestriction.getAscendingRangeIntervals().isEmpty()) { |
| 474 | // Length restriction is optional |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 479 | .listIterator(); |
| 480 | boolean isMatched = false; |
| 481 | while (rangeListIterator.hasNext()) { |
| 482 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 483 | BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue(); |
| 484 | BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue(); |
| 485 | // convert (encode) back and check length |
| 486 | if ((binary.toString().length() >= startValue.intValue()) && |
| 487 | (binary.toString().length() <= endValue.intValue())) { |
| 488 | isMatched = true; |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return isMatched; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 494 | } |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 495 | } |