blob: d37a8c7e7b64598eca27f93e1ac4cd393eb11ade [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;
20import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053021import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar Scf044422016-02-09 19:53:45 +053022
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053023import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053024
Vinod Kumar Scf044422016-02-09 19:53:45 +053025/*
26 * Reference:RFC 6020.
27 * The "type" statement takes as an argument a string that is the name
28 * of a YANG built-in type or a derived type, followed by an optional
29 * block of sub-statements that are used to put further restrictions
30 * on the type.
31 *
32 * The restrictions that can be applied depend on the type being restricted.
33 * The type's sub-statements
34 *
35 * +------------------+---------+-------------+------------------------------------+
36 * | substatement | section | cardinality | mapped data type |
37 * +------------------+---------+-------------+------------------------------------+
38 * | bit | 9.7.4 | 0..n | - YangBit used in YangBits |
39 * | enum | 9.6.4 | 0..n | - YangEnum used in YangEnumeration |
40 * | length | 9.4.4 | 0..1 | - used for string |
41 * | path | 9.9.2 | 0..1 | - TODO leaf-ref |
42 * | pattern | 9.4.6 | 0..n | - used for string |
43 * | range | 9.2.4 | 0..1 | - used for integer data type |
44 * | require-instance | 9.13.2 | 0..1 | - TODO instance-identifier |
45 * | type | 7.4 | 0..n | - TODO union |
46 * +------------------+---------+-------------+------------------------------------+
47 */
48
49/**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Represents the data type information.
Vinod Kumar Scf044422016-02-09 19:53:45 +053051 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053052 * @param <T> YANG data type info
Vinod Kumar Scf044422016-02-09 19:53:45 +053053 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053054public class YangType<T>
55 implements Parsable, Resolvable {
Vinod Kumar Scf044422016-02-09 19:53:45 +053056
57 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053058 * YANG node identifier.
Vinod Kumar Scf044422016-02-09 19:53:45 +053059 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053060 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar Scf044422016-02-09 19:53:45 +053061
62 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053063 * Java package in which the Java type is defined.
64 */
65 private String javaPackage;
66
67 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +053068 * YANG data type.
69 */
70 private YangDataTypes dataType;
71
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053072 /**
73 * Additional information about data type, example restriction info, named
74 * values, etc. The extra information is based on the data type. Based on
75 * the data type, the extended info can vary.
76 */
77 private T dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +053078
79 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053080 * Status of resolution. If completely resolved enum value is "RESOLVED",
81 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
82 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053083 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053084 */
85 private ResolvableStatus resolvableStatus;
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Creates a YANG type object.
Vinod Kumar Scf044422016-02-09 19:53:45 +053089 */
90 public YangType() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053091
92 nodeIdentifier = new YangNodeIdentifier();
93 resolvableStatus = ResolvableStatus.UNRESOLVED;
94 }
95
96 /**
97 * Returns prefix associated with data type name.
98 *
99 * @return prefix associated with data type name
100 */
101 public String getPrefix() {
102 return nodeIdentifier.getPrefix();
103 }
104
105 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 * Sets prefix associated with data type name.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107 *
108 * @param prefix prefix associated with data type name
109 */
110 public void setPrefix(String prefix) {
111 nodeIdentifier.setPrefix(prefix);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530112 }
113
114 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530115 * Returns the name of data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530116 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530117 * @return the name of data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530118 */
119 public String getDataTypeName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530120 return nodeIdentifier.getName();
Vinod Kumar Scf044422016-02-09 19:53:45 +0530121 }
122
123 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530124 * Sets the name of the data type.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530125 *
126 * @param typeName the name to set
127 */
128 public void setDataTypeName(String typeName) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 nodeIdentifier.setName(typeName);
Vinod Kumar Scf044422016-02-09 19:53:45 +0530130 }
131
132 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530133 * Returns the Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530134 *
135 * @return Java package where the type is defined
136 */
137 public String getJavaPackage() {
138 return javaPackage;
139 }
140
141 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530142 * Sets Java package where the type is defined.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530143 *
144 * @param javaPackage Java package where the type is defined
145 */
146 public void setJavaPackage(String javaPackage) {
147 this.javaPackage = javaPackage;
148 }
149
150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Returns the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530152 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 * @return the data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530154 */
155 public YangDataTypes getDataType() {
156 return dataType;
157 }
158
159 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530160 * Sets the type of data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530161 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162 * @param dataType data type
Vinod Kumar Scf044422016-02-09 19:53:45 +0530163 */
164 public void setDataType(YangDataTypes dataType) {
165 this.dataType = dataType;
166 }
167
168 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530169 * Returns the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530170 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 * @return the data type meta data
Vinod Kumar Scf044422016-02-09 19:53:45 +0530172 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530173 public T getDataTypeExtendedInfo() {
174 return dataTypeExtendedInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530175 }
176
177 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530178 * Sets the data type meta data.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530179 *
180 * @param dataTypeInfo the meta data to set
181 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530182 public void setDataTypeExtendedInfo(T dataTypeInfo) {
183 this.dataTypeExtendedInfo = dataTypeInfo;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530184 }
185
186 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530187 * Returns node identifier.
188 *
189 * @return node identifier
190 */
191 public YangNodeIdentifier getNodeIdentifier() {
192 return nodeIdentifier;
193 }
194
195 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530196 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530197 *
198 * @param nodeIdentifier the node identifier
199 */
200 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
201 this.nodeIdentifier = nodeIdentifier;
202 }
203
204 /**
Vinod Kumar Scf044422016-02-09 19:53:45 +0530205 * Returns the type of the parsed data.
206 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530207 * @return returns TYPE_DATA
Vinod Kumar Scf044422016-02-09 19:53:45 +0530208 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530209 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530210 public YangConstructType getYangConstructType() {
211 return YangConstructType.TYPE_DATA;
Vinod Kumar Scf044422016-02-09 19:53:45 +0530212 }
213
214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530216 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530217 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530218 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530219 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530220 public void validateDataOnEntry()
221 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530222 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530223 }
224
225 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530226 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar Scf044422016-02-09 19:53:45 +0530227 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530228 * @throws DataModelException a violation of data model rules
Vinod Kumar Scf044422016-02-09 19:53:45 +0530229 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530230 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530231 public void validateDataOnExit()
232 throws DataModelException {
Vinod Kumar Scf044422016-02-09 19:53:45 +0530233 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar Scf044422016-02-09 19:53:45 +0530234 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530235
236 @Override
237 public ResolvableStatus getResolvableStatus() {
238 return resolvableStatus;
239 }
240
241 @Override
242 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
243 this.resolvableStatus = resolvableStatus;
244 }
245
246 @Override
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530247 public void resolve() throws DataModelException {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530248 /*
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530249 * Check whether the data type is derived.
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530250 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530251 if (getDataType() != DERIVED) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530252 throw new DataModelException("Linker Error: Resolve should only be called for derived data types.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530253 }
254
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530255 // Check if the derived info is present.
256 YangDerivedInfo<?> derivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
257 if (derivedInfo == null) {
258 throw new DataModelException("Linker Error: Derived information is missing.");
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530259 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530260
261 // Initiate the resolution
262 setResolvableStatus(derivedInfo.resolve());
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530263 }
Vinod Kumar Scf044422016-02-09 19:53:45 +0530264}