blob: 694df3213e4944b1173e3ab8da6fdbb2c29d81eb [file] [log] [blame]
Vinod Kumar S67e7be62016-02-11 20:13:28 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S67e7be62016-02-11 20:13:28 +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 S67e7be62016-02-11 20:13:28 +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 S67e7be62016-02-11 20:13:28 +053023
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053024import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
25
Vinod Kumar S67e7be62016-02-11 20:13:28 +053026/*-
27 * Reference 6020.
28 *
29 * The "belongs-to" statement specifies the module to which the
30 * submodule belongs. The argument is an identifier that is the name of
31 * the module.
32 *
33 * A submodule MUST only be included by the module to which it belongs,
34 * or by another submodule that belongs to that module.
35 *
36 * The mandatory "prefix" sub-statement assigns a prefix for the module
37 * to which the submodule belongs. All definitions in the local
38 * submodule and any included submodules can be accessed by using the
39 * prefix.
40 *
41 * The belongs-to's sub-statements
42 *
43 * +--------------+---------+-------------+
44 * | substatement | section | cardinality |
45 * +--------------+---------+-------------+
46 * | prefix | 7.1.4 | 1 |
47 * +--------------+---------+-------------+
48 */
49
50/**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Represents the belongs-to data type information.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053052 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053053public class YangBelongsTo implements Parsable, LocationInfo {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053054
55 /**
56 * Reference RFC 6020.
57 *
58 * The "belongs-to" statement specifies the module to which the submodule
59 * belongs. The argument is an identifier that is the name of the module.
60 */
61 private String belongsToModuleName;
62
63 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +053064 * Module node to which sub-module belongs to.
65 */
66 private YangNode moduleNode;
67
68 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +053069 * Reference RFC 6020.
70 *
71 * The mandatory "prefix" substatement assigns a prefix for the module to
72 * which the submodule belongs. All definitions in the local submodule and
73 * any included submodules can be accessed by using the prefix.
74 */
75 private String prefix;
76
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053077 // Error Line number.
78 private int lineNumber;
79
80 // Error character position.
81 private int charPosition;
82
Vinod Kumar S67e7be62016-02-11 20:13:28 +053083 /**
84 * Create a belongs to object.
85 */
86 public YangBelongsTo() {
87
88 }
89
90 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053091 * Returns the belongs to module name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053092 *
93 * @return the belongs to module name
94 */
95 public String getBelongsToModuleName() {
96 return belongsToModuleName;
97 }
98
99 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530100 * Sets the belongs to module name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530101 *
102 * @param belongsToModuleName the belongs to module name to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530103 */
104 public void setBelongsToModuleName(String belongsToModuleName) {
105 this.belongsToModuleName = belongsToModuleName;
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Returns the prefix.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530110 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530111 * @return the prefix
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530112 */
113 public String getPrefix() {
114 return prefix;
115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Sets the prefix.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530119 *
120 * @param prefix the prefix to set
121 */
122 public void setPrefix(String prefix) {
123 this.prefix = prefix;
124 }
125
126 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530127 * Returns the module data model node.
128 *
129 * @return the module data model node
130 */
131 public YangNode getModuleNode() {
132 return moduleNode;
133 }
134
135 /**
136 * Sets the module node.
137 *
138 * @param moduleNode module data model node
139 */
140 public void setModuleNode(YangNode moduleNode) {
141 this.moduleNode = moduleNode;
142 }
143
144 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530145 * Returns the type of the data as belongs-to.
146 *
147 * @return ParsedDataType returns BELONGS_TO_DATA
148 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530149 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530150 public YangConstructType getYangConstructType() {
151 return YangConstructType.BELONGS_TO_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530152 }
153
154 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530155 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530156 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530157 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530158 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530159 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530160 public void validateDataOnEntry() throws DataModelException {
161 // TODO auto-generated method stub, to be implemented by parser
162 }
163
164 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530165 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530166 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530168 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530169 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530170 public void validateDataOnExit() throws DataModelException {
171 // TODO auto-generated method stub, to be implemented by parser
172 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530173
174 @Override
175 public int getLineNumber() {
176 return lineNumber;
177 }
178
179 @Override
180 public int getCharPosition() {
181 return charPosition;
182 }
183
184 @Override
185 public void setLineNumber(int lineNumber) {
186 this.lineNumber = lineNumber;
187 }
188
189 @Override
190 public void setCharPosition(int charPositionInLine) {
191 this.charPosition = charPositionInLine;
192 }
193
194 /**
195 * Links the belongs to with a module.
196 *
197 * @param yangFileInfoSet YANG file information set
198 * @throws DataModelException a violation in data model rule
199 */
200 public void linkWithModule(Set<YangFileInfo> yangFileInfoSet)
201 throws DataModelException {
202 String belongsToModuleName = getBelongsToModuleName();
203 YangNode moduleNode = findReferredNode(yangFileInfoSet, belongsToModuleName);
204 if (moduleNode != null) {
205 if (moduleNode instanceof YangModule) {
206 setModuleNode(moduleNode);
207 return;
208 }
209 }
210 DataModelException exception = new DataModelException("YANG file error : Module " + belongsToModuleName +
211 "to which sub-module belongs to is not found.");
212 exception.setLine(getLineNumber());
213 exception.setCharPosition(getCharPosition());
214 throw exception;
215 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530216}