blob: 5970a2486ef05199e4f5aa57fd13c6d3a8d80bf9 [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 "include" statement is used to make content from a submodule
30 * available to that submodule's parent module, or to another submodule
31 * of that parent module. The argument is an identifier that is the
32 * name of the submodule to include.
33 * The includes's Substatements
34 *
35 * +---------------+---------+-------------+------------------+
36 * | substatement | section | cardinality |data model mapping|
37 * +---------------+---------+-------------+------------------+
38 * | revision-date | 7.1.5.1 | 0..1 | string |
39 * +---------------+---------+-------------+------------------+
40 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053041
Vinod Kumar S19f39c72016-02-09 20:12:31 +053042/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents the information about the included sub-modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053044 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053045public class YangInclude
Bharat saraswal96dfef02016-06-16 00:29:12 +053046 implements Parsable, LocationInfo, Serializable {
47
48 private static final long serialVersionUID = 806201644L;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053049
50 /**
51 * Name of the sub-module that is being included.
52 */
53 private String subModuleName;
54
55 /**
56 * The include's "revision-date" statement is used to specify the exact
57 * version of the submodule to import.
58 */
59 private String revision;
60
61 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053062 * Reference to node which is included.
63 */
64 private YangNode includedNode;
65
66 // Error Line number.
Bharat saraswal96dfef02016-06-16 00:29:12 +053067 private transient int lineNumber;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053068
69 // Error character position.
Bharat saraswal96dfef02016-06-16 00:29:12 +053070 private transient int charPosition;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053071
72 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Creates a YANG include.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053074 */
75 public YangInclude() {
76 }
77
78 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053079 * Returns the name of included sub-module.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053080 *
81 * @return the sub-module name
82 */
83 public String getSubModuleName() {
84 return subModuleName;
85 }
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Sets the name of included sub-modules.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053089 *
90 * @param subModuleName the sub-module name to set
91 */
92 public void setSubModuleName(String subModuleName) {
93 this.subModuleName = subModuleName;
94 }
95
96 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053097 * Returns the revision.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053098 *
99 * @return the revision
100 */
101 public String getRevision() {
102 return revision;
103 }
104
105 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 * Sets the revision.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530107 *
108 * @param revision the revision to set
109 */
110 public void setRevision(String revision) {
111 this.revision = revision;
112 }
113
114 /**
115 * Returns the type of parsed data.
116 *
117 * @return returns INCLUDE_DATA
118 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530120 public YangConstructType getYangConstructType() {
121 return YangConstructType.INCLUDE_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530122 }
123
124 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530125 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530126 *
127 * @throws DataModelException a violation of data model rules
128 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530129 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530130 public void validateDataOnEntry()
131 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530132 // TODO auto-generated method stub, to be implemented by parser
133
134 }
135
136 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530137 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530138 *
139 * @throws DataModelException a violation of data model rules
140 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530141 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530142 public void validateDataOnExit()
143 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530144 // TODO auto-generated method stub, to be implemented by parser
145
146 }
147
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530148 public YangNode getIncludedNode() {
149 return includedNode;
150 }
151
152 public void setIncludedNode(YangNode includedNode) {
153 this.includedNode = includedNode;
154 }
155
156 @Override
157 public int getLineNumber() {
158 return lineNumber;
159 }
160
161 @Override
162 public int getCharPosition() {
163 return charPosition;
164 }
165
166 @Override
167 public void setLineNumber(int lineNumber) {
168 this.lineNumber = lineNumber;
169 }
170
171 @Override
172 public void setCharPosition(int charPositionInLine) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530173 charPosition = charPositionInLine;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530174 }
175
176 /**
177 * Adds reference to an include.
178 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530179 * @param yangNodeSet YANG node set
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530180 * @return YANG sub module node
181 * @throws DataModelException a violation of data model rules
182 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530183 public YangSubModule addReferenceToInclude(Set<YangNode> yangNodeSet) throws DataModelException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530184 String includedSubModuleName = getSubModuleName();
185 String includedSubModuleRevision = getRevision();
186 YangNode subModuleNode = null;
187
188 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530189 * Find the included sub-module node for a given module name with a
190 * specified revision if revision is not null.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530191 */
192 if (includedSubModuleRevision != null) {
193 String includedSubModuleNameWithRevision = includedSubModuleName + "@" + includedSubModuleRevision;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530194 subModuleNode = findReferredNode(yangNodeSet, includedSubModuleNameWithRevision);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530195 }
196
197 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530198 * Find the imported sub module node for a given module name without
199 * revision if can't find with revision.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530200 */
201 if (subModuleNode == null) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530202 subModuleNode = findReferredNode(yangNodeSet, includedSubModuleName);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530203 }
204
205 if (subModuleNode != null) {
206 if (subModuleNode instanceof YangSubModule) {
207 if (getRevision() == null || getRevision().isEmpty()) {
208 setIncludedNode(subModuleNode);
209 return (YangSubModule) subModuleNode;
210 }
211 // Match revision if inclusion is with revision.
212 if (((YangSubModule) subModuleNode).getRevision().getRevDate().equals(includedSubModuleRevision)) {
213 setIncludedNode(subModuleNode);
214 return (YangSubModule) subModuleNode;
215 }
216 }
217 }
218 // Exception if there is no match.
219 DataModelException exception = new DataModelException("YANG file error : Included sub module " +
220 includedSubModuleName + "with a given revision is not found.");
221 exception.setLine(getLineNumber());
222 exception.setCharPosition(getCharPosition());
223 throw exception;
224 }
225
226 /**
227 * Reports an error when included sub-module doesn't meet condition that
228 * "included sub-modules should belong module, as defined by the
229 * "belongs-to" statement or sub-modules are only allowed to include other
230 * sub-modules belonging to the same module.
231 *
232 * @throws DataModelException a violation in data model rule
233 */
234 public void reportIncludeError() throws DataModelException {
235 DataModelException exception = new DataModelException("YANG file error : Included sub-module " +
236 getSubModuleName() + "doesn't belongs to parent module also it doesn't belongs" +
237 "to sub-module belonging to the same parent module.");
238 exception.setLine(getLineNumber());
239 exception.setCharPosition(getCharPosition());
240 throw exception;
241 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530242}