blob: 9cb8dde7a8762f4cc6f4ff6418abe4880fc5e904 [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
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 S67e7be62016-02-11 20:13:28 +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 S67e7be62016-02-11 20:13:28 +053024
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053025import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode;
26
Vinod Kumar S67e7be62016-02-11 20:13:28 +053027/*-
28 * Reference 6020.
29 *
30 * The "belongs-to" statement specifies the module to which the
31 * submodule belongs. The argument is an identifier that is the name of
32 * the module.
33 *
34 * A submodule MUST only be included by the module to which it belongs,
35 * or by another submodule that belongs to that module.
36 *
37 * The mandatory "prefix" sub-statement assigns a prefix for the module
38 * to which the submodule belongs. All definitions in the local
39 * submodule and any included submodules can be accessed by using the
40 * prefix.
41 *
42 * The belongs-to's sub-statements
43 *
44 * +--------------+---------+-------------+
45 * | substatement | section | cardinality |
46 * +--------------+---------+-------------+
47 * | prefix | 7.1.4 | 1 |
48 * +--------------+---------+-------------+
49 */
50
51/**
Bharat saraswald9822e92016-04-05 15:13:44 +053052 * Represents the belongs-to data type information.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053053 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053054public class YangBelongsTo implements Parsable, LocationInfo, Serializable {
55
56 private static final long serialVersionUID = 806201639L;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053057
58 /**
59 * Reference RFC 6020.
60 *
61 * The "belongs-to" statement specifies the module to which the submodule
62 * belongs. The argument is an identifier that is the name of the module.
63 */
64 private String belongsToModuleName;
65
66 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +053067 * Module node to which sub-module belongs to.
68 */
69 private YangNode moduleNode;
70
71 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +053072 * Reference RFC 6020.
73 *
74 * The mandatory "prefix" substatement assigns a prefix for the module to
75 * which the submodule belongs. All definitions in the local submodule and
76 * any included submodules can be accessed by using the prefix.
77 */
78 private String prefix;
79
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053080 // Error Line number.
Bharat saraswal96dfef02016-06-16 00:29:12 +053081 private transient int lineNumber;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053082
83 // Error character position.
Bharat saraswal96dfef02016-06-16 00:29:12 +053084 private transient int charPosition;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053085
Vinod Kumar S67e7be62016-02-11 20:13:28 +053086 /**
87 * Create a belongs to object.
88 */
89 public YangBelongsTo() {
90
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Returns the belongs to module name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053095 *
96 * @return the belongs to module name
97 */
98 public String getBelongsToModuleName() {
99 return belongsToModuleName;
100 }
101
102 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530103 * Sets the belongs to module name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530104 *
105 * @param belongsToModuleName the belongs to module name to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530106 */
107 public void setBelongsToModuleName(String belongsToModuleName) {
108 this.belongsToModuleName = belongsToModuleName;
109 }
110
111 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530112 * Returns the prefix.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530113 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530114 * @return the prefix
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530115 */
116 public String getPrefix() {
117 return prefix;
118 }
119
120 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530121 * Sets the prefix.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530122 *
123 * @param prefix the prefix to set
124 */
125 public void setPrefix(String prefix) {
126 this.prefix = prefix;
127 }
128
129 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530130 * Returns the module data model node.
131 *
132 * @return the module data model node
133 */
134 public YangNode getModuleNode() {
135 return moduleNode;
136 }
137
138 /**
139 * Sets the module node.
140 *
141 * @param moduleNode module data model node
142 */
143 public void setModuleNode(YangNode moduleNode) {
144 this.moduleNode = moduleNode;
145 }
146
147 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530148 * Returns the type of the data as belongs-to.
149 *
150 * @return ParsedDataType returns BELONGS_TO_DATA
151 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530152 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 public YangConstructType getYangConstructType() {
154 return YangConstructType.BELONGS_TO_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530155 }
156
157 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530158 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530161 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530162 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530163 public void validateDataOnEntry() throws DataModelException {
164 // TODO auto-generated method stub, to be implemented by parser
165 }
166
167 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530168 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530169 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530170 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530171 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530172 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530173 public void validateDataOnExit() throws DataModelException {
174 // TODO auto-generated method stub, to be implemented by parser
175 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530176
177 @Override
178 public int getLineNumber() {
179 return lineNumber;
180 }
181
182 @Override
183 public int getCharPosition() {
184 return charPosition;
185 }
186
187 @Override
188 public void setLineNumber(int lineNumber) {
189 this.lineNumber = lineNumber;
190 }
191
192 @Override
193 public void setCharPosition(int charPositionInLine) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530194 charPosition = charPositionInLine;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530195 }
196
197 /**
198 * Links the belongs to with a module.
199 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530200 * @param yangNodeSet YANG file information set
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530201 * @throws DataModelException a violation in data model rule
202 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530203 public void linkWithModule(Set<YangNode> yangNodeSet)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530204 throws DataModelException {
205 String belongsToModuleName = getBelongsToModuleName();
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530206 YangNode moduleNode = findReferredNode(yangNodeSet, belongsToModuleName);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530207 if (moduleNode != null) {
208 if (moduleNode instanceof YangModule) {
209 setModuleNode(moduleNode);
210 return;
211 }
212 }
213 DataModelException exception = new DataModelException("YANG file error : Module " + belongsToModuleName +
214 "to which sub-module belongs to is not found.");
215 exception.setLine(getLineNumber());
216 exception.setCharPosition(getCharPosition());
217 throw exception;
218 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530219}