Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.yangutils.datamodel; |
| 17 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame^] | 18 | import java.util.Set; |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 20 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame^] | 21 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 23 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame^] | 24 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode; |
| 25 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 26 | /*- |
| 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 saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Represents the belongs-to data type information. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 52 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame^] | 53 | public class YangBelongsTo implements Parsable, LocationInfo { |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 54 | |
| 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 Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 64 | * Module node to which sub-module belongs to. |
| 65 | */ |
| 66 | private YangNode moduleNode; |
| 67 | |
| 68 | /** |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 69 | * 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 Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame^] | 77 | // Error Line number. |
| 78 | private int lineNumber; |
| 79 | |
| 80 | // Error character position. |
| 81 | private int charPosition; |
| 82 | |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 83 | /** |
| 84 | * Create a belongs to object. |
| 85 | */ |
| 86 | public YangBelongsTo() { |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 91 | * Returns the belongs to module name. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 92 | * |
| 93 | * @return the belongs to module name |
| 94 | */ |
| 95 | public String getBelongsToModuleName() { |
| 96 | return belongsToModuleName; |
| 97 | } |
| 98 | |
| 99 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 100 | * Sets the belongs to module name. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 101 | * |
| 102 | * @param belongsToModuleName the belongs to module name to set |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 103 | */ |
| 104 | public void setBelongsToModuleName(String belongsToModuleName) { |
| 105 | this.belongsToModuleName = belongsToModuleName; |
| 106 | } |
| 107 | |
| 108 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 109 | * Returns the prefix. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 110 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 111 | * @return the prefix |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 112 | */ |
| 113 | public String getPrefix() { |
| 114 | return prefix; |
| 115 | } |
| 116 | |
| 117 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 118 | * Sets the prefix. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 119 | * |
| 120 | * @param prefix the prefix to set |
| 121 | */ |
| 122 | public void setPrefix(String prefix) { |
| 123 | this.prefix = prefix; |
| 124 | } |
| 125 | |
| 126 | /** |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 127 | * 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 S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 145 | * Returns the type of the data as belongs-to. |
| 146 | * |
| 147 | * @return ParsedDataType returns BELONGS_TO_DATA |
| 148 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 149 | @Override |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 150 | public YangConstructType getYangConstructType() { |
| 151 | return YangConstructType.BELONGS_TO_DATA; |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 155 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 156 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 157 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 158 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 159 | @Override |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 160 | public void validateDataOnEntry() throws DataModelException { |
| 161 | // TODO auto-generated method stub, to be implemented by parser |
| 162 | } |
| 163 | |
| 164 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 165 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 166 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 167 | * @throws DataModelException a violation of data model rules |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 168 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 169 | @Override |
Vinod Kumar S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 170 | public void validateDataOnExit() throws DataModelException { |
| 171 | // TODO auto-generated method stub, to be implemented by parser |
| 172 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame^] | 173 | |
| 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 S | 67e7be6 | 2016-02-11 20:13:28 +0530 | [diff] [blame] | 216 | } |