Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +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 | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 18 | import java.util.Set; |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 20 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 23 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findReferredNode; |
| 25 | |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 26 | /* |
| 27 | * Reference:RFC 6020. |
| 28 | * The "import" statement makes definitions from one module available |
| 29 | * inside another module or submodule. The argument is the name of the |
| 30 | * module to import, and the statement is followed by a block of |
| 31 | * sub statements that holds detailed import information. |
| 32 | * When a module is imported, the importing module may: |
| 33 | * o use any grouping and typedef defined at the top level in the |
| 34 | * imported module or its submodules. |
| 35 | * |
| 36 | * o use any extension, feature, and identity defined in the imported |
| 37 | * module or its submodules. |
| 38 | * |
| 39 | * o use any node in the imported module's schema tree in "must", |
| 40 | * "path", and "when" statements, or as the target node in "augment" |
| 41 | * and "deviation" statements. |
| 42 | * |
| 43 | * The mandatory "prefix" sub statement assigns a prefix for the imported |
| 44 | * module that is scoped to the importing module or submodule. Multiple |
| 45 | * "import" statements may be specified to import from different |
| 46 | * modules. |
| 47 | * When the optional "revision-date" sub-statement is present, any |
| 48 | * typedef, grouping, extension, feature, and identity referenced by |
| 49 | * definitions in the local module are taken from the specified revision |
| 50 | * of the imported module. It is an error if the specified revision of |
| 51 | * the imported module does not exist. If no "revision-date" |
| 52 | * sub-statement is present, it is undefined from which revision of the |
| 53 | * module they are taken. |
| 54 | * |
| 55 | * Multiple revisions of the same module MUST NOT be imported. |
| 56 | * |
| 57 | * The import's Substatements |
| 58 | * |
| 59 | * +---------------+---------+-------------+------------------+ |
| 60 | * | substatement | section | cardinality |data model mapping| |
| 61 | * +---------------+---------+-------------+------------------+ |
| 62 | * | prefix | 7.1.4 | 1 | string | |
| 63 | * | revision-date | 7.1.5.1 | 0..1 | string | |
| 64 | * +---------------+---------+-------------+------------------+ |
| 65 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 66 | |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 67 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 68 | * Represents the information about the imported modules. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 69 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 70 | public class YangImport |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 71 | implements Parsable, LocationInfo { |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Name of the module that is being imported. |
| 75 | */ |
| 76 | private String name; |
| 77 | |
| 78 | /** |
| 79 | * Prefix used to identify the entities from the imported module. |
| 80 | */ |
| 81 | private String prefixId; |
| 82 | |
| 83 | /** |
| 84 | * Reference:RFC 6020. |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 85 | * <p> |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 86 | * The import's "revision-date" statement is used to specify the exact |
| 87 | * version of the module to import. The "revision-date" statement MUST match |
| 88 | * the most recent "revision" statement in the imported module. organization |
| 89 | * which defined the YANG module. |
| 90 | */ |
| 91 | private String revision; |
| 92 | |
| 93 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 94 | * Reference to node which is imported. |
| 95 | */ |
| 96 | private YangNode importedNode; |
| 97 | |
| 98 | // Error Line number. |
| 99 | private int lineNumber; |
| 100 | |
| 101 | // Error character position. |
| 102 | private int charPosition; |
| 103 | |
| 104 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | * Creates a YANG import. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 106 | */ |
| 107 | public YangImport() { |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 111 | * Returns the imported module name. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 112 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 113 | * @return the module name |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 114 | */ |
| 115 | public String getModuleName() { |
| 116 | return name; |
| 117 | } |
| 118 | |
| 119 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 120 | * Sets module name. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 121 | * |
| 122 | * @param moduleName the module name to set |
| 123 | */ |
| 124 | public void setModuleName(String moduleName) { |
| 125 | name = moduleName; |
| 126 | } |
| 127 | |
| 128 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 129 | * Returns the prefix used to identify the entities from the imported module. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 130 | * |
| 131 | * @return the prefix used to identify the entities from the imported |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 132 | * module |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 133 | */ |
| 134 | public String getPrefixId() { |
| 135 | return prefixId; |
| 136 | } |
| 137 | |
| 138 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 139 | * Sets prefix identifier. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 140 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 141 | * @param prefixId set the prefix identifier of the imported module |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 142 | */ |
| 143 | public void setPrefixId(String prefixId) { |
| 144 | this.prefixId = prefixId; |
| 145 | } |
| 146 | |
| 147 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 148 | * Returns the revision of the imported module. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 149 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 150 | * @return the revision of the imported module |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 151 | */ |
| 152 | public String getRevision() { |
| 153 | return revision; |
| 154 | } |
| 155 | |
| 156 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 157 | * Sets the revision of the imported module. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 158 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 159 | * @param rev set the revision of the imported module |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 160 | */ |
| 161 | public void setRevision(String rev) { |
| 162 | revision = rev; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns the type of the parsed data. |
| 167 | * |
| 168 | * @return returns IMPORT_DATA |
| 169 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 170 | @Override |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 171 | public YangConstructType getYangConstructType() { |
| 172 | return YangConstructType.IMPORT_DATA; |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 176 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 177 | * |
| 178 | * @throws DataModelException a violation of data model rules |
| 179 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 180 | @Override |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 181 | public void validateDataOnEntry() |
| 182 | throws DataModelException { |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 183 | // TODO auto-generated method stub, to be implemented by parser |
| 184 | |
| 185 | } |
| 186 | |
| 187 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 188 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 189 | * |
| 190 | * @throws DataModelException a violation of data model rules |
| 191 | */ |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 192 | @Override |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 193 | public void validateDataOnExit() |
| 194 | throws DataModelException { |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 195 | // TODO auto-generated method stub, to be implemented by parser |
| 196 | |
| 197 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Returns imported node. |
| 201 | * |
| 202 | * @return imported node |
| 203 | */ |
| 204 | public YangNode getImportedNode() { |
| 205 | return importedNode; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Sets imported node. |
| 210 | * |
| 211 | * @param importedNode imported node |
| 212 | */ |
| 213 | public void setImportedNode(YangNode importedNode) { |
| 214 | this.importedNode = importedNode; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public int getLineNumber() { |
| 219 | return lineNumber; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int getCharPosition() { |
| 224 | return charPosition; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void setLineNumber(int lineNumber) { |
| 229 | this.lineNumber = lineNumber; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void setCharPosition(int charPositionInLine) { |
| 234 | this.charPosition = charPositionInLine; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Adds reference to an import. |
| 239 | * |
| 240 | * @param yangFileInfoSet YANG file info set |
| 241 | * @throws DataModelException a violation of data model rules |
| 242 | */ |
| 243 | public void addReferenceToImport(Set<YangFileInfo> yangFileInfoSet) throws DataModelException { |
| 244 | String importedModuleName = getModuleName(); |
| 245 | String importedModuleRevision = getRevision(); |
| 246 | YangNode moduleNode = null; |
| 247 | /* |
| 248 | * Find the imported module node for a given module name |
| 249 | * with a specified revision if revision is not null. |
| 250 | */ |
| 251 | if (importedModuleRevision != null) { |
| 252 | String importedModuleNameWithRevision = importedModuleName + "@" + importedModuleRevision; |
| 253 | moduleNode = findReferredNode(yangFileInfoSet, importedModuleNameWithRevision); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Find the imported module node for a given module name |
| 258 | * without revision if can't find with revision. |
| 259 | */ |
| 260 | if (moduleNode == null) { |
| 261 | moduleNode = findReferredNode(yangFileInfoSet, importedModuleName); |
| 262 | } |
| 263 | |
| 264 | if (moduleNode != null) { |
| 265 | if (moduleNode instanceof YangModule) { |
| 266 | if (getRevision() == null || getRevision().isEmpty()) { |
| 267 | setImportedNode(moduleNode); |
| 268 | return; |
| 269 | } |
| 270 | // Match revision if import is with revision. |
| 271 | if (((YangModule) moduleNode).getRevision().getRevDate().equals(importedModuleRevision)) { |
| 272 | setImportedNode(moduleNode); |
| 273 | return; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Exception if there is no match. |
| 279 | DataModelException exception = new DataModelException("YANG file error : Imported module " |
| 280 | + importedModuleName + " with revision " + importedModuleRevision + " is not found."); |
| 281 | exception.setLine(getLineNumber()); |
| 282 | exception.setCharPosition(getCharPosition()); |
| 283 | throw exception; |
| 284 | } |
Vinod Kumar S | 6a33667 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 285 | } |