blob: ef5d25a8e3521885f30b3a9660d6bfd024350f6e [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
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053018import java.util.Set;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053019import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053021import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053023
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053024import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
25
Vinod Kumar S19f39c72016-02-09 20:12:31 +053026/*
27 * Reference:RFC 6020.
28 * The "import" statement makes definitions from one module available
29 * inside another module or submodule. The argument is the name of the
30 * module to import, and the statement is followed by a block of
31 * sub statements that holds detailed import information.
32 * When a module is imported, the importing module may:
33 * o use any grouping and typedef defined at the top level in the
34 * imported module or its submodules.
35 *
36 * o use any extension, feature, and identity defined in the imported
37 * module or its submodules.
38 *
39 * o use any node in the imported module's schema tree in "must",
40 * "path", and "when" statements, or as the target node in "augment"
41 * and "deviation" statements.
42 *
43 * The mandatory "prefix" sub statement assigns a prefix for the imported
44 * module that is scoped to the importing module or submodule. Multiple
45 * "import" statements may be specified to import from different
46 * modules.
47 * When the optional "revision-date" sub-statement is present, any
48 * typedef, grouping, extension, feature, and identity referenced by
49 * definitions in the local module are taken from the specified revision
50 * of the imported module. It is an error if the specified revision of
51 * the imported module does not exist. If no "revision-date"
52 * sub-statement is present, it is undefined from which revision of the
53 * module they are taken.
54 *
55 * Multiple revisions of the same module MUST NOT be imported.
56 *
57 * The import's Substatements
58 *
59 * +---------------+---------+-------------+------------------+
60 * | substatement | section | cardinality |data model mapping|
61 * +---------------+---------+-------------+------------------+
62 * | prefix | 7.1.4 | 1 | string |
63 * | revision-date | 7.1.5.1 | 0..1 | string |
64 * +---------------+---------+-------------+------------------+
65 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053066
Vinod Kumar S19f39c72016-02-09 20:12:31 +053067/**
Bharat saraswald9822e92016-04-05 15:13:44 +053068 * Represents the information about the imported modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053069 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053070public class YangImport
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053071 implements Parsable, LocationInfo {
Vinod Kumar S19f39c72016-02-09 20:12:31 +053072
73 /**
74 * Name of the module that is being imported.
75 */
76 private String name;
77
78 /**
79 * Prefix used to identify the entities from the imported module.
80 */
81 private String prefixId;
82
83 /**
84 * Reference:RFC 6020.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053085 * <p>
Vinod Kumar S19f39c72016-02-09 20:12:31 +053086 * The import's "revision-date" statement is used to specify the exact
87 * version of the module to import. The "revision-date" statement MUST match
88 * the most recent "revision" statement in the imported module. organization
89 * which defined the YANG module.
90 */
91 private String revision;
92
93 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053094 * Reference to node which is imported.
95 */
96 private YangNode importedNode;
97
98 // Error Line number.
99 private int lineNumber;
100
101 // Error character position.
102 private int charPosition;
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Creates a YANG import.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530106 */
107 public YangImport() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530108 }
109
110 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530111 * Returns the imported module name.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530112 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 * @return the module name
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530114 */
115 public String getModuleName() {
116 return name;
117 }
118
119 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530120 * Sets module name.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530121 *
122 * @param moduleName the module name to set
123 */
124 public void setModuleName(String moduleName) {
125 name = moduleName;
126 }
127
128 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530129 * Returns the prefix used to identify the entities from the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530130 *
131 * @return the prefix used to identify the entities from the imported
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530132 * module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530133 */
134 public String getPrefixId() {
135 return prefixId;
136 }
137
138 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530139 * Sets prefix identifier.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530140 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530141 * @param prefixId set the prefix identifier of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530142 */
143 public void setPrefixId(String prefixId) {
144 this.prefixId = prefixId;
145 }
146
147 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530148 * Returns the revision of the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530149 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530150 * @return the revision of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530151 */
152 public String getRevision() {
153 return revision;
154 }
155
156 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530157 * Sets the revision of the imported module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530158 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530159 * @param rev set the revision of the imported module
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530160 */
161 public void setRevision(String rev) {
162 revision = rev;
163 }
164
165 /**
166 * Returns the type of the parsed data.
167 *
168 * @return returns IMPORT_DATA
169 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530170 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 public YangConstructType getYangConstructType() {
172 return YangConstructType.IMPORT_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530173 }
174
175 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530176 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530177 *
178 * @throws DataModelException a violation of data model rules
179 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530180 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530181 public void validateDataOnEntry()
182 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530183 // TODO auto-generated method stub, to be implemented by parser
184
185 }
186
187 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530188 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530189 *
190 * @throws DataModelException a violation of data model rules
191 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530192 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530193 public void validateDataOnExit()
194 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530195 // TODO auto-generated method stub, to be implemented by parser
196
197 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530198
199 /**
200 * Returns imported node.
201 *
202 * @return imported node
203 */
204 public YangNode getImportedNode() {
205 return importedNode;
206 }
207
208 /**
209 * Sets imported node.
210 *
211 * @param importedNode imported node
212 */
213 public void setImportedNode(YangNode importedNode) {
214 this.importedNode = importedNode;
215 }
216
217 @Override
218 public int getLineNumber() {
219 return lineNumber;
220 }
221
222 @Override
223 public int getCharPosition() {
224 return charPosition;
225 }
226
227 @Override
228 public void setLineNumber(int lineNumber) {
229 this.lineNumber = lineNumber;
230 }
231
232 @Override
233 public void setCharPosition(int charPositionInLine) {
234 this.charPosition = charPositionInLine;
235 }
236
237 /**
238 * Adds reference to an import.
239 *
240 * @param yangFileInfoSet YANG file info set
241 * @throws DataModelException a violation of data model rules
242 */
243 public void addReferenceToImport(Set<YangFileInfo> yangFileInfoSet) throws DataModelException {
244 String importedModuleName = getModuleName();
245 String importedModuleRevision = getRevision();
246 YangNode moduleNode = null;
247 /*
248 * Find the imported module node for a given module name
249 * with a specified revision if revision is not null.
250 */
251 if (importedModuleRevision != null) {
252 String importedModuleNameWithRevision = importedModuleName + "@" + importedModuleRevision;
253 moduleNode = findReferredNode(yangFileInfoSet, importedModuleNameWithRevision);
254 }
255
256 /*
257 * Find the imported module node for a given module name
258 * without revision if can't find with revision.
259 */
260 if (moduleNode == null) {
261 moduleNode = findReferredNode(yangFileInfoSet, importedModuleName);
262 }
263
264 if (moduleNode != null) {
265 if (moduleNode instanceof YangModule) {
266 if (getRevision() == null || getRevision().isEmpty()) {
267 setImportedNode(moduleNode);
268 return;
269 }
270 // Match revision if import is with revision.
271 if (((YangModule) moduleNode).getRevision().getRevDate().equals(importedModuleRevision)) {
272 setImportedNode(moduleNode);
273 return;
274 }
275 }
276 }
277
278 // Exception if there is no match.
279 DataModelException exception = new DataModelException("YANG file error : Imported module "
280 + importedModuleName + " with revision " + importedModuleRevision + " is not found.");
281 exception.setLine(getLineNumber());
282 exception.setCharPosition(getCharPosition());
283 throw exception;
284 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530285}