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