blob: 736c30061432b8d95c6da3903902c0b5613448f0 [file] [log] [blame]
Vinod Kumar S5a39e012016-02-16 01:37:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.yangutils.datamodel;
17
18import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
19import org.onosproject.yangutils.parser.Parsable;
Vinod Kumar S08710982016-03-03 19:55:30 +053020import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S5a39e012016-02-16 01:37:16 +053021
22/*-
23 * Reference RFC 6020.
24 *
25 * The "typedef" statement defines a new type that may be used locally in the
26 * module, in modules or submodules which include it, and by other modules that
27 * import from it. The new type is called the "derived type", and the type from
28 * which it was derived is called the "base type". All derived types can be
29 * traced back to a YANG built-in type.
30 *
31 * The "typedef" statement's argument is an identifier that is the name of the
32 * type to be defined, and MUST be followed by a block of sub-statements that
33 * holds detailed typedef information.
34 *
35 * The name of the type MUST NOT be one of the YANG built-in types. If the
36 * typedef is defined at the top level of a YANG module or submodule, the name
37 * of the type to be defined MUST be unique within the module.
38 * The typedef's sub-statements
39 *
40 * +--------------+---------+-------------+------------------+
41 * | substatement | section | cardinality |data model mapping|
42 * +--------------+---------+-------------+------------------+
43 * | default | 7.3.4 | 0..1 |-string |
44 * | description | 7.19.3 | 0..1 |-string |
45 * | reference | 7.19.4 | 0..1 |-string |
46 * | status | 7.19.2 | 0..1 |-YangStatus |
47 * | type | 7.3.2 | 1 |-yangType |
48 * | units | 7.3.3 | 0..1 |-string |
49 * +--------------+---------+-------------+------------------+
50 */
51/**
52 * Data model node to maintain information defined in YANG typedef.
53 */
54public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
55
Vinod Kumar S5a39e012016-02-16 01:37:16 +053056 /**
57 * Default value in string, needs to be converted to the target object,
58 * based on the type.
59 */
60 private String defaultValueInString;
61
62 /**
63 * Description of new type.
64 */
65 private String description;
66
67 /**
68 * reference string.
69 */
70 private String reference;
71
72 /**
73 * Status of the data type.
74 */
75 private YangStatusType status;
76
77 /**
Vinod Kumar S48848f72016-02-25 15:52:16 +053078 * Maintain the derived type information.
Vinod Kumar S5a39e012016-02-16 01:37:16 +053079 */
Vinod Kumar S48848f72016-02-25 15:52:16 +053080 private YangType<YangDerivedType> derivedType;
Vinod Kumar S5a39e012016-02-16 01:37:16 +053081
82 /**
83 * Units of the data type.
84 */
85 private String units;
86
87 /**
88 * Create a typedef node.
89 */
90 public YangTypeDef() {
91 super(YangNodeType.TYPEDEF_NODE);
92 }
93
Vinod Kumar S5a39e012016-02-16 01:37:16 +053094 /**
95 * Get the default value.
96 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053097 * @return the default value
Vinod Kumar S5a39e012016-02-16 01:37:16 +053098 */
99 public String getDefaultValueInString() {
100 return defaultValueInString;
101 }
102
103 /**
104 * Set the default value.
105 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530106 * @param defaultValueInString the default value
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530107 */
108 public void setDefaultValueInString(String defaultValueInString) {
109 this.defaultValueInString = defaultValueInString;
110 }
111
112 /**
113 * Get the description.
114 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530115 * @return the description
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530116 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530117 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530118 public String getDescription() {
119 return description;
120 }
121
122 /**
123 * Set the description.
124 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530125 * @param description set the description
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530126 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530127 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530128 public void setDescription(String description) {
129 this.description = description;
130 }
131
132 /**
133 * Get the textual reference.
134 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530135 * @return the reference
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530136 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530137 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530138 public String getReference() {
139 return reference;
140 }
141
142 /**
143 * Set the textual reference.
144 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530145 * @param reference the reference to set
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530146 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530147 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530148 public void setReference(String reference) {
149 this.reference = reference;
150 }
151
152 /**
153 * Get the status.
154 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530155 * @return the status
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530156 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530157 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530158 public YangStatusType getStatus() {
159 return status;
160 }
161
162 /**
163 * Set the status.
164 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530165 * @param status the status to set
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530166 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530167 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530168 public void setStatus(YangStatusType status) {
169 this.status = status;
170 }
171
172 /**
Vinod Kumar S48848f72016-02-25 15:52:16 +0530173 * Get the derived type.
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530174 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530175 * @return the derived type
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530176 */
Vinod Kumar S48848f72016-02-25 15:52:16 +0530177 public YangType<YangDerivedType> getDerivedType() {
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530178 return derivedType;
179 }
180
181 /**
Vinod Kumar S48848f72016-02-25 15:52:16 +0530182 * Set the derived type.
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530183 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530184 * @param derivedType the derived type
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530185 */
Vinod Kumar S48848f72016-02-25 15:52:16 +0530186 public void setDerivedType(YangType<YangDerivedType> derivedType) {
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530187 this.derivedType = derivedType;
188 }
189
190 /**
191 * Get the unit.
192 *
193 * @return the units
194 */
195 public String getUnits() {
196 return units;
197 }
198
199 /**
200 * Set the unit.
201 *
202 * @param units the units to set
203 */
204 public void setUnits(String units) {
205 this.units = units;
206 }
207
208 /**
209 * Returns the type of the data.
210 *
211 * @return returns TYPEDEF_DATA
212 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530213 @Override
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530214 public YangConstructType getYangConstructType() {
215 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530216 }
217
218 /**
219 * Validate the data on entering the corresponding parse tree node.
220 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530221 * @throws DataModelException a violation of data model rules
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530222 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530223 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530224 public void validateDataOnEntry() throws DataModelException {
225 // TODO auto-generated method stub, to be implemented by parser
226 }
227
228 /**
229 * Validate the data on exiting the corresponding parse tree node.
230 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530231 * @throws DataModelException a violation of data model rules
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530232 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530233 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530234 public void validateDataOnExit() throws DataModelException {
Vinod Kumar S48848f72016-02-25 15:52:16 +0530235 YangType<YangDerivedType> type = getDerivedType();
236 if (type == null) {
237 throw new DataModelException("Typedef does not have type info.");
238 }
Vinod Kumar S08710982016-03-03 19:55:30 +0530239 if (type.getDataType() != YangDataTypes.DERIVED
240 || type.getDataTypeName() == null) {
Vinod Kumar S48848f72016-02-25 15:52:16 +0530241 throw new DataModelException("Typedef type is not derived.");
242 }
243
244 YangDerivedType derivedTypeInfo = type.getDataTypeExtendedInfo();
245 if (derivedTypeInfo == null) {
246 throw new DataModelException("derrived type does not have derived info.");
247 }
248
249 YangType<?> baseType = derivedTypeInfo.getBaseType();
250 if (baseType == null) {
251 throw new DataModelException("Base type of a derived type is missing.");
252 }
253
254 if (derivedTypeInfo.getEffectiveYangBuiltInType() == null) {
255 /* resolve the effective type from the data tree. */
256 /*
257 * TODO: try to resolve the nested reference, if possible in the
258 * partial tree, otherwise we need to resolve finally when the
259 * complete module is created.
260 */
261 YangModule.addToResolveList(this);
262 }
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530263 }
264
Vinod Kumar Sc26bf192016-02-23 22:36:57 +0530265 /**
266 * Get the YANG name of the typedef.
267 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530268 * @return YANG name of the typedef
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530269 */
270 @Override
271 public String getName() {
Vinod Kumar S48848f72016-02-25 15:52:16 +0530272 if (getDerivedType() != null) {
273 return getDerivedType().getDataTypeName();
274 }
275 return null;
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530276 }
277
Vinod Kumar Sc26bf192016-02-23 22:36:57 +0530278 /**
279 * Set YANG name of the typedef.
280 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530281 * @param name YANG name of the typedef
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530282 */
283 @Override
284 public void setName(String name) {
Vinod Kumar S48848f72016-02-25 15:52:16 +0530285 if (getDerivedType() == null) {
286 throw new RuntimeException(
287 "Derrived Type info needs to be set in parser when the typedef listner is processed");
288 }
289 getDerivedType().setDataTypeName(name);
290 getDerivedType().setDataType(YangDataTypes.DERIVED);
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530291 }
292
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530293}