blob: f6405e1ba82979c824805e4f6dcfbadaaea19ed8 [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;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053019import java.util.Date;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import java.util.Set;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021
Vinod Kumar S19f39c72016-02-09 20:12:31 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053025
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053026import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
27
Vinod Kumar S19f39c72016-02-09 20:12:31 +053028/*
29 * Reference:RFC 6020.
30 * The "import" statement makes definitions from one module available
31 * inside another module or submodule. The argument is the name of the
32 * module to import, and the statement is followed by a block of
33 * sub statements that holds detailed import information.
34 * When a module is imported, the importing module may:
35 * o use any grouping and typedef defined at the top level in the
36 * imported module or its submodules.
37 *
38 * o use any extension, feature, and identity defined in the imported
39 * module or its submodules.
40 *
41 * o use any node in the imported module's schema tree in "must",
42 * "path", and "when" statements, or as the target node in "augment"
43 * and "deviation" statements.
44 *
45 * The mandatory "prefix" sub statement assigns a prefix for the imported
46 * module that is scoped to the importing module or submodule. Multiple
47 * "import" statements may be specified to import from different
48 * modules.
49 * When the optional "revision-date" sub-statement is present, any
50 * typedef, grouping, extension, feature, and identity referenced by
51 * definitions in the local module are taken from the specified revision
52 * of the imported module. It is an error if the specified revision of
53 * the imported module does not exist. If no "revision-date"
54 * sub-statement is present, it is undefined from which revision of the
55 * module they are taken.
56 *
57 * Multiple revisions of the same module MUST NOT be imported.
58 *
59 * The import's Substatements
60 *
61 * +---------------+---------+-------------+------------------+
62 * | substatement | section | cardinality |data model mapping|
63 * +---------------+---------+-------------+------------------+
64 * | prefix | 7.1.4 | 1 | string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053065 * | revision-date | 7.1.5.1 | 0..1 | Date |
Vinod Kumar S19f39c72016-02-09 20:12:31 +053066 * +---------------+---------+-------------+------------------+
67 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053068
Vinod Kumar S19f39c72016-02-09 20:12:31 +053069/**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Represents the information about the imported modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053071 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053072public class YangImport
Bharat saraswal96dfef02016-06-16 00:29:12 +053073 implements Parsable, LocationInfo, Serializable {
74
75 private static final long serialVersionUID = 806201642L;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053076
77 /**
78 * Name of the module that is being imported.
79 */
80 private String name;
81
82 /**
83 * Prefix used to identify the entities from the imported module.
84 */
85 private String prefixId;
86
87 /**
88 * Reference:RFC 6020.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053089 * <p>
Vinod Kumar S19f39c72016-02-09 20:12:31 +053090 * The import's "revision-date" statement is used to specify the exact
91 * version of the module to import. The "revision-date" statement MUST match
92 * the most recent "revision" statement in the imported module. organization
93 * which defined the YANG module.
94 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053095 private Date revision;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053096
97 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053098 * Reference to node which is imported.
99 */
100 private YangNode importedNode;
101
102 // Error Line number.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530103 private transient int lineNumber;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530104
105 // Error character position.
Bharat saraswal96dfef02016-06-16 00:29:12 +0530106 private transient int charPosition;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Creates a YANG import.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530110 */
111 public YangImport() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530112 }
113
114 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530115 * Returns the imported module name.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530116 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530117 * @return the module name
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530118 */
119 public String getModuleName() {
120 return name;
121 }
122
123 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530124 * Sets module name.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530125 *
126 * @param moduleName the module name to set
127 */
128 public void setModuleName(String moduleName) {
129 name = moduleName;
130 }
131
132 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530133 * Returns the prefix used to identify the entities from the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530134 *
135 * @return the prefix used to identify the entities from the imported
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530136 * module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530137 */
138 public String getPrefixId() {
139 return prefixId;
140 }
141
142 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530143 * Sets prefix identifier.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530144 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530145 * @param prefixId set the prefix identifier of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530146 */
147 public void setPrefixId(String prefixId) {
148 this.prefixId = prefixId;
149 }
150
151 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530152 * Returns the revision of the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530153 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530154 * @return the revision of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530155 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530156 public Date getRevision() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530157 return revision;
158 }
159
160 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530161 * Sets the revision of the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530162 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530163 * @param rev set the revision of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530164 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530165 public void setRevision(Date rev) {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530166 revision = rev;
167 }
168
169 /**
170 * Returns the type of the parsed data.
171 *
172 * @return returns IMPORT_DATA
173 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530174 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530175 public YangConstructType getYangConstructType() {
176 return YangConstructType.IMPORT_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530177 }
178
179 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530180 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530181 *
182 * @throws DataModelException a violation of data model rules
183 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530184 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530185 public void validateDataOnEntry()
186 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530187 // TODO auto-generated method stub, to be implemented by parser
188
189 }
190
191 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530192 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530193 *
194 * @throws DataModelException a violation of data model rules
195 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530196 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530197 public void validateDataOnExit()
198 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530199 // TODO auto-generated method stub, to be implemented by parser
200
201 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530202
203 /**
204 * Returns imported node.
205 *
206 * @return imported node
207 */
208 public YangNode getImportedNode() {
209 return importedNode;
210 }
211
212 /**
213 * Sets imported node.
214 *
215 * @param importedNode imported node
216 */
217 public void setImportedNode(YangNode importedNode) {
218 this.importedNode = importedNode;
219 }
220
221 @Override
222 public int getLineNumber() {
223 return lineNumber;
224 }
225
226 @Override
227 public int getCharPosition() {
228 return charPosition;
229 }
230
231 @Override
232 public void setLineNumber(int lineNumber) {
233 this.lineNumber = lineNumber;
234 }
235
236 @Override
237 public void setCharPosition(int charPositionInLine) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530238 charPosition = charPositionInLine;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530239 }
240
241 /**
242 * Adds reference to an import.
243 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530244 * @param yangNodeSet YANG file info set
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530245 * @throws DataModelException a violation of data model rules
246 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530247 public void addReferenceToImport(Set<YangNode> yangNodeSet) throws DataModelException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530248 String importedModuleName = getModuleName();
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530249 Date importedModuleRevision = getRevision();
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530250 YangNode moduleNode = null;
251 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530252 * Find the imported module node for a given module name with a
253 * specified revision if revision is not null.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530254 */
255 if (importedModuleRevision != null) {
256 String importedModuleNameWithRevision = importedModuleName + "@" + importedModuleRevision;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530257 moduleNode = findReferredNode(yangNodeSet, importedModuleNameWithRevision);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530258 }
259
260 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530261 * Find the imported module node for a given module name without
262 * revision if can't find with revision.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530263 */
264 if (moduleNode == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530265 moduleNode = findReferredNode(yangNodeSet, importedModuleName);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530266 }
267
268 if (moduleNode != null) {
269 if (moduleNode instanceof YangModule) {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530270 if (getRevision() == null) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530271 setImportedNode(moduleNode);
272 return;
273 }
274 // Match revision if import is with revision.
275 if (((YangModule) moduleNode).getRevision().getRevDate().equals(importedModuleRevision)) {
276 setImportedNode(moduleNode);
277 return;
278 }
279 }
280 }
281
282 // Exception if there is no match.
283 DataModelException exception = new DataModelException("YANG file error : Imported module "
284 + importedModuleName + " with revision " + importedModuleRevision + " is not found.");
285 exception.setLine(getLineNumber());
286 exception.setCharPosition(getCharPosition());
287 throw exception;
288 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530289}