blob: 48b796fd56513de22abe3aa5c70b02d86d3fcaa4 [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 "include" statement is used to make content from a submodule
31 * available to that submodule's parent module, or to another submodule
32 * of that parent module. The argument is an identifier that is the
33 * name of the submodule to include.
34 * The includes's Substatements
35 *
36 * +---------------+---------+-------------+------------------+
37 * | substatement | section | cardinality |data model mapping|
38 * +---------------+---------+-------------+------------------+
39 * | revision-date | 7.1.5.1 | 0..1 | string |
40 * +---------------+---------+-------------+------------------+
41 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053042
Vinod Kumar S19f39c72016-02-09 20:12:31 +053043/**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Represents the information about the included sub-modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053045 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053046public class YangInclude
Bharat saraswal96dfef02016-06-16 00:29:12 +053047 implements Parsable, LocationInfo, Serializable {
48
49 private static final long serialVersionUID = 806201644L;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053050
51 /**
52 * Name of the sub-module that is being included.
53 */
54 private String subModuleName;
55
56 /**
57 * The include's "revision-date" statement is used to specify the exact
58 * version of the submodule to import.
59 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053060 private Date revision;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053061
62 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053063 * Reference to node which is included.
64 */
65 private YangNode includedNode;
66
67 // Error Line number.
Bharat saraswal96dfef02016-06-16 00:29:12 +053068 private transient int lineNumber;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053069
70 // Error character position.
Bharat saraswal96dfef02016-06-16 00:29:12 +053071 private transient int charPosition;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053072
73 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053074 * Creates a YANG include.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053075 */
76 public YangInclude() {
77 }
78
79 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Returns the name of included sub-module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053081 *
82 * @return the sub-module name
83 */
84 public String getSubModuleName() {
85 return subModuleName;
86 }
87
88 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053089 * Sets the name of included sub-modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053090 *
91 * @param subModuleName the sub-module name to set
92 */
93 public void setSubModuleName(String subModuleName) {
94 this.subModuleName = subModuleName;
95 }
96
97 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053098 * Returns the revision.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053099 *
100 * @return the revision
101 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530102 public Date getRevision() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530103 return revision;
104 }
105
106 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530107 * Sets the revision.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530108 *
109 * @param revision the revision to set
110 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530111 public void setRevision(Date revision) {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530112 this.revision = revision;
113 }
114
115 /**
116 * Returns the type of parsed data.
117 *
118 * @return returns INCLUDE_DATA
119 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530120 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530121 public YangConstructType getYangConstructType() {
122 return YangConstructType.INCLUDE_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530123 }
124
125 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530126 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530127 *
128 * @throws DataModelException a violation of data model rules
129 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530130 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530131 public void validateDataOnEntry()
132 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530133 // TODO auto-generated method stub, to be implemented by parser
134
135 }
136
137 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530138 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530139 *
140 * @throws DataModelException a violation of data model rules
141 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530142 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530143 public void validateDataOnExit()
144 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530145 // TODO auto-generated method stub, to be implemented by parser
146
147 }
148
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530149 public YangNode getIncludedNode() {
150 return includedNode;
151 }
152
153 public void setIncludedNode(YangNode includedNode) {
154 this.includedNode = includedNode;
155 }
156
157 @Override
158 public int getLineNumber() {
159 return lineNumber;
160 }
161
162 @Override
163 public int getCharPosition() {
164 return charPosition;
165 }
166
167 @Override
168 public void setLineNumber(int lineNumber) {
169 this.lineNumber = lineNumber;
170 }
171
172 @Override
173 public void setCharPosition(int charPositionInLine) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530174 charPosition = charPositionInLine;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530175 }
176
177 /**
178 * Adds reference to an include.
179 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530180 * @param yangNodeSet YANG node set
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530181 * @return YANG sub module node
182 * @throws DataModelException a violation of data model rules
183 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530184 public YangSubModule addReferenceToInclude(Set<YangNode> yangNodeSet) throws DataModelException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530185 String includedSubModuleName = getSubModuleName();
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530186 Date includedSubModuleRevision = getRevision();
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530187 YangNode subModuleNode = null;
188
189 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530190 * Find the included sub-module node for a given module name with a
191 * specified revision if revision is not null.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530192 */
193 if (includedSubModuleRevision != null) {
194 String includedSubModuleNameWithRevision = includedSubModuleName + "@" + includedSubModuleRevision;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530195 subModuleNode = findReferredNode(yangNodeSet, includedSubModuleNameWithRevision);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530196 }
197
198 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530199 * Find the imported sub module node for a given module name without
200 * revision if can't find with revision.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530201 */
202 if (subModuleNode == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530203 subModuleNode = findReferredNode(yangNodeSet, includedSubModuleName);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530204 }
205
206 if (subModuleNode != null) {
207 if (subModuleNode instanceof YangSubModule) {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530208 if (getRevision() == null) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530209 setIncludedNode(subModuleNode);
210 return (YangSubModule) subModuleNode;
211 }
212 // Match revision if inclusion is with revision.
213 if (((YangSubModule) subModuleNode).getRevision().getRevDate().equals(includedSubModuleRevision)) {
214 setIncludedNode(subModuleNode);
215 return (YangSubModule) subModuleNode;
216 }
217 }
218 }
219 // Exception if there is no match.
220 DataModelException exception = new DataModelException("YANG file error : Included sub module " +
221 includedSubModuleName + "with a given revision is not found.");
222 exception.setLine(getLineNumber());
223 exception.setCharPosition(getCharPosition());
224 throw exception;
225 }
226
227 /**
228 * Reports an error when included sub-module doesn't meet condition that
229 * "included sub-modules should belong module, as defined by the
230 * "belongs-to" statement or sub-modules are only allowed to include other
231 * sub-modules belonging to the same module.
232 *
233 * @throws DataModelException a violation in data model rule
234 */
235 public void reportIncludeError() throws DataModelException {
236 DataModelException exception = new DataModelException("YANG file error : Included sub-module " +
237 getSubModuleName() + "doesn't belongs to parent module also it doesn't belongs" +
238 "to sub-module belonging to the same parent module.");
239 exception.setLine(getLineNumber());
240 exception.setCharPosition(getCharPosition());
241 throw exception;
242 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530243}