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