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 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 18 | import java.util.Set; |
Vinod Kumar S | 19f39c7 | 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 | 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 | 19f39c7 | 2016-02-09 20:12:31 +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 | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 26 | /* |
| 27 | * Reference:RFC 6020. |
| 28 | * The "include" statement is used to make content from a submodule |
| 29 | * available to that submodule's parent module, or to another submodule |
| 30 | * of that parent module. The argument is an identifier that is the |
| 31 | * name of the submodule to include. |
| 32 | * The includes's Substatements |
| 33 | * |
| 34 | * +---------------+---------+-------------+------------------+ |
| 35 | * | substatement | section | cardinality |data model mapping| |
| 36 | * +---------------+---------+-------------+------------------+ |
| 37 | * | revision-date | 7.1.5.1 | 0..1 | string | |
| 38 | * +---------------+---------+-------------+------------------+ |
| 39 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 40 | |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 41 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 42 | * Represents the information about the included sub-modules. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 43 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 44 | public class YangInclude |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 45 | implements Parsable, LocationInfo { |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Name of the sub-module that is being included. |
| 49 | */ |
| 50 | private String subModuleName; |
| 51 | |
| 52 | /** |
| 53 | * The include's "revision-date" statement is used to specify the exact |
| 54 | * version of the submodule to import. |
| 55 | */ |
| 56 | private String revision; |
| 57 | |
| 58 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 59 | * Reference to node which is included. |
| 60 | */ |
| 61 | private YangNode includedNode; |
| 62 | |
| 63 | // Error Line number. |
| 64 | private int lineNumber; |
| 65 | |
| 66 | // Error character position. |
| 67 | private int charPosition; |
| 68 | |
| 69 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 70 | * Creates a YANG include. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 71 | */ |
| 72 | public YangInclude() { |
| 73 | } |
| 74 | |
| 75 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 76 | * Returns the name of included sub-module. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 77 | * |
| 78 | * @return the sub-module name |
| 79 | */ |
| 80 | public String getSubModuleName() { |
| 81 | return subModuleName; |
| 82 | } |
| 83 | |
| 84 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 85 | * Sets the name of included sub-modules. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 86 | * |
| 87 | * @param subModuleName the sub-module name to set |
| 88 | */ |
| 89 | public void setSubModuleName(String subModuleName) { |
| 90 | this.subModuleName = subModuleName; |
| 91 | } |
| 92 | |
| 93 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 94 | * Returns the revision. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 95 | * |
| 96 | * @return the revision |
| 97 | */ |
| 98 | public String getRevision() { |
| 99 | return revision; |
| 100 | } |
| 101 | |
| 102 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 103 | * Sets the revision. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 104 | * |
| 105 | * @param revision the revision to set |
| 106 | */ |
| 107 | public void setRevision(String revision) { |
| 108 | this.revision = revision; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the type of parsed data. |
| 113 | * |
| 114 | * @return returns INCLUDE_DATA |
| 115 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 116 | @Override |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 117 | public YangConstructType getYangConstructType() { |
| 118 | return YangConstructType.INCLUDE_DATA; |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 122 | * Validates the data on entering the corresponding parse tree node. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 123 | * |
| 124 | * @throws DataModelException a violation of data model rules |
| 125 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 126 | @Override |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 127 | public void validateDataOnEntry() |
| 128 | throws DataModelException { |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 129 | // TODO auto-generated method stub, to be implemented by parser |
| 130 | |
| 131 | } |
| 132 | |
| 133 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 134 | * Validates the data on exiting the corresponding parse tree node. |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 135 | * |
| 136 | * @throws DataModelException a violation of data model rules |
| 137 | */ |
Vinod Kumar S | 0c330cd | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 138 | @Override |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 139 | public void validateDataOnExit() |
| 140 | throws DataModelException { |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 141 | // TODO auto-generated method stub, to be implemented by parser |
| 142 | |
| 143 | } |
| 144 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 145 | public YangNode getIncludedNode() { |
| 146 | return includedNode; |
| 147 | } |
| 148 | |
| 149 | public void setIncludedNode(YangNode includedNode) { |
| 150 | this.includedNode = includedNode; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int getLineNumber() { |
| 155 | return lineNumber; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public int getCharPosition() { |
| 160 | return charPosition; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void setLineNumber(int lineNumber) { |
| 165 | this.lineNumber = lineNumber; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void setCharPosition(int charPositionInLine) { |
| 170 | this.charPosition = charPositionInLine; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Adds reference to an include. |
| 175 | * |
| 176 | * @param yangFileInfoSet YANG file info set |
| 177 | * @return YANG sub module node |
| 178 | * @throws DataModelException a violation of data model rules |
| 179 | */ |
| 180 | public YangSubModule addReferenceToInclude(Set<YangFileInfo> yangFileInfoSet) throws DataModelException { |
| 181 | String includedSubModuleName = getSubModuleName(); |
| 182 | String includedSubModuleRevision = getRevision(); |
| 183 | YangNode subModuleNode = null; |
| 184 | |
| 185 | /* |
| 186 | * Find the included sub-module node for a given module name |
| 187 | * with a specified revision if revision is not null. |
| 188 | */ |
| 189 | if (includedSubModuleRevision != null) { |
| 190 | String includedSubModuleNameWithRevision = includedSubModuleName + "@" + includedSubModuleRevision; |
| 191 | subModuleNode = findReferredNode(yangFileInfoSet, includedSubModuleNameWithRevision); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Find the imported sub module node for a given module name |
| 196 | * without revision if can't find with revision. |
| 197 | */ |
| 198 | if (subModuleNode == null) { |
| 199 | subModuleNode = findReferredNode(yangFileInfoSet, includedSubModuleName); |
| 200 | } |
| 201 | |
| 202 | if (subModuleNode != null) { |
| 203 | if (subModuleNode instanceof YangSubModule) { |
| 204 | if (getRevision() == null || getRevision().isEmpty()) { |
| 205 | setIncludedNode(subModuleNode); |
| 206 | return (YangSubModule) subModuleNode; |
| 207 | } |
| 208 | // Match revision if inclusion is with revision. |
| 209 | if (((YangSubModule) subModuleNode).getRevision().getRevDate().equals(includedSubModuleRevision)) { |
| 210 | setIncludedNode(subModuleNode); |
| 211 | return (YangSubModule) subModuleNode; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | // Exception if there is no match. |
| 216 | DataModelException exception = new DataModelException("YANG file error : Included sub module " + |
| 217 | includedSubModuleName + "with a given revision is not found."); |
| 218 | exception.setLine(getLineNumber()); |
| 219 | exception.setCharPosition(getCharPosition()); |
| 220 | throw exception; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Reports an error when included sub-module doesn't meet condition that |
| 225 | * "included sub-modules should belong module, as defined by the |
| 226 | * "belongs-to" statement or sub-modules are only allowed to include other |
| 227 | * sub-modules belonging to the same module. |
| 228 | * |
| 229 | * @throws DataModelException a violation in data model rule |
| 230 | */ |
| 231 | public void reportIncludeError() throws DataModelException { |
| 232 | DataModelException exception = new DataModelException("YANG file error : Included sub-module " + |
| 233 | getSubModuleName() + "doesn't belongs to parent module also it doesn't belongs" + |
| 234 | "to sub-module belonging to the same parent module."); |
| 235 | exception.setLine(getLineNumber()); |
| 236 | exception.setCharPosition(getCharPosition()); |
| 237 | throw exception; |
| 238 | } |
Vinod Kumar S | 19f39c7 | 2016-02-09 20:12:31 +0530 | [diff] [blame] | 239 | } |