Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | cd48133 | 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 | |
| 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 20 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 22 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 24 | |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 25 | /* |
| 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 50 | * Represents the data type information. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 51 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 52 | * @param <T> YANG data type info |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 53 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 54 | public class YangType<T> |
| 55 | implements Parsable, Resolvable { |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 56 | |
| 57 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 58 | * YANG node identifier. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 59 | */ |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 60 | private YangNodeIdentifier nodeIdentifier; |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 63 | * Java package in which the Java type is defined. |
| 64 | */ |
| 65 | private String javaPackage; |
| 66 | |
| 67 | /** |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 68 | * YANG data type. |
| 69 | */ |
| 70 | private YangDataTypes dataType; |
| 71 | |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 72 | /** |
| 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 S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 78 | |
| 79 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 80 | * 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 S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 83 | * "INTRA_FILE_RESOLVED". |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 84 | */ |
| 85 | private ResolvableStatus resolvableStatus; |
| 86 | |
| 87 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Creates a YANG type object. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 89 | */ |
| 90 | public YangType() { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 91 | |
| 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 106 | * Sets prefix associated with data type name. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 107 | * |
| 108 | * @param prefix prefix associated with data type name |
| 109 | */ |
| 110 | public void setPrefix(String prefix) { |
| 111 | nodeIdentifier.setPrefix(prefix); |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 115 | * Returns the name of data type. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 116 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 117 | * @return the name of data type |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 118 | */ |
| 119 | public String getDataTypeName() { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 120 | return nodeIdentifier.getName(); |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 124 | * Sets the name of the data type. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 125 | * |
| 126 | * @param typeName the name to set |
| 127 | */ |
| 128 | public void setDataTypeName(String typeName) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 129 | nodeIdentifier.setName(typeName); |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 133 | * Returns the Java package where the type is defined. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 134 | * |
| 135 | * @return Java package where the type is defined |
| 136 | */ |
| 137 | public String getJavaPackage() { |
| 138 | return javaPackage; |
| 139 | } |
| 140 | |
| 141 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 142 | * Sets Java package where the type is defined. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 143 | * |
| 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 151 | * Returns the type of data. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 152 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 153 | * @return the data type |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 154 | */ |
| 155 | public YangDataTypes getDataType() { |
| 156 | return dataType; |
| 157 | } |
| 158 | |
| 159 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 160 | * Sets the type of data. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 161 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 162 | * @param dataType data type |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 163 | */ |
| 164 | public void setDataType(YangDataTypes dataType) { |
| 165 | this.dataType = dataType; |
| 166 | } |
| 167 | |
| 168 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 169 | * Returns the data type meta data. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 170 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 171 | * @return the data type meta data |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 172 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 173 | public T getDataTypeExtendedInfo() { |
| 174 | return dataTypeExtendedInfo; |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 178 | * Sets the data type meta data. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 179 | * |
| 180 | * @param dataTypeInfo the meta data to set |
| 181 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 182 | public void setDataTypeExtendedInfo(T dataTypeInfo) { |
| 183 | this.dataTypeExtendedInfo = dataTypeInfo; |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 187 | * Returns node identifier. |
| 188 | * |
| 189 | * @return node identifier |
| 190 | */ |
| 191 | public YangNodeIdentifier getNodeIdentifier() { |
| 192 | return nodeIdentifier; |
| 193 | } |
| 194 | |
| 195 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 196 | * Sets node identifier. |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 197 | * |
| 198 | * @param nodeIdentifier the node identifier |
| 199 | */ |
| 200 | public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) { |
| 201 | this.nodeIdentifier = nodeIdentifier; |
| 202 | } |
| 203 | |
| 204 | /** |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 205 | * Returns the type of the parsed data. |
| 206 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 207 | * @return returns TYPE_DATA |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 208 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 209 | @Override |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 210 | public YangConstructType getYangConstructType() { |
| 211 | return YangConstructType.TYPE_DATA; |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 215 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 216 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 217 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 218 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 219 | @Override |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 220 | public void validateDataOnEntry() |
| 221 | throws DataModelException { |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 222 | // TODO auto-generated method stub, to be implemented by parser |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 226 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 227 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 228 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 229 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 230 | @Override |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 231 | public void validateDataOnExit() |
| 232 | throws DataModelException { |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 233 | // TODO auto-generated method stub, to be implemented by parser |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 234 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 235 | |
| 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 Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 247 | public void resolve() throws DataModelException { |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 248 | /* |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 249 | * Check whether the data type is derived. |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 250 | */ |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 251 | if (getDataType() != DERIVED) { |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 252 | throw new DataModelException("Linker Error: Resolve should only be called for derived data types."); |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 253 | } |
| 254 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 255 | // 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 S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 259 | } |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 260 | |
| 261 | // Initiate the resolution |
| 262 | setResolvableStatus(derivedInfo.resolve()); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 263 | } |
Vinod Kumar S | cd48133 | 2016-02-09 19:53:45 +0530 | [diff] [blame] | 264 | } |