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