blob: d331a7243ca5c8aa01652c73e5d6daf308fa213b [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 "include" statement is used to make content from a submodule
29 * available to that submodule's parent module, or to another submodule
30 * of that parent module. The argument is an identifier that is the
31 * name of the submodule to include.
32 * The includes's Substatements
33 *
34 * +---------------+---------+-------------+------------------+
35 * | substatement | section | cardinality |data model mapping|
36 * +---------------+---------+-------------+------------------+
37 * | revision-date | 7.1.5.1 | 0..1 | string |
38 * +---------------+---------+-------------+------------------+
39 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053040
Vinod Kumar S19f39c72016-02-09 20:12:31 +053041/**
Bharat saraswald9822e92016-04-05 15:13:44 +053042 * Represents the information about the included sub-modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053043 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053044public class YangInclude
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053045 implements Parsable, LocationInfo {
Vinod Kumar S19f39c72016-02-09 20:12:31 +053046
47 /**
48 * Name of the sub-module that is being included.
49 */
50 private String subModuleName;
51
52 /**
53 * The include's "revision-date" statement is used to specify the exact
54 * version of the submodule to import.
55 */
56 private String revision;
57
58 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053059 * Reference to node which is included.
60 */
61 private YangNode includedNode;
62
63 // Error Line number.
64 private int lineNumber;
65
66 // Error character position.
67 private int charPosition;
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Creates a YANG include.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053071 */
72 public YangInclude() {
73 }
74
75 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Returns the name of included sub-module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053077 *
78 * @return the sub-module name
79 */
80 public String getSubModuleName() {
81 return subModuleName;
82 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Sets the name of included sub-modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053086 *
87 * @param subModuleName the sub-module name to set
88 */
89 public void setSubModuleName(String subModuleName) {
90 this.subModuleName = subModuleName;
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Returns the revision.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053095 *
96 * @return the revision
97 */
98 public String getRevision() {
99 return revision;
100 }
101
102 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530103 * Sets the revision.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530104 *
105 * @param revision the revision to set
106 */
107 public void setRevision(String revision) {
108 this.revision = revision;
109 }
110
111 /**
112 * Returns the type of parsed data.
113 *
114 * @return returns INCLUDE_DATA
115 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530116 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530117 public YangConstructType getYangConstructType() {
118 return YangConstructType.INCLUDE_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530119 }
120
121 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530122 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530123 *
124 * @throws DataModelException a violation of data model rules
125 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530126 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530127 public void validateDataOnEntry()
128 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530129 // TODO auto-generated method stub, to be implemented by parser
130
131 }
132
133 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530134 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530135 *
136 * @throws DataModelException a violation of data model rules
137 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530138 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530139 public void validateDataOnExit()
140 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530141 // TODO auto-generated method stub, to be implemented by parser
142
143 }
144
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530145 public YangNode getIncludedNode() {
146 return includedNode;
147 }
148
149 public void setIncludedNode(YangNode includedNode) {
150 this.includedNode = includedNode;
151 }
152
153 @Override
154 public int getLineNumber() {
155 return lineNumber;
156 }
157
158 @Override
159 public int getCharPosition() {
160 return charPosition;
161 }
162
163 @Override
164 public void setLineNumber(int lineNumber) {
165 this.lineNumber = lineNumber;
166 }
167
168 @Override
169 public void setCharPosition(int charPositionInLine) {
170 this.charPosition = charPositionInLine;
171 }
172
173 /**
174 * Adds reference to an include.
175 *
176 * @param yangFileInfoSet YANG file info set
177 * @return YANG sub module node
178 * @throws DataModelException a violation of data model rules
179 */
180 public YangSubModule addReferenceToInclude(Set<YangFileInfo> yangFileInfoSet) throws DataModelException {
181 String includedSubModuleName = getSubModuleName();
182 String includedSubModuleRevision = getRevision();
183 YangNode subModuleNode = null;
184
185 /*
186 * Find the included sub-module node for a given module name
187 * with a specified revision if revision is not null.
188 */
189 if (includedSubModuleRevision != null) {
190 String includedSubModuleNameWithRevision = includedSubModuleName + "@" + includedSubModuleRevision;
191 subModuleNode = findReferredNode(yangFileInfoSet, includedSubModuleNameWithRevision);
192 }
193
194 /*
195 * Find the imported sub module node for a given module name
196 * without revision if can't find with revision.
197 */
198 if (subModuleNode == null) {
199 subModuleNode = findReferredNode(yangFileInfoSet, includedSubModuleName);
200 }
201
202 if (subModuleNode != null) {
203 if (subModuleNode instanceof YangSubModule) {
204 if (getRevision() == null || getRevision().isEmpty()) {
205 setIncludedNode(subModuleNode);
206 return (YangSubModule) subModuleNode;
207 }
208 // Match revision if inclusion is with revision.
209 if (((YangSubModule) subModuleNode).getRevision().getRevDate().equals(includedSubModuleRevision)) {
210 setIncludedNode(subModuleNode);
211 return (YangSubModule) subModuleNode;
212 }
213 }
214 }
215 // Exception if there is no match.
216 DataModelException exception = new DataModelException("YANG file error : Included sub module " +
217 includedSubModuleName + "with a given revision is not found.");
218 exception.setLine(getLineNumber());
219 exception.setCharPosition(getCharPosition());
220 throw exception;
221 }
222
223 /**
224 * Reports an error when included sub-module doesn't meet condition that
225 * "included sub-modules should belong module, as defined by the
226 * "belongs-to" statement or sub-modules are only allowed to include other
227 * sub-modules belonging to the same module.
228 *
229 * @throws DataModelException a violation in data model rule
230 */
231 public void reportIncludeError() throws DataModelException {
232 DataModelException exception = new DataModelException("YANG file error : Included sub-module " +
233 getSubModuleName() + "doesn't belongs to parent module also it doesn't belongs" +
234 "to sub-module belonging to the same parent module.");
235 exception.setLine(getLineNumber());
236 exception.setCharPosition(getCharPosition());
237 throw exception;
238 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530239}