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 { |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 292 | if (!((YangRangeRestriction) getDataTypeExtendedInfo()).isValidValueString(value)) { |
| 293 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
| 294 | getDataType()); |
| 295 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 296 | } |
| 297 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 298 | } |
| 299 | case DECIMAL64: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 300 | // Fraction-Digits and range needs to get it from yang |
| 301 | YangDecimal64<YangRangeRestriction> decimal64 = |
| 302 | (YangDecimal64<YangRangeRestriction>) getDataTypeExtendedInfo(); |
| 303 | validateDecimal64(value, decimal64.getFractionDigit(), |
| 304 | decimal64.getRangeRestrictedExtendedInfo()); |
| 305 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 306 | } |
| 307 | case STRING: { |
Bharat saraswal | 6a5911f | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 308 | if (getDataTypeExtendedInfo() == null) { |
| 309 | break; |
| 310 | } else if (!(((YangStringRestriction) getDataTypeExtendedInfo()).isValidStringOnLengthRestriction(value) |
| 311 | && ((YangStringRestriction) getDataTypeExtendedInfo()) |
| 312 | .isValidStringOnPatternRestriction(value))) { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 313 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 314 | getDataType()); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 315 | } |
| 316 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 317 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 318 | case BOOLEAN: |
| 319 | if (!(value.equals(DataModelUtils.TRUE) || value.equals(DataModelUtils.FALSE))) { |
| 320 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 321 | getDataType()); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 322 | } |
| 323 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 324 | case ENUMERATION: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 325 | Iterator<YangEnum> iterator = ((YangEnumeration) getDataTypeExtendedInfo()).getEnumSet().iterator(); |
| 326 | boolean isValidated = false; |
| 327 | while (iterator.hasNext()) { |
Bharat saraswal | 6a5911f | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 328 | YangEnum enumTemp = iterator.next(); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 329 | if (enumTemp.getNamedValue().equals(value)) { |
| 330 | isValidated = true; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (!isValidated) { |
| 336 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 337 | getDataType()); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 338 | } |
| 339 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 340 | } |
| 341 | case BITS: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 342 | YangBits bits = (YangBits) getDataTypeExtendedInfo(); |
| 343 | if (bits.fromString(value) == null) { |
| 344 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 345 | getDataType()); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 346 | } |
| 347 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 348 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 349 | case BINARY: { |
| 350 | if (!isValidBinary(value, (YangRangeRestriction) getDataTypeExtendedInfo())) { |
| 351 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 352 | getDataType()); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 353 | } |
| 354 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 355 | } |
| 356 | case LEAFREF: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 357 | YangLeafRef<?> leafRef = (YangLeafRef<?>) getDataTypeExtendedInfo(); |
| 358 | leafRef.validateDataOnExit(); |
| 359 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 360 | } |
| 361 | case IDENTITYREF: { |
| 362 | // TODO TBD |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 363 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 364 | } |
| 365 | case EMPTY: { |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 366 | if (value.length() > 0) { |
| 367 | throw new DataTypeException("YANG file error : Input value \"" + value |
| 368 | + "\" is not allowed for a data type " + getDataType()); |
| 369 | } |
| 370 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 371 | } |
| 372 | case UNION: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 373 | ListIterator<YangType<?>> listIterator = ((YangUnion) getDataTypeExtendedInfo()).getTypeList() |
| 374 | .listIterator(); |
| 375 | boolean isValidated = false; |
| 376 | while (listIterator.hasNext()) { |
| 377 | YangType<?> type = (YangType<?>) listIterator.next(); |
| 378 | try { |
| 379 | type.isValidValue(value); |
| 380 | // If it is not thrown exception then validation is success |
| 381 | isValidated = true; |
| 382 | break; |
| 383 | } catch (Exception e) { |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (!isValidated) { |
| 388 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 389 | getDataType()); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 390 | } |
| 391 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 392 | } |
| 393 | case INSTANCE_IDENTIFIER: { |
| 394 | // TODO TBD |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 395 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 396 | } |
| 397 | case DERIVED: { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 398 | YangDataTypes dataType = ((YangDerivedInfo) getDataTypeExtendedInfo()).getEffectiveBuiltInType(); |
| 399 | if (isOfRangeRestrictedType(dataType)) { |
| 400 | if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() == null) { |
| 401 | getDataObjectFromString(value, |
| 402 | ((YangDerivedInfo) getDataTypeExtendedInfo()) |
| 403 | .getEffectiveBuiltInType()); |
| 404 | } else { |
| 405 | if (!((YangRangeRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo()) |
| 406 | .getResolvedExtendedInfo()).isValidValueString(value)) { |
| 407 | throw new DataTypeException("YANG file error : Input value \"" + value |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 408 | + "\" is not a valid " + dataType); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 409 | } |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 410 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 411 | } else if (dataType == YangDataTypes.STRING) { |
| 412 | if (((YangDerivedInfo) getDataTypeExtendedInfo()).getResolvedExtendedInfo() != null) { |
| 413 | YangStringRestriction stringRestriction = |
| 414 | ((YangStringRestriction) ((YangDerivedInfo) getDataTypeExtendedInfo()) |
| 415 | .getResolvedExtendedInfo()); |
| 416 | if (!(stringRestriction.isValidStringOnLengthRestriction(value) && |
| 417 | stringRestriction.isValidStringOnPatternRestriction(value))) { |
| 418 | throw new DataTypeException("YANG file error : Input value \"" + value |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 419 | + "\" is not a valid " + dataType); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } else if (dataType == YangDataTypes.BITS) { |
| 423 | YangBits bits = (YangBits) getDataTypeExtendedInfo(); |
| 424 | if (bits.fromString(value) == null) { |
| 425 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 426 | dataType); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 427 | } |
| 428 | } else if (dataType == YangDataTypes.BINARY) { |
| 429 | if (!isValidBinary(value, (YangRangeRestriction) ((YangDerivedInfo) |
| 430 | getDataTypeExtendedInfo()).getResolvedExtendedInfo())) { |
| 431 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" is not a valid " + |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 432 | dataType); |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 433 | } |
| 434 | } else if (dataType == YangDataTypes.DECIMAL64) { |
| 435 | YangDerivedInfo derivedInfo = (YangDerivedInfo) getDataTypeExtendedInfo(); |
| 436 | YangTypeDef typedef = (YangTypeDef) derivedInfo.getReferredTypeDef(); |
| 437 | YangType<YangDecimal64> decimal64Type = |
| 438 | (YangType<YangDecimal64>) typedef.getTypeList().iterator().next(); |
| 439 | YangDecimal64<YangRangeRestriction> decimal64 = decimal64Type.getDataTypeExtendedInfo(); |
| 440 | // Fraction-Digits and range needs to get it from yang |
| 441 | validateDecimal64(value, decimal64.getFractionDigit(), |
| 442 | decimal64.getRangeRestrictedExtendedInfo()); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 443 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 444 | break; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 445 | } |
| 446 | default: { |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 447 | throw new DataTypeException("YANG file error : Input value \"" + value + "\" received for " + |
| 448 | "unsupported data type " + getDataType()); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 449 | } |
| 450 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
| 454 | /** |
| 455 | * Checks whether specific string is valid decimal64 value. |
| 456 | * |
| 457 | * @param value decimal64 value |
| 458 | */ |
| 459 | private void validateDecimal64(String value, int fractionDigit, YangRangeRestriction rangeRestriction) |
| 460 | throws DataModelException { |
| 461 | YangDecimal64<YangRangeRestriction> decimal64 = YangDecimal64.fromString(value); |
| 462 | decimal64.setFractionDigit(fractionDigit); |
| 463 | decimal64.setRangeRestrictedExtendedInfo(rangeRestriction); |
| 464 | decimal64.validateDecimal64(); |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 468 | * Checks whether specific string is valid binary. |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 469 | * |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 470 | * @param value binary value |
| 471 | * @return true if validation success otherwise false |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 472 | */ |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 473 | private boolean isValidBinary(String value, YangRangeRestriction lengthRestriction) { |
| 474 | YangBinary binary = new YangBinary(value); |
| 475 | |
| 476 | // After decoding binary, its length should not be zero |
| 477 | if (binary.getBinaryData().length == 0) { |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 478 | return false; |
| 479 | } |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame] | 480 | |
| 481 | if (lengthRestriction == null || lengthRestriction.getAscendingRangeIntervals() == null |
| 482 | || lengthRestriction.getAscendingRangeIntervals().isEmpty()) { |
| 483 | // Length restriction is optional |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | ListIterator<YangRangeInterval<YangUint64>> rangeListIterator = lengthRestriction.getAscendingRangeIntervals() |
| 488 | .listIterator(); |
| 489 | boolean isMatched = false; |
| 490 | while (rangeListIterator.hasNext()) { |
| 491 | YangRangeInterval rangeInterval = rangeListIterator.next(); |
| 492 | BigInteger startValue = ((YangUint64) rangeInterval.getStartValue()).getValue(); |
| 493 | BigInteger endValue = ((YangUint64) rangeInterval.getEndValue()).getValue(); |
| 494 | // convert (encode) back and check length |
| 495 | if ((binary.toString().length() >= startValue.intValue()) && |
| 496 | (binary.toString().length() <= endValue.intValue())) { |
| 497 | isMatched = true; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return isMatched; |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 503 | } |
Vinod Kumar S | cf04442 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 504 | } |