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