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