blob: a19167a5e641377d2b2b174470af64138e1659e4 [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;
20
Vinod Kumar Scf044422016-02-09 19:53:45 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.datamodel.utils.ResolvableStatus;
24import org.onosproject.yangutils.datamodel.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>
Bharat saraswal96dfef02016-06-16 00:29:12 +053058 implements Parsable, Resolvable, Serializable {
59
60 private static final long serialVersionUID = 8062016054L;
Vinod Kumar Scf044422016-02-09 19:53:45 +053061
62 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053063 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053064 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053065 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053066
67 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053068 * Java package in which the Java type is defined.
69 */
70 private String javaPackage;
71
72 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +053073 * YANG data type.
74 */
75 private YangDataTypes dataType;
76
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053077 /**
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 Scf044422016-02-09 19:53:45 +053083
84 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053085 * 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 Sd4deb062016-04-15 18:08:57 +053088 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053089 */
90 private ResolvableStatus resolvableStatus;
91
92 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053093 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +053094 */
95 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053096
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 saraswald9822e92016-04-05 15:13:44 +0530111 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530112 *
113 * @param prefix prefix associated with data type name
114 */
115 public void setPrefix(String prefix) {
116 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530117 }
118
119 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530120 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530121 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530122 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530123 */
124 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530125 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530126 }
127
128 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530129 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530130 *
131 * @param typeName the name to set
132 */
133 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530134 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530135 }
136
137 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530138 * Returns the Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530139 *
140 * @return Java package where the type is defined
141 */
142 public String getJavaPackage() {
143 return javaPackage;
144 }
145
146 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530147 * Sets Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530148 *
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 saraswald9822e92016-04-05 15:13:44 +0530156 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530157 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530158 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530159 */
160 public YangDataTypes getDataType() {
161 return dataType;
162 }
163
164 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530165 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530166 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530168 */
169 public void setDataType(YangDataTypes dataType) {
170 this.dataType = dataType;
171 }
172
173 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530174 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530175 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530176 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530177 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530178 public T getDataTypeExtendedInfo() {
179 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530180 }
181
182 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530183 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530184 *
185 * @param dataTypeInfo the meta data to set
186 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530187 public void setDataTypeExtendedInfo(T dataTypeInfo) {
188 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530189 }
190
191 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530192 * Returns node identifier.
193 *
194 * @return node identifier
195 */
196 public YangNodeIdentifier getNodeIdentifier() {
197 return nodeIdentifier;
198 }
199
200 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530201 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530202 *
203 * @param nodeIdentifier the node identifier
204 */
205 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
206 this.nodeIdentifier = nodeIdentifier;
207 }
208
209 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530210 * Returns the type of the parsed data.
211 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530212 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530213 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530214 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530215 public YangConstructType getYangConstructType() {
216 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530217 }
218
219 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530220 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530221 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530222 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530223 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530224 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530225 public void validateDataOnEntry()
226 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530227 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530228 }
229
230 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530231 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530232 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530233 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530234 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530235 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530236 public void validateDataOnExit()
237 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530238 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530239 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530240
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-Huawei2ee9e7e2016-06-01 14:30:22 +0530252 public void resolve()
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530253 throws DataModelException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530254 /*
255 * Check whether the data type is derived.
256 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530257 if (getDataType() != DERIVED) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530258 throw new DataModelException("Linker Error: Resolve should only be called for derived data types.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530259 }
260
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530261 // Check if the derived info is present.
262 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
263 if (derivedInfo == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530264 throw new DataModelException("Linker Error: Derived information is missing.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530265 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530266
267 // Initiate the resolution
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530268 try {
269 setResolvableStatus(derivedInfo.resolve());
270 } catch (DataModelException e) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530271 throw new DataModelException(e.getMessage());
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530272 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530273 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530274}