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