blob: 44216197eff828dc325fd57742899e92b0793b6f [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
19import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import org.onosproject.yangutils.linker.exceptions.LinkerException;
21import org.onosproject.yangutils.linker.impl.Resolvable;
22import org.onosproject.yangutils.linker.impl.ResolvableStatus;
Vinod Kumar Scf044422016-02-09 19:53:45 +053023import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053024import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar Scf044422016-02-09 19:53:45 +053025
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053026import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053027
Vinod Kumar Scf044422016-02-09 19:53:45 +053028/*
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 saraswald9822e92016-04-05 15:13:44 +053053 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053054 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053055 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053056 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053057public class YangType<T>
58 implements Parsable, Resolvable {
Vinod Kumar Scf044422016-02-09 19:53:45 +053059
60 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053061 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053062 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053063 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053064
65 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053066 * Java package in which the Java type is defined.
67 */
68 private String javaPackage;
69
70 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +053071 * YANG data type.
72 */
73 private YangDataTypes dataType;
74
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053075 /**
76 * Additional information about data type, example restriction info, named
77 * values, etc. The extra information is based on the data type. Based on
78 * the data type, the extended info can vary.
79 */
80 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053081
82 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053083 * Status of resolution. If completely resolved enum value is "RESOLVED",
84 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
85 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053086 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053087 */
88 private ResolvableStatus resolvableStatus;
89
90 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053091 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +053092 */
93 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053094
95 nodeIdentifier = new YangNodeIdentifier();
96 resolvableStatus = ResolvableStatus.UNRESOLVED;
97 }
98
99 /**
100 * Returns prefix associated with data type name.
101 *
102 * @return prefix associated with data type name
103 */
104 public String getPrefix() {
105 return nodeIdentifier.getPrefix();
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530110 *
111 * @param prefix prefix associated with data type name
112 */
113 public void setPrefix(String prefix) {
114 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530119 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530120 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530121 */
122 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530123 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530124 }
125
126 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530128 *
129 * @param typeName the name to set
130 */
131 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530132 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530133 }
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Returns the Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530137 *
138 * @return Java package where the type is defined
139 */
140 public String getJavaPackage() {
141 return javaPackage;
142 }
143
144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Sets Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530146 *
147 * @param javaPackage Java package where the type is defined
148 */
149 public void setJavaPackage(String javaPackage) {
150 this.javaPackage = javaPackage;
151 }
152
153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530155 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530156 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530157 */
158 public YangDataTypes getDataType() {
159 return dataType;
160 }
161
162 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530163 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530164 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530165 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530166 */
167 public void setDataType(YangDataTypes dataType) {
168 this.dataType = dataType;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530175 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530176 public T getDataTypeExtendedInfo() {
177 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530178 }
179
180 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530181 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530182 *
183 * @param dataTypeInfo the meta data to set
184 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530185 public void setDataTypeExtendedInfo(T dataTypeInfo) {
186 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530187 }
188
189 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530190 * Returns node identifier.
191 *
192 * @return node identifier
193 */
194 public YangNodeIdentifier getNodeIdentifier() {
195 return nodeIdentifier;
196 }
197
198 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530199 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530200 *
201 * @param nodeIdentifier the node identifier
202 */
203 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
204 this.nodeIdentifier = nodeIdentifier;
205 }
206
207 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530208 * Returns the type of the parsed data.
209 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530210 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530211 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530212 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530213 public YangConstructType getYangConstructType() {
214 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530215 }
216
217 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530218 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530219 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530220 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530221 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530222 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530223 public void validateDataOnEntry()
224 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530225 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530226 }
227
228 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530229 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530230 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530231 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530232 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530233 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530234 public void validateDataOnExit()
235 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530236 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530237 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530238
239 @Override
240 public ResolvableStatus getResolvableStatus() {
241 return resolvableStatus;
242 }
243
244 @Override
245 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
246 this.resolvableStatus = resolvableStatus;
247 }
248
249 @Override
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530250 public void resolve() throws LinkerException {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530251 /*
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530252 * Check whether the data type is derived.
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530253 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530254 if (getDataType() != DERIVED) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530255 throw new LinkerException("Linker Error: Resolve should only be called for derived data types.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530256 }
257
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530258 // Check if the derived info is present.
259 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
260 if (derivedInfo == null) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530261 throw new LinkerException("Linker Error: Derived information is missing.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530262 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530263
264 // Initiate the resolution
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530265 try {
266 setResolvableStatus(derivedInfo.resolve());
267 } catch (DataModelException e) {
268 throw new LinkerException(e.getMessage());
269 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530270 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530271}