blob: 0fcf663cb7b94a92ba52b42f949674bf218588df [file] [log] [blame]
Vinod Kumar S19f39c72016-02-09 20:12:31 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S19f39c72016-02-09 20:12:31 +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 */
16package org.onosproject.yangutils.datamodel;
17
Bharat saraswal96dfef02016-06-16 00:29:12 +053018import java.io.Serializable;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053019import java.util.Set;
Bharat saraswal96dfef02016-06-16 00:29:12 +053020
Vinod Kumar S19f39c72016-02-09 20:12:31 +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.YangConstructType;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053024
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053025import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
26
Vinod Kumar S19f39c72016-02-09 20:12:31 +053027/*
28 * Reference:RFC 6020.
29 * The "import" statement makes definitions from one module available
30 * inside another module or submodule. The argument is the name of the
31 * module to import, and the statement is followed by a block of
32 * sub statements that holds detailed import information.
33 * When a module is imported, the importing module may:
34 * o use any grouping and typedef defined at the top level in the
35 * imported module or its submodules.
36 *
37 * o use any extension, feature, and identity defined in the imported
38 * module or its submodules.
39 *
40 * o use any node in the imported module's schema tree in "must",
41 * "path", and "when" statements, or as the target node in "augment"
42 * and "deviation" statements.
43 *
44 * The mandatory "prefix" sub statement assigns a prefix for the imported
45 * module that is scoped to the importing module or submodule. Multiple
46 * "import" statements may be specified to import from different
47 * modules.
48 * When the optional "revision-date" sub-statement is present, any
49 * typedef, grouping, extension, feature, and identity referenced by
50 * definitions in the local module are taken from the specified revision
51 * of the imported module. It is an error if the specified revision of
52 * the imported module does not exist. If no "revision-date"
53 * sub-statement is present, it is undefined from which revision of the
54 * module they are taken.
55 *
56 * Multiple revisions of the same module MUST NOT be imported.
57 *
58 * The import's Substatements
59 *
60 * +---------------+---------+-------------+------------------+
61 * | substatement | section | cardinality |data model mapping|
62 * +---------------+---------+-------------+------------------+
63 * | prefix | 7.1.4 | 1 | string |
64 * | revision-date | 7.1.5.1 | 0..1 | string |
65 * +---------------+---------+-------------+------------------+
66 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053067
Vinod Kumar S19f39c72016-02-09 20:12:31 +053068/**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Represents the information about the imported modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053070 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053071public class YangImport
Bharat saraswal96dfef02016-06-16 00:29:12 +053072 implements Parsable, LocationInfo, Serializable {
73
74 private static final long serialVersionUID = 806201642L;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053075
76 /**
77 * Name of the module that is being imported.
78 */
79 private String name;
80
81 /**
82 * Prefix used to identify the entities from the imported module.
83 */
84 private String prefixId;
85
86 /**
87 * Reference:RFC 6020.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053088 * <p>
Vinod Kumar S19f39c72016-02-09 20:12:31 +053089 * The import's "revision-date" statement is used to specify the exact
90 * version of the module to import. The "revision-date" statement MUST match
91 * the most recent "revision" statement in the imported module. organization
92 * which defined the YANG module.
93 */
94 private String revision;
95
96 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053097 * Reference to node which is imported.
98 */
99 private YangNode importedNode;
100
101 // Error Line number.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530102 private transient int lineNumber;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530103
104 // Error character position.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530105 private transient int charPosition;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530106
107 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530108 * Creates a YANG import.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530109 */
110 public YangImport() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530111 }
112
113 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 * Returns the imported module name.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530115 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530116 * @return the module name
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530117 */
118 public String getModuleName() {
119 return name;
120 }
121
122 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530123 * Sets module name.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530124 *
125 * @param moduleName the module name to set
126 */
127 public void setModuleName(String moduleName) {
128 name = moduleName;
129 }
130
131 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 * Returns the prefix used to identify the entities from the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530133 *
134 * @return the prefix used to identify the entities from the imported
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530135 * module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530136 */
137 public String getPrefixId() {
138 return prefixId;
139 }
140
141 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530142 * Sets prefix identifier.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @param prefixId set the prefix identifier of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530145 */
146 public void setPrefixId(String prefixId) {
147 this.prefixId = prefixId;
148 }
149
150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Returns the revision of the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530152 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 * @return the revision of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530154 */
155 public String getRevision() {
156 return revision;
157 }
158
159 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530160 * Sets the revision of the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530161 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162 * @param rev set the revision of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530163 */
164 public void setRevision(String rev) {
165 revision = rev;
166 }
167
168 /**
169 * Returns the type of the parsed data.
170 *
171 * @return returns IMPORT_DATA
172 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530173 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 public YangConstructType getYangConstructType() {
175 return YangConstructType.IMPORT_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530176 }
177
178 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530179 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530180 *
181 * @throws DataModelException a violation of data model rules
182 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530183 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530184 public void validateDataOnEntry()
185 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530186 // TODO auto-generated method stub, to be implemented by parser
187
188 }
189
190 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530191 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530192 *
193 * @throws DataModelException a violation of data model rules
194 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530195 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530196 public void validateDataOnExit()
197 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530198 // TODO auto-generated method stub, to be implemented by parser
199
200 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530201
202 /**
203 * Returns imported node.
204 *
205 * @return imported node
206 */
207 public YangNode getImportedNode() {
208 return importedNode;
209 }
210
211 /**
212 * Sets imported node.
213 *
214 * @param importedNode imported node
215 */
216 public void setImportedNode(YangNode importedNode) {
217 this.importedNode = importedNode;
218 }
219
220 @Override
221 public int getLineNumber() {
222 return lineNumber;
223 }
224
225 @Override
226 public int getCharPosition() {
227 return charPosition;
228 }
229
230 @Override
231 public void setLineNumber(int lineNumber) {
232 this.lineNumber = lineNumber;
233 }
234
235 @Override
236 public void setCharPosition(int charPositionInLine) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530237 charPosition = charPositionInLine;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530238 }
239
240 /**
241 * Adds reference to an import.
242 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530243 * @param yangNodeSet YANG file info set
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530244 * @throws DataModelException a violation of data model rules
245 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530246 public void addReferenceToImport(Set<YangNode> yangNodeSet) throws DataModelException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530247 String importedModuleName = getModuleName();
248 String importedModuleRevision = getRevision();
249 YangNode moduleNode = null;
250 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530251 * Find the imported module node for a given module name with a
252 * specified revision if revision is not null.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530253 */
254 if (importedModuleRevision != null) {
255 String importedModuleNameWithRevision = importedModuleName + "@" + importedModuleRevision;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530256 moduleNode = findReferredNode(yangNodeSet, importedModuleNameWithRevision);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530257 }
258
259 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530260 * Find the imported module node for a given module name without
261 * revision if can't find with revision.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530262 */
263 if (moduleNode == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530264 moduleNode = findReferredNode(yangNodeSet, importedModuleName);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530265 }
266
267 if (moduleNode != null) {
268 if (moduleNode instanceof YangModule) {
269 if (getRevision() == null || getRevision().isEmpty()) {
270 setImportedNode(moduleNode);
271 return;
272 }
273 // Match revision if import is with revision.
274 if (((YangModule) moduleNode).getRevision().getRevDate().equals(importedModuleRevision)) {
275 setImportedNode(moduleNode);
276 return;
277 }
278 }
279 }
280
281 // Exception if there is no match.
282 DataModelException exception = new DataModelException("YANG file error : Imported module "
283 + importedModuleName + " with revision " + importedModuleRevision + " is not found.");
284 exception.setLine(getLineNumber());
285 exception.setCharPosition(getCharPosition());
286 throw exception;
287 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530288}